1
1
package.path = package.path .. ' ../src'
2
2
3
3
local frame = require ' websocket.frame'
4
+ local tools = require ' websocket.tools'
4
5
5
6
local bytes = string.char
6
7
@@ -18,7 +19,7 @@ describe(
18
19
function ()
19
20
assert .is_same (type (frame ),' table' )
20
21
end )
21
-
22
+
22
23
it (
23
24
' provides a decode and a encode function' ,
24
25
function ()
@@ -27,7 +28,7 @@ describe(
27
28
assert .is .same (type (frame .encode_close ),' function' )
28
29
assert .is .same (type (frame .decode_close ),' function' )
29
30
end )
30
-
31
+
31
32
it (
32
33
' provides correct OPCODES' ,
33
34
function ()
@@ -38,8 +39,29 @@ describe(
38
39
assert .is .same (frame .PING ,9 )
39
40
assert .is .same (frame .PONG ,10 )
40
41
end )
41
-
42
-
42
+
43
+ it (' .encode_header_small is correct' ,
44
+ function ()
45
+ local enc = frame .encode_header_small (123 ,33 )
46
+ local enc_ref = tools .write_int8 (123 ).. tools .write_int8 (33 )
47
+ assert .is_same (enc , enc_ref )
48
+ end )
49
+
50
+ it (' .encode_header_medium is correct' ,
51
+ function ()
52
+ local enc = frame .encode_header_medium (123 ,33 ,555 )
53
+ local enc_ref = tools .write_int8 (123 ).. tools .write_int8 (33 ).. tools .write_int16 (555 )
54
+ assert .is_same (enc , enc_ref )
55
+ end )
56
+
57
+ it (' .encode_header_big is correct' ,
58
+ function ()
59
+ local enc = frame .encode_header_big (123 ,33 ,555 ,12987 )
60
+ local enc_ref = tools .write_int8 (123 ).. tools .write_int8 (33 ).. tools .write_int32 (555 ).. tools .write_int32 (12987 )
61
+ assert .is_same (enc , enc_ref )
62
+ end )
63
+
64
+
43
65
it (
44
66
' RFC: decode a single-frame unmasked text message' ,
45
67
function ()
@@ -50,7 +72,7 @@ describe(
50
72
assert .is_same (rest ,' foo' )
51
73
assert .is_false (masked )
52
74
end )
53
-
75
+
54
76
it (
55
77
' RFC: decode a single-frame unmasked text message bytewise and check min length' ,
56
78
function ()
@@ -70,7 +92,7 @@ describe(
70
92
end
71
93
end
72
94
end )
73
-
95
+
74
96
it (
75
97
' RFC: decode a single-frame masked text message' ,
76
98
function ()
@@ -81,7 +103,7 @@ describe(
81
103
assert .is_same (rest ,' foo' )
82
104
assert .is_truthy (masked )
83
105
end )
84
-
106
+
85
107
it (
86
108
' RFC: decode a fragmented test message' ,
87
109
function ()
@@ -91,15 +113,15 @@ describe(
91
113
assert .is .same (decoded ,' Hel' )
92
114
assert .is_same (rest ,' ' )
93
115
assert .is_falsy (masked )
94
-
116
+
95
117
decoded ,fin ,opcode ,rest ,masked = frame .decode (lo )
96
118
assert .is_true (fin )
97
119
assert .is_same (opcode ,0x0 )
98
120
assert .is .same (decoded ,' lo' )
99
121
assert .is_same (rest ,' ' )
100
122
assert .is_falsy (masked )
101
123
end )
102
-
124
+
103
125
it (
104
126
' refuse incomplete unmasked frame' ,
105
127
function ()
@@ -108,7 +130,7 @@ describe(
108
130
assert .is_same (fin ,# hello_unmasked - 4 )
109
131
assert .is_falsy (opcode )
110
132
end )
111
-
133
+
112
134
it (
113
135
' refuse incomplete masked frame' ,
114
136
function ()
@@ -117,7 +139,7 @@ describe(
117
139
assert .is_same (fin ,# hello_masked - 4 )
118
140
assert .is_falsy (opcode )
119
141
end )
120
-
142
+
121
143
it (
122
144
' encode single-frame unmasked text' ,
123
145
function ()
@@ -135,15 +157,15 @@ describe(
135
157
assert .is_same (opcode ,frame .TEXT )
136
158
assert .is_falsy (masked )
137
159
end )
138
-
160
+
139
161
local random_text = function (len )
140
162
local chars = {}
141
163
for i = 1 ,len do
142
164
chars [i ] = string.char (math.random (33 ,126 ))
143
165
end
144
166
return table.concat (chars )
145
167
end
146
-
168
+
147
169
it (
148
170
' encode and decode single-frame of length 127 unmasked text' ,
149
171
function ()
@@ -159,7 +181,7 @@ describe(
159
181
assert .is_same (rest ,' ' )
160
182
assert .is_falsy (masked )
161
183
end )
162
-
184
+
163
185
it (
164
186
' encode and decode single-frame of length 0xffff-1 unmasked text' ,
165
187
function ()
@@ -175,7 +197,7 @@ describe(
175
197
assert .is_same (rest ,' ' )
176
198
assert .is_falsy (masked )
177
199
end )
178
-
200
+
179
201
it (
180
202
' encode and decode single-frame of length 0xffff+1 unmasked text' ,
181
203
function ()
@@ -189,7 +211,7 @@ describe(
189
211
assert .is_true (fin )
190
212
assert .is_same (opcode ,frame .TEXT )
191
213
end )
192
-
214
+
193
215
it (
194
216
' encode single-frame masked text' ,
195
217
function ()
@@ -201,7 +223,7 @@ describe(
201
223
assert .is_same (rest ,' ' )
202
224
assert .is_truthy (masked )
203
225
end )
204
-
226
+
205
227
it (
206
228
' encode fragmented unmasked text' ,
207
229
function ()
@@ -210,14 +232,14 @@ describe(
210
232
assert .is_falsy (fin )
211
233
assert .is_same (opcode ,0x1 )
212
234
assert .is .same (decoded ,' Hel' )
213
-
235
+
214
236
local lo = frame .encode (' lo' ,frame .CONTINUATION ,false )
215
237
decoded ,fin ,opcode = frame .decode (lo )
216
238
assert .is_true (fin )
217
239
assert .is_same (opcode ,0x0 )
218
240
assert .is .same (decoded ,' lo' )
219
241
end )
220
-
242
+
221
243
it (
222
244
' encodes and decodes close packet correctly' ,
223
245
function ()
@@ -229,5 +251,5 @@ describe(
229
251
assert .is_same (dcode ,code )
230
252
assert .is_same (dreason ,reason )
231
253
end )
232
-
254
+
233
255
end )
0 commit comments