1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

x.websocket: move to net.websocket module (#10648)

This commit is contained in:
Tomas Hellström
2021-07-03 01:56:00 +02:00
committed by GitHub
parent c44a47acb1
commit ec973f5c6e
41 changed files with 1656 additions and 67 deletions

View File

@ -1,25 +0,0 @@
module openssl
import time
enum Select {
read
write
except
}
pub struct SSLConn {
mut:
sslctx &C.SSL_CTX
ssl &C.SSL
handle int
duration time.Duration
}
pub fn new_ssl_conn() &SSLConn {
return &SSLConn{
sslctx: 0
ssl: 0
handle: 0
}
}

View File

@ -1,244 +0,0 @@
module openssl
import net
import net.openssl as nssl
import time
// shutdown closes the ssl connection and do clean up
pub fn (mut s SSLConn) shutdown() ? {
if s.ssl != 0 {
mut res := 0
for {
res = C.SSL_shutdown(voidptr(s.ssl))
if res < 0 {
err_res := nssl.ssl_error(res, s.ssl) or {
break // We break to free rest of resources
}
if err_res == .ssl_error_want_read {
for {
ready := @select(s.handle, .read, s.duration) ?
if ready {
break
}
}
continue
} else if err_res == .ssl_error_want_write {
for {
ready := @select(s.handle, .write, s.duration) ?
if ready {
break
}
}
continue
} else {
unsafe { C.SSL_free(voidptr(s.ssl)) }
if s.sslctx != 0 {
C.SSL_CTX_free(s.sslctx)
}
return error('unexepedted ssl error $err_res')
}
if s.ssl != 0 {
unsafe { C.SSL_free(voidptr(s.ssl)) }
}
if s.sslctx != 0 {
C.SSL_CTX_free(s.sslctx)
}
return error('Could not connect using SSL. ($err_res),err')
} else if res == 0 {
continue
} else if res == 1 {
break
}
}
C.SSL_free(voidptr(s.ssl))
}
if s.sslctx != 0 {
C.SSL_CTX_free(s.sslctx)
}
}
// connect to server using open ssl
pub fn (mut s SSLConn) connect(mut tcp_conn net.TcpConn, hostname string) ? {
s.handle = tcp_conn.sock.handle
s.duration = tcp_conn.read_timeout()
s.sslctx = unsafe { C.SSL_CTX_new(C.SSLv23_client_method()) }
if s.sslctx == 0 {
return error("Couldn't get ssl context")
}
// TODO: Fix option to enable/disable checks for valid
// certificates to allow both secure and self signed
// for now the checks are not done at all to comply
// to current autobahn tests
// C.SSL_CTX_set_verify_depth(s.sslctx, 4)
// flags := C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION
// C.SSL_CTX_set_options(s.sslctx, flags)
// mut res := C.SSL_CTX_load_verify_locations(s.sslctx, 'random-org-chain.pem', 0)
s.ssl = unsafe { &C.SSL(C.SSL_new(s.sslctx)) }
if s.ssl == 0 {
return error("Couldn't create OpenSSL instance.")
}
// preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
// mut res := C.SSL_set_cipher_list(s.ssl, preferred_ciphers.str)
// if res != 1 {
// println('http: openssl: cipher failed')
// }
mut res := C.SSL_set_tlsext_host_name(voidptr(s.ssl), voidptr(hostname.str))
if res != 1 {
return error('cannot set host name')
}
if C.SSL_set_fd(voidptr(s.ssl), tcp_conn.sock.handle) != 1 {
return error("Couldn't assign ssl to socket.")
}
for {
res = C.SSL_connect(voidptr(s.ssl))
if res != 1 {
err_res := nssl.ssl_error(res, s.ssl) ?
if err_res == .ssl_error_want_read {
for {
ready := @select(s.handle, .read, s.duration) ?
if ready {
break
}
}
continue
} else if err_res == .ssl_error_want_write {
for {
ready := @select(s.handle, .write, s.duration) ?
if ready {
break
}
}
continue
}
return error('Could not connect using SSL. ($err_res),err')
}
break
}
}
pub fn (mut s SSLConn) socket_read_into_ptr(buf_ptr &byte, len int) ?int {
mut res := 0
for {
res = C.SSL_read(voidptr(s.ssl), buf_ptr, len)
if res < 0 {
err_res := nssl.ssl_error(res, s.ssl) ?
if err_res == .ssl_error_want_read {
for {
ready := @select(s.handle, .read, s.duration) ?
if ready {
break
}
}
continue
} else if err_res == .ssl_error_want_write {
for {
ready := @select(s.handle, .write, s.duration) ?
if ready {
break
}
}
continue
} else if err_res == .ssl_error_zero_return {
return 0
}
return error('Could not read using SSL. ($err_res)')
}
break
}
return res
}
pub fn (mut s SSLConn) read_into(mut buffer []byte) ?int {
res := s.socket_read_into_ptr(&byte(buffer.data), buffer.len) ?
return res
}
// write number of bytes to SSL connection
pub fn (mut s SSLConn) write(bytes []byte) ?int {
unsafe {
mut ptr_base := &byte(bytes.data)
mut total_sent := 0
for total_sent < bytes.len {
ptr := ptr_base + total_sent
remaining := bytes.len - total_sent
mut sent := C.SSL_write(voidptr(s.ssl), ptr, remaining)
if sent <= 0 {
err_res := nssl.ssl_error(sent, s.ssl) ?
if err_res == .ssl_error_want_read {
for {
ready := @select(s.handle, .read, s.duration) ?
if ready {
break
}
}
} else if err_res == .ssl_error_want_write {
for {
ready := @select(s.handle, .write, s.duration) ?
if ready {
break
}
}
continue
} else if err_res == .ssl_error_zero_return {
return error('ssl write on closed connection') // Todo error_with_code close
}
return error_with_code('Could not write SSL. ($err_res),err', int(err_res))
}
total_sent += sent
}
return total_sent
}
}
/*
This is basically a copy of Emily socket implementation of select.
This have to be consolidated into common net lib features
when merging this to V
*/
[typedef]
pub struct C.fd_set {
}
// Select waits for an io operation (specified by parameter `test`) to be available
fn @select(handle int, test Select, timeout time.Duration) ?bool {
set := C.fd_set{}
C.FD_ZERO(&set)
C.FD_SET(handle, &set)
seconds := timeout.milliseconds() / 1000
microseconds := timeout - (seconds * time.second)
mut tt := C.timeval{
tv_sec: u64(seconds)
tv_usec: u64(microseconds)
}
mut timeval_timeout := &tt
// infinite timeout is signaled by passing null as the timeout to
// select
if timeout == net.infinite_timeout {
timeval_timeout = &C.timeval(0)
}
match test {
.read {
net.socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout)) ?
}
.write {
net.socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout)) ?
}
.except {
net.socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout)) ?
}
}
return C.FD_ISSET(handle, &set)
}

View File

@ -1,20 +0,0 @@
# Autobahn tests
This is the autobahn automatic tests on build.
The performance tests are skipped due to timeouts in Github actions.
## Run it locally
### Test the client
This is how to test the client:
1. Run the docker autobahn test suite by running the `docker-compose up`
2. From the `local_run` folder, compile and run `autobahn_client.v` to test non ws (no TLS) and
`autobahn_client_wss.v` to run the TLS tests
3. Open `http://localhost:8080` and browse client test results for non TLS and `https://localhost:8081`
if you ran the wss tests (it uses local certificat so you will get trust error but just accept use)
### Test the server
Todo: add information here

View File

@ -1,33 +0,0 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or { println('error should be ok: $err') }
}
// update the reports
uri := 'ws://autobahn_server:9001/updateReports?agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.connect() ?
ws.listen() ?
}
fn handle_case(case_nr int) ? {
uri := 'ws://autobahn_server:9001/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
// autobahn tests expects to send same message back
if msg.opcode == .pong {
// We just wanna pass text and binary message back to autobahn
return
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,35 +0,0 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or { println('error should be ok: $err') }
}
// update the reports
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.connect() ?
ws.listen() ?
}
fn handle_case(case_nr int) ? {
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
// autobahn tests expects to send same message back
if msg.opcode == .pong {
// We just wanna pass text and binary message back to autobahn
return
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,27 +0,0 @@
// use this to test websocket server to the autobahn test
module main
import x.websocket
fn main() {
mut s := websocket.new_server(.ip6, 9002, '/')
s.on_message(on_message)
s.listen() or { panic(err) }
}
fn handle_case(case_nr int) ? {
uri := 'ws://localhost:9002/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
// autobahn tests expects to send same message back
if msg.opcode == .pong {
// We just wanna pass text and binary message back to autobahn
return
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,21 +0,0 @@
version: '3'
services:
server:
container_name: autobahn_server
build: fuzzing_server
ports:
- "9001:9001"
- "8080:8080"
server_wss:
container_name: autobahn_server_wss
build: fuzzing_server_wss
ports:
- "9002:9002"
- "8081:8080"
client:
container_name: autobahn_client
build:
dockerfile: vlib/x/websocket/tests/autobahn/ws_test/Dockerfile
context: ../../../../../

View File

@ -1,5 +0,0 @@
FROM crossbario/autobahn-testsuite
COPY check_results.py /check_results.py
RUN chmod +x /check_results.py
COPY config /config

View File

@ -1,46 +0,0 @@
import json
nr_of_client_errs = 0
nr_of_client_tests = 0
nr_of_server_errs = 0
nr_of_server_tests = 0
with open("/reports/clients/index.json") as f:
data = json.load(f)
for i in data["v-client"]:
# Count errors
if (
data["v-client"][i]["behavior"] == "FAILED"
or data["v-client"][i]["behaviorClose"] == "FAILED"
):
nr_of_client_errs = nr_of_client_errs + 1
nr_of_client_tests = nr_of_client_tests + 1
with open("/reports/servers/index.json") as f:
data = json.load(f)
for i in data["AutobahnServer"]:
if (
data["AutobahnServer"][i]["behavior"] == "FAILED"
or data["AutobahnServer"][i]["behaviorClose"] == "FAILED"
):
nr_of_server_errs = nr_of_server_errs + 1
nr_of_server_tests = nr_of_server_tests + 1
if nr_of_client_errs > 0 or nr_of_server_errs > 0:
print(
"FAILED AUTOBAHN TESTS, CLIENT ERRORS {0}(of {1}), SERVER ERRORS {2}(of {3})".format(
nr_of_client_errs, nr_of_client_tests, nr_of_server_errs, nr_of_server_tests
)
)
exit(1)
print(
"TEST SUCCESS!, CLIENT TESTS({0}), SERVER TESTS ({1})".format(
nr_of_client_tests, nr_of_server_tests
)
)

View File

@ -1,22 +0,0 @@
{
"options": {
"failByDrop": false
},
"outdir": "./reports/servers",
"servers": [
{
"agent": "AutobahnServer",
"url": "ws://autobahn_client:9002"
}
],
"cases": [
"*"
],
"exclude-cases": [
"9.*",
"11.*",
"12.*",
"13.*"
],
"exclude-agent-cases": {}
}

View File

@ -1,14 +0,0 @@
{
"url": "ws://127.0.0.1:9001",
"outdir": "./reports/clients",
"cases": [
"*"
],
"exclude-cases": [
"9.*",
"11.*",
"12.*",
"13.*"
],
"exclude-agent-cases": {}
}

View File

@ -1,9 +0,0 @@
FROM crossbario/autobahn-testsuite
COPY check_results.py /check_results.py
RUN chmod +x /check_results.py
COPY config /config
RUN chmod +rx /config/server.crt
RUN chmod +rx /config/server.key
EXPOSE 9002 9002

View File

@ -1,35 +0,0 @@
import json
nr_of_client_errs = 0
nr_of_client_tests = 0
nr_of_server_errs = 0
nr_of_server_tests = 0
with open("/reports/clients/index.json") as f:
data = json.load(f)
for i in data["v-client"]:
# Count errors
if (
data["v-client"][i]["behavior"] == "FAILED"
or data["v-client"][i]["behaviorClose"] == "FAILED"
):
nr_of_client_errs = nr_of_client_errs + 1
nr_of_client_tests = nr_of_client_tests + 1
if nr_of_client_errs > 0 or nr_of_server_errs > 0:
print(
"FAILED AUTOBAHN TESTS, CLIENT ERRORS {0}(of {1}), SERVER ERRORS {2}(of {3})".format(
nr_of_client_errs, nr_of_client_tests, nr_of_server_errs, nr_of_server_tests
)
)
exit(1)
print(
"TEST SUCCESS!, CLIENT TESTS({0}), SERVER TESTS ({1})".format(
nr_of_client_tests, nr_of_server_tests
)
)

View File

@ -1,16 +0,0 @@
{
"url": "wss://127.0.0.1:9002",
"outdir": "./reports/clients",
"key": "/config/server.key",
"cert": "/config/server.crt",
"cases": [
"*"
],
"exclude-cases": [
"9.*",
"11.*",
"12.*",
"13.*"
],
"exclude-agent-cases": {}
}

View File

@ -1,19 +0,0 @@
-----BEGIN CERTIFICATE-----
MIIDETCCAfkCFAtFKlcdB3jhD+AXPul81dwmZcs/MA0GCSqGSIb3DQEBCwUAMEUx
CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl
cm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMjAxMTIxMDgyNjQ5WhcNMzAxMTE5MDgy
NjQ5WjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UE
CgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEAnbysLfcIr9+wpoJjb5r728j2e07agedOzh8VLuGnHqmKOUPN
f8Ik707kEoBcFY7UM2A9G/1RMIysGp8eleQLMtNdeYc3KlKHBGFrOM3i4gCd7G44
lERuKP1PKzRQ6RdVNUXn51XjfxjHWo7kHCEVvZowxvzxLxhwbSwmEmgzcQ1T6vj6
Cdop87sdq00F+eOCfTdy+cl+R65sbImVdfY4EQ0QWAVdF3X6njLjpdmteppggbEa
ECv3R3qNIV7/rflIPm1efbqp7R1ugvjLPJZ1u12ovtqkgsWbnEyzST8hbEEjsOTJ
/cPkH2DaLdh7fMgfcVmqnYXd9T+gpsNGv98DjwIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQBG9GxUOjcrFd1ept9AOTzbxvIUvBiqIEzrL2/+3T1yPPAWQzOmBfZhIVVm
EZeeU3xcvd7+AmX+2FPCAD+evjSHjKY048X1YksQS7mYChSgeJiknoJi3mAEAyw6
oYGVkETksZLQfXtWTjgljbIQrwTA1s+EW0jvmvaJnWD3/8nFqmfly2/kxVsTcGEa
wJGEUS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PN
f8Ik707kEoBcFY7UM2A9G/1RMIysGp8eleQLMtNdeYc3KlKHBGFrOM3i4gCd7G44
lERuKP1PKzRQ6RdVNUXn51XjfxjHWo7kHCEVvZowxvzxLxhwbSwmEmgzcQ1T6vj6
Cdop87sdq00F+eOCfTdy+cl+R65sbImVdfY4EQ0QWAVdF3X6njLjpdmteppggbEa
ECv3R3qNIV7/rflIPm1efbqp7R1ugvjLPJZ1u12ovtqkgsWbnEyzST8hbEEjsOTJ
/cPkH2DaLdh7fMgfcVmqnYXd9T+gpsNGv98DjwIDAQABMA0GCSqGSIb3DQEBCwUA
A4IBAQBG9GxUOjcrFd1ept9AOTzbxvIUvBiqIEzrL2/+3T1yPPAWQzOmBfZhIVVm
EZeeU3xcvd7+AmX+2FPCAD+evjSHjKY048X1YksQS7mYChSgeJiknoJi3mAEAyw6
oYGVkETksZLQfXtWTjgljbIQrwTA1s+EW0jvmvaJnWD3/8nFqmfly2/kxVsTcGEa
wJGEUS53Cq6y6lLZ+ojjjj1iVCQ94U6L/0xPB9hgXOyL2+iQj+n38ruatnUNF77C
UKS7N9BFF42eqVY83Xab0m25s93m8Z7J/63qu0eeA8p5t7+8lbGvOYpwReknLRMf
pJfgSEWqWfSaetihbJl2Fmzg2SeJ
-----END CERTIFICATE-----

View File

@ -1,12 +0,0 @@
# Use this as docker builder with https://github.com/nektos/act
# build with: docker build tests/autobahn/. -t myimage
# use in act: act -P ubuntu-latest=myimage
FROM node:12.6-buster-slim
COPY config/fuzzingserver.json /config/fuzzingserver.json
RUN chmod +775 /config/fuzzingserver.json
RUN apt-get update && \
apt-get install -y \
docker \
docker-compose

View File

@ -1,33 +0,0 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or { println('error should be ok: $err') }
}
// update the reports
uri := 'ws://localhost:9001/updateReports?agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.connect() ?
ws.listen() ?
}
fn handle_case(case_nr int) ? {
uri := 'ws://localhost:9001/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
// autobahn tests expects to send same message back
if msg.opcode == .pong {
// We just wanna pass text and binary message back to autobahn
return
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,35 +0,0 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or { println('error should be ok: $err') }
}
// update the reports
// uri := 'wss://localhost:9002/updateReports?agent=v-client'
uri := 'wss://autobahn_server_wss:9002/updateReports?agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.connect() ?
ws.listen() ?
}
fn handle_case(case_nr int) ? {
uri := 'wss://autobahn_server_wss:9002/runCase?case=$case_nr&agent=v-client'
// uri := 'wss://localhost:9002/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message) ? {
// autobahn tests expects to send same message back
if msg.opcode == .pong {
// We just wanna pass text and binary message back to autobahn
return
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,12 +0,0 @@
FROM thevlang/vlang:buster-build
COPY ./ /src/
WORKDIR /src
RUN make CC=clang
RUN /src/v /src/vlib/x/websocket/tests/autobahn/autobahn_server.v
RUN chmod +x /src/vlib/x/websocket/tests/autobahn/autobahn_server
ENTRYPOINT [ "/src/vlib/x/websocket/tests/autobahn/autobahn_server" ]

View File

@ -1,11 +1,13 @@
// websocket module implements websocket client and a websocket server
// attribution: @thecoderr the author of original websocket client
// advice that the implementation is deprecated and moved to the net.websocket module!
// it will be removed in later versions
[manualfree]
module websocket
import net
import net.http
import x.openssl
import net.openssl
import net.urllib
import time
import log
@ -74,6 +76,7 @@ pub enum OPCode {
}
// new_client instance a new websocket client
[deprecated: 'use net.websocket module instead']
pub fn new_client(address string) ?&Client {
uri := parse_uri(address) ?
return &Client{

View File

@ -1,7 +1,7 @@
module websocket
import net
import x.openssl
import net.openssl
import log
import time
import rand
@ -35,6 +35,7 @@ pub mut:
}
// new_server instance a new websocket server on provided port and route
[deprecated: 'use net.websocket module instead']
pub fn new_server(family net.AddrFamily, port int, route string) &Server {
return &Server{
ls: 0

View File

@ -1,122 +0,0 @@
import os
import net
import x.websocket
import time
import rand
// TODO: fix connecting to ipv4 websockets
// (the server seems to work with .ip, but
// Client can not connect, it needs to be passed
// .ip too?)
struct WebsocketTestResults {
pub mut:
nr_messages int
nr_pong_received int
}
// Do not run these tests everytime, since they are flaky.
// They have their own specialized CI runner.
const github_job = os.getenv('GITHUB_JOB')
const should_skip = github_job != '' && github_job != 'websocket_tests'
// tests with internal ws servers
fn test_ws_ipv6() {
if should_skip {
return
}
port := 30000 + rand.intn(1024)
go start_server(.ip6, port)
time.sleep(500 * time.millisecond)
ws_test(.ip6, 'ws://localhost:$port') or { assert false }
}
// tests with internal ws servers
fn test_ws_ipv4() {
// TODO: fix client
if true || should_skip {
return
}
port := 30000 + rand.intn(1024)
go start_server(.ip, port)
time.sleep(500 * time.millisecond)
ws_test(.ip, 'ws://localhost:$port') or { assert false }
}
fn start_server(family net.AddrFamily, listen_port int) ? {
mut s := websocket.new_server(family, listen_port, '')
// make that in execution test time give time to execute at least one time
s.ping_interval = 1
s.on_connect(fn (mut s websocket.ServerClient) ?bool {
// here you can look att the client info and accept or not accept
// just returning a true/false
if s.resource_name != '/' {
panic('unexpected resource name in test')
return false
}
return true
}) ?
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
match msg.opcode {
.pong { ws.write_string('pong') or { panic(err) } }
else { ws.write(msg.payload, msg.opcode) or { panic(err) } }
}
})
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
// not used
})
s.listen() or {}
}
// ws_test tests connect to the websocket server from websocket client
fn ws_test(family net.AddrFamily, uri string) ? {
eprintln('connecting to $uri ...')
mut test_results := WebsocketTestResults{}
mut ws := websocket.new_client(uri) ?
ws.on_open(fn (mut ws websocket.Client) ? {
ws.pong() ?
assert true
})
ws.on_error(fn (mut ws websocket.Client, err string) ? {
println('error: $err')
// this can be thrown by internet connection problems
assert false
})
ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, mut res WebsocketTestResults) ? {
println('client got type: $msg.opcode payload:\n$msg.payload')
if msg.opcode == .text_frame {
smessage := msg.payload.bytestr()
match smessage {
'pong' {
res.nr_pong_received++
}
'a' {
res.nr_messages++
}
else {
assert false
}
}
} else {
println('Binary message: $msg')
}
}, test_results)
ws.connect() or { panic('fail to connect') }
go ws.listen()
text := ['a'].repeat(2)
for msg in text {
ws.write(msg.bytes(), .text_frame) or { panic('fail to write to websocket') }
// sleep to give time to recieve response before send a new one
time.sleep(100 * time.millisecond)
}
// sleep to give time to recieve response before asserts
time.sleep(1500 * time.millisecond)
// We expect at least 2 pongs, one sent directly and one indirectly
assert test_results.nr_pong_received >= 2
assert test_results.nr_messages == 2
}