2021-06-16 10:44:38 +00:00
|
|
|
## nim-ws
|
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import pkg/[chronos, stew/byteutils]
|
|
|
|
import ../asyncunit
|
|
|
|
import ./base64ext, ./hexext
|
|
|
|
import ../../ws/ws, ../helpers
|
|
|
|
|
2021-06-20 04:27:50 +00:00
|
|
|
suite "multiple extensions flow":
|
2021-06-16 10:44:38 +00:00
|
|
|
var server: HttpServer
|
|
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
|
|
let hexFactory = hexFactory()
|
|
|
|
let base64Factory = base64Factory(padding = true)
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
server.stop()
|
|
|
|
await server.closeWait()
|
|
|
|
|
|
|
|
test "hex to base64 ext flow":
|
|
|
|
let testData = "hello world"
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
factories = [hexFactory, base64Factory],
|
|
|
|
)
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
let recvData = await ws.recv()
|
|
|
|
await ws.send(recvData,
|
|
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let client = await WebSocket.connect(
|
|
|
|
host = "127.0.0.1",
|
|
|
|
port = Port(8888),
|
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
factories = @[hexFactory, base64Factory]
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send(testData)
|
|
|
|
let res = await client.recv()
|
|
|
|
check testData.toBytes() == res
|
|
|
|
await client.close()
|
|
|
|
|
|
|
|
test "base64 to hex ext flow":
|
|
|
|
let testData = "hello world"
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
factories = [hexFactory, base64Factory],
|
|
|
|
)
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
let recvData = await ws.recv()
|
|
|
|
await ws.send(recvData,
|
|
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
|
|
|
|
|
|
|
await waitForClose(ws)
|
|
|
|
|
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let client = await WebSocket.connect(
|
|
|
|
host = "127.0.0.1",
|
|
|
|
port = Port(8888),
|
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
factories = @[base64Factory, hexFactory]
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send(testData)
|
|
|
|
let res = await client.recv()
|
|
|
|
check testData.toBytes() == res
|
|
|
|
await client.close()
|