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

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

This commit is contained in:
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/time/',
'vlib/vweb/',
'vlib/x/websocket/websocket_server.v',
'vlib/x/websocket/websocket_client.v',
'vlib/x/websocket/',
]
)

View File

@ -30,14 +30,14 @@ fn (mut ws Client) handshake() ? {
ws.debug_log('sending handshake: $handshake')
ws.socket_write(handshake_bytes) ?
ws.read_handshake(seckey) ?
unsafe {handshake_bytes.free()}
unsafe { handshake_bytes.free() }
}
// handle_server_handshake manages websocket server handshake process
fn (mut s Server) handle_server_handshake(mut c Client) ?(string, &ServerClient) {
msg := c.read_handshake_str() ?
handshake_response, client := s.parse_client_handshake(msg, mut c) ?
unsafe {msg.free()}
unsafe { msg.free() }
return handshake_response, client
}
@ -83,7 +83,7 @@ fn (mut s Server) parse_client_handshake(client_handshake string, mut c Client)
// we ignore other headers like protocol for now
}
}
unsafe {keys.free()}
unsafe { keys.free() }
}
if flags.len < 3 {
return error_with_code('invalid client handshake, $client_handshake', 4)
@ -130,7 +130,7 @@ fn (mut ws Client) read_handshake_str() ?string {
fn (mut ws Client) read_handshake(seckey string) ? {
mut msg := ws.read_handshake_str() ?
ws.check_handshake_response(msg, seckey) ?
unsafe {msg.free()}
unsafe { msg.free() }
}
// check_handshake_response checks the response from handshake and returns
@ -164,13 +164,13 @@ fn (mut ws Client) check_handshake_response(handshake_response string, seckey st
7)
}
ws.flags << .has_accept
unsafe {challenge.free()}
unsafe { challenge.free() }
}
else {}
}
unsafe {keys.free()}
unsafe { keys.free() }
}
unsafe {lines.free()}
unsafe { lines.free() }
if ws.flags.len < 3 {
ws.close(1002, 'invalid websocket HTTP headers') ?
return error_with_code('invalid websocket HTTP headers', 8)

View File

@ -18,8 +18,10 @@ struct Fragment {
// Frame represents a data frame header
struct Frame {
mut:
header_len int = 2 // length of the websocket header part
frame_size int = 2 // size of total frame
// length of the websocket header part
header_len int = 2
// size of total frame
frame_size int = 2
fin bool // true if final fragment of message
rsv1 bool // reserved for future use in websocket RFC
rsv2 bool // reserved for future use in websocket RFC
@ -31,7 +33,7 @@ mut:
}
const (
invalid_close_codes = [999, 1004, 1005, 1006, 1014, 1015, 1016, 1100, 2000, 2999, 5000, 65536]
invalid_close_codes = [999, 1004, 1005, 1006, 1014, 1015, 1016, 1100, 2000, 2999, 5000, 65536]
)
// validate_client validates client frame rules from RFC6455
@ -129,7 +131,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
opcode: OPCode(frame.opcode)
payload: frame_payload.clone()
}
unsafe {frame_payload.free()}
unsafe { frame_payload.free() }
return msg
}
// if the message is fragmented we just put it on fragments
@ -139,7 +141,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
data: frame_payload.clone()
opcode: frame.opcode
}
unsafe {frame_payload.free()}
unsafe { frame_payload.free() }
continue
}
if ws.fragments.len == 0 {
@ -152,7 +154,7 @@ pub fn (mut ws Client) read_next_message() ?Message {
opcode: OPCode(frame.opcode)
payload: frame_payload.clone()
}
unsafe {frame_payload.free()}
unsafe { frame_payload.free() }
return msg
}
defer {

View File

@ -1,38 +1,33 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 ..304 {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or {
println('error should be ok: $err')
}
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()?
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)?
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect()?
ws.listen()?
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
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)
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,40 +1,35 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 ..304 {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or {
println('error should be ok: $err')
}
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()?
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)?
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect()?
ws.listen()?
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
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)
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,5 +1,4 @@
// use this to test websocket server to the autobahn test
module main
import x.websocket
@ -12,19 +11,17 @@ fn main() {
fn handle_case(case_nr int) ? {
uri := 'ws://localhost:9002/runCase?case=$case_nr&agent=v-client'
mut ws := websocket.new_client(uri)?
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect()?
ws.listen()?
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
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)
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,38 +1,33 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 ..304 {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or {
println('error should be ok: $err')
}
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()?
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)?
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect()?
ws.listen()?
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
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)
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -1,40 +1,35 @@
// use this test to test the websocket client in the autobahn test
module main
import x.websocket
fn main() {
for i in 1 ..304 {
for i in 1 .. 304 {
println('\ncase: $i')
handle_case(i) or {
println('error should be ok: $err')
}
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()?
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)?
mut ws := websocket.new_client(uri) ?
ws.on_message(on_message)
ws.connect()?
ws.listen()?
ws.connect() ?
ws.listen() ?
}
fn on_message(mut ws websocket.Client, msg &websocket.Message)? {
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)
}
ws.write(msg.payload, msg.opcode) or { panic(err) }
}

View File

@ -22,7 +22,7 @@ fn htonl64(payload_len u64) []byte {
fn create_masking_key() []byte {
mask_bit := byte(rand.intn(255))
buf := []byte{len: 4, init: `0`}
unsafe {C.memcpy(buf.data, &mask_bit, 4)}
unsafe { C.memcpy(buf.data, &mask_bit, 4) }
return buf
}

View File

@ -5,9 +5,7 @@ import time
fn test_ws() {
go start_server()
time.sleep_ms(100)
ws_test('ws://localhost:30000') or {
assert false
}
ws_test('ws://localhost:30000') or { assert false }
}
fn start_server() ? {
@ -24,14 +22,12 @@ fn start_server() ? {
return true
}) ?
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
ws.write(msg.payload, msg.opcode) or {
panic(err)
}
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 {}
s.listen() or { }
}
// ws_test tests connect to the websocket server from websocket client
@ -61,15 +57,11 @@ fn ws_test(uri string) ? {
println('Binary message: $msg')
}
})
ws.connect() or {
panic('fail to connect')
}
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')
}
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_ms(100)
}