add deflate compression test cases

* binary data roundtrip
* text data roundtrip
This commit is contained in:
jangko 2021-06-20 11:16:48 +07:00
parent 14d8e51f53
commit 5a93ad7867
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 3725 additions and 0 deletions

5
.gitattributes vendored Normal file
View File

@ -0,0 +1,5 @@
# prevent automatic EOL conversion by git
# on test data, because it will make test
# failed
tests/extensions/data/* -crlf

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

View File

@ -0,0 +1,110 @@
## 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 std/os
import pkg/[chronos, stew/byteutils, stew/io2]
import ../asyncunit
import ../../ws/ws, ../helpers
import ../../ws/extensions/compression/deflate
const
dataFolder = "tests" / "extensions" / "data"
suite "permessage deflate compression":
var server: HttpServer
let address = initTAddress("127.0.0.1:8888")
let deflateFactory = deflateFactory()
teardown:
server.stop()
await server.closeWait()
test "text compression":
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:
let recvData = await ws.recv()
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(
host = "127.0.0.1",
port = Port(8888),
path = "/ws",
protocols = @["proto"],
factories = @[deflateFactory]
)
await client.send(textData, Opcode.Text)
var recvData: seq[byte]
while recvData.len < textData.len:
let res = await client.recv()
recvData.add res
if client.readyState == ReadyState.Closed:
break
check textData == recvData
await client.close()
test "binary data compression":
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:
let recvData = await ws.recv()
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(
host = "127.0.0.1",
port = Port(8888),
path = "/ws",
protocols = @["proto"],
factories = @[deflateFactory]
)
await client.send(binaryData, Opcode.Binary)
var recvData: seq[byte]
while recvData.len < binaryData.len:
let res = await client.recv()
recvData.add res
if client.readyState == ReadyState.Closed:
break
check binaryData == recvData
await client.close()

View File

@ -13,3 +13,4 @@ import ./testframes
import ./testutf8
import ./testextutils
import ./extensions/testexts
import ./extensions/testcompression