aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-06-02 12:31:00 +0200
committerLoïc Hoguin <[email protected]>2017-06-02 12:31:00 +0200
commit767da623f1f7329cb0b0d86c3c1876ccf098d60a (patch)
tree7a59761479939b84aad858680d258307430d65bb /test
parentcbf7972f10a5ef96e544df446144bb1a05bcc7cf (diff)
downloadcowboy-767da623f1f7329cb0b0d86c3c1876ccf098d60a.tar.gz
cowboy-767da623f1f7329cb0b0d86c3c1876ccf098d60a.tar.bz2
cowboy-767da623f1f7329cb0b0d86c3c1876ccf098d60a.zip
Fix terminate not being called on connection close in HTTP/1.1
Introduces the new stream_handler_SUITE test suite. More cases will be added later on.
Diffstat (limited to 'test')
-rw-r--r--test/handlers/stream_handler_h.erl34
-rw-r--r--test/stream_handler_SUITE.erl78
2 files changed, 112 insertions, 0 deletions
diff --git a/test/handlers/stream_handler_h.erl b/test/handlers/stream_handler_h.erl
new file mode 100644
index 0000000..bb4a6da
--- /dev/null
+++ b/test/handlers/stream_handler_h.erl
@@ -0,0 +1,34 @@
+%% This module behaves differently depending on a specific header.
+
+-module(stream_handler_h).
+-behavior(cowboy_stream).
+
+-export([init/3]).
+-export([data/4]).
+-export([info/3]).
+-export([terminate/3]).
+-export([early_error/5]).
+
+init(StreamID, Req, Opts) ->
+ %% @todo Vary behavior depending on x-test-case.
+ Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
+ Pid ! {Pid, self(), init, StreamID, Req, Opts},
+ {[{headers, 200, #{}}], Pid}.
+
+data(StreamID, IsFin, Data, State=Pid) ->
+ Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
+ {[], State}.
+
+info(StreamID, Info, State=Pid) ->
+ Pid ! {Pid, self(), info, StreamID, Info, State},
+ {[], State}.
+
+terminate(StreamID, Reason, State=Pid) ->
+ Pid ! {Pid, self(), terminate, StreamID, Reason, State},
+ ok.
+
+%% This clause can only test for early errors that reached the required header.
+early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
+ Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
+ Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
+ Resp.
diff --git a/test/stream_handler_SUITE.erl b/test/stream_handler_SUITE.erl
new file mode 100644
index 0000000..1cc4f89
--- /dev/null
+++ b/test/stream_handler_SUITE.erl
@@ -0,0 +1,78 @@
+%% Copyright (c) 2017, Loïc Hoguin <[email protected]>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+-module(stream_handler_SUITE).
+-compile(export_all).
+
+-import(ct_helper, [config/2]).
+-import(ct_helper, [doc/1]).
+-import(cowboy_test, [gun_open/1]).
+
+%% ct.
+
+all() ->
+ cowboy_test:common_all().
+
+groups() ->
+ cowboy_test:common_groups(ct_helper:all(?MODULE)).
+
+init_per_group(Name = http, Config) ->
+ cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config);
+init_per_group(Name = https, Config) ->
+ cowboy_test:init_https(Name, #{stream_handlers => [stream_handler_h]}, Config);
+init_per_group(Name = h2, Config) ->
+ cowboy_test:init_http2(Name, #{stream_handlers => [stream_handler_h]}, Config);
+init_per_group(Name = h2c, Config) ->
+ Config1 = cowboy_test:init_http(Name, #{stream_handlers => [stream_handler_h]}, Config),
+ lists:keyreplace(protocol, 1, Config1, {protocol, http2});
+init_per_group(Name = http_compress, Config) ->
+ cowboy_test:init_http(Name, #{
+ stream_handlers => [cowboy_compress_h, stream_handler_h]
+ }, Config);
+init_per_group(Name = https_compress, Config) ->
+ cowboy_test:init_https(Name, #{
+ stream_handlers => [cowboy_compress_h, stream_handler_h]
+ }, Config);
+init_per_group(Name = h2_compress, Config) ->
+ cowboy_test:init_http2(Name, #{
+ stream_handlers => [cowboy_compress_h, stream_handler_h]
+ }, Config);
+init_per_group(Name = h2c_compress, Config) ->
+ Config1 = cowboy_test:init_http(Name, #{
+ stream_handlers => [cowboy_compress_h, stream_handler_h]
+ }, Config),
+ lists:keyreplace(protocol, 1, Config1, {protocol, http2}).
+
+end_per_group(Name, _) ->
+ cowboy:stop_listener(Name).
+
+%% Tests.
+
+terminate_on_socket_close(Config) ->
+ doc("Confirm terminate/3 is called when the socket gets closed brutally."),
+ Self = self(),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/long_polling", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"x-test-case">>, <<"stream">>},
+ {<<"x-test-pid">>, pid_to_list(Self)}
+ ]),
+ %% Confirm init/3 is called and receive the beginning of the response.
+ Pid = receive {Self, P, init, _, _, _} -> P after 1000 -> error(timeout) end,
+ {response, nofin, 200, _} = gun:await(ConnPid, Ref),
+ %% Close the socket.
+ ok = gun:close(ConnPid),
+ %% Confirm terminate/3 is called.
+ receive {Self, Pid, terminate, _, _, _} -> ok after 1000 -> error(timeout) end,
+ ok.