Repository navigation
Expand file tree
/
Copy pathwebsocket_spec.lua
More file actions
324 lines (321 loc) · 8.8 KB
/
Copy pathwebsocket_spec.lua
File metadata and controls
324 lines (321 loc) · 8.8 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
describe("http.websocket module's internal functions work", function()
local websocket = require "http.websocket"
it("build_frame works for simple cases", function()
-- Examples from RFC 6455 Section 5.7
-- A single-frame unmasked text message
assert.same(string.char(0x81,0x05,0x48,0x65,0x6c,0x6c,0x6f), websocket.build_frame {
FIN = true;
MASK = false;
opcode = 0x1;
data = "Hello";
})
-- A single-frame masked text message
assert.same(string.char(0x81,0x85,0x37,0xfa,0x21,0x3d,0x7f,0x9f,0x4d,0x51,0x58), websocket.build_frame {
FIN = true;
MASK = true;
key = {0x37,0xfa,0x21,0x3d};
opcode = 0x1;
data = "Hello";
})
end)
it("build_frame validates opcode", function()
assert.has.errors(function()
websocket.build_frame { opcode = -1; }
end)
assert.has.errors(function()
websocket.build_frame { opcode = 16; }
end)
end)
it("build_frame validates data length", function()
assert.has.errors(function()
websocket.build_frame {
opcode = 0x8;
data = ("f"):rep(200);
}
end)
end)
it("build_close works for common case", function()
assert.same({
opcode = 0x8;
FIN = true;
MASK = false;
data = "\3\232";
}, websocket.build_close(1000, nil, false))
assert.same({
opcode = 0x8;
FIN = true;
MASK = false;
data = "\3\232error";
}, websocket.build_close(1000, "error", false))
end)
it("build_close validates string length", function()
assert.has.errors(function() websocket.build_close(1000, ("f"):rep(200), false) end)
end)
it("build_close can generate frames without a code", function()
assert.same({
opcode = 0x8;
FIN = true;
MASK = false;
data = "";
}, websocket.build_close(nil, nil, false))
end)
it("parse_close works", function()
assert.same({nil, nil}, {websocket.parse_close ""})
assert.same({1000, nil}, {websocket.parse_close "\3\232"})
assert.same({1000, "error"}, {websocket.parse_close "\3\232error"})
end)
end)
describe("http.websocket", function()
local websocket = require "http.websocket"
it("__tostring works", function()
local ws = websocket.new_from_uri("wss://example.com")
assert.same("http.websocket{", tostring(ws):match("^.-%{"))
end)
it("close on a new websocket doesn't throw an error", function()
local ws = websocket.new_from_uri("wss://example.com")
ws:close() -- this shouldn't throw
end)
end)
describe("http.websocket module two sided tests", function()
local server = require "http.server"
local util = require "http.util"
local websocket = require "http.websocket"
local cs = require "cqueues.socket"
local cqueues = require "cqueues"
local function new_pair()
local c, s = cs.pair()
local ws_client = websocket.new("client")
ws_client.socket = c
ws_client.readyState = 1
local ws_server = websocket.new("server")
ws_server.socket = s
ws_server.readyState = 1
return ws_client, ws_server
end
it("works with a socketpair", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send("hello"))
assert.same("world", c:receive())
assert(c:close())
end)
cq:wrap(function()
assert.same("hello", s:receive())
assert(s:send("world"))
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("doesn't fail when data contains a \\r\\n", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send("hel\r\nlo"))
assert.same("wor\r\nld", c:receive())
assert(c:close())
end)
cq:wrap(function()
assert.same("hel\r\nlo", s:receive())
assert(s:send("wor\r\nld"))
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
local function send_receive_test(name, data, data_type)
it(name, function()
data_type = data_type or "text"
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send(data, data_type))
assert.same({data, data_type}, {assert(c:receive())})
assert(c:close())
end)
cq:wrap(function()
assert.same({data, data_type}, {assert(s:receive())})
assert(s:send(data, data_type))
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
end
send_receive_test("works with small size frames", "f")
send_receive_test("works with medium size frames", ("f"):rep(200))
send_receive_test("works with large size frames", ("f"):rep(100000))
send_receive_test("works with binary frames", "\0\1\127\255", "binary")
it("fails when text isn't valid utf8", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send("\230", "text"))
local ok, _, errno = c:receive()
assert.same(nil, ok)
assert.same(1007, errno)
assert(c:close())
end)
cq:wrap(function()
local ok, _, errno = s:receive()
assert.same(nil, ok)
assert.same(1007, errno)
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("fails when text isn't valid utf8 (utf16 surrogates)", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send("\237\160\128", "text"))
local ok, _, errno = c:receive()
assert.same(nil, ok)
assert.same(1007, errno)
assert(c:close())
end)
cq:wrap(function()
local ok, _, errno = s:receive()
assert.same(nil, ok)
assert.same(1007, errno)
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("doesn't allow invalid utf8 in close messages", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:close(1000, "\237\160\128"))
end)
cq:wrap(function()
local ok, _, errno = s:receive()
assert.same(nil, ok)
assert.same(1007, errno)
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
for _, flag in ipairs{"RSV1", "RSV2", "RSV3"} do
it("fails correctly on "..flag.." flag set", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send_frame({
opcode = 1;
[flag] = true;
}))
assert(c:close())
end)
cq:wrap(function()
local ok, _, errno = s:receive()
assert.same(nil, ok)
assert.same(1002, errno)
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
end
it("doesn't blow up when given pings", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send_ping())
assert(c:send("test"))
assert(c:close())
end)
cq:wrap(function()
assert.same("test", s:receive())
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("ignores unsolicited pongs", function()
local cq = cqueues.new()
local c, s = new_pair()
cq:wrap(function()
assert(c:send_pong())
assert(c:send("test"))
assert(c:close())
end)
cq:wrap(function()
assert.same("test", s:receive())
assert(s:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("works when using uri string constructor", function()
local cq = cqueues.new()
local s = server.listen {
host = "localhost";
port = 0;
onstream = function(s, stream)
local headers = assert(stream:get_headers())
local ws = websocket.new_from_stream(stream, headers)
assert(ws:accept())
assert(ws:close())
s:close()
end;
}
assert(s:listen())
local _, host, port = s:localname()
cq:wrap(function()
assert_loop(s)
end)
cq:wrap(function()
local ws = websocket.new_from_uri("ws://"..util.to_authority(host, port, "ws"));
assert(ws:connect())
assert(ws:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
it("works when using uri table constructor and protocols", function()
local new_headers = require "http.headers".new
local cq = cqueues.new()
local s = server.listen {
host = "localhost";
port = 0;
onstream = function(s, stream)
local headers = assert(stream:get_headers())
local ws = websocket.new_from_stream(stream, headers)
local response_headers = new_headers()
response_headers:upsert(":status", "101")
response_headers:upsert("server", "lua-http websocket test")
assert(ws:accept {
headers = response_headers;
protocols = {"my awesome-protocol", "foo"};
})
-- Should prefer client protocol preference
assert.same("foo", ws.protocol)
assert(ws:close())
s:close()
end;
}
assert(s:listen())
local _, host, port = s:localname()
cq:wrap(function()
assert_loop(s)
end)
cq:wrap(function()
local ws = websocket.new_from_uri({
scheme = "ws";
host = host;
port = port;
}, {"foo", "my-awesome-protocol", "bar"})
assert(ws:connect())
assert.same("foo", ws.protocol)
assert.same("lua-http websocket test", ws.headers:get("server"))
assert(ws:close())
end)
assert_loop(cq, TEST_TIMEOUT)
assert.truthy(cq:empty())
end)
end)