mirror of
https://github.com/logos-storage/nim-websock.git
synced 2026-01-09 09:03:13 +00:00
* Use seq[byte] to store data. * Working bytes conversion. * Refactor the code. * Add test. * Add websocket test and fix closing handshake. * Add MsgReader to read data in external buffer. * rework frame reading * don't do toTitleCase * fix examples * use byte for more comfort * rework message reading + api * fix tests * adding specific exception types * minor cleanup * fixing tests * more tests * check the fin flag at the correct place * info for debug * split data not encoded frames * more tests * wip - control messages * closing flow and more explicit exception handling * test close and pings * add tests task to nimble * adding ci * change recv semantics * add frame tests * remove echo * better frame tests * fix * fix * handle continuation frames properly * more close logic handling * wip tests * handle close reasons properly * test control frames encoding * don't pass ws to event callbacks * fix masking and use correct base64 encoding * fix ci * addressing review comments * fix client example * i386 ci fix * wip ci * fix reading offset * don't read if socket closed * fix ci * wip * don't read if socket is closed Co-authored-by: Arijit Das <arijit@status.im> Co-authored-by: Arijit Das <arijitad.in@gmail.com>
281 lines
6.2 KiB
Nim
281 lines
6.2 KiB
Nim
import unittest
|
|
|
|
include ../src/ws
|
|
include ../src/http
|
|
include ../src/random
|
|
|
|
# TODO: Fix Test.
|
|
|
|
var maskKey: array[4, char]
|
|
|
|
suite "Test data frames":
|
|
test "# 7bit length text":
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\1\8hi there")
|
|
|
|
test "# 7bit length text fin bit":
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\129\8hi there")
|
|
|
|
test "# 7bit length binary":
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\2\8hi there")
|
|
|
|
test "# 7bit length binary fin bit":
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\130\8hi there")
|
|
|
|
test "# 7bit length continuation":
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Cont,
|
|
mask: false,
|
|
data: toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\0\8hi there")
|
|
|
|
test "# 7+16 length text":
|
|
var data = ""
|
|
for i in 0..32:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\1\126\4\98" & data)
|
|
|
|
test "# 7+16 length text fin bit":
|
|
var data = ""
|
|
for i in 0..32:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\1\126\4\98" & data)
|
|
|
|
test "# 7+16 length binary":
|
|
var data = ""
|
|
for i in 0..32:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\2\126\4\98" & data)
|
|
|
|
test "# 7+16 length binary fin bit":
|
|
var data = ""
|
|
for i in 0..32:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\130\126\4\98" & data)
|
|
|
|
test "# 7+16 length continuation":
|
|
var data = ""
|
|
for i in 0..32:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Cont,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\0\126\4\98" & data)
|
|
|
|
test "# 7+64 length text":
|
|
var data = ""
|
|
for i in 0..3200:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\1\127\0\0\0\0\0\1\169\34" & data)
|
|
|
|
test "# 7+64 length fin bit":
|
|
var data = ""
|
|
for i in 0..3200:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\129\127\0\0\0\0\0\1\169\34" & data)
|
|
|
|
test "# 7+64 length binary":
|
|
var data = ""
|
|
for i in 0..3200:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\2\127\0\0\0\0\0\1\169\34" & data)
|
|
|
|
test "# 7+64 length binary fin bit":
|
|
var data = ""
|
|
for i in 0..3200:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Binary,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\130\127\0\0\0\0\0\1\169\34" & data)
|
|
|
|
test "# 7+64 length binary":
|
|
var data = ""
|
|
for i in 0..3200:
|
|
data.add "How are you this is the payload!!!"
|
|
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Cont,
|
|
mask: false,
|
|
data: toBytes(data),
|
|
maskKey: maskKey
|
|
)) == toBytes("\0\127\0\0\0\0\0\1\169\34" & data)
|
|
|
|
test "# masking":
|
|
let data = encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Text,
|
|
mask: true,
|
|
data: toBytes("hi there"),
|
|
maskKey: ['\xCF', '\xD8', '\x05', 'e']
|
|
))
|
|
|
|
check data == toBytes("\129\136\207\216\5e\167\177%\17\167\189w\0")
|
|
|
|
suite "Test control frames":
|
|
|
|
test "Close":
|
|
check encodeFrame(Frame(
|
|
fin: true,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Close,
|
|
mask: false,
|
|
data: @[3'u8, 232'u8] & toBytes("hi there"),
|
|
maskKey: maskKey
|
|
)) == toBytes("\136\10\3\232hi there")
|
|
|
|
test "Ping":
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Ping,
|
|
mask: false,
|
|
maskKey: maskKey
|
|
)) == toBytes("\9\0")
|
|
|
|
test "Pong":
|
|
check encodeFrame(Frame(
|
|
fin: false,
|
|
rsv1: false,
|
|
rsv2: false,
|
|
rsv3: false,
|
|
opcode: Opcode.Pong,
|
|
mask: false,
|
|
maskKey: maskKey
|
|
)) == toBytes("\10\0")
|