test-cleancode: add the rest of vlib/x/websocket/

pull/7640/head
Delyan Angelov 2020-12-28 07:25:46 +02:00
parent b65353794c
commit b7a5dbf7b4
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
10 changed files with 67 additions and 97 deletions

View File

@ -69,8 +69,7 @@ const (
'vlib/strings/', 'vlib/strings/',
'vlib/time/', 'vlib/time/',
'vlib/vweb/', 'vlib/vweb/',
'vlib/x/websocket/websocket_server.v', 'vlib/x/websocket/',
'vlib/x/websocket/websocket_client.v',
] ]
) )

View File

@ -18,8 +18,10 @@ struct Fragment {
// Frame represents a data frame header // Frame represents a data frame header
struct Frame { struct Frame {
mut: mut:
header_len int = 2 // length of the websocket header part // length of the websocket header part
frame_size int = 2 // size of total frame header_len int = 2
// size of total frame
frame_size int = 2
fin bool // true if final fragment of message fin bool // true if final fragment of message
rsv1 bool // reserved for future use in websocket RFC rsv1 bool // reserved for future use in websocket RFC
rsv2 bool // reserved for future use in websocket RFC rsv2 bool // reserved for future use in websocket RFC

View File

@ -1,5 +1,4 @@
// use this test to test the websocket client in the autobahn test // use this test to test the websocket client in the autobahn test
module main module main
import x.websocket import x.websocket
@ -7,9 +6,7 @@ import x.websocket
fn main() { fn main() {
for i in 1 .. 304 { for i in 1 .. 304 {
println('\ncase: $i') println('\ncase: $i')
handle_case(i) or { handle_case(i) or { println('error should be ok: $err') }
println('error should be ok: $err')
}
} }
// update the reports // update the reports
uri := 'ws://autobahn_server:9001/updateReports?agent=v-client' uri := 'ws://autobahn_server:9001/updateReports?agent=v-client'
@ -32,7 +29,5 @@ fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
// We just wanna pass text and binary message back to autobahn // We just wanna pass text and binary message back to autobahn
return return
} }
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
} }

View File

@ -1,5 +1,4 @@
// use this test to test the websocket client in the autobahn test // use this test to test the websocket client in the autobahn test
module main module main
import x.websocket import x.websocket
@ -7,9 +6,7 @@ import x.websocket
fn main() { fn main() {
for i in 1 .. 304 { for i in 1 .. 304 {
println('\ncase: $i') println('\ncase: $i')
handle_case(i) or { handle_case(i) or { println('error should be ok: $err') }
println('error should be ok: $err')
}
} }
// update the reports // update the reports
// uri := 'wss://localhost:9002/updateReports?agent=v-client' // uri := 'wss://localhost:9002/updateReports?agent=v-client'
@ -34,7 +31,5 @@ fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
// We just wanna pass text and binary message back to autobahn // We just wanna pass text and binary message back to autobahn
return return
} }
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
} }

View File

@ -1,5 +1,4 @@
// use this to test websocket server to the autobahn test // use this to test websocket server to the autobahn test
module main module main
import x.websocket import x.websocket
@ -24,7 +23,5 @@ fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
// We just wanna pass text and binary message back to autobahn // We just wanna pass text and binary message back to autobahn
return return
} }
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
} }

View File

@ -1,5 +1,4 @@
// use this test to test the websocket client in the autobahn test // use this test to test the websocket client in the autobahn test
module main module main
import x.websocket import x.websocket
@ -7,9 +6,7 @@ import x.websocket
fn main() { fn main() {
for i in 1 .. 304 { for i in 1 .. 304 {
println('\ncase: $i') println('\ncase: $i')
handle_case(i) or { handle_case(i) or { println('error should be ok: $err') }
println('error should be ok: $err')
}
} }
// update the reports // update the reports
uri := 'ws://localhost:9001/updateReports?agent=v-client' uri := 'ws://localhost:9001/updateReports?agent=v-client'
@ -32,7 +29,5 @@ fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
// We just wanna pass text and binary message back to autobahn // We just wanna pass text and binary message back to autobahn
return return
} }
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
} }

View File

@ -1,5 +1,4 @@
// use this test to test the websocket client in the autobahn test // use this test to test the websocket client in the autobahn test
module main module main
import x.websocket import x.websocket
@ -7,9 +6,7 @@ import x.websocket
fn main() { fn main() {
for i in 1 .. 304 { for i in 1 .. 304 {
println('\ncase: $i') println('\ncase: $i')
handle_case(i) or { handle_case(i) or { println('error should be ok: $err') }
println('error should be ok: $err')
}
} }
// update the reports // update the reports
// uri := 'wss://localhost:9002/updateReports?agent=v-client' // uri := 'wss://localhost:9002/updateReports?agent=v-client'
@ -34,7 +31,5 @@ fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
// We just wanna pass text and binary message back to autobahn // We just wanna pass text and binary message back to autobahn
return return
} }
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
} }

View File

@ -5,9 +5,7 @@ import time
fn test_ws() { fn test_ws() {
go start_server() go start_server()
time.sleep_ms(100) time.sleep_ms(100)
ws_test('ws://localhost:30000') or { ws_test('ws://localhost:30000') or { assert false }
assert false
}
} }
fn start_server() ? { fn start_server() ? {
@ -24,9 +22,7 @@ fn start_server() ? {
return true return true
}) ? }) ?
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? { s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
ws.write(msg.payload, msg.opcode) or { ws.write(msg.payload, msg.opcode) or { panic(err) }
panic(err)
}
}) })
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? { s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
// not used // not used
@ -61,15 +57,11 @@ fn ws_test(uri string) ? {
println('Binary message: $msg') println('Binary message: $msg')
} }
}) })
ws.connect() or { ws.connect() or { panic('fail to connect') }
panic('fail to connect')
}
go ws.listen() go ws.listen()
text := ['a'].repeat(2) text := ['a'].repeat(2)
for msg in text { for msg in text {
ws.write(msg.bytes(), .text_frame) or { ws.write(msg.bytes(), .text_frame) or { panic('fail to write to websocket') }
panic('fail to write to websocket')
}
// sleep to give time to recieve response before send a new one // sleep to give time to recieve response before send a new one
time.sleep_ms(100) time.sleep_ms(100)
} }