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/handlers/switch_protocol_flush_h.erl | 50 ++ test/http_SUITE.erl | 799 +-------------------- test/http_SUITE_data/http_chunked.erl | 15 - test/http_SUITE_data/http_echo_body.erl | 21 - test/http_SUITE_data/http_errors.erl | 17 - test/http_SUITE_data/http_handler.erl | 10 - test/http_SUITE_data/http_loop_stream_recv.erl | 34 - test/http_SUITE_data/http_req_attr.erl | 15 - test/http_SUITE_data/http_set_resp.erl | 25 - test/http_SUITE_data/rest_empty_resource.erl | 6 - test/http_SUITE_data/rest_expires.erl | 22 - test/http_SUITE_data/rest_expires_binary.erl | 18 - test/http_SUITE_data/rest_forbidden_resource.erl | 32 - test/http_SUITE_data/rest_missing_callbacks.erl | 24 - test/http_SUITE_data/rest_nodelete_resource.erl | 18 - test/http_SUITE_data/rest_param_all.erl | 36 - test/http_SUITE_data/rest_patch_resource.erl | 38 - .../http_SUITE_data/rest_post_charset_resource.erl | 19 - test/http_SUITE_data/rest_postonly_resource.erl | 18 - test/http_SUITE_data/rest_resource_etags.erl | 37 - test/http_SUITE_data/rest_simple_resource.erl | 14 - test/old_http_SUITE.erl | 796 ++++++++++++++++++++ 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 + test/sys_SUITE.erl | 4 - 42 files changed, 1291 insertions(+), 1196 deletions(-) create mode 100644 test/handlers/switch_protocol_flush_h.erl delete mode 100644 test/http_SUITE_data/http_chunked.erl delete mode 100644 test/http_SUITE_data/http_echo_body.erl delete mode 100644 test/http_SUITE_data/http_errors.erl delete mode 100644 test/http_SUITE_data/http_handler.erl delete mode 100644 test/http_SUITE_data/http_loop_stream_recv.erl delete mode 100644 test/http_SUITE_data/http_req_attr.erl delete mode 100644 test/http_SUITE_data/http_set_resp.erl delete mode 100644 test/http_SUITE_data/rest_empty_resource.erl delete mode 100644 test/http_SUITE_data/rest_expires.erl delete mode 100644 test/http_SUITE_data/rest_expires_binary.erl delete mode 100644 test/http_SUITE_data/rest_forbidden_resource.erl delete mode 100644 test/http_SUITE_data/rest_missing_callbacks.erl delete mode 100644 test/http_SUITE_data/rest_nodelete_resource.erl delete mode 100644 test/http_SUITE_data/rest_param_all.erl delete mode 100644 test/http_SUITE_data/rest_patch_resource.erl delete mode 100644 test/http_SUITE_data/rest_post_charset_resource.erl delete mode 100644 test/http_SUITE_data/rest_postonly_resource.erl delete mode 100644 test/http_SUITE_data/rest_resource_etags.erl delete mode 100644 test/http_SUITE_data/rest_simple_resource.erl create mode 100644 test/old_http_SUITE.erl 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') diff --git a/test/handlers/switch_protocol_flush_h.erl b/test/handlers/switch_protocol_flush_h.erl new file mode 100644 index 0000000..3d525ee --- /dev/null +++ b/test/handlers/switch_protocol_flush_h.erl @@ -0,0 +1,50 @@ +%% This module is used to test the flushing of messages when +%% switch_protocol is executed by cowboy_http. + +-module(switch_protocol_flush_h). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). +-export([takeover/7]). +-export([validate/1]). + +init(StreamID, Req, _) -> + Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))), + %% Send ourselves a few messages that may or may not be flushed. + self() ! good, + self() ! {'EXIT', Pid, normal}, + self() ! {system, a, b}, + self() ! {{self(), StreamID}, hello}, + self() ! {'$gen_call', a, b}, + self() ! {timeout, make_ref(), ?MODULE}, + self() ! {ranch_tcp, socket, <<"123">>}, + {[{switch_protocol, #{}, ?MODULE, Pid}], undefined}. + +info(_, _, State) -> + {[], State}. + +terminate(_, _, _) -> + ok. + +takeover(_, _, _, _, _, _, Pid) -> + Msgs = receive_all([]), + Pid ! {Pid, Msgs}, + exit(normal). + +receive_all(Acc) -> + receive + Msg -> + receive_all([Msg|Acc]) + after 0 -> + Acc + end. + +validate(Msgs) -> + [ + {ranch_tcp, socket, <<"123">>}, + {'$gen_call', a, b}, + {system, a, b}, + good + ] = Msgs, + ok. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index a971e45..16d7e76 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -1,5 +1,4 @@ -%% Copyright (c) 2011-2017, Loïc Hoguin -%% Copyright (c) 2011, Anthony Ramine +%% Copyright (c) 2018, Loïc Hoguin %% %% Permission to use, copy, modify, and/or distribute this software for any %% purpose with or without fee is hereby granted, provided that the above @@ -18,779 +17,33 @@ -compile(nowarn_export_all). -import(ct_helper, [config/2]). +-import(ct_helper, [doc/1]). -import(cowboy_test, [gun_open/1]). --import(cowboy_test, [gun_open/2]). --import(cowboy_test, [gun_down/1]). --import(cowboy_test, [raw_open/1]). --import(cowboy_test, [raw_send/2]). --import(cowboy_test, [raw_recv_head/1]). --import(cowboy_test, [raw_expect_recv/2]). -%% ct. - -all() -> - [ - {group, http}, - {group, https}, - {group, http_compress}, - {group, https_compress}, - {group, parse_host}, - {group, request_timeout_infinity}, - {group, idle_timeout_infinity}, - {group, set_env}, - {group, router_compile} - ]. - -groups() -> - Tests = ct_helper:all(?MODULE) -- [ - parse_host, set_env_dispatch, path_allow_colon, - request_timeout_infinity, idle_timeout_infinity - ], - [ - {http, [], Tests}, %% @todo parallel - {https, [parallel], Tests}, - {http_compress, [parallel], Tests}, - {https_compress, [parallel], Tests}, - {parse_host, [], [ - parse_host - ]}, - {request_timeout_infinity, [], [ - request_timeout_infinity - ]}, - {idle_timeout_infinity, [], [ - idle_timeout_infinity - ]}, - {set_env, [], [ - set_env_dispatch - ]}, - {router_compile, [], [ - path_allow_colon - ]} - ]. - -init_per_group(Name = http, Config) -> - cowboy_test:init_http(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config); -init_per_group(Name = https, Config) -> - cowboy_test:init_https(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config); -init_per_group(Name = http_compress, Config) -> - cowboy_test:init_http(Name, #{ - env => #{dispatch => init_dispatch(Config)}, - compress => true - }, Config); -init_per_group(Name = https_compress, Config) -> - cowboy_test:init_https(Name, #{ - env => #{dispatch => init_dispatch(Config)}, - compress => true - }, Config); -init_per_group(parse_host, Config) -> - Dispatch = cowboy_router:compile([ - {'_', [ - {"/req_attr", http_req_attr, []} - ]} +all() -> [{group, clear}]. + +groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}]. + +init_routes(_) -> [ + {"localhost", [ + {"/", hello_h, []} + ]} +]. + +switch_protocol_flush(Config) -> + doc("Confirm that switch_protocol does not flush unrelated messages."), + ProtoOpts = #{ + env => #{dispatch => cowboy_router:compile(init_routes(Config))}, + stream_handlers => [switch_protocol_flush_h] + }, + {ok, _} = cowboy:start_clear(switch_protocol_flush, [{port, 0}], ProtoOpts), + Port = ranch:get_port(switch_protocol_flush), + Self = self(), + ConnPid = gun_open([{port, Port}, {type, tcp}, {protocol, http}|Config]), + _ = gun:get(ConnPid, "/", [ + {<<"x-test-pid">>, pid_to_list(Self)} ]), - {ok, _} = cowboy:start_clear(parse_host, [{port, 0}], #{ - env => #{dispatch => Dispatch} - }), - Port = ranch:get_port(parse_host), - [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; -init_per_group(request_timeout_infinity, Config) -> - Ref = request_timeout_infinity, - {ok, Pid} = cowboy:start_clear(Ref, [{port, 0}], #{ - env => #{dispatch => init_dispatch(Config)}, - request_timeout => infinity - }), - Port = ranch:get_port(request_timeout_infinity), - [{pid, Pid}, {ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; -init_per_group(idle_timeout_infinity, Config) -> - Ref = idle_timeout_infinity, - {ok, Pid} = cowboy:start_clear(Ref, [{port, 0}], #{ - env => #{dispatch => init_dispatch(Config)}, - idle_timeout => infinity - }), - Port = ranch:get_port(idle_timeout_infinity), - [{pid, Pid}, {ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; -init_per_group(set_env, Config) -> - {ok, _} = cowboy:start_clear(set_env, [{port, 0}], #{ - env => #{dispatch => []} - }), - Port = ranch:get_port(set_env), - [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; -init_per_group(router_compile, Config) -> - Config. - -end_per_group(router_compile, _) -> - ok; -end_per_group(Name, _) -> - ok = cowboy:stop_listener(Name). - -%% Dispatch configuration. - -init_dispatch(_) -> - cowboy_router:compile([ - {"localhost", [ - {"/chunked_response", http_chunked, []}, - {"/headers/dupe", http_handler, - [{headers, #{<<"connection">> => <<"close">>}}]}, - {"/set_resp/header", http_set_resp, - [{headers, #{<<"vary">> => <<"Accept">>}}]}, - {"/set_resp/overwrite", http_set_resp, - [{headers, #{<<"server">> => <<"DesireDrive/1.0">>}}]}, - {"/set_resp/body", http_set_resp, - [{body, <<"A flameless dance does not equal a cycle">>}]}, - {"/handler_errors", http_errors, []}, - {"/echo/body", http_echo_body, []}, - {"/param_all", rest_param_all, []}, - {"/bad_accept", rest_simple_resource, []}, - {"/bad_content_type", rest_patch_resource, []}, - {"/simple", rest_simple_resource, []}, - {"/forbidden_post", rest_forbidden_resource, [true]}, - {"/simple_post", rest_forbidden_resource, [false]}, - {"/missing_get_callbacks", rest_missing_callbacks, []}, - {"/missing_put_callbacks", rest_missing_callbacks, []}, - {"/nodelete", rest_nodelete_resource, []}, - {"/post_charset", rest_post_charset_resource, []}, - {"/postonly", rest_postonly_resource, []}, - {"/patch", rest_patch_resource, []}, - {"/resetags", rest_resource_etags, []}, - {"/rest_expires", rest_expires, []}, - {"/rest_expires_binary", rest_expires_binary, []}, - {"/rest_empty_resource", rest_empty_resource, []}, - {"/loop_stream_recv", http_loop_stream_recv, []}, - {"/", http_handler, []} - ]} - ]). - -%% Convenience functions. - -do_raw(Data, Config) -> - Client = raw_open(Config), - ok = raw_send(Client, Data), - case catch raw_recv_head(Client) of - {'EXIT', _} -> closed; - Resp -> element(2, cow_http:parse_status_line(Resp)) - end. - -do_get(Path, Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, Path), - {response, _, Status, _} = gun:await(ConnPid, Ref), - gun:close(ConnPid), - Status. - -%% Tests. - -check_raw_status(Config) -> - Huge = [$0 || _ <- lists:seq(1, 5000)], - HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77=" - "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]), - ResponsePacket = -"HTTP/1.0 302 Found\r -Location: http://www.google.co.il/\r -Cache-Control: private\r -Content-Type: text/html; charset=UTF-8\r -Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r -Date: Sun, 04 Dec 2011 15:55:01 GMT\r -Server: gws\r -Content-Length: 221\r -X-XSS-Protection: 1; mode=block\r -X-Frame-Options: SAMEORIGIN\r -\r - -302 Moved -

302 Moved

-The document has moved -here. -", - Tests = [ - {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n" - "Set-Cookie: ", HugeCookie, "\r\n\r\n"]}, - {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {400, "\n"}, - {400, "Garbage\r\n\r\n"}, - {400, "\r\n\r\n\r\n\r\n\r\n\r\n"}, - {400, " / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {400, "GET HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"}, - {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"}, - {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"}, - {400, ResponsePacket}, - {408, "GET / HTTP/1.1\r\n"}, - {408, "GET / HTTP/1.1\r\nHost: localhost"}, - {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"}, - {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"}, - {closed, Huge}, - {431, "GET / HTTP/1.1\r\n" ++ Huge}, - {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"}, - {closed, ""}, - {closed, "\r\n"}, - {closed, "\r\n\r\n"}, - {closed, "GET / HTTP/1.1"} - ], - _ = [{Status, Packet} = begin - Ret = do_raw(Packet, Config), - {Ret, Packet} - end || {Status, Packet} <- Tests], - ok. - -check_status(Config) -> - Tests = [ - {200, "/"}, - {200, "/simple"}, - {404, "/not/found"}, - {500, "/handler_errors?case=init_before_reply"} - ], - _ = [{Status, URL} = begin - Ret = do_get(URL, Config), - {Ret, URL} - end || {Status, URL} <- Tests]. - -%% Check if sending requests whose size is around the MTU breaks something. -echo_body(Config) -> - MTU = ct_helper:get_loopback_mtu(), - _ = [begin - Body = list_to_binary(lists:duplicate(Size, $a)), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", [], Body), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref) - end || Size <- lists:seq(MTU - 500, MTU)], - ok. - -%% Check if sending request whose size is bigger than 1000000 bytes causes 413 -echo_body_max_length(Config) -> - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>), - {response, nofin, 413, _} = gun:await(ConnPid, Ref), - ok. - -error_init_after_reply(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - ok. - -headers_dupe(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/headers/dupe"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - %% Ensure that only one connection header was received. - [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>], - gun_down(ConnPid). - -http10_chunkless(Config) -> - ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}), - Ref = gun:get(ConnPid, "/chunked_response"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), - {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref), - gun_down(ConnPid). - -http10_hostless(Config) -> - Name = http10_hostless, - Port10 = config(port, Config) + 10, - {Transport, Protocol} = case config(type, Config) of - tcp -> {ranch_tcp, cowboy_clear}; - ssl -> {ranch_ssl, cowboy_tls} - end, - ranch:start_listener(Name, 5, Transport, - config(opts, Config) ++ [{port, Port10}], - Protocol, #{ - env =>#{dispatch => cowboy_router:compile([ - {'_', [{"/http1.0/hostless", http_handler, []}]}])}, - max_keepalive => 50, - timeout => 500 - }), - 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", - [{port, Port10}|Config]), - cowboy:stop_listener(http10_hostless). - -http10_keepalive_default(Config) -> - Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n", - Client = raw_open(Config), - ok = raw_send(Client, Normal), - case catch raw_recv_head(Client) of - {'EXIT', _} -> error(closed); - Data -> - %% Cowboy always advertises itself as HTTP/1.1. - {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), - {Headers, _} = cow_http:parse_headers(Rest), - {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers) - end, - ok = raw_send(Client, Normal), - case catch raw_recv_head(Client) of - {'EXIT', _} -> closed; - _ -> error(not_closed) - end. - -http10_keepalive_forced(Config) -> - Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n", - Client = raw_open(Config), - ok = raw_send(Client, Keepalive), - case catch raw_recv_head(Client) of - {'EXIT', _} -> error(closed); - Data -> - %% Cowboy always advertises itself as HTTP/1.1. - {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), - {Headers, _} = cow_http:parse_headers(Rest), - {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers) - end, - ok = raw_send(Client, Keepalive), - case catch raw_recv_head(Client) of - {'EXIT', Err} -> error({closed, Err}); - _ -> ok - end. - -keepalive_nl(Config) -> - ConnPid = gun_open(Config), - Refs = [begin - Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]), - gun:dbg_send_raw(ConnPid, <<"\r\n">>), - Ref - end || _ <- lists:seq(1, 10)], - _ = [begin - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - false = lists:keymember(<<"connection">>, 1, Headers) - end || Ref <- Refs], - ok. - -keepalive_stream_loop(Config) -> - ConnPid = gun_open(Config), - Refs = [begin - Ref = gun:post(ConnPid, "/loop_stream_recv", - [{<<"content-type">>, <<"application/octet-stream">>}]), - _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>) - || ID <- lists:seq(1, 250)], - gun:data(ConnPid, Ref, fin, <<>>), - Ref - end || _ <- lists:seq(1, 10)], - _ = [begin - {response, fin, 200, _} = gun:await(ConnPid, Ref) - end || Ref <- Refs], - ok. - -do_nc(Config, Input) -> - Cat = os:find_executable("cat"), - Nc = os:find_executable("nc"), - case {Cat, Nc} of - {false, _} -> - {skip, {notfound, cat}}; - {_, false} -> - {skip, {notfound, nc}}; - _Good -> - %% Throw garbage at the server then check if it's still up. - StrPort = integer_to_list(config(port, Config)), - [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) - || _ <- lists:seq(1, 100)], - 200 = do_get("/", Config) - end. - -nc_rand(Config) -> - do_nc(Config, "/dev/urandom"). - -nc_zero(Config) -> - do_nc(Config, "/dev/zero"). - -parse_host(Config) -> - ConnPid = gun_open(Config), - Tests = [ - {<<"example.org:8080">>, <<"example.org\n8080">>}, - {<<"example.org">>, <<"example.org\n80">>}, - {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>}, - {<<"192.0.2.1">>, <<"192.0.2.1\n80">>}, - {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>}, - {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>}, - {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>}, - {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>} - ], - [begin - Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port", - [{<<"host">>, Host}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref) - end || {Host, Body} <- Tests], - ok. - -rest_param_all(Config) -> - ConnPid = gun_open(Config), - %% Accept without param. - Ref1 = gun:get(ConnPid, "/param_all", - [{<<"accept">>, <<"text/plain">>}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref1), - {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1), - %% Accept with param. - Ref2 = gun:get(ConnPid, "/param_all", - [{<<"accept">>, <<"text/plain;level=1">>}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref2), - {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2), - %% Accept with param and quality. - Ref3 = gun:get(ConnPid, "/param_all", - [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref3), - {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3), - Ref4 = gun:get(ConnPid, "/param_all", - [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref4), - {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4), - %% Without Accept. - Ref5 = gun:get(ConnPid, "/param_all"), - {response, nofin, 200, _} = gun:await(ConnPid, Ref5), - {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5), - %% Content-Type without param. - Ref6 = gun:put(ConnPid, "/param_all", - [{<<"content-type">>, <<"text/plain">>}]), - gun:data(ConnPid, Ref6, fin, "Hello world!"), - {response, fin, 204, _} = gun:await(ConnPid, Ref6), - %% Content-Type with param. - Ref7 = gun:put(ConnPid, "/param_all", - [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]), - gun:data(ConnPid, Ref7, fin, "Hello world!"), - {response, fin, 204, _} = gun:await(ConnPid, Ref7), - ok. - -rest_bad_accept(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/bad_accept", - [{<<"accept">>, <<"1">>}]), - {response, fin, 400, _} = gun:await(ConnPid, Ref), - ok. - -rest_bad_content_type(Config) -> - ConnPid = gun_open(Config), - Ref = gun:patch(ConnPid, "/bad_content_type", - [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>), - {response, fin, 415, _} = gun:await(ConnPid, Ref), - ok. - -rest_expires(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/rest_expires"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers), - {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers), - Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>, - ok. - -rest_expires_binary(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/rest_expires_binary"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers), - ok. - -rest_last_modified_undefined(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/simple", - [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - ok. - -rest_keepalive(Config) -> - ConnPid = gun_open(Config), - Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)], - _ = [begin - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - false = lists:keymember(<<"connection">>, 1, Headers) - end || Ref <- Refs], - ok. - -rest_keepalive_post(Config) -> - ConnPid = gun_open(Config), - Refs = [begin - Ref1 = gun:post(ConnPid, "/forbidden_post", [ - {<<"content-type">>, <<"text/plain">>}, - {<<"content-length">>, <<"12">>} - ]), - gun:data(ConnPid, Ref1, fin, "Hello world!"), - Ref2 = gun:post(ConnPid, "/simple_post", [ - {<<"content-type">>, <<"text/plain">>}, - {<<"content-length">>, <<"12">>} - ]), - gun:data(ConnPid, Ref2, fin, "Hello world!"), - {Ref1, Ref2} - end || _ <- lists:seq(1, 5)], - _ = [begin - {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1), - false = lists:keymember(<<"connection">>, 1, Headers1), - {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2), - false = lists:keymember(<<"connection">>, 1, Headers2) - end || {Ref1, Ref2} <- Refs], - ok. - -rest_missing_get_callbacks(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/missing_get_callbacks"), - {response, fin, 500, _} = gun:await(ConnPid, Ref), - ok. - -rest_missing_put_callbacks(Config) -> - ConnPid = gun_open(Config), - Ref = gun:put(ConnPid, "/missing_put_callbacks", - [{<<"content-type">>, <<"application/json">>}], <<"{}">>), - {response, fin, 500, _} = gun:await(ConnPid, Ref), - ok. - -rest_nodelete(Config) -> - ConnPid = gun_open(Config), - Ref = gun:delete(ConnPid, "/nodelete"), - {response, fin, 500, _} = gun:await(ConnPid, Ref), - ok. - -rest_options_default(Config) -> - ConnPid = gun_open(Config), - Ref = gun:options(ConnPid, "/rest_empty_resource"), - {response, fin, 200, Headers} = gun:await(ConnPid, Ref), - {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers), - ok. - -rest_patch(Config) -> - Tests = [ - {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>}, - {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>}, - {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>}, - {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>} - ], - ConnPid = gun_open(Config), - _ = [begin - Ref = gun:patch(ConnPid, "/patch", Headers, Body), - {response, fin, Status, _} = gun:await(ConnPid, Ref) - end || {Status, Headers, Body} <- Tests], - ok. - -rest_post_charset(Config) -> - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/post_charset", - [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"), - {response, fin, 204, _} = gun:await(ConnPid, Ref), - ok. - -rest_postonly(Config) -> - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/postonly", - [{<<"content-type">>, <<"text/plain">>}], "12345"), - {response, fin, 204, _} = gun:await(ConnPid, Ref), - ok. - -rest_resource_get_etag(Config, Type) -> - rest_resource_get_etag(Config, Type, []). - -rest_resource_get_etag(Config, Type, Headers) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers), - {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref), - case lists:keyfind(<<"etag">>, 1, RespHeaders) of - false -> {Status, false}; - {<<"etag">>, ETag} -> {Status, ETag} - end. - -rest_resource_etags(Config) -> - Tests = [ - {200, <<"W/\"etag-header-value\"">>, "tuple-weak"}, - {200, <<"\"etag-header-value\"">>, "tuple-strong"}, - {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, - {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"}, - {500, false, "binary-strong-unquoted"}, - {500, false, "binary-weak-unquoted"} - ], - _ = [{Status, ETag, Type} = begin - {Ret, RespETag} = rest_resource_get_etag(Config, Type), - {Ret, RespETag, Type} - end || {Status, ETag, Type} <- Tests]. - -rest_resource_etags_if_none_match(Config) -> - Tests = [ - {304, <<"W/\"etag-header-value\"">>, "tuple-weak"}, - {304, <<"\"etag-header-value\"">>, "tuple-strong"}, - {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, - {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"} - ], - _ = [{Status, Type} = begin - {Ret, _} = rest_resource_get_etag(Config, Type, - [{<<"if-none-match">>, ETag}]), - {Ret, Type} - end || {Status, ETag, Type} <- Tests]. - -set_env_dispatch(Config) -> - ConnPid1 = gun_open(Config), - Ref1 = gun:get(ConnPid1, "/"), - {response, fin, 400, _} = gun:await(ConnPid1, Ref1), - ok = cowboy:set_env(set_env, dispatch, - cowboy_router:compile([{'_', [{"/", http_handler, []}]}])), - ConnPid2 = gun_open(Config), - Ref2 = gun:get(ConnPid2, "/"), - {response, nofin, 200, _} = gun:await(ConnPid2, Ref2), - ok. - -path_allow_colon(_Config) -> - cowboy_router:compile([{'_', [{"/foo/bar:blah", http_handler, []}]}]), - ok. - -set_resp_body(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/set_resp/body"), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, <<"A flameless dance does not equal a cycle">>} - = gun:await_body(ConnPid, Ref), - ok. - -set_resp_header(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/set_resp/header"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers), - {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers), - ok. - -set_resp_overwrite(Config) -> - ConnPid = gun_open(Config), - Ref = gun:get(ConnPid, "/set_resp/overwrite"), - {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), - {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers), - ok. - -slowloris(Config) -> - Client = raw_open(Config), - try - [begin - ok = raw_send(Client, [C]), - receive after 250 -> ok end - end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n" - "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n" - "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"], - error(failure) - catch error:{badmatch, _} -> - ok - end. - -slowloris2(Config) -> - Client = raw_open(Config), - ok = raw_send(Client, "GET / HTTP/1.1\r\n"), - receive after 300 -> ok end, - ok = raw_send(Client, "Host: localhost\r\n"), - receive after 300 -> ok end, - Data = raw_recv_head(Client), - {_, 408, _, _} = cow_http:parse_status_line(Data), - ok. - -te_chunked(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]), - gun:data(ConnPid, Ref, fin, Body), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -do_body_to_chunks(_, <<>>, Acc) -> - lists:reverse([<<"0\r\n\r\n">>|Acc]); -do_body_to_chunks(ChunkSize, Body, Acc) -> - BodySize = byte_size(Body), - ChunkSize2 = case BodySize < ChunkSize of - true -> BodySize; - false -> ChunkSize - end, - << Chunk:ChunkSize2/binary, Rest/binary >> = Body, - ChunkSizeBin = integer_to_binary(ChunkSize2, 16), - do_body_to_chunks(ChunkSize, Rest, - [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). - -te_chunked_chopped(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", - [{<<"content-type">>, <<"text/plain">>}]), - _ = [begin - ok = gun:dbg_send_raw(ConnPid, << C >>), - receive after 10 -> ok end - end || << C >> <= Body2], - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -te_chunked_delayed(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = do_body_to_chunks(50, Body, []), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", - [{<<"content-type">>, <<"text/plain">>}]), - _ = [begin - ok = gun:dbg_send_raw(ConnPid, Chunk), - receive after 10 -> ok end - end || Chunk <- Chunks], - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -te_chunked_split_body(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = do_body_to_chunks(50, Body, []), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", - [{<<"content-type">>, <<"text/plain">>}]), - _ = [begin - case Chunk of - <<"0\r\n\r\n">> -> - ok = gun:dbg_send_raw(ConnPid, Chunk); - _ -> - [Size, ChunkBody, <<>>] = - binary:split(Chunk, [<<"\r\n">>], [global]), - PartASize = random:uniform(byte_size(ChunkBody)), - <> = ChunkBody, - ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]), - receive after 10 -> ok end, - ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>]) - end - end || Chunk <- Chunks], - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -te_chunked_split_crlf(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = do_body_to_chunks(50, Body, []), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", - [{<<"content-type">>, <<"text/plain">>}]), - _ = [begin - %% Split in the newline just before the end of the chunk. - Len = byte_size(Chunk) - (random:uniform(2) - 1), - << Chunk2:Len/binary, End/binary >> = Chunk, - ok = gun:dbg_send_raw(ConnPid, Chunk2), - receive after 10 -> ok end, - ok = gun:dbg_send_raw(ConnPid, End) - end || Chunk <- Chunks], - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -te_identity(Config) -> - Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - ConnPid = gun_open(Config), - Ref = gun:post(ConnPid, "/echo/body", [], Body), - {response, nofin, 200, _} = gun:await(ConnPid, Ref), - {ok, Body} = gun:await_body(ConnPid, Ref), - ok. - -request_timeout_infinity(Config) -> - Pid = config(pid, Config), - Ref = erlang:monitor(process, Pid), - _ = gun_open(Config), - receive - {'DOWN', Ref, process, Pid, Reason} -> - error(Reason) - after 1000 -> - ok - end. - -idle_timeout_infinity(Config) -> - Pid = config(pid, Config), - Ref = erlang:monitor(process, Pid), - ConnPid = gun_open(Config), - gun:post(ConnPid, "/echo/body", [], <<"TEST">>), receive - {'DOWN', Ref, process, Pid, Reason} -> - error(Reason) - after 1000 -> - ok + {Self, Events} -> + switch_protocol_flush_h:validate(Events) end. diff --git a/test/http_SUITE_data/http_chunked.erl b/test/http_SUITE_data/http_chunked.erl deleted file mode 100644 index 645eefe..0000000 --- a/test/http_SUITE_data/http_chunked.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% 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/http_SUITE_data/http_echo_body.erl b/test/http_SUITE_data/http_echo_body.erl deleted file mode 100644 index d803108..0000000 --- a/test/http_SUITE_data/http_echo_body.erl +++ /dev/null @@ -1,21 +0,0 @@ -%% 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/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl deleted file mode 100644 index 14e3d09..0000000 --- a/test/http_SUITE_data/http_errors.erl +++ /dev/null @@ -1,17 +0,0 @@ -%% 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/http_SUITE_data/http_handler.erl b/test/http_SUITE_data/http_handler.erl deleted file mode 100644 index eb31aa8..0000000 --- a/test/http_SUITE_data/http_handler.erl +++ /dev/null @@ -1,10 +0,0 @@ -%% 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/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl deleted file mode 100644 index 8c6f6b0..0000000 --- a/test/http_SUITE_data/http_loop_stream_recv.erl +++ /dev/null @@ -1,34 +0,0 @@ -%% 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/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl deleted file mode 100644 index c6a940e..0000000 --- a/test/http_SUITE_data/http_req_attr.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% 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/http_SUITE_data/http_set_resp.erl b/test/http_SUITE_data/http_set_resp.erl deleted file mode 100644 index e575aab..0000000 --- a/test/http_SUITE_data/http_set_resp.erl +++ /dev/null @@ -1,25 +0,0 @@ -%% 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/http_SUITE_data/rest_empty_resource.erl b/test/http_SUITE_data/rest_empty_resource.erl deleted file mode 100644 index 8a944cd..0000000 --- a/test/http_SUITE_data/rest_empty_resource.erl +++ /dev/null @@ -1,6 +0,0 @@ --module(rest_empty_resource). - --export([init/2]). - -init(Req, Opts) -> - {cowboy_rest, Req, Opts}. diff --git a/test/http_SUITE_data/rest_expires.erl b/test/http_SUITE_data/rest_expires.erl deleted file mode 100644 index 8665b06..0000000 --- a/test/http_SUITE_data/rest_expires.erl +++ /dev/null @@ -1,22 +0,0 @@ --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/http_SUITE_data/rest_expires_binary.erl b/test/http_SUITE_data/rest_expires_binary.erl deleted file mode 100644 index 4d6bd3c..0000000 --- a/test/http_SUITE_data/rest_expires_binary.erl +++ /dev/null @@ -1,18 +0,0 @@ --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/http_SUITE_data/rest_forbidden_resource.erl b/test/http_SUITE_data/rest_forbidden_resource.erl deleted file mode 100644 index 0a65228..0000000 --- a/test/http_SUITE_data/rest_forbidden_resource.erl +++ /dev/null @@ -1,32 +0,0 @@ --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/http_SUITE_data/rest_missing_callbacks.erl b/test/http_SUITE_data/rest_missing_callbacks.erl deleted file mode 100644 index bf77c22..0000000 --- a/test/http_SUITE_data/rest_missing_callbacks.erl +++ /dev/null @@ -1,24 +0,0 @@ --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/http_SUITE_data/rest_nodelete_resource.erl b/test/http_SUITE_data/rest_nodelete_resource.erl deleted file mode 100644 index b9f40bd..0000000 --- a/test/http_SUITE_data/rest_nodelete_resource.erl +++ /dev/null @@ -1,18 +0,0 @@ --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/http_SUITE_data/rest_param_all.erl b/test/http_SUITE_data/rest_param_all.erl deleted file mode 100644 index 784214b..0000000 --- a/test/http_SUITE_data/rest_param_all.erl +++ /dev/null @@ -1,36 +0,0 @@ --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/http_SUITE_data/rest_patch_resource.erl b/test/http_SUITE_data/rest_patch_resource.erl deleted file mode 100644 index 341920d..0000000 --- a/test/http_SUITE_data/rest_patch_resource.erl +++ /dev/null @@ -1,38 +0,0 @@ --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/http_SUITE_data/rest_post_charset_resource.erl b/test/http_SUITE_data/rest_post_charset_resource.erl deleted file mode 100644 index 0be6aa3..0000000 --- a/test/http_SUITE_data/rest_post_charset_resource.erl +++ /dev/null @@ -1,19 +0,0 @@ --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/http_SUITE_data/rest_postonly_resource.erl b/test/http_SUITE_data/rest_postonly_resource.erl deleted file mode 100644 index 942e55d..0000000 --- a/test/http_SUITE_data/rest_postonly_resource.erl +++ /dev/null @@ -1,18 +0,0 @@ --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/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl deleted file mode 100644 index 25b3080..0000000 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ /dev/null @@ -1,37 +0,0 @@ --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/http_SUITE_data/rest_simple_resource.erl b/test/http_SUITE_data/rest_simple_resource.erl deleted file mode 100644 index 68e1b95..0000000 --- a/test/http_SUITE_data/rest_simple_resource.erl +++ /dev/null @@ -1,14 +0,0 @@ --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}. diff --git a/test/old_http_SUITE.erl b/test/old_http_SUITE.erl new file mode 100644 index 0000000..539ab04 --- /dev/null +++ b/test/old_http_SUITE.erl @@ -0,0 +1,796 @@ +%% Copyright (c) 2011-2017, Loïc Hoguin +%% Copyright (c) 2011, Anthony Ramine +%% +%% 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(old_http_SUITE). +-compile(export_all). +-compile(nowarn_export_all). + +-import(ct_helper, [config/2]). +-import(cowboy_test, [gun_open/1]). +-import(cowboy_test, [gun_open/2]). +-import(cowboy_test, [gun_down/1]). +-import(cowboy_test, [raw_open/1]). +-import(cowboy_test, [raw_send/2]). +-import(cowboy_test, [raw_recv_head/1]). +-import(cowboy_test, [raw_expect_recv/2]). + +%% ct. + +all() -> + [ + {group, http}, + {group, https}, + {group, http_compress}, + {group, https_compress}, + {group, parse_host}, + {group, request_timeout_infinity}, + {group, idle_timeout_infinity}, + {group, set_env}, + {group, router_compile} + ]. + +groups() -> + Tests = ct_helper:all(?MODULE) -- [ + parse_host, set_env_dispatch, path_allow_colon, + request_timeout_infinity, idle_timeout_infinity + ], + [ + {http, [], Tests}, %% @todo parallel + {https, [parallel], Tests}, + {http_compress, [parallel], Tests}, + {https_compress, [parallel], Tests}, + {parse_host, [], [ + parse_host + ]}, + {request_timeout_infinity, [], [ + request_timeout_infinity + ]}, + {idle_timeout_infinity, [], [ + idle_timeout_infinity + ]}, + {set_env, [], [ + set_env_dispatch + ]}, + {router_compile, [], [ + path_allow_colon + ]} + ]. + +init_per_group(Name = http, Config) -> + cowboy_test:init_http(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config); +init_per_group(Name = https, Config) -> + cowboy_test:init_https(Name, #{env => #{dispatch => init_dispatch(Config)}}, Config); +init_per_group(Name = http_compress, Config) -> + cowboy_test:init_http(Name, #{ + env => #{dispatch => init_dispatch(Config)}, + compress => true + }, Config); +init_per_group(Name = https_compress, Config) -> + cowboy_test:init_https(Name, #{ + env => #{dispatch => init_dispatch(Config)}, + compress => true + }, Config); +init_per_group(parse_host, Config) -> + Dispatch = cowboy_router:compile([ + {'_', [ + {"/req_attr", http_req_attr, []} + ]} + ]), + {ok, _} = cowboy:start_clear(parse_host, [{port, 0}], #{ + env => #{dispatch => Dispatch} + }), + Port = ranch:get_port(parse_host), + [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; +init_per_group(request_timeout_infinity, Config) -> + Ref = request_timeout_infinity, + {ok, Pid} = cowboy:start_clear(Ref, [{port, 0}], #{ + env => #{dispatch => init_dispatch(Config)}, + request_timeout => infinity + }), + Port = ranch:get_port(request_timeout_infinity), + [{pid, Pid}, {ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; +init_per_group(idle_timeout_infinity, Config) -> + Ref = idle_timeout_infinity, + {ok, Pid} = cowboy:start_clear(Ref, [{port, 0}], #{ + env => #{dispatch => init_dispatch(Config)}, + idle_timeout => infinity + }), + Port = ranch:get_port(idle_timeout_infinity), + [{pid, Pid}, {ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; +init_per_group(set_env, Config) -> + {ok, _} = cowboy:start_clear(set_env, [{port, 0}], #{ + env => #{dispatch => []} + }), + Port = ranch:get_port(set_env), + [{type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config]; +init_per_group(router_compile, Config) -> + Config. + +end_per_group(router_compile, _) -> + ok; +end_per_group(Name, _) -> + ok = cowboy:stop_listener(Name). + +%% Dispatch configuration. + +init_dispatch(_) -> + cowboy_router:compile([ + {"localhost", [ + {"/chunked_response", http_chunked, []}, + {"/headers/dupe", http_handler, + [{headers, #{<<"connection">> => <<"close">>}}]}, + {"/set_resp/header", http_set_resp, + [{headers, #{<<"vary">> => <<"Accept">>}}]}, + {"/set_resp/overwrite", http_set_resp, + [{headers, #{<<"server">> => <<"DesireDrive/1.0">>}}]}, + {"/set_resp/body", http_set_resp, + [{body, <<"A flameless dance does not equal a cycle">>}]}, + {"/handler_errors", http_errors, []}, + {"/echo/body", http_echo_body, []}, + {"/param_all", rest_param_all, []}, + {"/bad_accept", rest_simple_resource, []}, + {"/bad_content_type", rest_patch_resource, []}, + {"/simple", rest_simple_resource, []}, + {"/forbidden_post", rest_forbidden_resource, [true]}, + {"/simple_post", rest_forbidden_resource, [false]}, + {"/missing_get_callbacks", rest_missing_callbacks, []}, + {"/missing_put_callbacks", rest_missing_callbacks, []}, + {"/nodelete", rest_nodelete_resource, []}, + {"/post_charset", rest_post_charset_resource, []}, + {"/postonly", rest_postonly_resource, []}, + {"/patch", rest_patch_resource, []}, + {"/resetags", rest_resource_etags, []}, + {"/rest_expires", rest_expires, []}, + {"/rest_expires_binary", rest_expires_binary, []}, + {"/rest_empty_resource", rest_empty_resource, []}, + {"/loop_stream_recv", http_loop_stream_recv, []}, + {"/", http_handler, []} + ]} + ]). + +%% Convenience functions. + +do_raw(Data, Config) -> + Client = raw_open(Config), + ok = raw_send(Client, Data), + case catch raw_recv_head(Client) of + {'EXIT', _} -> closed; + Resp -> element(2, cow_http:parse_status_line(Resp)) + end. + +do_get(Path, Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, Path), + {response, _, Status, _} = gun:await(ConnPid, Ref), + gun:close(ConnPid), + Status. + +%% Tests. + +check_raw_status(Config) -> + Huge = [$0 || _ <- lists:seq(1, 5000)], + HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77=" + "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]), + ResponsePacket = +"HTTP/1.0 302 Found\r +Location: http://www.google.co.il/\r +Cache-Control: private\r +Content-Type: text/html; charset=UTF-8\r +Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r +Date: Sun, 04 Dec 2011 15:55:01 GMT\r +Server: gws\r +Content-Length: 221\r +X-XSS-Protection: 1; mode=block\r +X-Frame-Options: SAMEORIGIN\r +\r + +302 Moved +

302 Moved

+The document has moved +here. +", + Tests = [ + {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n" + "Set-Cookie: ", HugeCookie, "\r\n\r\n"]}, + {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {400, "\n"}, + {400, "Garbage\r\n\r\n"}, + {400, "\r\n\r\n\r\n\r\n\r\n\r\n"}, + {400, " / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {400, "GET HTTP/1.1\r\nHost: localhost\r\n\r\n"}, + {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"}, + {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"}, + {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"}, + {400, ResponsePacket}, + {408, "GET / HTTP/1.1\r\n"}, + {408, "GET / HTTP/1.1\r\nHost: localhost"}, + {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"}, + {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"}, + {closed, Huge}, + {431, "GET / HTTP/1.1\r\n" ++ Huge}, + {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"}, + {closed, ""}, + {closed, "\r\n"}, + {closed, "\r\n\r\n"}, + {closed, "GET / HTTP/1.1"} + ], + _ = [{Status, Packet} = begin + Ret = do_raw(Packet, Config), + {Ret, Packet} + end || {Status, Packet} <- Tests], + ok. + +check_status(Config) -> + Tests = [ + {200, "/"}, + {200, "/simple"}, + {404, "/not/found"}, + {500, "/handler_errors?case=init_before_reply"} + ], + _ = [{Status, URL} = begin + Ret = do_get(URL, Config), + {Ret, URL} + end || {Status, URL} <- Tests]. + +%% Check if sending requests whose size is around the MTU breaks something. +echo_body(Config) -> + MTU = ct_helper:get_loopback_mtu(), + _ = [begin + Body = list_to_binary(lists:duplicate(Size, $a)), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref) + end || Size <- lists:seq(MTU - 500, MTU)], + ok. + +%% Check if sending request whose size is bigger than 1000000 bytes causes 413 +echo_body_max_length(Config) -> + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], << 0:10000000/unit:8 >>), + {response, nofin, 413, _} = gun:await(ConnPid, Ref), + ok. + +error_init_after_reply(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + ok. + +headers_dupe(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/headers/dupe"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + %% Ensure that only one connection header was received. + [<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>], + gun_down(ConnPid). + +http10_chunkless(Config) -> + ConnPid = gun_open(Config, #{http_opts => #{version => 'HTTP/1.0'}}), + Ref = gun:get(ConnPid, "/chunked_response"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + false = lists:keyfind(<<"transfer-encoding">>, 1, Headers), + {ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref), + gun_down(ConnPid). + +http10_hostless(Config) -> + Name = http10_hostless, + Port10 = config(port, Config) + 10, + {Transport, Protocol} = case config(type, Config) of + tcp -> {ranch_tcp, cowboy_clear}; + ssl -> {ranch_ssl, cowboy_tls} + end, + ranch:start_listener(Name, 5, Transport, + config(opts, Config) ++ [{port, Port10}], + Protocol, #{ + env =>#{dispatch => cowboy_router:compile([ + {'_', [{"/http1.0/hostless", http_handler, []}]}])}, + max_keepalive => 50, + timeout => 500 + }), + 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", + [{port, Port10}|Config]), + cowboy:stop_listener(http10_hostless). + +http10_keepalive_default(Config) -> + Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n", + Client = raw_open(Config), + ok = raw_send(Client, Normal), + case catch raw_recv_head(Client) of + {'EXIT', _} -> error(closed); + Data -> + %% Cowboy always advertises itself as HTTP/1.1. + {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), + {Headers, _} = cow_http:parse_headers(Rest), + {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers) + end, + ok = raw_send(Client, Normal), + case catch raw_recv_head(Client) of + {'EXIT', _} -> closed; + _ -> error(not_closed) + end. + +http10_keepalive_forced(Config) -> + Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n", + Client = raw_open(Config), + ok = raw_send(Client, Keepalive), + case catch raw_recv_head(Client) of + {'EXIT', _} -> error(closed); + Data -> + %% Cowboy always advertises itself as HTTP/1.1. + {'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data), + {Headers, _} = cow_http:parse_headers(Rest), + {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers) + end, + ok = raw_send(Client, Keepalive), + case catch raw_recv_head(Client) of + {'EXIT', Err} -> error({closed, Err}); + _ -> ok + end. + +keepalive_nl(Config) -> + ConnPid = gun_open(Config), + Refs = [begin + Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]), + gun:dbg_send_raw(ConnPid, <<"\r\n">>), + Ref + end || _ <- lists:seq(1, 10)], + _ = [begin + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + false = lists:keymember(<<"connection">>, 1, Headers) + end || Ref <- Refs], + ok. + +keepalive_stream_loop(Config) -> + ConnPid = gun_open(Config), + Refs = [begin + Ref = gun:post(ConnPid, "/loop_stream_recv", + [{<<"content-type">>, <<"application/octet-stream">>}]), + _ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>) + || ID <- lists:seq(1, 250)], + gun:data(ConnPid, Ref, fin, <<>>), + Ref + end || _ <- lists:seq(1, 10)], + _ = [begin + {response, fin, 200, _} = gun:await(ConnPid, Ref) + end || Ref <- Refs], + ok. + +do_nc(Config, Input) -> + Cat = os:find_executable("cat"), + Nc = os:find_executable("nc"), + case {Cat, Nc} of + {false, _} -> + {skip, {notfound, cat}}; + {_, false} -> + {skip, {notfound, nc}}; + _Good -> + %% Throw garbage at the server then check if it's still up. + StrPort = integer_to_list(config(port, Config)), + [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) + || _ <- lists:seq(1, 100)], + 200 = do_get("/", Config) + end. + +nc_rand(Config) -> + do_nc(Config, "/dev/urandom"). + +nc_zero(Config) -> + do_nc(Config, "/dev/zero"). + +parse_host(Config) -> + ConnPid = gun_open(Config), + Tests = [ + {<<"example.org:8080">>, <<"example.org\n8080">>}, + {<<"example.org">>, <<"example.org\n80">>}, + {<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>}, + {<<"192.0.2.1">>, <<"192.0.2.1\n80">>}, + {<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>}, + {<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>}, + {<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>}, + {<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>} + ], + [begin + Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port", + [{<<"host">>, Host}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref) + end || {Host, Body} <- Tests], + ok. + +rest_param_all(Config) -> + ConnPid = gun_open(Config), + %% Accept without param. + Ref1 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref1), + {ok, <<"[]">>} = gun:await_body(ConnPid, Ref1), + %% Accept with param. + Ref2 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref2), + {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2), + %% Accept with param and quality. + Ref3 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref3), + {ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3), + Ref4 = gun:get(ConnPid, "/param_all", + [{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref4), + {ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4), + %% Without Accept. + Ref5 = gun:get(ConnPid, "/param_all"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref5), + {ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5), + %% Content-Type without param. + Ref6 = gun:put(ConnPid, "/param_all", + [{<<"content-type">>, <<"text/plain">>}]), + gun:data(ConnPid, Ref6, fin, "Hello world!"), + {response, fin, 204, _} = gun:await(ConnPid, Ref6), + %% Content-Type with param. + Ref7 = gun:put(ConnPid, "/param_all", + [{<<"content-type">>, <<"text/plain; charset=utf-8">>}]), + gun:data(ConnPid, Ref7, fin, "Hello world!"), + {response, fin, 204, _} = gun:await(ConnPid, Ref7), + ok. + +rest_bad_accept(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/bad_accept", + [{<<"accept">>, <<"1">>}]), + {response, fin, 400, _} = gun:await(ConnPid, Ref), + ok. + +rest_bad_content_type(Config) -> + ConnPid = gun_open(Config), + Ref = gun:patch(ConnPid, "/bad_content_type", + [{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>), + {response, fin, 415, _} = gun:await(ConnPid, Ref), + ok. + +rest_expires(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/rest_expires"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, Expires} = lists:keyfind(<<"expires">>, 1, Headers), + {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers), + Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>, + ok. + +rest_expires_binary(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/rest_expires_binary"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"0">>} = lists:keyfind(<<"expires">>, 1, Headers), + ok. + +rest_last_modified_undefined(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/simple", + [{<<"if-modified-since">>, <<"Fri, 21 Sep 2012 22:36:14 GMT">>}]), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + ok. + +rest_keepalive(Config) -> + ConnPid = gun_open(Config), + Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)], + _ = [begin + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + false = lists:keymember(<<"connection">>, 1, Headers) + end || Ref <- Refs], + ok. + +rest_keepalive_post(Config) -> + ConnPid = gun_open(Config), + Refs = [begin + Ref1 = gun:post(ConnPid, "/forbidden_post", [ + {<<"content-type">>, <<"text/plain">>}, + {<<"content-length">>, <<"12">>} + ]), + gun:data(ConnPid, Ref1, fin, "Hello world!"), + Ref2 = gun:post(ConnPid, "/simple_post", [ + {<<"content-type">>, <<"text/plain">>}, + {<<"content-length">>, <<"12">>} + ]), + gun:data(ConnPid, Ref2, fin, "Hello world!"), + {Ref1, Ref2} + end || _ <- lists:seq(1, 5)], + _ = [begin + {response, fin, 403, Headers1} = gun:await(ConnPid, Ref1), + false = lists:keymember(<<"connection">>, 1, Headers1), + {response, fin, 303, Headers2} = gun:await(ConnPid, Ref2), + false = lists:keymember(<<"connection">>, 1, Headers2) + end || {Ref1, Ref2} <- Refs], + ok. + +rest_missing_get_callbacks(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/missing_get_callbacks"), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. + +rest_missing_put_callbacks(Config) -> + ConnPid = gun_open(Config), + Ref = gun:put(ConnPid, "/missing_put_callbacks", + [{<<"content-type">>, <<"application/json">>}], <<"{}">>), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. + +rest_nodelete(Config) -> + ConnPid = gun_open(Config), + Ref = gun:delete(ConnPid, "/nodelete"), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. + +rest_options_default(Config) -> + ConnPid = gun_open(Config), + Ref = gun:options(ConnPid, "/rest_empty_resource"), + {response, fin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers), + ok. + +rest_patch(Config) -> + Tests = [ + {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>}, + {400, [{<<"content-type">>, <<"text/plain">>}], <<"false">>}, + {400, [{<<"content-type">>, <<"text/plain">>}], <<"stop">>}, + {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>} + ], + ConnPid = gun_open(Config), + _ = [begin + Ref = gun:patch(ConnPid, "/patch", Headers, Body), + {response, fin, Status, _} = gun:await(ConnPid, Ref) + end || {Status, Headers, Body} <- Tests], + ok. + +rest_post_charset(Config) -> + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/post_charset", + [{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. + +rest_postonly(Config) -> + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/postonly", + [{<<"content-type">>, <<"text/plain">>}], "12345"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. + +rest_resource_get_etag(Config, Type) -> + rest_resource_get_etag(Config, Type, []). + +rest_resource_get_etag(Config, Type, Headers) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers), + {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref), + case lists:keyfind(<<"etag">>, 1, RespHeaders) of + false -> {Status, false}; + {<<"etag">>, ETag} -> {Status, ETag} + end. + +rest_resource_etags(Config) -> + Tests = [ + {200, <<"W/\"etag-header-value\"">>, "tuple-weak"}, + {200, <<"\"etag-header-value\"">>, "tuple-strong"}, + {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, + {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"}, + {500, false, "binary-strong-unquoted"}, + {500, false, "binary-weak-unquoted"} + ], + _ = [{Status, ETag, Type} = begin + {Ret, RespETag} = rest_resource_get_etag(Config, Type), + {Ret, RespETag, Type} + end || {Status, ETag, Type} <- Tests]. + +rest_resource_etags_if_none_match(Config) -> + Tests = [ + {304, <<"W/\"etag-header-value\"">>, "tuple-weak"}, + {304, <<"\"etag-header-value\"">>, "tuple-strong"}, + {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"}, + {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"} + ], + _ = [{Status, Type} = begin + {Ret, _} = rest_resource_get_etag(Config, Type, + [{<<"if-none-match">>, ETag}]), + {Ret, Type} + end || {Status, ETag, Type} <- Tests]. + +set_env_dispatch(Config) -> + ConnPid1 = gun_open(Config), + Ref1 = gun:get(ConnPid1, "/"), + {response, fin, 400, _} = gun:await(ConnPid1, Ref1), + ok = cowboy:set_env(set_env, dispatch, + cowboy_router:compile([{'_', [{"/", http_handler, []}]}])), + ConnPid2 = gun_open(Config), + Ref2 = gun:get(ConnPid2, "/"), + {response, nofin, 200, _} = gun:await(ConnPid2, Ref2), + ok. + +path_allow_colon(_Config) -> + cowboy_router:compile([{'_', [{"/foo/bar:blah", http_handler, []}]}]), + ok. + +set_resp_body(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/body"), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, <<"A flameless dance does not equal a cycle">>} + = gun:await_body(ConnPid, Ref), + ok. + +set_resp_header(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/header"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers), + {_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers), + ok. + +set_resp_overwrite(Config) -> + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/set_resp/overwrite"), + {response, nofin, 200, Headers} = gun:await(ConnPid, Ref), + {_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers), + ok. + +slowloris(Config) -> + Client = raw_open(Config), + try + [begin + ok = raw_send(Client, [C]), + receive after 250 -> ok end + end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n" + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n" + "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"], + error(failure) + catch error:{badmatch, _} -> + ok + end. + +slowloris2(Config) -> + Client = raw_open(Config), + ok = raw_send(Client, "GET / HTTP/1.1\r\n"), + receive after 300 -> ok end, + ok = raw_send(Client, "Host: localhost\r\n"), + receive after 300 -> ok end, + Data = raw_recv_head(Client), + {_, 408, _, _} = cow_http:parse_status_line(Data), + ok. + +te_chunked(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [{<<"content-type">>, <<"text/plain">>}]), + gun:data(ConnPid, Ref, fin, Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +do_body_to_chunks(_, <<>>, Acc) -> + lists:reverse([<<"0\r\n\r\n">>|Acc]); +do_body_to_chunks(ChunkSize, Body, Acc) -> + BodySize = byte_size(Body), + ChunkSize2 = case BodySize < ChunkSize of + true -> BodySize; + false -> ChunkSize + end, + << Chunk:ChunkSize2/binary, Rest/binary >> = Body, + ChunkSizeBin = integer_to_binary(ChunkSize2, 16), + do_body_to_chunks(ChunkSize, Rest, + [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). + +te_chunked_chopped(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"content-type">>, <<"text/plain">>}]), + _ = [begin + ok = gun:dbg_send_raw(ConnPid, << C >>), + receive after 10 -> ok end + end || << C >> <= Body2], + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +te_chunked_delayed(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"content-type">>, <<"text/plain">>}]), + _ = [begin + ok = gun:dbg_send_raw(ConnPid, Chunk), + receive after 10 -> ok end + end || Chunk <- Chunks], + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +te_chunked_split_body(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"content-type">>, <<"text/plain">>}]), + _ = [begin + case Chunk of + <<"0\r\n\r\n">> -> + ok = gun:dbg_send_raw(ConnPid, Chunk); + _ -> + [Size, ChunkBody, <<>>] = + binary:split(Chunk, [<<"\r\n">>], [global]), + PartASize = random:uniform(byte_size(ChunkBody)), + <> = ChunkBody, + ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]), + receive after 10 -> ok end, + ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>]) + end + end || Chunk <- Chunks], + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +te_chunked_split_crlf(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + Chunks = do_body_to_chunks(50, Body, []), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", + [{<<"content-type">>, <<"text/plain">>}]), + _ = [begin + %% Split in the newline just before the end of the chunk. + Len = byte_size(Chunk) - (random:uniform(2) - 1), + << Chunk2:Len/binary, End/binary >> = Chunk, + ok = gun:dbg_send_raw(ConnPid, Chunk2), + receive after 10 -> ok end, + ok = gun:dbg_send_raw(ConnPid, End) + end || Chunk <- Chunks], + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +te_identity(Config) -> + Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/echo/body", [], Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, Body} = gun:await_body(ConnPid, Ref), + ok. + +request_timeout_infinity(Config) -> + Pid = config(pid, Config), + Ref = erlang:monitor(process, Pid), + _ = gun_open(Config), + receive + {'DOWN', Ref, process, Pid, Reason} -> + error(Reason) + after 1000 -> + ok + end. + +idle_timeout_infinity(Config) -> + Pid = config(pid, Config), + Ref = erlang:monitor(process, Pid), + ConnPid = gun_open(Config), + gun:post(ConnPid, "/echo/body", [], <<"TEST">>), + receive + {'DOWN', Ref, process, Pid, Reason} -> + error(Reason) + after 1000 -> + ok + end. 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}. diff --git a/test/sys_SUITE.erl b/test/sys_SUITE.erl index bb77ede..b692521 100644 --- a/test/sys_SUITE.erl +++ b/test/sys_SUITE.erl @@ -108,10 +108,6 @@ proc_lib_initial_call_tls(Config) -> %% the receiving process module. When a system message is received, %% function handle_system_msg/6 is called to handle the request. -%% @todo The flush/0 function in cowboy_http needs to be fixed -%% so that it doesn't eat up system messages. It should only -%% flush messages that are specific to cowboy_http. - bad_system_from_h1(Config) -> doc("h1: Sending a system message with a bad From value results in a process crash."), {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]), -- cgit v1.2.3