2021-06-01 02:39:14 +00:00
|
|
|
import std/[strutils, random]
|
2021-05-25 22:39:10 +00:00
|
|
|
import pkg/[
|
2021-06-01 02:39:14 +00:00
|
|
|
httputils,
|
2021-05-25 22:39:10 +00:00
|
|
|
chronos,
|
|
|
|
chronicles,
|
|
|
|
stew/byteutils]
|
2021-04-13 22:05:58 +00:00
|
|
|
|
2021-05-25 22:39:10 +00:00
|
|
|
import ../ws/ws
|
2021-06-01 02:39:14 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
import ./asynctest
|
|
|
|
import ./helpers
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let
|
2021-06-11 20:04:09 +00:00
|
|
|
address* = initTAddress("127.0.0.1:8888")
|
|
|
|
|
|
|
|
var
|
|
|
|
server: HttpServer
|
2021-06-01 02:39:14 +00:00
|
|
|
|
2021-03-18 15:30:21 +00:00
|
|
|
suite "Test handshake":
|
2021-05-21 08:56:13 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
test "Should not select incorrect protocol":
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let
|
|
|
|
server = WSServer.new(protos = ["proto"])
|
|
|
|
ws = await server.handleRequest(request)
|
|
|
|
check ws.proto == ""
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
|
|
|
protocols = @["wrongproto"])
|
|
|
|
|
|
|
|
check session.proto == ""
|
|
|
|
await session.stream.closeWait()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Test for incorrect version":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["ws"])
|
2021-06-01 02:39:14 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
expect WSVersionError:
|
2021-05-25 14:02:32 +00:00
|
|
|
discard await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
|
|
|
expect WSFailedUpgradeError:
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-21 08:56:13 +00:00
|
|
|
version = 14)
|
|
|
|
|
|
|
|
test "Test for client headers":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-21 08:56:13 +00:00
|
|
|
check request.headers.getString("Connection").toUpperAscii() ==
|
|
|
|
"Upgrade".toUpperAscii()
|
|
|
|
check request.headers.getString("Upgrade").toUpperAscii() ==
|
|
|
|
"websocket".toUpperAscii()
|
|
|
|
check request.headers.getString("Cache-Control").toUpperAscii() ==
|
|
|
|
"no-cache".toUpperAscii()
|
|
|
|
check request.headers.getString("Sec-WebSocket-Version") == $WSDefaultVersion
|
|
|
|
check request.headers.contains("Sec-WebSocket-Key")
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await request.sendError(Http500)
|
|
|
|
|
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
|
|
|
expect WSFailedUpgradeError:
|
2021-06-01 02:39:14 +00:00
|
|
|
discard await connectClient()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
test "Test for incorrect scheme":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
2021-05-21 08:56:13 +00:00
|
|
|
expect WSProtoMismatchError:
|
2021-06-01 02:39:14 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
check ws.readyState == ReadyState.Closed
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
let uri = "wx://127.0.0.1:8888/ws"
|
|
|
|
expect WSWrongUriSchemeError:
|
|
|
|
discard await WebSocket.connect(
|
|
|
|
parseUri(uri),
|
|
|
|
protocols = @["proto"])
|
2021-04-14 11:07:38 +00:00
|
|
|
|
|
|
|
suite "Test transmission":
|
2021-05-21 08:56:13 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Server - test reading simple frame":
|
|
|
|
let testString = "Hello!"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-22 09:04:40 +00:00
|
|
|
let servRes = await ws.recv()
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
check string.fromBytes(servRes) == testString
|
2021-06-01 02:39:14 +00:00
|
|
|
await ws.waitForClose()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
await session.send(testString)
|
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send text message message with payload of length 65535":
|
|
|
|
let testString = rndStr(65535)
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-22 09:04:40 +00:00
|
|
|
let servRes = await ws.recv()
|
2021-05-21 08:56:13 +00:00
|
|
|
check string.fromBytes(servRes) == testString
|
2021-06-01 02:39:14 +00:00
|
|
|
await ws.waitForClose()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
await session.send(testString)
|
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Client - test reading simple frame":
|
2021-06-01 02:39:14 +00:00
|
|
|
let testString = "Hello!"
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await ws.send(testString)
|
|
|
|
await ws.close()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
var clientRes = await session.recv()
|
|
|
|
check string.fromBytes(clientRes) == testString
|
|
|
|
await waitForClose(session)
|
2021-04-13 22:05:58 +00:00
|
|
|
|
2021-04-14 11:07:38 +00:00
|
|
|
suite "Test ping-pong":
|
2021-05-21 08:56:13 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
2021-06-01 02:39:14 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Server - test ping-pong control messages":
|
2021-05-21 08:56:13 +00:00
|
|
|
var ping, pong = false
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-06-11 20:04:09 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
|
|
|
pong = true
|
|
|
|
)
|
2021-05-25 14:02:32 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await ws.ping()
|
|
|
|
await ws.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-06-11 20:04:09 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
|
|
|
ping = true
|
2021-05-21 08:56:13 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await waitForClose(session)
|
2021-05-21 08:56:13 +00:00
|
|
|
check:
|
|
|
|
ping
|
|
|
|
pong
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Client - test ping-pong control messages":
|
2021-06-01 02:39:14 +00:00
|
|
|
var ping, pong = false
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-06-11 20:04:09 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
|
|
|
ping = true
|
2021-06-01 02:39:14 +00:00
|
|
|
)
|
2021-05-21 08:41:12 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
await waitForClose(ws)
|
|
|
|
check:
|
|
|
|
ping
|
|
|
|
pong
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-06-11 20:04:09 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
|
|
|
pong = true
|
2021-06-01 02:39:14 +00:00
|
|
|
)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await session.ping()
|
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send ping with small text payload":
|
|
|
|
let testData = toBytes("Hello, world!")
|
2021-05-21 08:56:13 +00:00
|
|
|
var ping, pong = false
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-05-22 09:04:40 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
2021-06-11 20:04:09 +00:00
|
|
|
ping = data == testData)
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-22 09:04:40 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
2021-05-22 08:26:45 +00:00
|
|
|
pong = true
|
2021-05-21 08:56:13 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await session.ping(testData)
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
2021-06-11 20:04:09 +00:00
|
|
|
check:
|
|
|
|
ping
|
|
|
|
pong
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Test ping payload message length":
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
|
|
|
|
expect WSPayloadTooLarge:
|
|
|
|
discard await ws.recv()
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let str = rndStr(126)
|
|
|
|
let session = await connectClient()
|
|
|
|
await session.ping(str.toBytes())
|
|
|
|
await session.close()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
|
|
|
suite "Test framing":
|
2021-05-21 08:56:13 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
|
|
|
test "should split message into frames":
|
|
|
|
let testString = "1234567890"
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-11 20:04:09 +00:00
|
|
|
let frame1 = await ws.readFrame(@[])
|
2021-05-21 08:56:13 +00:00
|
|
|
check not isNil(frame1)
|
|
|
|
var data1 = newSeq[byte](frame1.remainder().int)
|
|
|
|
let read1 = await ws.stream.reader.readOnce(addr data1[0], data1.len)
|
|
|
|
check read1 == 5
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
let frame2 = await ws.readFrame(@[])
|
2021-05-21 08:56:13 +00:00
|
|
|
check not isNil(frame2)
|
|
|
|
var data2 = newSeq[byte](frame2.remainder().int)
|
|
|
|
let read2 = await ws.stream.reader.readOnce(addr data2[0], data2.len)
|
|
|
|
check read2 == 5
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-21 08:56:13 +00:00
|
|
|
frameSize = 5)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(testString)
|
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "should fail to read past max message size":
|
2021-06-01 02:39:14 +00:00
|
|
|
let testString = "1234567890"
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
await ws.send(testString)
|
|
|
|
await ws.close()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-04-13 22:05:58 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
expect WSMaxMessageSizeError:
|
|
|
|
discard await session.recv(5)
|
|
|
|
await waitForClose(session)
|
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
suite "Test Closing":
|
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
2021-03-18 15:30:21 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
test "Server closing":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
await ws.close()
|
2021-04-13 22:05:58 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
await waitForClose(session)
|
|
|
|
check session.readyState == ReadyState.Closed
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
test "Server closing with status":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
proc closeServer(status: StatusCodes, reason: string): CloseResult{.gcsafe,
|
2021-06-01 02:39:14 +00:00
|
|
|
raises: [Defect].} =
|
2021-05-21 08:56:13 +00:00
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusTooLarge
|
2021-06-01 02:39:14 +00:00
|
|
|
check reason == "Message too big!"
|
2021-05-21 08:56:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
return (StatusFulfilled, "")
|
2021-06-01 02:39:14 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
onClose = closeServer
|
|
|
|
)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
await ws.close()
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
proc clientClose(status: StatusCodes, reason: string): CloseResult {.gcsafe,
|
2021-06-01 02:39:14 +00:00
|
|
|
raises: [Defect].} =
|
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusFulfilled
|
|
|
|
return (StatusTooLarge, "Message too big!")
|
2021-06-01 02:39:14 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
|
|
|
onClose = clientClose)
|
|
|
|
|
|
|
|
await waitForClose(session)
|
|
|
|
check session.readyState == ReadyState.Closed
|
|
|
|
|
|
|
|
test "Client closing":
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Client closing with status":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-06-11 20:04:09 +00:00
|
|
|
proc closeServer(status: StatusCodes, reason: string): CloseResult{.gcsafe,
|
2021-05-21 08:56:13 +00:00
|
|
|
raises: [Defect].} =
|
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusFulfilled
|
|
|
|
return (StatusTooLarge, "Message too big!")
|
2021-05-21 08:56:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
onClose = closeServer
|
|
|
|
)
|
|
|
|
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
proc clientClose(status: StatusCodes, reason: string): CloseResult {.gcsafe,
|
2021-05-21 08:56:13 +00:00
|
|
|
raises: [Defect].} =
|
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusTooLarge
|
2021-05-21 08:56:13 +00:00
|
|
|
check reason == "Message too big!"
|
2021-06-11 20:04:09 +00:00
|
|
|
return (StatusFulfilled, "")
|
2021-05-21 08:56:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-21 08:56:13 +00:00
|
|
|
onClose = clientClose)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
|
|
|
check session.readyState == ReadyState.Closed
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Server closing with valid close code 3999":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await ws.close(code = StatusCodes(StatusLibsCodes.high))
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
proc closeClient(status: StatusCodes, reason: string): CloseResult
|
|
|
|
{.gcsafe, raises: [Defect].} =
|
2021-05-21 08:56:13 +00:00
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusCodes(StatusLibsCodes.high)
|
|
|
|
return (StatusCodes(StatusLibsCodes.high), "Reserved StatusCodes")
|
2021-05-21 08:56:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-21 08:56:13 +00:00
|
|
|
onClose = closeClient)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await waitForClose(session)
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Client closing with valid close code 3999":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-06-11 20:04:09 +00:00
|
|
|
proc closeServer(status: StatusCodes, reason: string): CloseResult{.gcsafe,
|
2021-05-21 08:56:13 +00:00
|
|
|
raises: [Defect].} =
|
|
|
|
try:
|
2021-06-11 20:04:09 +00:00
|
|
|
check status == StatusCodes(3999)
|
|
|
|
return (StatusCodes(3999), "Reserved StatusCodes")
|
2021-05-21 08:56:13 +00:00
|
|
|
except Exception as exc:
|
|
|
|
raise newException(Defect, exc.msg)
|
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
onClose = closeServer
|
|
|
|
)
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-06-11 20:04:09 +00:00
|
|
|
await session.close(code = StatusCodes(3999))
|
2021-05-21 08:56:13 +00:00
|
|
|
|
|
|
|
test "Server closing with Payload of length 2":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
# Close with payload of length 2
|
|
|
|
await ws.close(reason = "HH")
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let session = await connectClient()
|
|
|
|
await waitForClose(session)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
test "Client closing with Payload of length 2":
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
# Close with payload of length 2
|
|
|
|
await session.close(reason = "HH")
|
|
|
|
|
2021-05-22 09:04:40 +00:00
|
|
|
suite "Test Payload":
|
2021-05-21 08:56:13 +00:00
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-05-21 08:56:13 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Test payload of length 0":
|
|
|
|
let emptyStr = ""
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-11 20:04:09 +00:00
|
|
|
let servRes = await ws.recv()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
check:
|
|
|
|
servRes.len == 0
|
|
|
|
string.fromBytes(servRes) == emptyStr
|
2021-05-22 08:26:45 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
await ws.send(emptyStr)
|
|
|
|
await ws.waitForClose()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-06-11 20:04:09 +00:00
|
|
|
await session.send(emptyStr)
|
|
|
|
let clientRes = await session.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientRes.len == 0
|
|
|
|
string.fromBytes(clientRes) == emptyStr
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Test multiple payloads of length 0":
|
2021-05-21 08:56:13 +00:00
|
|
|
let emptyStr = ""
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-11 20:04:09 +00:00
|
|
|
for _ in 0..<3:
|
|
|
|
let servRes = await ws.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
servRes.len == 0
|
|
|
|
string.fromBytes(servRes) == emptyStr
|
|
|
|
|
|
|
|
for i in 0..3:
|
|
|
|
await ws.send(emptyStr)
|
2021-05-25 14:02:32 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
for i in 0..3:
|
|
|
|
await session.send(emptyStr)
|
|
|
|
|
|
|
|
for _ in 0..<3:
|
|
|
|
let clientRes = await session.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
clientRes.len == 0
|
|
|
|
string.fromBytes(clientRes) == emptyStr
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send two fragments":
|
|
|
|
var ping, pong = false
|
|
|
|
let testString = "1234567890"
|
|
|
|
let msg = toBytes(testString)
|
|
|
|
let maxFrameSize = 5
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(protos = ["proto"])
|
2021-06-11 20:04:09 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-11 20:04:09 +00:00
|
|
|
let respData = await ws.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
string.fromBytes(respData) == testString
|
2021-05-25 14:02:32 +00:00
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
|
|
|
frameSize = maxFrameSize)
|
|
|
|
|
|
|
|
let maskKey = genMaskKey(newRng())
|
|
|
|
await session.stream.writer.write(
|
|
|
|
(await Frame(
|
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: true,
|
|
|
|
data: msg[0..4],
|
|
|
|
maskKey: maskKey)
|
|
|
|
.encode()))
|
|
|
|
|
|
|
|
await session.stream.writer.write(
|
|
|
|
(await Frame(
|
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Cont,
|
|
|
|
mask: true,
|
|
|
|
data: msg[5..9],
|
|
|
|
maskKey: maskKey)
|
|
|
|
.encode()))
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send two fragments with a ping with payload in-between":
|
2021-05-21 08:56:13 +00:00
|
|
|
var ping, pong = false
|
2021-06-11 20:04:09 +00:00
|
|
|
let testString = "1234567890"
|
|
|
|
let msg = toBytes(testString)
|
|
|
|
let maxFrameSize = 5
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-05-22 08:26:45 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
2021-06-11 20:04:09 +00:00
|
|
|
ping = true
|
|
|
|
)
|
2021-04-14 11:07:38 +00:00
|
|
|
|
2021-05-25 14:02:32 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-06-11 20:04:09 +00:00
|
|
|
let respData = await ws.recv()
|
|
|
|
check:
|
|
|
|
string.fromBytes(respData) == testString
|
|
|
|
|
2021-05-21 08:56:13 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-05-21 08:56:13 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-06-11 20:04:09 +00:00
|
|
|
frameSize = maxFrameSize,
|
2021-05-22 09:04:40 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
2021-05-22 08:26:45 +00:00
|
|
|
pong = true
|
2021-05-21 08:56:13 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
let maskKey = genMaskKey(newRng())
|
|
|
|
await session.stream.writer.write(
|
|
|
|
(await Frame(
|
|
|
|
fin: false,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Text,
|
|
|
|
mask: true,
|
|
|
|
data: msg[0..4],
|
|
|
|
maskKey: maskKey)
|
|
|
|
.encode()))
|
|
|
|
|
|
|
|
await session.ping()
|
|
|
|
|
|
|
|
await session.stream.writer.write(
|
|
|
|
(await Frame(
|
|
|
|
fin: true,
|
|
|
|
rsv1: false,
|
|
|
|
rsv2: false,
|
|
|
|
rsv3: false,
|
|
|
|
opcode: Opcode.Cont,
|
|
|
|
mask: true,
|
|
|
|
data: msg[5..9],
|
|
|
|
maskKey: maskKey)
|
|
|
|
.encode()))
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.close()
|
2021-05-21 08:56:13 +00:00
|
|
|
check:
|
|
|
|
ping
|
|
|
|
pong
|
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send text message with multiple frames":
|
|
|
|
const FrameSize = 3000
|
|
|
|
let testData = rndStr(FrameSize * 3)
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
let res = await ws.recv()
|
|
|
|
|
|
|
|
check ws.binary == false
|
|
|
|
await ws.send(res, Opcode.Text)
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let ws = await connectClient(
|
|
|
|
address = address,
|
|
|
|
frameSize = FrameSize
|
|
|
|
)
|
|
|
|
|
|
|
|
await ws.send(testData)
|
|
|
|
let echoed = await ws.recv()
|
|
|
|
await ws.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
string.fromBytes(echoed) == testData
|
|
|
|
ws.binary == false
|
2021-04-15 08:00:52 +00:00
|
|
|
|
|
|
|
suite "Test Binary message with Payload":
|
|
|
|
teardown:
|
2021-06-01 02:39:14 +00:00
|
|
|
server.stop()
|
2021-04-15 08:00:52 +00:00
|
|
|
await server.closeWait()
|
|
|
|
|
|
|
|
test "Test binary message with single empty payload message":
|
|
|
|
let emptyData = newSeq[byte](0)
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-22 09:04:40 +00:00
|
|
|
let servRes = await ws.recv()
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
check:
|
|
|
|
servRes == emptyData
|
2021-05-22 09:04:40 +00:00
|
|
|
ws.binary == true
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-04-15 08:00:52 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
|
|
|
await session.send(emptyData, Opcode.Binary)
|
|
|
|
await session.close()
|
2021-04-15 08:00:52 +00:00
|
|
|
|
|
|
|
test "Test binary message with multiple empty payload":
|
|
|
|
let emptyData = newSeq[byte](0)
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
|
2021-05-22 09:04:40 +00:00
|
|
|
let servRes = await ws.recv()
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
check:
|
|
|
|
servRes == emptyData
|
2021-05-22 09:04:40 +00:00
|
|
|
ws.binary == true
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-04-15 08:00:52 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient()
|
2021-04-15 08:00:52 +00:00
|
|
|
|
|
|
|
for i in 0..3:
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(emptyData, Opcode.Binary)
|
|
|
|
await session.close()
|
2021-04-15 08:00:52 +00:00
|
|
|
|
|
|
|
test "Send binary data with small text payload":
|
|
|
|
let testData = rndBin(10)
|
2021-06-11 20:04:09 +00:00
|
|
|
trace "testData", testData = testData
|
2021-04-15 08:00:52 +00:00
|
|
|
var ping, pong = false
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-05-22 09:04:40 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
|
|
|
ping = true
|
2021-04-15 08:00:52 +00:00
|
|
|
)
|
2021-05-25 14:02:32 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-22 09:04:40 +00:00
|
|
|
|
|
|
|
let res = await ws.recv()
|
2021-04-15 08:00:52 +00:00
|
|
|
check:
|
|
|
|
res == testData
|
2021-05-22 09:04:40 +00:00
|
|
|
ws.binary == true
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-04-15 08:00:52 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-22 09:04:40 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
|
|
|
pong = true
|
2021-04-15 08:00:52 +00:00
|
|
|
)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(testData, Opcode.Binary)
|
|
|
|
await session.close()
|
2021-04-15 08:00:52 +00:00
|
|
|
|
|
|
|
test "Send binary message message with payload of length 125":
|
|
|
|
let testData = rndBin(125)
|
|
|
|
var ping, pong = false
|
2021-06-01 02:39:14 +00:00
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
2021-05-25 14:02:32 +00:00
|
|
|
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
2021-05-22 09:04:40 +00:00
|
|
|
onPing = proc(data: openArray[byte]) =
|
|
|
|
ping = true
|
2021-04-15 08:00:52 +00:00
|
|
|
)
|
2021-05-25 14:02:32 +00:00
|
|
|
let ws = await server.handleRequest(request)
|
2021-05-22 09:04:40 +00:00
|
|
|
|
|
|
|
let res = await ws.recv()
|
2021-04-15 08:00:52 +00:00
|
|
|
check:
|
|
|
|
res == testData
|
2021-05-22 09:04:40 +00:00
|
|
|
ws.binary == true
|
|
|
|
|
2021-04-15 08:00:52 +00:00
|
|
|
await waitForClose(ws)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
2021-04-15 08:00:52 +00:00
|
|
|
server.start()
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
let session = await connectClient(
|
|
|
|
address = initTAddress("127.0.0.1:8888"),
|
2021-05-22 09:04:40 +00:00
|
|
|
onPong = proc(data: openArray[byte]) =
|
|
|
|
pong = true
|
2021-04-15 08:00:52 +00:00
|
|
|
)
|
|
|
|
|
2021-06-01 02:39:14 +00:00
|
|
|
await session.send(testData, Opcode.Binary)
|
|
|
|
await session.close()
|
2021-04-15 08:00:52 +00:00
|
|
|
|
2021-06-11 20:04:09 +00:00
|
|
|
test "Send binary message with multiple frames":
|
|
|
|
const FrameSize = 3000
|
|
|
|
let testData = rndBin(FrameSize * 3)
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
check request.uri.path == WSPath
|
|
|
|
|
|
|
|
let server = WSServer.new(protos = ["proto"])
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
let res = await ws.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
ws.binary == true
|
|
|
|
res == testData
|
|
|
|
|
|
|
|
await ws.send(res, Opcode.Binary)
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
|
|
|
server = createServer(
|
|
|
|
address = address,
|
|
|
|
handler = handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let ws = await connectClient(
|
|
|
|
address = address,
|
|
|
|
frameSize = FrameSize
|
|
|
|
)
|
|
|
|
|
|
|
|
await ws.send(testData, Opcode.Binary)
|
|
|
|
let echoed = await ws.recv()
|
|
|
|
|
|
|
|
check:
|
|
|
|
echoed == testData
|
|
|
|
|
|
|
|
await ws.close()
|
|
|
|
|
|
|
|
check:
|
|
|
|
echoed == testData
|
|
|
|
ws.binary == true
|