diff options
Diffstat (limited to 'test/http_SUITE.erl')
-rw-r--r-- | test/http_SUITE.erl | 118 |
1 files changed, 90 insertions, 28 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 4a26612..095d6b5 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -29,6 +29,7 @@ -export([check_raw_status/1]). -export([check_status/1]). -export([chunked_response/1]). +-export([echo_body/1]). -export([error_chain_handle_after_reply/1]). -export([error_chain_handle_before_reply/1]). -export([error_handle_after_reply/1]). @@ -44,6 +45,8 @@ -export([nc_zero/1]). -export([onrequest/1]). -export([onrequest_reply/1]). +-export([onresponse_crash/1]). +-export([onresponse_reply/1]). -export([pipeline/1]). -export([rest_keepalive/1]). -export([rest_keepalive_post/1]). @@ -56,10 +59,10 @@ -export([static_attribute_etag/1]). -export([static_function_etag/1]). -export([static_mimetypes_function/1]). --export([static_test_file/1]). --export([static_test_file_css/1]). -export([static_specify_file/1]). -export([static_specify_file_catchall/1]). +-export([static_test_file/1]). +-export([static_test_file_css/1]). -export([stream_body_set_resp/1]). -export([te_chunked/1]). -export([te_chunked_delayed/1]). @@ -68,13 +71,14 @@ %% ct. all() -> - [{group, http}, {group, https}, {group, hooks}]. + [{group, http}, {group, https}, {group, onrequest}, {group, onresponse}]. groups() -> Tests = [ check_raw_status, check_status, chunked_response, + echo_body, error_chain_handle_after_reply, error_chain_handle_before_reply, error_handle_after_reply, @@ -100,10 +104,10 @@ groups() -> static_attribute_etag, static_function_etag, static_mimetypes_function, - static_test_file, - static_test_file_css, static_specify_file, static_specify_file_catchall, + static_test_file, + static_test_file_css, stream_body_set_resp, te_chunked, te_chunked_delayed, @@ -112,9 +116,13 @@ groups() -> [ {http, [], Tests}, {https, [], Tests}, - {hooks, [], [ + {onrequest, [], [ onrequest, onrequest_reply + ]}, + {onresponse, [], [ + onresponse_crash, + onresponse_reply ]} ]. @@ -132,7 +140,7 @@ init_per_group(http, Config) -> Port = 33080, Transport = cowboy_tcp_transport, Config1 = init_static_dir(Config), - cowboy:start_listener(http, 100, + {ok, _} = cowboy:start_listener(http, 100, Transport, [{port, Port}], cowboy_http_protocol, [ {dispatch, init_dispatch(Config1)}, @@ -154,7 +162,7 @@ init_per_group(https, Config) -> application:start(crypto), application:start(public_key), application:start(ssl), - {ok,_} = cowboy:start_listener(https, 100, + {ok, _} = cowboy:start_listener(https, 100, Transport, Opts ++ [{port, Port}], cowboy_http_protocol, [ {dispatch, init_dispatch(Config1)}, @@ -164,10 +172,10 @@ init_per_group(https, Config) -> {ok, Client} = cowboy_client:init(Opts), [{scheme, <<"https">>}, {port, Port}, {opts, Opts}, {transport, Transport}, {client, Client}|Config1]; -init_per_group(hooks, Config) -> +init_per_group(onrequest, Config) -> Port = 33082, Transport = cowboy_tcp_transport, - {ok, _} = cowboy:start_listener(hooks, 100, + {ok, _} = cowboy:start_listener(onrequest, 100, Transport, [{port, Port}], cowboy_http_protocol, [ {dispatch, init_dispatch(Config)}, @@ -177,6 +185,20 @@ init_per_group(hooks, Config) -> ]), {ok, Client} = cowboy_client:init([]), [{scheme, <<"http">>}, {port, Port}, {opts, []}, + {transport, Transport}, {client, Client}|Config]; +init_per_group(onresponse, Config) -> + Port = 33083, + Transport = cowboy_tcp_transport, + {ok, _} = cowboy:start_listener(onresponse, 100, + Transport, [{port, Port}], + cowboy_http_protocol, [ + {dispatch, init_dispatch(Config)}, + {max_keepalive, 50}, + {onresponse, fun onresponse_hook/3}, + {timeout, 500} + ]), + {ok, Client} = cowboy_client:init([]), + [{scheme, <<"http">>}, {port, Port}, {opts, []}, {transport, Transport}, {client, Client}|Config]. end_per_group(https, Config) -> @@ -189,8 +211,8 @@ end_per_group(https, Config) -> end_per_group(http, Config) -> cowboy:stop_listener(http), end_static_dir(Config); -end_per_group(hooks, _) -> - cowboy:stop_listener(hooks), +end_per_group(Name, _) -> + cowboy:stop_listener(Name), ok. %% Dispatch configuration. @@ -236,6 +258,7 @@ init_dispatch(Config) -> {[<<"simple_post">>], rest_forbidden_resource, [false]}, {[<<"nodelete">>], rest_nodelete_resource, []}, {[<<"resetags">>], rest_resource_etags, []}, + {[<<"loop_timeout">>], http_handler_loop_timeout, []}, {[], http_handler, []} ]} ]. @@ -360,6 +383,7 @@ check_status(Config) -> {102, "/long_polling"}, {200, "/"}, {200, "/simple"}, + {204, "/loop_timeout"}, {400, "/static/%2f"}, {400, "/static/%2e"}, {400, "/static/%2e%2e"}, @@ -382,6 +406,20 @@ chunked_response(Config) -> {ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}} = httpc:request(binary_to_list(build_url("/chunked_response", Config))). +%% Check if sending requests whose size is around the MTU breaks something. +echo_body(Config) -> + Client = ?config(client, Config), + {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]), + [begin + Body = list_to_binary(lists:duplicate(Size, $a)), + {ok, Client2} = cowboy_client:request(<<"POST">>, + build_url("/echo/body", Config), + [{<<"connection">>, <<"close">>}], + Body, Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Body, _} = cowboy_client:response_body(Client3) + end || Size <- lists:seq(MTU - 500, MTU)]. + error_chain_handle_after_reply(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, @@ -576,6 +614,28 @@ onrequest_hook(Req) -> Req3 end. +onresponse_crash(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/handler_errors?case=init_before_reply", Config), Client), + {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers). + +onresponse_reply(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/", Config), Client), + {ok, 777, Headers, Client3} = cowboy_client:response(Client2), + {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers), + %% Make sure we don't get the body initially sent. + {error, closed} = cowboy_client:response_body(Client3). + +%% Hook for the above onresponse tests. +onresponse_hook(_, Headers, Req) -> + {ok, Req2} = cowboy_http_req:reply( + <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), + Req2. + pipeline(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, @@ -753,6 +813,24 @@ static_mimetypes_function(Config) -> {<<"content-type">>, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers). +static_specify_file(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_specify_file", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/css">>} + = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). + +static_specify_file_catchall(Config) -> + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/static_specify_file/none", Config), Client), + {ok, 200, Headers, Client3} = cowboy_client:response(Client2), + {<<"content-type">>, <<"text/css">>} + = lists:keyfind(<<"content-type">>, 1, Headers), + {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). + static_test_file(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, @@ -769,22 +847,6 @@ static_test_file_css(Config) -> {<<"content-type">>, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers). -static_specify_file(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_specify_file", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), - {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), - {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). - -static_specify_file_catchall(Config) -> - Client = ?config(client, Config), - {ok, Client2} = cowboy_client:request(<<"GET">>, - build_url("/static_specify_file/none", Config), Client), - {ok, 200, Headers, Client3} = cowboy_client:response(Client2), - {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers), - {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3). - stream_body_set_resp(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, |