2017-01-02 19:36:36 +01:00
|
|
|
%% Copyright (c) 2016-2017, Loïc Hoguin <essen@ninenines.eu>
|
2016-06-20 17:31:31 +02:00
|
|
|
%%
|
|
|
|
%% 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(req_SUITE).
|
|
|
|
-compile(export_all).
|
2017-11-29 16:57:10 +01:00
|
|
|
-compile(nowarn_export_all).
|
2016-06-20 17:31:31 +02:00
|
|
|
|
|
|
|
-import(ct_helper, [config/2]).
|
|
|
|
-import(ct_helper, [doc/1]).
|
|
|
|
-import(cowboy_test, [gun_open/1]).
|
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
2020-04-01 21:11:59 +02:00
|
|
|
suite() ->
|
2020-04-06 12:27:41 +02:00
|
|
|
Timeout = case os:type() of
|
2020-04-08 10:28:27 +02:00
|
|
|
{win32, _} -> 120000;
|
2020-04-06 12:27:41 +02:00
|
|
|
_ -> 30000
|
|
|
|
end,
|
|
|
|
[{timetrap, Timeout}].
|
2020-04-01 21:11:59 +02:00
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
all() ->
|
|
|
|
cowboy_test:common_all().
|
|
|
|
|
|
|
|
groups() ->
|
2017-01-22 10:50:39 +01:00
|
|
|
cowboy_test:common_groups(ct_helper:all(?MODULE)).
|
2016-06-20 17:31:31 +02:00
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
init_per_suite(Config) ->
|
|
|
|
ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
|
|
|
|
Config.
|
|
|
|
|
|
|
|
end_per_suite(Config) ->
|
|
|
|
ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
|
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
init_per_group(Name, Config) ->
|
|
|
|
cowboy_test:init_common_groups(Name, Config, ?MODULE).
|
|
|
|
|
|
|
|
end_per_group(Name, _) ->
|
|
|
|
cowboy:stop_listener(Name).
|
|
|
|
|
|
|
|
%% Routes.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
init_dispatch(Config) ->
|
2016-06-20 17:31:31 +02:00
|
|
|
cowboy_router:compile([{"[...]", [
|
2016-08-10 11:49:31 +02:00
|
|
|
{"/static/[...]", cowboy_static, {dir, config(priv_dir, Config) ++ "/static"}},
|
|
|
|
%% @todo Seriously InitialState should be optional.
|
|
|
|
{"/resp/:key[/:arg]", resp_h, []},
|
|
|
|
{"/multipart[/:key]", multipart_h, []},
|
2016-06-20 17:31:31 +02:00
|
|
|
{"/args/:key/:arg[/:default]", echo_h, []},
|
2024-01-17 17:16:38 +01:00
|
|
|
{"/crash/:key/period", echo_h,
|
|
|
|
#{length => 999999999, period => 1000, timeout => 5000, crash => true}},
|
2016-08-10 11:49:31 +02:00
|
|
|
{"/no-opts/:key", echo_h, #{crash => true}},
|
|
|
|
{"/opts/:key/length", echo_h, #{length => 1000}},
|
2019-09-15 16:30:09 +02:00
|
|
|
{"/opts/:key/period", echo_h, #{length => 999999999, period => 2000}},
|
2016-08-10 11:49:31 +02:00
|
|
|
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
|
2017-10-30 16:21:25 +00:00
|
|
|
{"/100-continue/:key", echo_h, []},
|
2016-08-10 11:49:31 +02:00
|
|
|
{"/full/:key", echo_h, []},
|
2024-01-08 15:13:18 +01:00
|
|
|
{"/auto-sync/:key", echo_h, []},
|
|
|
|
{"/auto-async/:key", echo_h, []},
|
2019-10-02 19:12:05 +02:00
|
|
|
{"/spawn/:key", echo_h, []},
|
2016-08-10 11:49:31 +02:00
|
|
|
{"/no/:key", echo_h, []},
|
2017-09-05 15:28:11 +02:00
|
|
|
{"/direct/:key/[...]", echo_h, []},
|
2016-06-20 17:31:31 +02:00
|
|
|
{"/:key/[...]", echo_h, []}
|
|
|
|
]}]).
|
|
|
|
|
|
|
|
%% Internal.
|
|
|
|
|
|
|
|
do_body(Method, Path, Config) ->
|
|
|
|
do_body(Method, Path, [], Config).
|
|
|
|
|
|
|
|
do_body(Method, Path, Headers, Config) ->
|
2016-08-10 11:49:31 +02:00
|
|
|
do_body(Method, Path, Headers, <<>>, Config).
|
|
|
|
|
2017-01-22 10:50:39 +01:00
|
|
|
do_body(Method, Path, Headers0, Body, Config) ->
|
2016-06-20 17:31:31 +02:00
|
|
|
ConnPid = gun_open(Config),
|
2017-01-22 10:50:39 +01:00
|
|
|
Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:request(ConnPid, Method, Path, Headers, Body),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{ok, RespBody} = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref, infinity);
|
2016-06-20 17:31:31 +02:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid),
|
2017-01-22 10:50:39 +01:00
|
|
|
do_decode(RespHeaders, RespBody).
|
2016-08-10 11:49:31 +02:00
|
|
|
|
2017-09-14 13:42:17 +02:00
|
|
|
do_body_error(Method, Path, Headers0, Body, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:request(ConnPid, Method, Path, Headers, Body),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, _, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2017-09-14 13:42:17 +02:00
|
|
|
gun:close(ConnPid),
|
|
|
|
{Status, RespHeaders}.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
do_get(Path, Config) ->
|
2017-09-14 13:42:17 +02:00
|
|
|
do_get(Path, [], Config).
|
|
|
|
|
|
|
|
do_get(Path, Headers, Config) ->
|
2016-08-10 11:49:31 +02:00
|
|
|
ConnPid = gun_open(Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{ok, RespBody} = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref, infinity);
|
2016-08-10 11:49:31 +02:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid),
|
2017-01-22 10:50:39 +01:00
|
|
|
{Status, RespHeaders, do_decode(RespHeaders, RespBody)}.
|
2016-06-20 17:31:31 +02:00
|
|
|
|
|
|
|
do_get_body(Path, Config) ->
|
|
|
|
do_get_body(Path, [], Config).
|
|
|
|
|
|
|
|
do_get_body(Path, Headers, Config) ->
|
|
|
|
do_body("GET", Path, Headers, Config).
|
|
|
|
|
2017-10-29 19:52:27 +00:00
|
|
|
do_get_inform(Path, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
|
2020-04-02 14:57:10 +02:00
|
|
|
case gun:await(ConnPid, Ref, infinity) of
|
2017-10-29 19:52:27 +00:00
|
|
|
{response, _, RespStatus, RespHeaders} ->
|
|
|
|
%% We don't care about the body.
|
|
|
|
gun:close(ConnPid),
|
|
|
|
{RespStatus, RespHeaders};
|
|
|
|
{inform, InfoStatus, InfoHeaders} ->
|
|
|
|
{response, IsFin, RespStatus, RespHeaders}
|
2020-04-02 14:57:10 +02:00
|
|
|
= case gun:await(ConnPid, Ref, infinity) of
|
2017-10-29 19:52:27 +00:00
|
|
|
{inform, InfoStatus, InfoHeaders} ->
|
2020-04-02 14:57:10 +02:00
|
|
|
gun:await(ConnPid, Ref, infinity);
|
2017-10-29 19:52:27 +00:00
|
|
|
Response ->
|
|
|
|
Response
|
|
|
|
end,
|
|
|
|
{ok, RespBody} = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref, infinity);
|
2017-10-29 19:52:27 +00:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid),
|
|
|
|
{InfoStatus, InfoHeaders, RespStatus, RespHeaders, do_decode(RespHeaders, RespBody)}
|
|
|
|
end.
|
|
|
|
|
2017-01-22 10:50:39 +01:00
|
|
|
do_decode(Headers, Body) ->
|
|
|
|
case lists:keyfind(<<"content-encoding">>, 1, Headers) of
|
|
|
|
{_, <<"gzip">>} -> zlib:gunzip(Body);
|
|
|
|
_ -> Body
|
|
|
|
end.
|
|
|
|
|
2018-05-16 13:28:49 +02:00
|
|
|
do_get_error(Path, Config) ->
|
|
|
|
do_get_error(Path, [], Config).
|
|
|
|
|
|
|
|
do_get_error(Path, Headers, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2018-05-16 13:28:49 +02:00
|
|
|
Result = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref, infinity);
|
2018-05-16 13:28:49 +02:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
case Result of
|
|
|
|
{ok, RespBody} -> {Status, RespHeaders, do_decode(RespHeaders, RespBody)};
|
|
|
|
_ -> Result
|
|
|
|
end.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Tests: Request.
|
2016-06-20 17:31:31 +02:00
|
|
|
|
2016-06-21 19:04:52 +02:00
|
|
|
binding(Config) ->
|
|
|
|
doc("Value bound from request URI path with/without default."),
|
|
|
|
<<"binding">> = do_get_body("/args/binding/key", Config),
|
|
|
|
<<"binding">> = do_get_body("/args/binding/key/default", Config),
|
|
|
|
<<"default">> = do_get_body("/args/binding/undefined/default", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
bindings(Config) ->
|
|
|
|
doc("Values bound from request URI path."),
|
2017-02-19 16:51:16 +01:00
|
|
|
<<"#{key => <<\"bindings\">>}">> = do_get_body("/bindings", Config),
|
2016-06-21 19:04:52 +02:00
|
|
|
ok.
|
|
|
|
|
2017-10-25 20:17:21 +01:00
|
|
|
cert(Config) ->
|
|
|
|
case config(type, Config) of
|
|
|
|
tcp -> doc("TLS certificates can only be provided over TLS.");
|
|
|
|
ssl -> do_cert(Config)
|
|
|
|
end.
|
|
|
|
|
2023-03-30 15:38:29 +02:00
|
|
|
do_cert(Config) ->
|
2017-10-25 20:17:21 +01:00
|
|
|
doc("A client TLS certificate was provided."),
|
|
|
|
Cert = do_get_body("/cert", Config),
|
|
|
|
Cert = do_get_body("/direct/cert", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
cert_undefined(Config) ->
|
|
|
|
doc("No client TLS certificate was provided."),
|
2023-03-30 15:38:29 +02:00
|
|
|
<<"undefined">> = do_get_body("/cert", [{no_cert, true}|Config]),
|
|
|
|
<<"undefined">> = do_get_body("/direct/cert", [{no_cert, true}|Config]),
|
2017-10-25 20:17:21 +01:00
|
|
|
ok.
|
|
|
|
|
2016-06-21 19:04:52 +02:00
|
|
|
header(Config) ->
|
|
|
|
doc("Request header with/without default."),
|
|
|
|
<<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config),
|
|
|
|
<<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config),
|
|
|
|
<<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
headers(Config) ->
|
|
|
|
doc("Request headers."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_headers("/headers", Config),
|
|
|
|
do_headers("/direct/headers", Config).
|
|
|
|
|
|
|
|
do_headers(Path, Config) ->
|
2017-01-22 10:50:39 +01:00
|
|
|
%% We always send accept-encoding with this test suite's requests.
|
2019-09-06 15:37:42 +02:00
|
|
|
<<"#{<<\"accept-encoding\">> => <<\"gzip\">>,"
|
|
|
|
"<<\"content-length\">> => <<\"0\">>,"
|
|
|
|
"<<\"header\">> => <<\"value\">>", _/bits>>
|
2017-09-05 15:28:11 +02:00
|
|
|
= do_get_body(Path, [{<<"header">>, "value"}], Config),
|
2016-06-21 19:04:52 +02:00
|
|
|
ok.
|
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
host(Config) ->
|
|
|
|
doc("Request URI host."),
|
|
|
|
<<"localhost">> = do_get_body("/host", Config),
|
2017-09-05 15:28:11 +02:00
|
|
|
<<"localhost">> = do_get_body("/direct/host", Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
host_info(Config) ->
|
|
|
|
doc("Request host_info."),
|
|
|
|
<<"[<<\"localhost\">>]">> = do_get_body("/host_info", Config),
|
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% @todo Actually write the related unit tests.
|
2016-06-22 09:27:29 +02:00
|
|
|
match_cookies(Config) ->
|
|
|
|
doc("Matched request cookies."),
|
|
|
|
<<"#{}">> = do_get_body("/match/cookies", [{<<"cookie">>, "a=b; c=d"}], Config),
|
|
|
|
<<"#{a => <<\"b\">>}">> = do_get_body("/match/cookies/a", [{<<"cookie">>, "a=b; c=d"}], Config),
|
|
|
|
<<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
|
2023-03-30 15:39:13 +02:00
|
|
|
case do_get_body("/match/cookies/a/c", [{<<"cookie">>, "a=b; c=d"}], Config) of
|
|
|
|
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok;
|
|
|
|
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok
|
|
|
|
end,
|
2017-09-27 16:20:38 +02:00
|
|
|
%% Ensure match errors result in a 400 response.
|
|
|
|
{400, _, _} = do_get("/match/cookies/a/c",
|
|
|
|
[{<<"cookie">>, "a=b"}], Config),
|
2016-06-22 09:27:29 +02:00
|
|
|
%% This function is tested more extensively through unit tests.
|
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% @todo Actually write the related unit tests.
|
2016-06-22 09:27:29 +02:00
|
|
|
match_qs(Config) ->
|
|
|
|
doc("Matched request URI query string."),
|
|
|
|
<<"#{}">> = do_get_body("/match/qs?a=b&c=d", Config),
|
|
|
|
<<"#{a => <<\"b\">>}">> = do_get_body("/match/qs/a?a=b&c=d", Config),
|
|
|
|
<<"#{c => <<\"d\">>}">> = do_get_body("/match/qs/c?a=b&c=d", Config),
|
2023-03-30 15:39:13 +02:00
|
|
|
case do_get_body("/match/qs/a/c?a=b&c=d", Config) of
|
|
|
|
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok;
|
|
|
|
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok
|
|
|
|
end,
|
|
|
|
case do_get_body("/match/qs/a/c?a=b&c", Config) of
|
|
|
|
<<"#{a => <<\"b\">>,c => true}">> -> ok;
|
|
|
|
<<"#{c => true,a => <<\"b\">>}">> -> ok
|
|
|
|
end,
|
|
|
|
case do_get_body("/match/qs/a/c?a&c=d", Config) of
|
|
|
|
<<"#{a => true,c => <<\"d\">>}">> -> ok;
|
|
|
|
<<"#{c => <<\"d\">>,a => true}">> -> ok
|
|
|
|
end,
|
2017-09-27 16:20:38 +02:00
|
|
|
%% Ensure match errors result in a 400 response.
|
|
|
|
{400, _, _} = do_get("/match/qs/a/c?a=b", [], Config),
|
2016-06-22 09:27:29 +02:00
|
|
|
%% This function is tested more extensively through unit tests.
|
|
|
|
ok.
|
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
method(Config) ->
|
|
|
|
doc("Request method."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_method("/method", Config),
|
|
|
|
do_method("/direct/method", Config).
|
|
|
|
|
|
|
|
do_method(Path, Config) ->
|
2017-09-14 13:42:17 +02:00
|
|
|
<<"GET">> = do_body("GET", Path, Config),
|
|
|
|
<<>> = do_body("HEAD", Path, Config),
|
|
|
|
<<"OPTIONS">> = do_body("OPTIONS", Path, Config),
|
|
|
|
<<"PATCH">> = do_body("PATCH", Path, Config),
|
|
|
|
<<"POST">> = do_body("POST", Path, Config),
|
|
|
|
<<"PUT">> = do_body("PUT", Path, Config),
|
|
|
|
<<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", Path, Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
2016-06-21 19:04:52 +02:00
|
|
|
parse_cookies(Config) ->
|
|
|
|
doc("Request cookies."),
|
|
|
|
<<"[]">> = do_get_body("/parse_cookies", Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
|
|
|
|
= do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
|
|
|
|
= do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
|
|
|
|
= do_get_body("/parse_cookies",
|
|
|
|
[{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _, _} = do_get("/parse_cookies",
|
2020-03-30 17:46:42 +02:00
|
|
|
[{<<"cookie">>, "bad\tname=strawberry"}], Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
{400, _, _} = do_get("/parse_cookies",
|
|
|
|
[{<<"cookie">>, "goodname=strawberry\tmilkshake"}], Config),
|
2016-06-21 19:04:52 +02:00
|
|
|
ok.
|
|
|
|
|
2019-10-05 11:23:57 +02:00
|
|
|
filter_then_parse_cookies(Config) ->
|
|
|
|
doc("Filter cookies then parse them."),
|
|
|
|
<<"[]">> = do_get_body("/filter_then_parse_cookies", Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies",
|
|
|
|
[{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config),
|
|
|
|
<<"[]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies",
|
|
|
|
[{<<"cookie">>, "bad name=strawberry"}], Config),
|
|
|
|
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies",
|
|
|
|
[{<<"cookie">>, "bad name=strawberry; cake=strawberry"}], Config),
|
|
|
|
<<"[]">>
|
|
|
|
= do_get_body("/filter_then_parse_cookies",
|
|
|
|
[{<<"cookie">>, "Blocked by http://www.example.com/upgrade-to-remove"}], Config),
|
|
|
|
ok.
|
|
|
|
|
2016-06-21 19:04:52 +02:00
|
|
|
parse_header(Config) ->
|
|
|
|
doc("Parsed request header with/without default."),
|
|
|
|
<<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
|
|
|
|
= do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config),
|
|
|
|
<<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
|
|
|
|
= do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config),
|
|
|
|
%% Header not in request but with default defined by Cowboy.
|
|
|
|
<<"0">> = do_get_body("/args/parse_header/content-length", Config),
|
|
|
|
%% Header not in request and no default from Cowboy.
|
|
|
|
<<"undefined">> = do_get_body("/args/parse_header/upgrade", Config),
|
|
|
|
%% Header in request and with default provided.
|
|
|
|
<<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _, _} = do_get("/args/parse_header/accept",
|
|
|
|
[{<<"accept">>, "bad media type"}], Config),
|
2016-06-21 19:04:52 +02:00
|
|
|
ok.
|
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
parse_qs(Config) ->
|
|
|
|
doc("Parsed request URI query string."),
|
|
|
|
<<"[]">> = do_get_body("/parse_qs", Config),
|
|
|
|
<<"[{<<\"abc\">>,true}]">> = do_get_body("/parse_qs?abc", Config),
|
|
|
|
<<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> = do_get_body("/parse_qs?a=b&c=d+e", Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _, _} = do_get("/parse_qs?%%%%%%%", Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
path(Config) ->
|
|
|
|
doc("Request URI path."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_path("/path", Config),
|
|
|
|
do_path("/direct/path", Config).
|
|
|
|
|
|
|
|
do_path(Path0, Config) ->
|
|
|
|
Path = list_to_binary(Path0 ++ "/to/the/resource"),
|
|
|
|
Path = do_get_body(Path, Config),
|
|
|
|
Path = do_get_body([Path, "?query"], Config),
|
|
|
|
Path = do_get_body([Path, "?query#fragment"], Config),
|
|
|
|
Path = do_get_body([Path, "#fragment"], Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
path_info(Config) ->
|
|
|
|
doc("Request path_info."),
|
|
|
|
<<"undefined">> = do_get_body("/no/path_info", Config),
|
|
|
|
<<"[]">> = do_get_body("/path_info", Config),
|
|
|
|
<<"[]">> = do_get_body("/path_info/", Config),
|
|
|
|
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource", Config),
|
|
|
|
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query", Config),
|
|
|
|
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query#fragment", Config),
|
|
|
|
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource#fragment", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
peer(Config) ->
|
2017-10-25 20:17:21 +01:00
|
|
|
doc("Remote socket address."),
|
2016-06-20 17:31:31 +02:00
|
|
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
|
2017-09-05 15:28:11 +02:00
|
|
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
port(Config) ->
|
|
|
|
doc("Request URI port."),
|
|
|
|
Port = integer_to_binary(config(port, Config)),
|
|
|
|
Port = do_get_body("/port", Config),
|
2017-09-05 15:28:11 +02:00
|
|
|
Port = do_get_body("/direct/port", Config),
|
2018-05-16 10:42:25 +02:00
|
|
|
ExpectedPort = case config(type, Config) of
|
|
|
|
tcp -> <<"80">>;
|
|
|
|
ssl -> <<"443">>
|
|
|
|
end,
|
|
|
|
ExpectedPort = do_get_body("/port", [{<<"host">>, <<"localhost">>}], Config),
|
|
|
|
ExpectedPort = do_get_body("/direct/port", [{<<"host">>, <<"localhost">>}], Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
qs(Config) ->
|
|
|
|
doc("Request URI query string."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_qs("/qs", Config),
|
|
|
|
do_qs("/direct/qs", Config).
|
|
|
|
|
|
|
|
do_qs(Path, Config) ->
|
|
|
|
<<>> = do_get_body(Path, Config),
|
|
|
|
<<"abc">> = do_get_body(Path ++ "?abc", Config),
|
|
|
|
<<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config),
|
2016-06-20 17:31:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
scheme(Config) ->
|
|
|
|
doc("Request URI scheme."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_scheme("/scheme", Config),
|
|
|
|
do_scheme("/direct/scheme", Config).
|
|
|
|
|
|
|
|
do_scheme(Path, Config) ->
|
2016-06-20 17:31:31 +02:00
|
|
|
Transport = config(type, Config),
|
2017-09-05 15:28:11 +02:00
|
|
|
case do_get_body(Path, Config) of
|
2016-06-20 17:31:31 +02:00
|
|
|
<<"http">> when Transport =:= tcp -> ok;
|
|
|
|
<<"https">> when Transport =:= ssl -> ok
|
|
|
|
end.
|
|
|
|
|
2017-10-25 20:17:21 +01:00
|
|
|
sock(Config) ->
|
|
|
|
doc("Local socket address."),
|
|
|
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/sock", Config),
|
|
|
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/sock", Config),
|
|
|
|
ok.
|
|
|
|
|
2016-06-21 19:04:52 +02:00
|
|
|
uri(Config) ->
|
|
|
|
doc("Request URI building/modification."),
|
|
|
|
Scheme = case config(type, Config) of
|
|
|
|
tcp -> <<"http">>;
|
|
|
|
ssl -> <<"https">>
|
|
|
|
end,
|
|
|
|
SLen = byte_size(Scheme),
|
|
|
|
Port = integer_to_binary(config(port, Config)),
|
|
|
|
PLen = byte_size(Port),
|
|
|
|
%% Absolute form.
|
|
|
|
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >>
|
|
|
|
= do_get_body("/uri?qs", Config),
|
|
|
|
%% Origin form.
|
|
|
|
<< "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config),
|
|
|
|
%% Protocol relative.
|
|
|
|
<< "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >>
|
|
|
|
= do_get_body("/uri/protocol-relative?qs", Config),
|
|
|
|
%% No query string.
|
|
|
|
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >>
|
|
|
|
= do_get_body("/uri/no-qs?qs", Config),
|
|
|
|
%% No path or query string.
|
|
|
|
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary >>
|
|
|
|
= do_get_body("/uri/no-path?qs", Config),
|
|
|
|
%% Changed port.
|
|
|
|
<< Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >>
|
|
|
|
= do_get_body("/uri/set-port?qs", Config),
|
|
|
|
%% This function is tested more extensively through unit tests.
|
|
|
|
ok.
|
|
|
|
|
2016-06-20 17:31:31 +02:00
|
|
|
version(Config) ->
|
|
|
|
doc("Request HTTP version."),
|
2017-09-05 15:28:11 +02:00
|
|
|
do_version("/version", Config),
|
|
|
|
do_version("/direct/version", Config).
|
|
|
|
|
|
|
|
do_version(Path, Config) ->
|
2016-06-20 17:31:31 +02:00
|
|
|
Protocol = config(protocol, Config),
|
2017-09-05 15:28:11 +02:00
|
|
|
case do_get_body(Path, Config) of
|
2016-06-20 17:31:31 +02:00
|
|
|
<<"HTTP/1.1">> when Protocol =:= http -> ok;
|
|
|
|
<<"HTTP/2">> when Protocol =:= http2 -> ok
|
|
|
|
end.
|
2016-08-10 11:49:31 +02:00
|
|
|
|
|
|
|
%% Tests: Request body.
|
|
|
|
|
|
|
|
body_length(Config) ->
|
|
|
|
doc("Request body length."),
|
|
|
|
<<"0">> = do_get_body("/body_length", Config),
|
|
|
|
<<"12">> = do_body("POST", "/body_length", [], "hello world!", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
has_body(Config) ->
|
|
|
|
doc("Has a request body?"),
|
|
|
|
<<"false">> = do_get_body("/has_body", Config),
|
|
|
|
<<"true">> = do_body("POST", "/has_body", [], "hello world!", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
read_body(Config) ->
|
|
|
|
doc("Request body."),
|
|
|
|
<<>> = do_get_body("/read_body", Config),
|
|
|
|
<<"hello world!">> = do_body("POST", "/read_body", [], "hello world!", Config),
|
|
|
|
%% We expect to have read *at least* 1000 bytes.
|
|
|
|
<<0:8000, _/bits>> = do_body("POST", "/opts/read_body/length", [], <<0:8000000>>, Config),
|
|
|
|
%% The timeout value is set too low on purpose to ensure a crash occurs.
|
|
|
|
ok = do_read_body_timeout("/opts/read_body/timeout", <<0:8000000>>, Config),
|
|
|
|
%% 10MB body larger than default length.
|
|
|
|
<<0:80000000>> = do_body("POST", "/full/read_body", [], <<0:80000000>>, Config),
|
|
|
|
ok.
|
|
|
|
|
2018-05-17 16:23:33 +02:00
|
|
|
read_body_mtu(Config) ->
|
2019-09-15 21:34:14 +02:00
|
|
|
case os:type() of
|
|
|
|
{win32, _} ->
|
|
|
|
{skip, "Loopback MTU size is 0xFFFFFFFF on Windows."};
|
|
|
|
{unix, _} ->
|
|
|
|
doc("Request body whose sizes are around the MTU."),
|
|
|
|
MTU = ct_helper:get_loopback_mtu(),
|
|
|
|
_ = [begin
|
|
|
|
Body = <<0:Size/unit:8>>,
|
|
|
|
Body = do_body("POST", "/full/read_body", [], Body, Config)
|
|
|
|
end || Size <- lists:seq(MTU - 10, MTU + 10)],
|
|
|
|
ok
|
|
|
|
end.
|
2018-05-17 16:23:33 +02:00
|
|
|
|
|
|
|
read_body_period(Config) ->
|
2019-09-15 16:30:09 +02:00
|
|
|
doc("Read the request body for at most 2 seconds."),
|
2016-08-10 11:49:31 +02:00
|
|
|
ConnPid = gun_open(Config),
|
2018-05-17 16:23:33 +02:00
|
|
|
Body = <<0:8000000>>,
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:headers(ConnPid, "POST", "/opts/read_body/period", [
|
2016-08-10 11:49:31 +02:00
|
|
|
{<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
|
|
|
|
]),
|
2020-04-02 14:01:26 +02:00
|
|
|
%% The body is sent without fin. The server will read what it can
|
|
|
|
%% for 2 seconds. The test succeeds if we get some of the data back
|
|
|
|
%% (meaning the function will have returned after the period ends).
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:data(ConnPid, Ref, nofin, Body),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
{data, _, Data} = gun:await(ConnPid, Ref, infinity),
|
2020-04-02 14:01:26 +02:00
|
|
|
%% We expect to read at least some data.
|
|
|
|
true = Data =/= <<>>,
|
2018-05-17 16:23:33 +02:00
|
|
|
gun:close(ConnPid).
|
2016-08-10 11:49:31 +02:00
|
|
|
|
|
|
|
%% We expect a crash.
|
|
|
|
do_read_body_timeout(Path, Body, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:headers(ConnPid, "POST", Path, [
|
2016-08-10 11:49:31 +02:00
|
|
|
{<<"content-length">>, integer_to_binary(byte_size(Body))}
|
|
|
|
]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, _, 500, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2024-01-08 15:13:18 +01:00
|
|
|
read_body_auto(Config) ->
|
|
|
|
doc("Read the request body using auto mode."),
|
|
|
|
<<0:80000000>> = do_body("POST", "/auto-sync/read_body", [], <<0:80000000>>, Config),
|
|
|
|
<<0:80000000>> = do_body("POST", "/auto-async/read_body", [], <<0:80000000>>, Config),
|
|
|
|
ok.
|
|
|
|
|
2019-10-02 19:12:05 +02:00
|
|
|
read_body_spawn(Config) ->
|
|
|
|
doc("Confirm we can use cowboy_req:read_body/1,2 from another process."),
|
|
|
|
<<"hello world!">> = do_body("POST", "/spawn/read_body", [], "hello world!", Config),
|
|
|
|
ok.
|
|
|
|
|
2017-10-30 16:21:25 +00:00
|
|
|
read_body_expect_100_continue(Config) ->
|
|
|
|
doc("Request body with a 100-continue expect header."),
|
|
|
|
do_read_body_expect_100_continue("/read_body", Config).
|
|
|
|
|
|
|
|
read_body_expect_100_continue_user_sent(Config) ->
|
|
|
|
doc("Request body with a 100-continue expect header, 100 response sent by handler."),
|
|
|
|
do_read_body_expect_100_continue("/100-continue/read_body", Config).
|
|
|
|
|
|
|
|
do_read_body_expect_100_continue(Path, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Body = <<0:8000000>>,
|
|
|
|
Headers = [
|
|
|
|
{<<"accept-encoding">>, <<"gzip">>},
|
|
|
|
{<<"expect">>, <<"100-continue">>},
|
|
|
|
{<<"content-length">>, integer_to_binary(byte_size(Body))}
|
|
|
|
],
|
|
|
|
Ref = gun:post(ConnPid, Path, Headers),
|
2020-04-01 21:11:59 +02:00
|
|
|
{inform, 100, []} = gun:await(ConnPid, Ref, infinity),
|
2017-10-30 16:21:25 +00:00
|
|
|
gun:data(ConnPid, Ref, fin, Body),
|
2020-04-01 21:11:59 +02:00
|
|
|
{response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2017-10-30 16:21:25 +00:00
|
|
|
{ok, RespBody} = case IsFin of
|
2020-04-01 21:11:59 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref, infinity);
|
2017-10-30 16:21:25 +00:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid),
|
2023-12-01 10:45:41 +01:00
|
|
|
do_decode(RespHeaders, RespBody),
|
|
|
|
ok.
|
2017-10-30 16:21:25 +00:00
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
read_urlencoded_body(Config) ->
|
|
|
|
doc("application/x-www-form-urlencoded request body."),
|
|
|
|
<<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
|
|
|
|
<<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
|
|
|
|
<<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
|
|
|
|
= do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
|
|
|
|
%% The timeout value is set too low on purpose to ensure a crash occurs.
|
|
|
|
ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
|
2017-09-14 13:42:17 +02:00
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/read_urlencoded_body", [], "%%%%%", Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
2019-09-15 21:49:45 +02:00
|
|
|
read_urlencoded_body_too_large(Config) ->
|
|
|
|
doc("application/x-www-form-urlencoded request body too large. "
|
|
|
|
"Send a 10MB body, larger than the default length, to ensure a crash occurs."),
|
|
|
|
do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
|
|
|
|
string:chars($a, 10000000), Config).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% We expect a crash.
|
|
|
|
do_read_urlencoded_body_too_large(Path, Body, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:headers(ConnPid, "POST", Path, [
|
2016-08-10 11:49:31 +02:00
|
|
|
{<<"content-length">>, integer_to_binary(iolist_size(Body))}
|
|
|
|
]),
|
|
|
|
gun:data(ConnPid, Ref, fin, Body),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, _, 413, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2019-09-15 21:49:45 +02:00
|
|
|
read_urlencoded_body_too_long(Config) ->
|
|
|
|
doc("application/x-www-form-urlencoded request body sent too slow. "
|
2020-04-02 15:46:38 +02:00
|
|
|
"The body is simply not being sent fully. It is read by the handler "
|
2019-09-15 21:49:45 +02:00
|
|
|
"for at most 1 second. A crash occurs because we don't have the full body."),
|
|
|
|
do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% We expect a crash.
|
|
|
|
do_read_urlencoded_body_too_long(Path, Body, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
2019-09-06 15:37:42 +02:00
|
|
|
Ref = gun:headers(ConnPid, "POST", Path, [
|
2016-08-10 11:49:31 +02:00
|
|
|
{<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
|
|
|
|
]),
|
|
|
|
gun:data(ConnPid, Ref, nofin, Body),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, _, 408, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2017-09-14 13:42:17 +02:00
|
|
|
_ = case config(protocol, Config) of
|
|
|
|
http ->
|
|
|
|
%% 408 error responses should close HTTP/1.1 connections.
|
|
|
|
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders);
|
|
|
|
http2 ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2018-09-07 13:56:12 +02:00
|
|
|
read_and_match_urlencoded_body(Config) ->
|
|
|
|
doc("Read and match an application/x-www-form-urlencoded request body."),
|
|
|
|
<<"#{}">> = do_body("POST", "/match/body_qs", [], "a=b&c=d", Config),
|
|
|
|
<<"#{a => <<\"b\">>}">> = do_body("POST", "/match/body_qs/a", [], "a=b&c=d", Config),
|
|
|
|
<<"#{c => <<\"d\">>}">> = do_body("POST", "/match/body_qs/c", [], "a=b&c=d", Config),
|
2023-03-30 15:39:13 +02:00
|
|
|
case do_body("POST", "/match/body_qs/a/c", [], "a=b&c=d", Config) of
|
|
|
|
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok;
|
|
|
|
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok
|
|
|
|
end,
|
|
|
|
case do_body("POST", "/match/body_qs/a/c", [], "a=b&c", Config) of
|
|
|
|
<<"#{a => <<\"b\">>,c => true}">> -> ok;
|
|
|
|
<<"#{c => true,a => <<\"b\">>}">> -> ok
|
|
|
|
end,
|
|
|
|
case do_body("POST", "/match/body_qs/a/c", [], "a&c=d", Config) of
|
|
|
|
<<"#{a => true,c => <<\"d\">>}">> -> ok;
|
|
|
|
<<"#{c => <<\"d\">>,a => true}">> -> ok
|
|
|
|
end,
|
2018-09-07 13:56:12 +02:00
|
|
|
%% Ensure match errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/match/body_qs/a/c", [], "a=b", Config),
|
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/match/body_qs", [], "%%%%%", Config),
|
|
|
|
%% The timeout value is set too low on purpose to ensure a crash occurs.
|
|
|
|
ok = do_read_body_timeout("/opts/read_and_match_urlencoded_body/timeout", <<"abc">>, Config),
|
|
|
|
ok.
|
|
|
|
|
2019-09-15 21:49:45 +02:00
|
|
|
read_and_match_urlencoded_body_too_large(Config) ->
|
|
|
|
doc("Read and match an application/x-www-form-urlencoded request body too large. "
|
|
|
|
"Send a 10MB body, larger than the default length, to ensure a crash occurs."),
|
|
|
|
do_read_urlencoded_body_too_large(
|
|
|
|
"/no-opts/read_and_match_urlencoded_body",
|
|
|
|
string:chars($a, 10000000), Config).
|
|
|
|
|
|
|
|
read_and_match_urlencoded_body_too_long(Config) ->
|
|
|
|
doc("Read and match an application/x-www-form-urlencoded request body sent too slow. "
|
2020-04-02 15:46:38 +02:00
|
|
|
"The body is simply not being sent fully. It is read by the handler "
|
2019-09-15 21:49:45 +02:00
|
|
|
"for at most 1 second. A crash occurs because we don't have the full body."),
|
|
|
|
do_read_urlencoded_body_too_long(
|
|
|
|
"/crash/read_and_match_urlencoded_body/period", <<"abc">>, Config).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
multipart(Config) ->
|
|
|
|
doc("Multipart request body."),
|
|
|
|
do_multipart("/multipart", Config).
|
|
|
|
|
|
|
|
do_multipart(Path, Config) ->
|
|
|
|
LargeBody = iolist_to_binary(string:chars($a, 10000000)),
|
|
|
|
ReqBody = [
|
|
|
|
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
|
|
|
|
"--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
|
|
|
|
"--deadbeef--"
|
|
|
|
],
|
|
|
|
RespBody = do_body("POST", Path, [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], ReqBody, Config),
|
|
|
|
[
|
2017-06-09 16:57:11 +02:00
|
|
|
{#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
|
2016-08-10 11:49:31 +02:00
|
|
|
{LargeHeaders, LargeBody}
|
|
|
|
] = binary_to_term(RespBody),
|
2017-06-09 16:57:11 +02:00
|
|
|
#{
|
|
|
|
<<"content-type">> := <<"application/octet-stream">>,
|
|
|
|
<<"x-custom">> := <<"value">>
|
|
|
|
} = LargeHeaders,
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
2017-05-16 14:50:01 -07:00
|
|
|
multipart_error_empty(Config) ->
|
|
|
|
doc("Multipart request body is empty."),
|
|
|
|
%% We use an empty list as a body to make sure Gun knows
|
|
|
|
%% we want to send an empty body.
|
|
|
|
%% @todo This is a terrible hack. Improve Gun!
|
|
|
|
Body = [],
|
|
|
|
%% Ensure an empty body results in a 400 error.
|
|
|
|
{400, _} = do_body_error("POST", "/multipart", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], Body, Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
multipart_error_preamble_only(Config) ->
|
|
|
|
doc("Multipart request body only contains a preamble."),
|
|
|
|
%% Ensure an empty body results in a 400 error.
|
|
|
|
{400, _} = do_body_error("POST", "/multipart", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], <<"Preamble.">>, Config),
|
|
|
|
ok.
|
|
|
|
|
2017-09-14 13:42:17 +02:00
|
|
|
multipart_error_headers(Config) ->
|
|
|
|
doc("Multipart request body with invalid part headers."),
|
|
|
|
ReqBody = [
|
|
|
|
"--deadbeef\r\nbad-header text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
|
|
|
|
"--deadbeef--"
|
|
|
|
],
|
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/multipart", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], ReqBody, Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% The function to parse the multipart body currently does not crash,
|
|
|
|
%% as far as I can tell. There is therefore no test for it.
|
|
|
|
|
2017-05-16 14:50:01 -07:00
|
|
|
multipart_error_no_final_boundary(Config) ->
|
|
|
|
doc("Multipart request body with no final boundary."),
|
|
|
|
ReqBody = [
|
|
|
|
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
|
|
|
|
],
|
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/multipart", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], ReqBody, Config),
|
|
|
|
ok.
|
|
|
|
|
2017-09-14 13:42:17 +02:00
|
|
|
multipart_missing_boundary(Config) ->
|
|
|
|
doc("Multipart request body without a boundary in the media type."),
|
|
|
|
ReqBody = [
|
|
|
|
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
|
|
|
|
"--deadbeef--"
|
|
|
|
],
|
|
|
|
%% Ensure parse errors result in a 400 response.
|
|
|
|
{400, _} = do_body_error("POST", "/multipart", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed">>}
|
|
|
|
], ReqBody, Config),
|
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
read_part_skip_body(Config) ->
|
|
|
|
doc("Multipart request body skipping part bodies."),
|
|
|
|
LargeBody = iolist_to_binary(string:chars($a, 10000000)),
|
|
|
|
ReqBody = [
|
|
|
|
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
|
|
|
|
"--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
|
|
|
|
"--deadbeef--"
|
|
|
|
],
|
|
|
|
RespBody = do_body("POST", "/multipart/skip_body", [
|
|
|
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
|
|
|
], ReqBody, Config),
|
|
|
|
[
|
2017-06-09 16:57:11 +02:00
|
|
|
#{<<"content-type">> := <<"text/plain">>},
|
2016-08-10 11:49:31 +02:00
|
|
|
LargeHeaders
|
|
|
|
] = binary_to_term(RespBody),
|
2017-06-09 16:57:11 +02:00
|
|
|
#{
|
|
|
|
<<"content-type">> := <<"application/octet-stream">>,
|
|
|
|
<<"x-custom">> := <<"value">>
|
|
|
|
} = LargeHeaders,
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%% @todo When reading a multipart body, length and period
|
|
|
|
%% only apply to a single read_body call. We may want a
|
|
|
|
%% separate option to know how many reads we want to do
|
|
|
|
%% before we give up.
|
|
|
|
|
|
|
|
read_part2(Config) ->
|
|
|
|
doc("Multipart request body using read_part/2."),
|
|
|
|
%% Override the length and period values only, making
|
|
|
|
%% the request process use more read_body calls.
|
|
|
|
%%
|
|
|
|
%% We do not try a custom timeout value since this would
|
|
|
|
%% be the same test as read_body/2.
|
|
|
|
do_multipart("/multipart/read_part2", Config).
|
|
|
|
|
|
|
|
read_part_body2(Config) ->
|
|
|
|
doc("Multipart request body using read_part_body/2."),
|
|
|
|
%% Override the length and period values only, making
|
|
|
|
%% the request process use more read_body calls.
|
|
|
|
%%
|
|
|
|
%% We do not try a custom timeout value since this would
|
|
|
|
%% be the same test as read_body/2.
|
|
|
|
do_multipart("/multipart/read_part_body2", Config).
|
|
|
|
|
|
|
|
%% Tests: Response.
|
|
|
|
|
|
|
|
%% @todo We want to crash when calling set_resp_* or related
|
|
|
|
%% functions after the reply has been sent.
|
|
|
|
|
|
|
|
set_resp_cookie(Config) ->
|
|
|
|
doc("Response using set_resp_cookie."),
|
|
|
|
%% Single cookie, no options.
|
|
|
|
{200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
|
2023-03-29 15:20:48 +02:00
|
|
|
{_, <<"mycookie=myvalue">>}
|
2016-08-10 11:49:31 +02:00
|
|
|
= lists:keyfind(<<"set-cookie">>, 1, Headers1),
|
|
|
|
%% Single cookie, with options.
|
|
|
|
{200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
|
2023-03-29 15:20:48 +02:00
|
|
|
{_, <<"mycookie=myvalue; Path=/resp/set_resp_cookie4">>}
|
2016-08-10 11:49:31 +02:00
|
|
|
= lists:keyfind(<<"set-cookie">>, 1, Headers2),
|
|
|
|
%% Multiple cookies.
|
|
|
|
{200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
|
|
|
|
[_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
|
|
|
|
%% Overwrite previously set cookie.
|
|
|
|
{200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
|
2023-03-29 15:20:48 +02:00
|
|
|
{_, <<"mycookie=overwrite">>}
|
2016-08-10 11:49:31 +02:00
|
|
|
= lists:keyfind(<<"set-cookie">>, 1, Headers4),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
set_resp_header(Config) ->
|
|
|
|
doc("Response using set_resp_header."),
|
|
|
|
{200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _, _} = do_get("/resp/set_resp_header_cookie", Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
2016-11-13 15:39:40 +01:00
|
|
|
set_resp_headers(Config) ->
|
|
|
|
doc("Response using set_resp_headers."),
|
|
|
|
{200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
|
2017-01-04 19:45:35 +01:00
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers),
|
|
|
|
true = lists:keymember(<<"content-encoding">>, 1, Headers),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _, _} = do_get("/resp/set_resp_headers_cookie", Config),
|
2016-11-13 15:39:40 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
resp_header(Config) ->
|
|
|
|
doc("Response header with/without default."),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
resp_headers(Config) ->
|
|
|
|
doc("Get all response headers."),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
|
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
set_resp_body(Config) ->
|
|
|
|
doc("Response using set_resp_body."),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
|
|
|
|
{200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
|
|
|
|
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
|
|
|
|
{200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
|
|
|
|
ok.
|
|
|
|
|
2017-09-04 20:48:07 +02:00
|
|
|
set_resp_body_sendfile0(Config) ->
|
|
|
|
doc("Response using set_resp_body with a sendfile of length 0."),
|
|
|
|
Path = "/resp/set_resp_body/sendfile0",
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
%% First request.
|
|
|
|
Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, IsFin, 200, _} = gun:await(ConnPid, Ref1, infinity),
|
2017-09-04 20:48:07 +02:00
|
|
|
{ok, <<>>} = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref1, infinity);
|
2017-09-04 20:48:07 +02:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
%% Second request will confirm everything works as intended.
|
|
|
|
Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, IsFin, 200, _} = gun:await(ConnPid, Ref2, infinity),
|
2017-09-04 20:48:07 +02:00
|
|
|
{ok, <<>>} = case IsFin of
|
2020-04-02 14:57:10 +02:00
|
|
|
nofin -> gun:await_body(ConnPid, Ref2, infinity);
|
2017-09-04 20:48:07 +02:00
|
|
|
fin -> {ok, <<>>}
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid),
|
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
has_resp_header(Config) ->
|
|
|
|
doc("Has response header?"),
|
|
|
|
{200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
has_resp_body(Config) ->
|
|
|
|
doc("Has response body?"),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
delete_resp_header(Config) ->
|
|
|
|
doc("Delete response header."),
|
|
|
|
{200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
|
|
|
|
false = lists:keymember(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
2017-10-29 19:52:27 +00:00
|
|
|
inform2(Config) ->
|
|
|
|
doc("Informational response(s) without headers, followed by the real response."),
|
|
|
|
{102, [], 200, _, _} = do_get_inform("/resp/inform2/102", Config),
|
|
|
|
{102, [], 200, _, _} = do_get_inform("/resp/inform2/binary", Config),
|
|
|
|
{500, _} = do_get_inform("/resp/inform2/error", Config),
|
|
|
|
{102, [], 200, _, _} = do_get_inform("/resp/inform2/twice", Config),
|
2024-01-09 13:06:11 +01:00
|
|
|
%% @todo How to test this properly? This isn't enough.
|
|
|
|
{200, _} = do_get_inform("/resp/inform2/after_reply", Config),
|
2017-10-29 19:52:27 +00:00
|
|
|
ok.
|
|
|
|
|
|
|
|
inform3(Config) ->
|
|
|
|
doc("Informational response(s) with headers, followed by the real response."),
|
|
|
|
Headers = [{<<"ext-header">>, <<"ext-value">>}],
|
|
|
|
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/102", Config),
|
|
|
|
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/binary", Config),
|
|
|
|
{500, _} = do_get_inform("/resp/inform3/error", Config),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _} = do_get_inform("/resp/inform3/set_cookie", Config),
|
2017-10-29 19:52:27 +00:00
|
|
|
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/twice", Config),
|
2024-01-09 13:06:11 +01:00
|
|
|
%% @todo How to test this properly? This isn't enough.
|
|
|
|
{200, _} = do_get_inform("/resp/inform3/after_reply", Config),
|
2017-10-29 19:52:27 +00:00
|
|
|
ok.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
reply2(Config) ->
|
|
|
|
doc("Response with default headers and no body."),
|
|
|
|
{200, _, _} = do_get("/resp/reply2/200", Config),
|
|
|
|
{201, _, _} = do_get("/resp/reply2/201", Config),
|
|
|
|
{404, _, _} = do_get("/resp/reply2/404", Config),
|
|
|
|
{200, _, _} = do_get("/resp/reply2/binary", Config),
|
|
|
|
{500, _, _} = do_get("/resp/reply2/error", Config),
|
2024-01-09 13:06:11 +01:00
|
|
|
%% @todo How to test this properly? This isn't enough.
|
2016-08-10 11:49:31 +02:00
|
|
|
{200, _, _} = do_get("/resp/reply2/twice", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
reply3(Config) ->
|
|
|
|
doc("Response with additional headers and no body."),
|
|
|
|
{200, Headers1, _} = do_get("/resp/reply3/200", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers1),
|
|
|
|
{201, Headers2, _} = do_get("/resp/reply3/201", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers2),
|
|
|
|
{404, Headers3, _} = do_get("/resp/reply3/404", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers3),
|
|
|
|
{500, _, _} = do_get("/resp/reply3/error", Config),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _, _} = do_get("/resp/reply3/set_cookie", Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
reply4(Config) ->
|
|
|
|
doc("Response with additional headers and body."),
|
|
|
|
{200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
|
|
|
|
{201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
|
|
|
|
{404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
|
|
|
|
{500, _, _} = do_get("/resp/reply4/error", Config),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _, _} = do_get("/resp/reply4/set_cookie", Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_reply2(Config) ->
|
|
|
|
doc("Response with default headers and streamed body."),
|
|
|
|
Body = <<0:8000000>>,
|
|
|
|
{200, _, Body} = do_get("/resp/stream_reply2/200", Config),
|
|
|
|
{201, _, Body} = do_get("/resp/stream_reply2/201", Config),
|
|
|
|
{404, _, Body} = do_get("/resp/stream_reply2/404", Config),
|
|
|
|
{200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
|
|
|
|
{500, _, _} = do_get("/resp/stream_reply2/error", Config),
|
|
|
|
ok.
|
|
|
|
|
2024-01-09 13:06:11 +01:00
|
|
|
stream_reply2_twice(Config) ->
|
|
|
|
doc("Attempting to stream a response twice results in a crash. "
|
|
|
|
"This crash can only be properly detected in HTTP/2."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/stream_reply2/twice",
|
|
|
|
[{<<"accept-encoding">>, <<"gzip">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
Protocol = config(protocol, Config),
|
|
|
|
Flavor = config(flavor, Config),
|
|
|
|
case {Protocol, Flavor, gun:await_body(ConnPid, Ref, infinity)} of
|
|
|
|
%% In HTTP/1.1 we cannot propagate an error at that point.
|
|
|
|
%% The response will simply not have a body.
|
|
|
|
{http, vanilla, {ok, <<>>}} ->
|
|
|
|
ok;
|
|
|
|
%% When compression was used we do get gzip headers. But
|
|
|
|
%% we do not have any data in the zlib stream.
|
|
|
|
{http, compress, {ok, Data}} ->
|
|
|
|
Z = zlib:open(),
|
|
|
|
zlib:inflateInit(Z, 31),
|
|
|
|
0 = iolist_size(zlib:inflate(Z, Data)),
|
|
|
|
ok;
|
|
|
|
%% In HTTP/2 the stream gets reset with an appropriate error.
|
|
|
|
{http2, _, {error, {stream_error, {stream_error, internal_error, _}}}} ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
stream_reply3(Config) ->
|
|
|
|
doc("Response with additional headers and streamed body."),
|
|
|
|
Body = <<0:8000000>>,
|
|
|
|
{200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers1),
|
|
|
|
{201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers2),
|
|
|
|
{404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
|
|
|
|
true = lists:keymember(<<"content-type">>, 1, Headers3),
|
|
|
|
{500, _, _} = do_get("/resp/stream_reply3/error", Config),
|
2024-01-09 16:45:54 -03:00
|
|
|
%% The set-cookie header is special. set_resp_cookie must be used.
|
|
|
|
{500, _, _} = do_get("/resp/stream_reply3/set_cookie", Config),
|
2016-08-10 11:49:31 +02:00
|
|
|
ok.
|
|
|
|
|
2017-11-01 15:33:10 +00:00
|
|
|
stream_body_fin0(Config) ->
|
|
|
|
doc("Streamed body with last chunk of size 0."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/fin0", Config),
|
|
|
|
ok.
|
|
|
|
|
2018-11-09 17:42:37 +01:00
|
|
|
stream_body_multiple(Config) ->
|
|
|
|
doc("Streamed body via multiple calls."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/multiple", Config),
|
2019-09-13 14:20:04 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_body_loop(Config) ->
|
|
|
|
doc("Streamed body via a fast loop."),
|
|
|
|
{200, _, <<0:32000000/unit:8>>} = do_get("/resp/stream_body/loop", Config),
|
2018-11-09 17:42:37 +01:00
|
|
|
ok.
|
|
|
|
|
2017-11-01 15:33:10 +00:00
|
|
|
stream_body_nofin(Config) ->
|
|
|
|
doc("Unfinished streamed body."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/nofin", Config),
|
|
|
|
ok.
|
|
|
|
|
2018-11-09 17:42:37 +01:00
|
|
|
stream_body_sendfile(Config) ->
|
|
|
|
doc("Streamed body via multiple calls, including sendfile calls."),
|
|
|
|
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
|
|
|
|
ExpectedBody = iolist_to_binary([
|
|
|
|
<<"Hello ">>,
|
|
|
|
AppFile,
|
|
|
|
<<" interspersed ">>,
|
|
|
|
AppFile,
|
|
|
|
<<" world!">>
|
|
|
|
]),
|
|
|
|
{200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_body_sendfile_fin(Config) ->
|
|
|
|
doc("Streamed body via multiple calls, including a sendfile final call."),
|
|
|
|
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
|
|
|
|
ExpectedBody = iolist_to_binary([
|
|
|
|
<<"Hello! ">>,
|
|
|
|
AppFile
|
|
|
|
]),
|
|
|
|
{200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile_fin", Config),
|
|
|
|
ok.
|
|
|
|
|
2019-10-02 20:30:32 +02:00
|
|
|
stream_body_spawn(Config) ->
|
|
|
|
doc("Confirm we can use cowboy_req:stream_body/3 from another process."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/spawn", Config),
|
|
|
|
ok.
|
|
|
|
|
2018-05-16 13:28:49 +02:00
|
|
|
stream_body_content_length_multiple(Config) ->
|
|
|
|
doc("Streamed body via multiple calls."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_body_content_length_fin0(Config) ->
|
|
|
|
doc("Streamed body with last chunk of size 0."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/fin0", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_body_content_length_nofin(Config) ->
|
|
|
|
doc("Unfinished streamed body."),
|
|
|
|
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/nofin", Config),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_body_content_length_nofin_error(Config) ->
|
2019-09-16 11:34:51 +02:00
|
|
|
doc("Not all of the response body sent."),
|
2018-05-16 13:28:49 +02:00
|
|
|
case config(protocol, Config) of
|
|
|
|
http ->
|
|
|
|
case do_get_error("/resp/stream_body_content_length/nofin-error", Config) of
|
2019-09-16 11:34:51 +02:00
|
|
|
%% When compression is used content-length is not sent.
|
2018-05-16 13:28:49 +02:00
|
|
|
{200, Headers, <<"Hello">>} ->
|
|
|
|
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
|
2019-09-16 11:34:51 +02:00
|
|
|
%% The server closes the connection when the body couldn't be sent fully.
|
2019-09-06 15:37:42 +02:00
|
|
|
{error, {stream_error, closed}} ->
|
2019-09-16 11:34:51 +02:00
|
|
|
receive
|
2019-10-02 10:10:43 +02:00
|
|
|
{gun_down, ConnPid, _, _, _} ->
|
2019-09-16 11:34:51 +02:00
|
|
|
gun:close(ConnPid)
|
|
|
|
after 1000 ->
|
|
|
|
error(timeout)
|
|
|
|
end
|
2018-05-16 13:28:49 +02:00
|
|
|
end;
|
|
|
|
http2 ->
|
|
|
|
%% @todo HTTP2 should have the same content-length checks
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2020-07-03 11:02:59 +02:00
|
|
|
stream_body_concurrent(Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]),
|
|
|
|
Ref2 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, infinity),
|
|
|
|
{ok, _} = gun:await_body(ConnPid, Ref1, infinity),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref2, infinity),
|
|
|
|
{ok, _} = gun:await_body(ConnPid, Ref2, infinity),
|
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% @todo Crash when calling stream_body after the fin flag has been set.
|
|
|
|
%% @todo Crash when calling stream_body after calling reply.
|
|
|
|
%% @todo Crash when calling stream_body before calling stream_reply.
|
|
|
|
|
2018-06-27 13:40:11 +02:00
|
|
|
stream_events_single(Config) ->
|
|
|
|
doc("Streamed event."),
|
|
|
|
{200, Headers, <<
|
|
|
|
"event: add_comment\n"
|
|
|
|
"data: Comment text.\n"
|
|
|
|
"data: With many lines.\n"
|
|
|
|
"\n"
|
|
|
|
>>} = do_get("/resp/stream_events/single", Config),
|
|
|
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_events_list(Config) ->
|
|
|
|
doc("Streamed list of events."),
|
|
|
|
{200, Headers, <<
|
|
|
|
"event: add_comment\n"
|
|
|
|
"data: Comment text.\n"
|
|
|
|
"data: With many lines.\n"
|
|
|
|
"\n"
|
|
|
|
": Set retry higher\n"
|
|
|
|
": with many lines also.\n"
|
|
|
|
"retry: 10000\n"
|
|
|
|
"\n"
|
|
|
|
"id: 123\n"
|
|
|
|
"event: add_comment\n"
|
|
|
|
"data: Closing!\n"
|
|
|
|
"\n"
|
|
|
|
>>} = do_get("/resp/stream_events/list", Config),
|
|
|
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
stream_events_multiple(Config) ->
|
|
|
|
doc("Streamed events via multiple calls."),
|
|
|
|
{200, Headers, <<
|
|
|
|
"event: add_comment\n"
|
|
|
|
"data: Comment text.\n"
|
|
|
|
"data: With many lines.\n"
|
|
|
|
"\n"
|
|
|
|
": Set retry higher\n"
|
|
|
|
": with many lines also.\n"
|
|
|
|
"retry: 10000\n"
|
|
|
|
"\n"
|
|
|
|
"id: 123\n"
|
|
|
|
"event: add_comment\n"
|
|
|
|
"data: Closing!\n"
|
|
|
|
"\n"
|
|
|
|
>>} = do_get("/resp/stream_events/multiple", Config),
|
|
|
|
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
|
|
|
|
2017-11-15 14:58:49 +01:00
|
|
|
stream_trailers(Config) ->
|
|
|
|
doc("Stream body followed by trailer headers."),
|
|
|
|
{200, RespHeaders, <<"Hello world!">>, [
|
|
|
|
{<<"grpc-status">>, <<"0">>}
|
|
|
|
]} = do_trailers("/resp/stream_trailers", Config),
|
|
|
|
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
|
|
|
|
ok.
|
|
|
|
|
2017-11-20 15:46:23 +01:00
|
|
|
stream_trailers_large(Config) ->
|
|
|
|
doc("Stream large body followed by trailer headers."),
|
2019-12-23 21:00:35 +08:00
|
|
|
{200, RespHeaders, <<0:80000000>>, [
|
2017-11-20 15:46:23 +01:00
|
|
|
{<<"grpc-status">>, <<"0">>}
|
|
|
|
]} = do_trailers("/resp/stream_trailers/large", Config),
|
|
|
|
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
|
|
|
|
ok.
|
|
|
|
|
2017-11-15 14:58:49 +01:00
|
|
|
stream_trailers_no_te(Config) ->
|
2017-11-20 12:17:44 +01:00
|
|
|
doc("Stream body followed by trailer headers without a te header in the request."),
|
2017-11-15 14:58:49 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/stream_trailers", [
|
|
|
|
{<<"accept-encoding">>, <<"gzip">>}
|
|
|
|
]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
2017-11-20 12:17:44 +01:00
|
|
|
%% @todo Do we want to remove the trailer header automatically?
|
|
|
|
% false = lists:keyfind(<<"trailer">>, 1, RespHeaders),
|
2020-04-02 14:57:10 +02:00
|
|
|
{ok, RespBody} = gun:await_body(ConnPid, Ref, infinity),
|
2017-11-20 12:17:44 +01:00
|
|
|
<<"Hello world!">> = do_decode(RespHeaders, RespBody),
|
2017-11-15 14:58:49 +01:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2024-01-09 16:45:54 -03:00
|
|
|
stream_trailers_set_cookie(Config) ->
|
|
|
|
doc("Trying to send set-cookie in trailers should result in a crash."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/stream_trailers/set_cookie", [
|
|
|
|
{<<"accept-encoding">>, <<"gzip">>},
|
|
|
|
{<<"te">>, <<"trailers">>}
|
|
|
|
]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
case config(protocol, Config) of
|
|
|
|
http ->
|
|
|
|
%% Trailers are not sent because of the stream error.
|
|
|
|
{ok, _Body} = gun:await_body(ConnPid, Ref, infinity),
|
|
|
|
{error, timeout} = gun:await_body(ConnPid, Ref, 1000),
|
|
|
|
ok;
|
|
|
|
http2 ->
|
|
|
|
{error, {stream_error, {stream_error, internal_error, _}}}
|
|
|
|
= gun:await_body(ConnPid, Ref, infinity),
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2017-11-15 14:58:49 +01:00
|
|
|
do_trailers(Path, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, Path, [
|
|
|
|
{<<"accept-encoding">>, <<"gzip">>},
|
|
|
|
{<<"te">>, <<"trailers">>}
|
|
|
|
]),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, nofin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
{ok, RespBody, Trailers} = gun:await_body(ConnPid, Ref, infinity),
|
2017-11-15 14:58:49 +01:00
|
|
|
gun:close(ConnPid),
|
|
|
|
{Status, RespHeaders, do_decode(RespHeaders, RespBody), Trailers}.
|
|
|
|
|
|
|
|
%% @todo Crash when calling stream_trailers twice.
|
|
|
|
%% @todo Crash when calling stream_trailers after the fin flag has been set.
|
|
|
|
%% @todo Crash when calling stream_trailers after calling reply.
|
|
|
|
%% @todo Crash when calling stream_trailers before calling stream_reply.
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Tests: Push.
|
|
|
|
|
|
|
|
%% @todo We want to crash when push is called after reply has been initiated.
|
|
|
|
|
|
|
|
push(Config) ->
|
|
|
|
case config(protocol, Config) of
|
|
|
|
http -> do_push_http("/resp/push", Config);
|
|
|
|
http2 -> do_push_http2(Config)
|
|
|
|
end.
|
|
|
|
|
2024-01-09 13:06:11 +01:00
|
|
|
push_after_reply(Config) ->
|
|
|
|
doc("Trying to push a response after the final response results in a crash."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/push/after_reply", []),
|
|
|
|
%% @todo How to test this properly? This isn't enough.
|
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
gun:close(ConnPid).
|
|
|
|
|
2016-08-10 11:49:31 +02:00
|
|
|
push_method(Config) ->
|
|
|
|
case config(protocol, Config) of
|
|
|
|
http -> do_push_http("/resp/push/method", Config);
|
|
|
|
http2 -> do_push_http2_method(Config)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
push_origin(Config) ->
|
|
|
|
case config(protocol, Config) of
|
|
|
|
http -> do_push_http("/resp/push/origin", Config);
|
|
|
|
http2 -> do_push_http2_origin(Config)
|
|
|
|
end.
|
|
|
|
|
|
|
|
push_qs(Config) ->
|
|
|
|
case config(protocol, Config) of
|
|
|
|
http -> do_push_http("/resp/push/qs", Config);
|
|
|
|
http2 -> do_push_http2_qs(Config)
|
|
|
|
end.
|
|
|
|
|
|
|
|
do_push_http(Path, Config) ->
|
|
|
|
doc("Ignore pushed responses when protocol is HTTP/1.1."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, Path, []),
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
2024-01-09 13:06:11 +01:00
|
|
|
gun:close(ConnPid).
|
2016-08-10 11:49:31 +02:00
|
|
|
|
|
|
|
do_push_http2(Config) ->
|
|
|
|
doc("Pushed responses."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/push", []),
|
|
|
|
%% We expect two pushed resources.
|
|
|
|
Origin = iolist_to_binary([
|
|
|
|
case config(type, Config) of
|
|
|
|
tcp -> "http";
|
|
|
|
ssl -> "https"
|
|
|
|
end,
|
|
|
|
"://localhost:",
|
|
|
|
integer_to_binary(config(port, Config))
|
|
|
|
]),
|
|
|
|
OriginLen = byte_size(Origin),
|
|
|
|
{push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
|
2020-04-02 14:57:10 +02:00
|
|
|
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
|
2020-04-02 14:57:10 +02:00
|
|
|
[{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Pushed CSS.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
|
2020-04-02 14:57:10 +02:00
|
|
|
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Pushed TXT is 406 because the pushed accept header uses an undefined type.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 406, _} = gun:await(ConnPid, PushTXT, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Let's not forget about the response to the client's request.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
|
|
|
do_push_http2_method(Config) ->
|
|
|
|
doc("Pushed response with non-GET method."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/push/method", []),
|
|
|
|
%% Pushed CSS.
|
2020-04-02 14:57:10 +02:00
|
|
|
{push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
{response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
|
|
|
|
%% Let's not forget about the response to the client's request.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
|
|
|
do_push_http2_origin(Config) ->
|
|
|
|
doc("Pushed response with custom scheme/host/port."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/push/origin", []),
|
|
|
|
%% Pushed CSS.
|
|
|
|
{push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
|
2020-04-02 14:57:10 +02:00
|
|
|
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
|
2020-04-02 14:57:10 +02:00
|
|
|
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Let's not forget about the response to the client's request.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|
|
|
|
|
|
|
|
do_push_http2_qs(Config) ->
|
|
|
|
doc("Pushed response with query string."),
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resp/push/qs", []),
|
|
|
|
%% Pushed CSS.
|
|
|
|
Origin = iolist_to_binary([
|
|
|
|
case config(type, Config) of
|
|
|
|
tcp -> "http";
|
|
|
|
ssl -> "https"
|
|
|
|
end,
|
|
|
|
"://localhost:",
|
|
|
|
integer_to_binary(config(port, Config))
|
|
|
|
]),
|
|
|
|
OriginLen = byte_size(Origin),
|
|
|
|
{push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
|
2020-04-02 14:57:10 +02:00
|
|
|
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
|
|
|
|
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
|
2020-04-02 14:57:10 +02:00
|
|
|
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
%% Let's not forget about the response to the client's request.
|
2020-04-02 14:57:10 +02:00
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
|
2016-08-10 11:49:31 +02:00
|
|
|
gun:close(ConnPid).
|