2021-06-27 06:35:36 +00:00
|
|
|
## nim-websock
|
2022-07-21 15:38:13 +00:00
|
|
|
## Copyright (c) 2021-2022 Status Research & Development GmbH
|
2021-06-20 04:16:48 +00:00
|
|
|
## 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 std/os
|
2022-07-21 15:38:13 +00:00
|
|
|
import pkg/[chronos/unittest2/asynctests, stew/io2]
|
2021-09-21 07:44:07 +00:00
|
|
|
import ../../websock/websock
|
2021-06-27 06:35:36 +00:00
|
|
|
import ../../websock/extensions/compression/deflate
|
2021-06-20 04:16:48 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
dataFolder = "tests" / "extensions" / "data"
|
|
|
|
|
|
|
|
suite "permessage deflate compression":
|
2022-06-20 07:19:38 +00:00
|
|
|
setup:
|
|
|
|
var server: HttpServer
|
|
|
|
let address = initTAddress("127.0.0.1:8888")
|
|
|
|
let deflateFactory = deflateFactory()
|
2021-06-20 04:16:48 +00:00
|
|
|
|
|
|
|
teardown:
|
2022-06-20 07:19:38 +00:00
|
|
|
if server != nil:
|
|
|
|
server.stop()
|
|
|
|
waitFor server.closeWait()
|
2021-06-20 04:16:48 +00:00
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "text compression":
|
2021-06-20 04:16:48 +00:00
|
|
|
let textData = io2.readAllBytes(dataFolder / "alice29.txt").get()
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
factories = [deflateFactory],
|
|
|
|
)
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
|
|
|
|
while ws.readyState != ReadyState.Closed:
|
2021-08-04 16:23:56 +00:00
|
|
|
let recvData = await ws.recvMsg()
|
2021-06-20 04:16:48 +00:00
|
|
|
if ws.readyState == ReadyState.Closed:
|
|
|
|
break
|
|
|
|
await ws.send(recvData,
|
|
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
|
|
|
|
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let client = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
host = "127.0.0.1:8888",
|
2021-06-20 04:16:48 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
factories = @[deflateFactory]
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send(textData, Opcode.Text)
|
|
|
|
|
|
|
|
var recvData: seq[byte]
|
|
|
|
while recvData.len < textData.len:
|
2021-08-04 16:23:56 +00:00
|
|
|
let res = await client.recvMsg()
|
2021-06-20 04:16:48 +00:00
|
|
|
recvData.add res
|
|
|
|
if client.readyState == ReadyState.Closed:
|
|
|
|
break
|
|
|
|
|
|
|
|
check textData == recvData
|
|
|
|
await client.close()
|
|
|
|
|
2022-06-20 07:19:38 +00:00
|
|
|
asyncTest "binary data compression":
|
2021-06-20 04:16:48 +00:00
|
|
|
let binaryData = io2.readAllBytes(dataFolder / "fireworks.jpg").get()
|
|
|
|
proc handle(request: HttpRequest) {.async.} =
|
|
|
|
let server = WSServer.new(
|
|
|
|
protos = ["proto"],
|
|
|
|
factories = [deflateFactory],
|
|
|
|
)
|
|
|
|
let ws = await server.handleRequest(request)
|
|
|
|
while ws.readyState != ReadyState.Closed:
|
2021-08-04 16:23:56 +00:00
|
|
|
let recvData = await ws.recvMsg()
|
2021-06-20 04:16:48 +00:00
|
|
|
if ws.readyState == ReadyState.Closed:
|
|
|
|
break
|
|
|
|
await ws.send(recvData,
|
|
|
|
if ws.binary: Opcode.Binary else: Opcode.Text)
|
|
|
|
|
|
|
|
server = HttpServer.create(
|
|
|
|
address,
|
|
|
|
handle,
|
|
|
|
flags = {ReuseAddr})
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
let client = await WebSocket.connect(
|
2021-07-15 00:51:39 +00:00
|
|
|
host = "127.0.0.1:8888",
|
2021-06-20 04:16:48 +00:00
|
|
|
path = "/ws",
|
|
|
|
protocols = @["proto"],
|
|
|
|
factories = @[deflateFactory]
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.send(binaryData, Opcode.Binary)
|
|
|
|
|
|
|
|
var recvData: seq[byte]
|
|
|
|
while recvData.len < binaryData.len:
|
2021-08-04 16:23:56 +00:00
|
|
|
let res = await client.recvMsg()
|
2021-06-20 04:16:48 +00:00
|
|
|
recvData.add res
|
|
|
|
if client.readyState == ReadyState.Closed:
|
|
|
|
break
|
|
|
|
|
|
|
|
check binaryData == recvData
|
|
|
|
|
|
|
|
await client.close()
|