-
Notifications
You must be signed in to change notification settings - Fork 23
/
test_protocol_arr.nim
45 lines (39 loc) · 1.4 KB
/
test_protocol_arr.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
include ../src/ws
block:
# Start server
proc cb(req: Request) {.async.} =
if req.headers.hasKey("Sec-WebSocket-Protocol"):
if "foo" in req.headers["Sec-WebSocket-Protocol"].toSeq():
var ws = await newWebSocket(req, protocol = "foo")
await ws.send("Welcome foo1")
ws.close()
elif "foo2" in req.headers["Sec-WebSocket-Protocol"].toSeq():
var ws = await newWebSocket(req, protocol = "foo2")
await ws.send("Welcome foo2")
ws.close()
else:
await req.respond(Http404, "Invalid")
var server = newAsyncHttpServer()
asyncCheck server.serve(Port(9002), cb)
block:
var ws = waitFor newWebSocket("ws://127.0.0.1:9002/ws", protocols = @["foo", "foo2"])
assert ws.protocol == "foo"
assert waitFor(ws.receiveStrPacket()) == "Welcome foo1"
ws.close()
block:
var ws = waitFor newWebSocket("ws://127.0.0.1:9002/ws", protocols = @["foo2"])
assert ws.protocol == "foo2"
assert waitFor(ws.receiveStrPacket()) == "Welcome foo2"
ws.close()
block:
var ws = waitFor newWebSocket("ws://127.0.0.1:9002/ws", protocols = @["foo"])
assert ws.protocol == "foo"
assert waitFor(ws.receiveStrPacket()) == "Welcome foo1"
ws.close()
block:
try:
var ws = waitFor newWebSocket("ws://127.0.0.1:9002/ws", protocols = @["notfoo"])
assert false
except WebSocketError:
assert true
server.close()