diff options
Diffstat (limited to 'test/ws_SUITE.erl')
-rw-r--r-- | test/ws_SUITE.erl | 208 |
1 files changed, 199 insertions, 9 deletions
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index 9741329..34befda 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -30,9 +30,14 @@ -export([ws8_init_shutdown/1]). -export([ws8_single_bytes/1]). -export([ws13/1]). +-export([ws_send_close/1]). +-export([ws_send_close_payload/1]). -export([ws_send_many/1]). -export([ws_text_fragments/1]). -export([ws_timeout_hibernate/1]). +-export([ws_timeout_cancel/1]). +-export([ws_timeout_reset/1]). +-export([ws_upgrade_with_opts/1]). %% ct. @@ -46,9 +51,14 @@ groups() -> ws8_init_shutdown, ws8_single_bytes, ws13, + ws_send_close, + ws_send_close_payload, ws_send_many, ws_text_fragments, - ws_timeout_hibernate + ws_timeout_hibernate, + ws_timeout_cancel, + ws_timeout_reset, + ws_upgrade_with_opts ], [{ws, [], BaseTests}]. @@ -85,8 +95,28 @@ init_dispatch() -> {[<<"websocket">>], websocket_handler, []}, {[<<"ws_echo_handler">>], websocket_echo_handler, []}, {[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []}, - {[<<"ws_send_many">>], ws_send_many_handler, []}, - {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []} + {[<<"ws_send_many">>], ws_send_many_handler, [ + {sequence, [ + {text, <<"one">>}, + {text, <<"two">>}, + {text, <<"seven!">>}]} + ]}, + {[<<"ws_send_close">>], ws_send_many_handler, [ + {sequence, [ + {text, <<"send">>}, + close, + {text, <<"won't be received">>}]} + ]}, + {[<<"ws_send_close_payload">>], ws_send_many_handler, [ + {sequence, [ + {text, <<"send">>}, + {close, 1001, <<"some text!">>}, + {text, <<"won't be received">>}]} + ]}, + {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []}, + {[<<"ws_timeout_cancel">>], ws_timeout_cancel_handler, []}, + {[<<"ws_upgrade_with_opts">>], ws_upgrade_with_opts_handler, + <<"failure">>} ]} ]. @@ -310,6 +340,64 @@ ws13(Config) -> {error, closed} = gen_tcp:recv(Socket, 0, 6000), ok. +ws_send_close(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_send_close HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + %% We catch all frames at once and check them directly. + {ok, Many} = gen_tcp:recv(Socket, 8, 6000), + << 1:1, 0:3, 1:4, 0:1, 4:7, "send", + 1:1, 0:3, 8:4, 0:8 >> = Many, + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws_send_close_payload(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_send_close_payload HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + %% We catch all frames at once and check them directly. + {ok, Many} = gen_tcp:recv(Socket, 20, 6000), + << 1:1, 0:3, 1:4, 0:1, 4:7, "send", + 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >> = Many, + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + ws_send_many(Config) -> {port, Port} = lists:keyfind(port, 1, Config), {ok, Socket} = gen_tcp:connect("localhost", Port, @@ -332,12 +420,11 @@ ws_send_many(Config) -> {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers), - {ok, << 1:1, 0:3, 1:4, 0:1, 3:7, "one" >>} - = gen_tcp:recv(Socket, 0, 6000), - {ok, << 1:1, 0:3, 1:4, 0:1, 3:7, "two" >>} - = gen_tcp:recv(Socket, 0, 6000), - {ok, << 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} - = gen_tcp:recv(Socket, 0, 6000), + %% We catch all frames at once and check them directly. + {ok, Many} = gen_tcp:recv(Socket, 18, 6000), + << 1:1, 0:3, 1:4, 0:1, 3:7, "one", + 1:1, 0:3, 1:4, 0:1, 3:7, "two", + 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >> = Many, ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), {error, closed} = gen_tcp:recv(Socket, 0, 6000), @@ -424,6 +511,109 @@ ws_timeout_hibernate(Config) -> {error, closed} = gen_tcp:recv(Socket, 0, 6000), ok. +ws_timeout_cancel(Config) -> + %% Erlang messages to a socket should not cancel the timeout + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_timeout_cancel HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws_timeout_reset(Config) -> + %% Erlang messages across a socket should reset the timeout + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_timeout_cancel HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: WebSocket\r\n" + "Origin: http://localhost\r\n" + "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n" + "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-location", "ws://localhost/ws_timeout_cancel"} + = lists:keyfind("sec-websocket-location", 1, Headers), + {"sec-websocket-origin", "http://localhost"} + = lists:keyfind("sec-websocket-origin", 1, Headers), + ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>), + {ok, Body} = gen_tcp:recv(Socket, 0, 6000), + <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body, + ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>), + {ok, << 0, "msg sent", 255 >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = timer:sleep(500), + ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>), + {ok, << 0, "msg sent", 255 >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = timer:sleep(500), + ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>), + {ok, << 0, "msg sent", 255 >>} + = gen_tcp:recv(Socket, 0, 6000), + ok = timer:sleep(500), + ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>), + {ok, << 0, "msg sent", 255 >>} + = gen_tcp:recv(Socket, 0, 6000), + {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + +ws_upgrade_with_opts(Config) -> + {port, Port} = lists:keyfind(port, 1, Config), + {ok, Socket} = gen_tcp:connect("localhost", Port, + [binary, {active, false}, {packet, raw}]), + ok = gen_tcp:send(Socket, [ + "GET /ws_upgrade_with_opts HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Origin: http://localhost\r\n" + "Sec-WebSocket-Version: 8\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "\r\n"]), + {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), + {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} + = erlang:decode_packet(http, Handshake, []), + [Headers, <<>>] = websocket_headers( + erlang:decode_packet(httph, Rest, []), []), + {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), + {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), + {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} + = lists:keyfind("sec-websocket-accept", 1, Headers), + {ok, Response} = gen_tcp:recv(Socket, 9, 6000), + << 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response, + ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close + {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000), + {error, closed} = gen_tcp:recv(Socket, 0, 6000), + ok. + %% Internal. websocket_headers({ok, http_eoh, Rest}, Acc) -> |