forked from sanmiguel/websocket_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc_SUITE.erl
312 lines (295 loc) · 11.8 KB
/
wc_SUITE.erl
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
-module(wc_SUITE).
-include_lib("common_test/include/ct.hrl").
-define(print(Value), io:format("~n~p~n", [Value])).
-export([
all/0,
init_per_suite/1,
end_per_suite/1
]).
-export([
test_text_frames/1,
test_binary_frames/1,
test_control_frames/1,
test_quick_response/1,
test_bad_request/1,
test_keepalive_opt/1,
test_keepalive_timeout/1
]).
all() ->
[
test_text_frames,
test_binary_frames,
test_control_frames,
test_quick_response,
test_bad_request,
test_keepalive_opt,
test_keepalive_timeout
].
init_per_suite(Config) ->
{ok, Apps} = application:ensure_all_started(cowboy),
{ok,_} = echo_server:start(),
[{apps, Apps} | Config].
end_per_suite(Config) ->
[ ok = application:stop(A) || A <- lists:reverse(?config(apps, Config))],
Config.
test_text_frames(_) ->
{ok, Pid} = ws_client:start_link(),
receive {ok, Pid} -> ok after 5000 -> recv_timeout end,
%% Short message
Short = short_msg(),
ws_client:send_text(Pid, Short),
{text, Short} = ws_client:recv(Pid),
ok = ws_client:sync_send_text(Pid, Short),
{text, Short} = ws_client:recv(Pid),
%% Payload length greater than 125 (actual 150).
Medium = medium_msg(),
ws_client:send_text(Pid, Medium),
{text, Medium} = ws_client:recv(Pid),
%% Now check that websocket_client:send is working
ok = websocket_client:send(Pid, {text, Medium}),
{text, Medium} = ws_client:recv(Pid),
%% Payload length greater than 65535
Long = long_msg(),
ws_client:send_text(Pid, Long),
{text, Long} = ws_client:recv(Pid),
ws_client:stop(Pid),
ok.
test_binary_frames(_) ->
{ok, Pid} = ws_client:start_link(),
receive {ok, Pid} -> ok after 5000 -> recv_timeout end,
%% Short message
Short = short_msg(),
ws_client:send_binary(Pid, Short),
{binary, Short} = ws_client:recv(Pid),
%% Payload length greater than 125 (actual 150).
Medium = medium_msg(),
ws_client:send_binary(Pid, Medium),
{binary, Medium} = ws_client:recv(Pid),
%% Payload length greater than 65535
Long = long_msg(),
ws_client:send_binary(Pid, Long),
{binary, Long} = ws_client:recv(Pid),
ws_client:stop(Pid),
ok.
test_control_frames(_) ->
{ok, Pid} = ws_client:start_link(),
receive {ok, Pid} -> ok after 5000 -> recv_timeout end,
%% Send ping with short message
Short = short_msg(),
ok = ws_client:sync_send_ping(Pid, Short),
%% Send ping without message
{pong, Short} = ws_client:recv(Pid),
ok = ws_client:sync_send_ping(Pid, <<>>),
{pong, <<>>} = ws_client:recv(Pid),
ws_client:stop(Pid),
ok.
test_quick_response(_) ->
%% Connect to the server and...
{ok, Pid} = ws_client:start_link("ws://localhost:8080/hello/?q=world!"),
receive {ok, Pid} -> ok after 5000 -> recv_timeout end,
%% ...make sure we receive the first frame.
{text, <<"world!">>} = ws_client:recv(Pid, 500),
ws_client:stop(Pid),
%% Also, make sure the HTTP response is parsed correctly.
{ok, Pid2} = ws_client:start_link("ws://localhost:8080/hello/?q=Hello%0D%0A%0D%0AWorld%0D%0A%0D%0A!"),
receive {ok, Pid2} -> ok after 5000 -> recv_timeout end,
{text, <<"Hello\r\n\r\nWorld\r\n\r\n!">>} = ws_client:recv(Pid2, 500),
ws_client:stop(Pid2),
ok.
test_bad_request(_) ->
%% Since this is now a real OTP Special Process, we need to trap exits to
%% receive the error reason properly
process_flag(trap_exit, true),
%% Connect to the server and wait for a error
%% Don't forget this behaviour is largely controlled by the handler
%% A different handler might just reconnect, but this one terminates.
{ok, Pid400} = ws_client:start_link("ws://localhost:8080/hello/?code=400"),
receive
{'EXIT', Pid400, {error, {400, <<"Bad Request">>}} } ->
ok
after 1000 ->
ct:fail(timeout)
end,
{ok, Pid403} = ws_client:start_link("ws://localhost:8080/hello/?code=403"),
receive
{'EXIT', Pid403, {error, {403, <<"Forbidden">>}}} ->
ok
after 1000 ->
ct:fail(timeout)
end.
test_keepalive_opt(_) ->
{ok, Pid} = ws_client:start_link("ws://localhost:8080", 100),
receive {ok, Pid} -> ok after 500 -> ct:fail(timeout) end,
{pong, <<"foo">>} = ws_client:recv(Pid, 500),
ws_client:stop(Pid),
ok.
test_keepalive_timeout(_) ->
{ok, S} = gen_tcp:listen(9090, [binary, {packet, 0}, {active, true}]),
process_flag(trap_exit, true),
{ok, C} = ws_client:start_link("ws://localhost:9090", 500),
receive
{'EXIT', C, {error, keepalive_timeout}} -> ok;
Other -> ct:fail({unexpected, Other})
end.
short_msg() ->
<<"hello">>.
medium_msg() ->
<<"ttttttttttttttttttttttttt"
"ttttttttttttttttttttttttt"
"ttttttttttttttttttttttttt"
"ttttttttttttttttttttttttt"
"ttttttttttttttttttttttttt"
"ttttttttttttttttttttttttt">>.
long_msg() ->
Medium = medium_msg(),
%% 600 bytes
L = << Medium/binary, Medium/binary, Medium/binary, Medium/binary >>,
%% 2400 bytes
L1 = << L/binary, L/binary, L/binary, L/binary >>,
%% 9600 bytes
L2 = << L1/binary, L1/binary, L1/binary, L1/binary >>,
%% 38400 bytes
L3 = << L2/binary, L2/binary, L2/binary, L2/binary >>,
%% 76800 bytes
<< L3/binary, L3/binary >>.
%%% 4-bit integer 0-F
%%% 3-7, B-F unsupported
%control_opcode() ->
% oneof([
% 16#8, % connection close
% 16#9, % ping
% 16#A % pong
% ]).
%noncontrol_opcode() ->
% oneof([
% 16#0, % continuation
% 16#1, % test frame
% 16#2 % binary frame
% ]).
%
%opcode() ->
% oneof([control_opcode(), noncontrol_opcode()]).
%
%websocket_frame() ->
% % 0 1 2 3
% % 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
% % +-+-+-+-+-------+-+-------------+-------------------------------+
% % |F|R|R|R| opcode|M| Payload len | Extended payload length |
% % |I|S|S|S| (4) |A| (7) | (16/64) |
% % |N|V|V|V| |S| | (if payload len==126/127) |
% % | |1|2|3| |K| | |
% % +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
% % | Extended payload length continued, if payload len == 127 |
% % + - - - - - - - - - - - - - - - +-------------------------------+
% % | |Masking-key, if MASK set to 1 |
% % +-------------------------------+-------------------------------+
% % | Masking-key (continued) | Payload Data |
% % +-------------------------------- - - - - - - - - - - - - - - - +
% % : Payload Data continued ... :
% % + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
% % | Payload Data continued ... |
% % +---------------------------------------------------------------+
% Fin = 1, %% Single frame
% RSV = 0, %% Extensions unsupported
% Masked = 1, %% Masking required
% %% Extensions as-yet unsupported
% OpCode = opcode(),
% << Fin:1, RSV:3, OpCode:4, Masked:1, 126:7, Len:16, Key:32, Rest:Len/bits >>,
% << Fin:1, RSV:3, OpCode:4, Masked:1, 127:7, 0:1, Len:63, Key:32, Rest:Len/bits >>,
% << Fin:1, RSV:3, OpCode:4, Masked:1, Len:7, Key:32, Rest:Len/bits >>.
%
%% Control frames cannot be fragments
%% Non-control frames may be fragmented
%
%
%
% % ws-frame = frame-fin ; 1 bit in length
% % frame-rsv1 ; 1 bit in length
% % frame-rsv2 ; 1 bit in length
% % frame-rsv3 ; 1 bit in length
% % frame-opcode ; 4 bits in length
% % frame-masked ; 1 bit in length
% % frame-payload-length ; either 7, 7+16,
% % ; or 7+64 bits in
% % ; length
% % [ frame-masking-key ] ; 32 bits in length
% % frame-payload-data ; n*8 bits in
% % ; length, where
% % ; n >= 0
%
% % frame-fin = %x0 ; more frames of this message follow
% % / %x1 ; final frame of this message
% % ; 1 bit in length
%
% % frame-rsv1 = %x0 / %x1
% % ; 1 bit in length, MUST be 0 unless
% % ; negotiated otherwise
%
% % frame-rsv2 = %x0 / %x1
% % ; 1 bit in length, MUST be 0 unless
% % ; negotiated otherwise
%
% % frame-rsv3 = %x0 / %x1
% % ; 1 bit in length, MUST be 0 unless
% % ; negotiated otherwise
%
% % frame-opcode = frame-opcode-non-control /
% % frame-opcode-control /
% % frame-opcode-cont
%
% % frame-opcode-cont = %x0 ; frame continuation
%
% % frame-opcode-non-control= %x1 ; text frame
% % / %x2 ; binary frame
% % / %x3-7
% % ; 4 bits in length,
% % ; reserved for further non-control frames
%
% % frame-opcode-control = %x8 ; connection close
% % / %x9 ; ping
% % / %xA ; pong
% % / %xB-F ; reserved for further control
% % ; frames
% % ; 4 bits in length
% %
% % frame-masked = %x0
% % ; frame is not masked, no frame-masking-key
% % / %x1
% % ; frame is masked, frame-masking-key present
% % ; 1 bit in length
%
% % frame-payload-length = ( %x00-7D )
% % / ( %x7E frame-payload-length-16 )
% % / ( %x7F frame-payload-length-63 )
% % ; 7, 7+16, or 7+64 bits in length,
% % ; respectively
%
% % frame-payload-length-16 = %x0000-FFFF ; 16 bits in length
%
% % frame-payload-length-63 = %x0000000000000000-7FFFFFFFFFFFFFFF
% % ; 64 bits in length
%
% % frame-masking-key = 4( %x00-FF )
% % ; present only if frame-masked is 1
% % ; 32 bits in length
%
% % frame-payload-data = (frame-masked-extension-data
% % frame-masked-application-data)
% % ; when frame-masked is 1
% % / (frame-unmasked-extension-data
% % frame-unmasked-application-data)
% % ; when frame-masked is 0
%
% % frame-masked-extension-data = *( %x00-FF )
% % ; reserved for future extensibility
% % ; n*8 bits in length, where n >= 0
%
% % frame-masked-application-data = *( %x00-FF )
% % ; n*8 bits in length, where n >= 0
%
% % frame-unmasked-extension-data = *( %x00-FF )
% % ; reserved for future extensibility
% % ; n*8 bits in length, where n >= 0
%
% % frame-unmasked-application-data = *( %x00-FF )
% % ; n*8 bits in length, where n >= 0