From 5964273cc4354b7378b8bdf49fe455d2b46d7c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 26 Mar 2018 18:49:09 +0200 Subject: Fix the flushing of messages when switching to Websocket We now flush messages that are specific to cowboy_http only. Stream handlers should also flush their own specific messages if necessary, although timeouts will be flushed regardless of where they originate from. Also renames the http_SUITE to old_http_SUITE to distinguish new tests from old tests. Most old tests need to be removed or converted eventually as they're legacy tests from Cowboy 1.0. --- test/old_http_SUITE_data/http_chunked.erl | 15 +++++++++ test/old_http_SUITE_data/http_echo_body.erl | 21 ++++++++++++ test/old_http_SUITE_data/http_errors.erl | 17 ++++++++++ test/old_http_SUITE_data/http_handler.erl | 10 ++++++ test/old_http_SUITE_data/http_loop_stream_recv.erl | 34 +++++++++++++++++++ test/old_http_SUITE_data/http_req_attr.erl | 15 +++++++++ test/old_http_SUITE_data/http_set_resp.erl | 25 ++++++++++++++ test/old_http_SUITE_data/rest_empty_resource.erl | 6 ++++ test/old_http_SUITE_data/rest_expires.erl | 22 +++++++++++++ test/old_http_SUITE_data/rest_expires_binary.erl | 18 ++++++++++ .../rest_forbidden_resource.erl | 32 ++++++++++++++++++ .../old_http_SUITE_data/rest_missing_callbacks.erl | 24 ++++++++++++++ .../old_http_SUITE_data/rest_nodelete_resource.erl | 18 ++++++++++ test/old_http_SUITE_data/rest_param_all.erl | 36 ++++++++++++++++++++ test/old_http_SUITE_data/rest_patch_resource.erl | 38 ++++++++++++++++++++++ .../rest_post_charset_resource.erl | 19 +++++++++++ .../old_http_SUITE_data/rest_postonly_resource.erl | 18 ++++++++++ test/old_http_SUITE_data/rest_resource_etags.erl | 37 +++++++++++++++++++++ test/old_http_SUITE_data/rest_simple_resource.erl | 14 ++++++++ 19 files changed, 419 insertions(+) create mode 100644 test/old_http_SUITE_data/http_chunked.erl create mode 100644 test/old_http_SUITE_data/http_echo_body.erl create mode 100644 test/old_http_SUITE_data/http_errors.erl create mode 100644 test/old_http_SUITE_data/http_handler.erl create mode 100644 test/old_http_SUITE_data/http_loop_stream_recv.erl create mode 100644 test/old_http_SUITE_data/http_req_attr.erl create mode 100644 test/old_http_SUITE_data/http_set_resp.erl create mode 100644 test/old_http_SUITE_data/rest_empty_resource.erl create mode 100644 test/old_http_SUITE_data/rest_expires.erl create mode 100644 test/old_http_SUITE_data/rest_expires_binary.erl create mode 100644 test/old_http_SUITE_data/rest_forbidden_resource.erl create mode 100644 test/old_http_SUITE_data/rest_missing_callbacks.erl create mode 100644 test/old_http_SUITE_data/rest_nodelete_resource.erl create mode 100644 test/old_http_SUITE_data/rest_param_all.erl create mode 100644 test/old_http_SUITE_data/rest_patch_resource.erl create mode 100644 test/old_http_SUITE_data/rest_post_charset_resource.erl create mode 100644 test/old_http_SUITE_data/rest_postonly_resource.erl create mode 100644 test/old_http_SUITE_data/rest_resource_etags.erl create mode 100644 test/old_http_SUITE_data/rest_simple_resource.erl (limited to 'test/old_http_SUITE_data') diff --git a/test/old_http_SUITE_data/http_chunked.erl b/test/old_http_SUITE_data/http_chunked.erl new file mode 100644 index 0000000..645eefe --- /dev/null +++ b/test/old_http_SUITE_data/http_chunked.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_chunked). + +-export([init/2]). + +init(Req, Opts) -> + Req2 = cowboy_req:stream_reply(200, Req), + %% Try an empty chunk to make sure the stream doesn't get closed. + cowboy_req:stream_body([<<>>], nofin, Req2), + timer:sleep(100), + cowboy_req:stream_body("chunked_handler\r\n", nofin, Req2), + timer:sleep(100), + cowboy_req:stream_body("works fine!", fin, Req2), + {ok, Req2, Opts}. diff --git a/test/old_http_SUITE_data/http_echo_body.erl b/test/old_http_SUITE_data/http_echo_body.erl new file mode 100644 index 0000000..d803108 --- /dev/null +++ b/test/old_http_SUITE_data/http_echo_body.erl @@ -0,0 +1,21 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_echo_body). + +-export([init/2]). + +init(Req, Opts) -> + true = cowboy_req:has_body(Req), + Req3 = case cowboy_req:read_body(Req, #{length => 1000000}) of + {ok, Body, Req2} -> handle_body(Req2, Body); + {more, _, Req2} -> handle_badlength(Req2) + end, + {ok, Req3, Opts}. + +handle_badlength(Req) -> + cowboy_req:reply(413, #{}, <<"Request entity too large">>, Req). + +handle_body(Req, Body) -> + Size = cowboy_req:body_length(Req), + Size = byte_size(Body), + cowboy_req:reply(200, #{}, Body, Req). diff --git a/test/old_http_SUITE_data/http_errors.erl b/test/old_http_SUITE_data/http_errors.erl new file mode 100644 index 0000000..14e3d09 --- /dev/null +++ b/test/old_http_SUITE_data/http_errors.erl @@ -0,0 +1,17 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_errors). + +-export([init/2]). + +init(Req, _Opts) -> + #{'case' := Case} = cowboy_req:match_qs(['case'], Req), + case_init(Case, Req). + +case_init(<<"init_before_reply">> = Case, _Req) -> + ct_helper_error_h:ignore(?MODULE, case_init, 2), + error(Case); +case_init(<<"init_after_reply">> = Case, Req) -> + ct_helper_error_h:ignore(?MODULE, case_init, 2), + _ = cowboy_req:reply(200, #{}, "http_handler_crashes", Req), + error(Case). diff --git a/test/old_http_SUITE_data/http_handler.erl b/test/old_http_SUITE_data/http_handler.erl new file mode 100644 index 0000000..eb31aa8 --- /dev/null +++ b/test/old_http_SUITE_data/http_handler.erl @@ -0,0 +1,10 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler). + +-export([init/2]). + +init(Req, Opts) -> + Headers = proplists:get_value(headers, Opts, #{}), + Body = proplists:get_value(body, Opts, "http_handler"), + {ok, cowboy_req:reply(200, Headers, Body, Req), Opts}. diff --git a/test/old_http_SUITE_data/http_loop_stream_recv.erl b/test/old_http_SUITE_data/http_loop_stream_recv.erl new file mode 100644 index 0000000..8c6f6b0 --- /dev/null +++ b/test/old_http_SUITE_data/http_loop_stream_recv.erl @@ -0,0 +1,34 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_loop_stream_recv). + +-export([init/2]). +-export([info/3]). +-export([terminate/3]). + +init(Req, _) -> + receive after 100 -> ok end, + self() ! stream, + {cowboy_loop, Req, undefined}. + +info(stream, Req, undefined) -> + stream(Req, 1, <<>>). + +stream(Req, ID, Acc) -> + case cowboy_req:read_body(Req) of + {ok, <<>>, Req2} -> + {stop, cowboy_req:reply(200, Req2), undefined}; + {_, Data, Req2} -> + parse_id(Req2, ID, << Acc/binary, Data/binary >>) + end. + +parse_id(Req, ID, Data) -> + case Data of + << ID:32, Rest/bits >> -> + parse_id(Req, ID + 1, Rest); + _ -> + stream(Req, ID, Data) + end. + +terminate(stop, _, _) -> + ok. diff --git a/test/old_http_SUITE_data/http_req_attr.erl b/test/old_http_SUITE_data/http_req_attr.erl new file mode 100644 index 0000000..c6a940e --- /dev/null +++ b/test/old_http_SUITE_data/http_req_attr.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @todo That module was clearly meant to do more than one +%% thing and yet doesn't. +-module(http_req_attr). + +-export([init/2]). + +init(Req, Opts) -> + #{attr := Attr} = cowboy_req:match_qs([attr], Req), + <<"host_and_port">> = Attr, + Host = cowboy_req:host(Req), + Port = cowboy_req:port(Req), + Value = [Host, "\n", integer_to_list(Port)], + {ok, cowboy_req:reply(200, #{}, Value, Req), Opts}. diff --git a/test/old_http_SUITE_data/http_set_resp.erl b/test/old_http_SUITE_data/http_set_resp.erl new file mode 100644 index 0000000..e575aab --- /dev/null +++ b/test/old_http_SUITE_data/http_set_resp.erl @@ -0,0 +1,25 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_set_resp). + +-export([init/2]). + +init(Req, Opts) -> + Headers = proplists:get_value(headers, Opts, #{}), + Body = proplists:get_value(body, Opts, <<"http_handler_set_resp">>), + Req2 = lists:foldl(fun({Name, Value}, R) -> + cowboy_req:set_resp_header(Name, Value, R) + end, Req, maps:to_list(Headers)), + Req3 = cowboy_req:set_resp_body(Body, Req2), + Req4 = cowboy_req:set_resp_header(<<"x-cowboy-test">>, <<"ok">>, Req3), + Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, Req4), + case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req5) of + false -> {ok, Req5, Opts}; + true -> + case cowboy_req:has_resp_body(Req5) of + false -> + {ok, Req5, Opts}; + true -> + {ok, cowboy_req:reply(200, Req5), Opts} + end + end. diff --git a/test/old_http_SUITE_data/rest_empty_resource.erl b/test/old_http_SUITE_data/rest_empty_resource.erl new file mode 100644 index 0000000..8a944cd --- /dev/null +++ b/test/old_http_SUITE_data/rest_empty_resource.erl @@ -0,0 +1,6 @@ +-module(rest_empty_resource). + +-export([init/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. diff --git a/test/old_http_SUITE_data/rest_expires.erl b/test/old_http_SUITE_data/rest_expires.erl new file mode 100644 index 0000000..8665b06 --- /dev/null +++ b/test/old_http_SUITE_data/rest_expires.erl @@ -0,0 +1,22 @@ +-module(rest_expires). + +-export([init/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). +-export([expires/2]). +-export([last_modified/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + +expires(Req, State) -> + {{{2012, 9, 21}, {22, 36, 14}}, Req, State}. + +last_modified(Req, State) -> + {{{2012, 9, 21}, {22, 36, 14}}, Req, State}. diff --git a/test/old_http_SUITE_data/rest_expires_binary.erl b/test/old_http_SUITE_data/rest_expires_binary.erl new file mode 100644 index 0000000..4d6bd3c --- /dev/null +++ b/test/old_http_SUITE_data/rest_expires_binary.erl @@ -0,0 +1,18 @@ +-module(rest_expires_binary). + +-export([init/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). +-export([expires/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + +expires(Req, State) -> + {<<"0">>, Req, State}. diff --git a/test/old_http_SUITE_data/rest_forbidden_resource.erl b/test/old_http_SUITE_data/rest_forbidden_resource.erl new file mode 100644 index 0000000..0a65228 --- /dev/null +++ b/test/old_http_SUITE_data/rest_forbidden_resource.erl @@ -0,0 +1,32 @@ +-module(rest_forbidden_resource). + +-export([init/2]). +-export([allowed_methods/2]). +-export([forbidden/2]). +-export([content_types_provided/2]). +-export([content_types_accepted/2]). +-export([to_text/2]). +-export([from_text/2]). + +init(Req, [Forbidden]) -> + {cowboy_rest, Req, Forbidden}. + +allowed_methods(Req, State) -> + {[<<"GET">>, <<"HEAD">>, <<"POST">>], Req, State}. + +forbidden(Req, State=true) -> + {true, Req, State}; +forbidden(Req, State=false) -> + {false, Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, to_text}], Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, from_text}], Req, State}. + +to_text(Req, State) -> + {<<"This is REST!">>, Req, State}. + +from_text(Req, State) -> + {{true, cowboy_req:path(Req)}, Req, State}. diff --git a/test/old_http_SUITE_data/rest_missing_callbacks.erl b/test/old_http_SUITE_data/rest_missing_callbacks.erl new file mode 100644 index 0000000..bf77c22 --- /dev/null +++ b/test/old_http_SUITE_data/rest_missing_callbacks.erl @@ -0,0 +1,24 @@ +-module(rest_missing_callbacks). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_accepted/2]). +-export([content_types_provided/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"GET">>, <<"PUT">>], Req, State}. + +content_types_accepted(Req, State) -> + ct_helper_error_h:ignore(cowboy_rest, process_content_type, 3), + {[ + {<<"application/json">>, put_application_json} + ], Req, State}. + +content_types_provided(Req, State) -> + ct_helper_error_h:ignore(cowboy_rest, set_resp_body, 2), + {[ + {<<"text/plain">>, get_text_plain} + ], Req, State}. diff --git a/test/old_http_SUITE_data/rest_nodelete_resource.erl b/test/old_http_SUITE_data/rest_nodelete_resource.erl new file mode 100644 index 0000000..b9f40bd --- /dev/null +++ b/test/old_http_SUITE_data/rest_nodelete_resource.erl @@ -0,0 +1,18 @@ +-module(rest_nodelete_resource). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"GET">>, <<"HEAD">>, <<"DELETE">>], Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. diff --git a/test/old_http_SUITE_data/rest_param_all.erl b/test/old_http_SUITE_data/rest_param_all.erl new file mode 100644 index 0000000..784214b --- /dev/null +++ b/test/old_http_SUITE_data/rest_param_all.erl @@ -0,0 +1,36 @@ +-module(rest_param_all). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). +-export([content_types_accepted/2]). +-export([put_text_plain/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"GET">>, <<"PUT">>], Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, '*'}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {_, _, Param} = maps:get(media_type, Req, {<<"text">>, <<"plain">>, []}), + Body = if + Param == '*' -> + <<"'*'">>; + Param == [] -> + <<"[]">>; + Param /= [] -> + iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param]) + end, + {Body, Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}. + +put_text_plain(Req0, State) -> + {ok, _, Req} = cowboy_req:read_body(Req0), + {true, Req, State}. diff --git a/test/old_http_SUITE_data/rest_patch_resource.erl b/test/old_http_SUITE_data/rest_patch_resource.erl new file mode 100644 index 0000000..341920d --- /dev/null +++ b/test/old_http_SUITE_data/rest_patch_resource.erl @@ -0,0 +1,38 @@ +-module(rest_patch_resource). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). +-export([content_types_accepted/2]). +-export([patch_text_plain/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. + +content_types_accepted(Req, State) -> + case cowboy_req:method(Req) of + <<"PATCH">> -> + {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req, State}; + _ -> + {[], Req, State} + end. + +patch_text_plain(Req, State) -> + case cowboy_req:read_body(Req) of + {ok, <<"stop">>, Req0} -> + {stop, cowboy_req:reply(400, Req0), State}; + {ok, <<"false">>, Req0} -> + {false, Req0, State}; + {ok, _Body, Req0} -> + {true, Req0, State} + end. diff --git a/test/old_http_SUITE_data/rest_post_charset_resource.erl b/test/old_http_SUITE_data/rest_post_charset_resource.erl new file mode 100644 index 0000000..0be6aa3 --- /dev/null +++ b/test/old_http_SUITE_data/rest_post_charset_resource.erl @@ -0,0 +1,19 @@ +-module(rest_post_charset_resource). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_accepted/2]). +-export([from_text/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"POST">>], Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"text">>, <<"plain">>, [{<<"charset">>, <<"utf-8">>}]}, + from_text}], Req, State}. + +from_text(Req, State) -> + {true, Req, State}. diff --git a/test/old_http_SUITE_data/rest_postonly_resource.erl b/test/old_http_SUITE_data/rest_postonly_resource.erl new file mode 100644 index 0000000..942e55d --- /dev/null +++ b/test/old_http_SUITE_data/rest_postonly_resource.erl @@ -0,0 +1,18 @@ +-module(rest_postonly_resource). + +-export([init/2]). +-export([allowed_methods/2]). +-export([content_types_accepted/2]). +-export([from_text/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +allowed_methods(Req, State) -> + {[<<"POST">>], Req, State}. + +content_types_accepted(Req, State) -> + {[{{<<"text">>, <<"plain">>, '*'}, from_text}], Req, State}. + +from_text(Req, State) -> + {true, Req, State}. diff --git a/test/old_http_SUITE_data/rest_resource_etags.erl b/test/old_http_SUITE_data/rest_resource_etags.erl new file mode 100644 index 0000000..25b3080 --- /dev/null +++ b/test/old_http_SUITE_data/rest_resource_etags.erl @@ -0,0 +1,37 @@ +-module(rest_resource_etags). + +-export([init/2]). +-export([generate_etag/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +generate_etag(Req, State) -> + #{type := Type} = cowboy_req:match_qs([type], Req), + case Type of + %% Correct return values from generate_etag/2. + <<"tuple-weak">> -> + {{weak, <<"etag-header-value">>}, Req, State}; + <<"tuple-strong">> -> + {{strong, <<"etag-header-value">>}, Req, State}; + %% Backwards compatible return values from generate_etag/2. + <<"binary-weak-quoted">> -> + {<<"W/\"etag-header-value\"">>, Req, State}; + <<"binary-strong-quoted">> -> + {<<"\"etag-header-value\"">>, Req, State}; + %% Invalid return values from generate_etag/2. + <<"binary-strong-unquoted">> -> + ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1), + {<<"etag-header-value">>, Req, State}; + <<"binary-weak-unquoted">> -> + ct_helper_error_h:ignore(cow_http_hd, parse_etag, 1), + {<<"W/etag-header-value">>, Req, State} + end. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. diff --git a/test/old_http_SUITE_data/rest_simple_resource.erl b/test/old_http_SUITE_data/rest_simple_resource.erl new file mode 100644 index 0000000..68e1b95 --- /dev/null +++ b/test/old_http_SUITE_data/rest_simple_resource.erl @@ -0,0 +1,14 @@ +-module(rest_simple_resource). + +-export([init/2]). +-export([content_types_provided/2]). +-export([get_text_plain/2]). + +init(Req, Opts) -> + {cowboy_rest, Req, Opts}. + +content_types_provided(Req, State) -> + {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. + +get_text_plain(Req, State) -> + {<<"This is REST!">>, Req, State}. -- cgit v1.2.3