| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(req_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
|
| 23 |
%% ct. |
| 24 |
|
| 25 |
suite() -> |
| 26 |
Timeout = case os:type() of |
| 27 |
{win32, _} -> 120000; |
| 28 |
_ -> 30000 |
| 29 |
end, |
| 30 |
[{timetrap, Timeout}]. |
| 31 |
|
| 32 |
all() -> |
| 33 |
cowboy_test:common_all(). |
| 34 |
|
| 35 |
groups() -> |
| 36 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 37 |
|
| 38 |
init_per_suite(Config) -> |
| 39 |
ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"), |
| 40 |
Config. |
| 41 |
|
| 42 |
end_per_suite(Config) -> |
| 43 |
ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static"). |
| 44 |
|
| 45 |
init_per_group(Name, Config) -> |
| 46 |
cowboy_test:init_common_groups(Name, Config, ?MODULE). |
| 47 |
|
| 48 |
end_per_group(Name, _) -> |
| 49 |
cowboy_test:stop_group(Name). |
| 50 |
|
| 51 |
%% Routes. |
| 52 |
|
| 53 |
init_dispatch(Config) -> |
| 54 |
cowboy_router:compile([{"[...]", [ |
| 55 |
{"/static/[...]", cowboy_static, {dir, config(priv_dir, Config) ++ "/static"}}, |
| 56 |
%% @todo Seriously InitialState should be optional. |
| 57 |
{"/resp/:key[/:arg]", resp_h, []}, |
| 58 |
{"/multipart[/:key]", multipart_h, []}, |
| 59 |
{"/args/:key/:arg[/:default]", echo_h, []}, |
| 60 |
{"/crash/:key/period", echo_h, |
| 61 |
#{length => 999999999, period => 1000, timeout => 5000, crash => true}}, |
| 62 |
{"/no-opts/:key", echo_h, #{crash => true}}, |
| 63 |
{"/opts/:key/length", echo_h, #{length => 1000}}, |
| 64 |
{"/opts/:key/max_keys", echo_h, #{max_keys => 5}}, |
| 65 |
{"/opts/:key/period", echo_h, #{length => 999999999, period => 2000}}, |
| 66 |
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}}, |
| 67 |
{"/100-continue/:key", echo_h, []}, |
| 68 |
{"/full/:key", echo_h, []}, |
| 69 |
{"/auto-sync/:key", echo_h, []}, |
| 70 |
{"/auto-async/:key", echo_h, []}, |
| 71 |
{"/spawn/:key", echo_h, []}, |
| 72 |
{"/no/:key", echo_h, []}, |
| 73 |
{"/direct/:key/[...]", echo_h, []}, |
| 74 |
{"/:key/[...]", echo_h, []} |
| 75 |
]}]). |
| 76 |
|
| 77 |
%% Internal. |
| 78 |
|
| 79 |
do_body(Method, Path, Config) -> |
| 80 |
do_body(Method, Path, [], Config). |
| 81 |
|
| 82 |
do_body(Method, Path, Headers, Config) -> |
| 83 |
do_body(Method, Path, Headers, <<>>, Config). |
| 84 |
|
| 85 |
do_body(Method, Path, Headers0, Body, Config) -> |
| 86 |
ConnPid = gun_open(Config), |
| 87 |
Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0], |
| 88 |
Ref = gun:request(ConnPid, Method, Path, Headers, Body), |
| 89 |
{response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 90 |
{ok, RespBody} = case IsFin of |
| 91 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 92 |
fin -> {ok, <<>>} |
| 93 |
end, |
| 94 |
gun:close(ConnPid), |
| 95 |
do_decode(RespHeaders, RespBody). |
| 96 |
|
| 97 |
do_body_error(Method, Path, Headers0, Body, Config) -> |
| 98 |
ConnPid = gun_open(Config), |
| 99 |
Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0], |
| 100 |
Ref = gun:request(ConnPid, Method, Path, Headers, Body), |
| 101 |
{response, _, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 102 |
gun:close(ConnPid), |
| 103 |
{Status, RespHeaders}. |
| 104 |
|
| 105 |
do_get(Path, Config) -> |
| 106 |
do_get(Path, [], Config). |
| 107 |
|
| 108 |
do_get(Path, Headers, Config) -> |
| 109 |
ConnPid = gun_open(Config), |
| 110 |
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]), |
| 111 |
case gun:await(ConnPid, Ref, infinity) of |
| 112 |
{response, IsFin, Status, RespHeaders} -> |
| 113 |
{ok, RespBody} = case IsFin of |
| 114 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 115 |
fin -> {ok, <<>>} |
| 116 |
end, |
| 117 |
gun:close(ConnPid), |
| 118 |
{Status, RespHeaders, do_decode(RespHeaders, RespBody)}; |
| 119 |
{error, {stream_error, Error}} -> |
| 120 |
Error |
| 121 |
end. |
| 122 |
|
| 123 |
do_get_body(Path, Config) -> |
| 124 |
do_get_body(Path, [], Config). |
| 125 |
|
| 126 |
do_get_body(Path, Headers, Config) -> |
| 127 |
do_body("GET", Path, Headers, Config). |
| 128 |
|
| 129 |
do_get_inform(Path, Config) -> |
| 130 |
ConnPid = gun_open(Config), |
| 131 |
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]), |
| 132 |
case gun:await(ConnPid, Ref, infinity) of |
| 133 |
{response, _, RespStatus, RespHeaders} -> |
| 134 |
%% We don't care about the body. |
| 135 |
gun:close(ConnPid), |
| 136 |
{RespStatus, RespHeaders}; |
| 137 |
{inform, InfoStatus, InfoHeaders} -> |
| 138 |
{response, IsFin, RespStatus, RespHeaders} |
| 139 |
= case gun:await(ConnPid, Ref, infinity) of |
| 140 |
{inform, InfoStatus, InfoHeaders} -> |
| 141 |
gun:await(ConnPid, Ref, infinity); |
| 142 |
Response -> |
| 143 |
Response |
| 144 |
end, |
| 145 |
{ok, RespBody} = case IsFin of |
| 146 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 147 |
fin -> {ok, <<>>} |
| 148 |
end, |
| 149 |
gun:close(ConnPid), |
| 150 |
{InfoStatus, InfoHeaders, RespStatus, RespHeaders, do_decode(RespHeaders, RespBody)}; |
| 151 |
{error, {stream_error, Error}} -> |
| 152 |
Error |
| 153 |
end. |
| 154 |
|
| 155 |
do_decode(Headers, Body) -> |
| 156 |
case lists:keyfind(<<"content-encoding">>, 1, Headers) of |
| 157 |
{_, <<"gzip">>} -> zlib:gunzip(Body); |
| 158 |
_ -> Body |
| 159 |
end. |
| 160 |
|
| 161 |
do_get_error(Path, Config) -> |
| 162 |
do_get_error(Path, [], Config). |
| 163 |
|
| 164 |
do_get_error(Path, Headers, Config) -> |
| 165 |
ConnPid = gun_open(Config), |
| 166 |
Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]), |
| 167 |
{response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 168 |
Result = case IsFin of |
| 169 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 170 |
fin -> {ok, <<>>} |
| 171 |
end, |
| 172 |
case Result of |
| 173 |
{ok, RespBody} -> {Status, RespHeaders, do_decode(RespHeaders, RespBody)}; |
| 174 |
_ -> Result |
| 175 |
end. |
| 176 |
|
| 177 |
%% Tests: Request. |
| 178 |
|
| 179 |
binding(Config) -> |
| 180 |
doc("Value bound from request URI path with/without default."), |
| 181 |
<<"binding">> = do_get_body("/args/binding/key", Config), |
| 182 |
<<"binding">> = do_get_body("/args/binding/key/default", Config), |
| 183 |
<<"default">> = do_get_body("/args/binding/undefined/default", Config), |
| 184 |
ok. |
| 185 |
|
| 186 |
bindings(Config) -> |
| 187 |
doc("Values bound from request URI path."), |
| 188 |
<<"#{key => <<\"bindings\">>}">> = do_get_body("/bindings", Config), |
| 189 |
ok. |
| 190 |
|
| 191 |
cert(Config) -> |
| 192 |
case config(type, Config) of |
| 193 |
tcp -> doc("TLS certificates can only be provided over TLS."); |
| 194 |
ssl -> do_cert(Config); |
| 195 |
quic -> do_cert(Config) |
| 196 |
end. |
| 197 |
|
| 198 |
do_cert(Config) -> |
| 199 |
doc("A client TLS certificate was provided."), |
| 200 |
Cert = do_get_body("/cert", Config), |
| 201 |
Cert = do_get_body("/direct/cert", Config), |
| 202 |
ok. |
| 203 |
|
| 204 |
cert_undefined(Config) -> |
| 205 |
doc("No client TLS certificate was provided."), |
| 206 |
<<"undefined">> = do_get_body("/cert", [{no_cert, true}|Config]), |
| 207 |
<<"undefined">> = do_get_body("/direct/cert", [{no_cert, true}|Config]), |
| 208 |
ok. |
| 209 |
|
| 210 |
header(Config) -> |
| 211 |
doc("Request header with/without default."), |
| 212 |
<<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config), |
| 213 |
<<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config), |
| 214 |
<<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config), |
| 215 |
ok. |
| 216 |
|
| 217 |
headers(Config) -> |
| 218 |
doc("Request headers."), |
| 219 |
do_headers("/headers", Config), |
| 220 |
do_headers("/direct/headers", Config). |
| 221 |
|
| 222 |
do_headers(Path, Config) -> |
| 223 |
%% We always send accept-encoding with this test suite's requests. |
| 224 |
<<"#{<<\"accept-encoding\">> => <<\"gzip\">>," |
| 225 |
"<<\"content-length\">> => <<\"0\">>," |
| 226 |
"<<\"header\">> => <<\"value\">>", _/bits>> |
| 227 |
= do_get_body(Path, [{<<"header">>, "value"}], Config), |
| 228 |
ok. |
| 229 |
|
| 230 |
host(Config) -> |
| 231 |
doc("Request URI host."), |
| 232 |
<<"localhost">> = do_get_body("/host", Config), |
| 233 |
<<"localhost">> = do_get_body("/direct/host", Config), |
| 234 |
ok. |
| 235 |
|
| 236 |
host_info(Config) -> |
| 237 |
doc("Request host_info."), |
| 238 |
<<"[<<\"localhost\">>]">> = do_get_body("/host_info", Config), |
| 239 |
ok. |
| 240 |
|
| 241 |
%% @todo Actually write the related unit tests. |
| 242 |
match_cookies(Config) -> |
| 243 |
doc("Matched request cookies."), |
| 244 |
<<"#{}">> = do_get_body("/match/cookies", [{<<"cookie">>, "a=b; c=d"}], Config), |
| 245 |
<<"#{a => <<\"b\">>}">> = do_get_body("/match/cookies/a", [{<<"cookie">>, "a=b; c=d"}], Config), |
| 246 |
<<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config), |
| 247 |
case do_get_body("/match/cookies/a/c", [{<<"cookie">>, "a=b; c=d"}], Config) of |
| 248 |
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok; |
| 249 |
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok |
| 250 |
end, |
| 251 |
%% Ensure match errors result in a 400 response. |
| 252 |
{400, _, _} = do_get("/match/cookies/a/c", |
| 253 |
[{<<"cookie">>, "a=b"}], Config), |
| 254 |
%% Ensure too many cookies result in a 400 response. |
| 255 |
{200, _, _} = do_get("/match/cookies_with_max_cookies", |
| 256 |
[{<<"cookie">>, "a=b; c=d; e=f; g=h; i=j"}], Config), |
| 257 |
{400, _, _} = do_get("/match/cookies_with_max_cookies", |
| 258 |
[{<<"cookie">>, "a=b; c=d; e=f; g=h; i=j; k=l"}], Config), |
| 259 |
%% This function is tested more extensively through unit tests. |
| 260 |
ok. |
| 261 |
|
| 262 |
%% @todo Actually write the related unit tests. |
| 263 |
match_qs(Config) -> |
| 264 |
doc("Matched request URI query string."), |
| 265 |
<<"#{}">> = do_get_body("/match/qs?a=b&c=d", Config), |
| 266 |
<<"#{a => <<\"b\">>}">> = do_get_body("/match/qs/a?a=b&c=d", Config), |
| 267 |
<<"#{c => <<\"d\">>}">> = do_get_body("/match/qs/c?a=b&c=d", Config), |
| 268 |
case do_get_body("/match/qs/a/c?a=b&c=d", Config) of |
| 269 |
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok; |
| 270 |
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok |
| 271 |
end, |
| 272 |
case do_get_body("/match/qs/a/c?a=b&c", Config) of |
| 273 |
<<"#{a => <<\"b\">>,c => true}">> -> ok; |
| 274 |
<<"#{c => true,a => <<\"b\">>}">> -> ok |
| 275 |
end, |
| 276 |
case do_get_body("/match/qs/a/c?a&c=d", Config) of |
| 277 |
<<"#{a => true,c => <<\"d\">>}">> -> ok; |
| 278 |
<<"#{c => <<\"d\">>,a => true}">> -> ok |
| 279 |
end, |
| 280 |
%% Ensure match errors result in a 400 response. |
| 281 |
{400, _, _} = do_get("/match/qs/a/c?a=b", [], Config), |
| 282 |
{400, _, _} = do_get("/match/qs_with_constraints", [], Config), |
| 283 |
%% Ensure too many keys result in a 400 response. |
| 284 |
{200, _, _} = do_get("/match/qs_with_max_keys?a=b&c=d&e=f&g=h&i=j", Config), |
| 285 |
{400, _, _} = do_get("/match/qs_with_max_keys?a=b&c=d&e=f&g=h&i=j&k=l", Config), |
| 286 |
%% This function is tested more extensively through unit tests. |
| 287 |
ok. |
| 288 |
|
| 289 |
method(Config) -> |
| 290 |
doc("Request method."), |
| 291 |
do_method("/method", Config), |
| 292 |
do_method("/direct/method", Config). |
| 293 |
|
| 294 |
do_method(Path, Config) -> |
| 295 |
<<"GET">> = do_body("GET", Path, Config), |
| 296 |
<<>> = do_body("HEAD", Path, Config), |
| 297 |
<<"OPTIONS">> = do_body("OPTIONS", Path, Config), |
| 298 |
<<"PATCH">> = do_body("PATCH", Path, Config), |
| 299 |
<<"POST">> = do_body("POST", Path, Config), |
| 300 |
<<"PUT">> = do_body("PUT", Path, Config), |
| 301 |
<<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", Path, Config), |
| 302 |
ok. |
| 303 |
|
| 304 |
parse_cookies(Config) -> |
| 305 |
doc("Request cookies."), |
| 306 |
<<"[]">> = do_get_body("/parse_cookies", Config), |
| 307 |
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">> |
| 308 |
= do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config), |
| 309 |
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">> |
| 310 |
= do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config), |
| 311 |
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">> |
| 312 |
= do_get_body("/parse_cookies", |
| 313 |
[{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config), |
| 314 |
%% Ensure parse errors result in a 400 response. |
| 315 |
{400, _, _} = do_get("/parse_cookies", |
| 316 |
[{<<"cookie">>, "bad\tname=strawberry"}], Config), |
| 317 |
{400, _, _} = do_get("/parse_cookies", |
| 318 |
[{<<"cookie">>, "goodname=strawberry\tmilkshake"}], Config), |
| 319 |
%% Ensure too many cookies result in a 400 response. |
| 320 |
{200, _, _} = do_get("/parse_cookies_with_max_cookies", |
| 321 |
[{<<"cookie">>, "a=b; c=d; e=f; g=h; i=j"}], Config), |
| 322 |
{400, _, _} = do_get("/parse_cookies_with_max_cookies", |
| 323 |
[{<<"cookie">>, "a=b; c=d; e=f; g=h; i=j; k=l"}], Config), |
| 324 |
ok. |
| 325 |
|
| 326 |
filter_then_parse_cookies(Config) -> |
| 327 |
doc("Filter cookies then parse them."), |
| 328 |
<<"[]">> = do_get_body("/filter_then_parse_cookies", Config), |
| 329 |
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">> |
| 330 |
= do_get_body("/filter_then_parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config), |
| 331 |
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">> |
| 332 |
= do_get_body("/filter_then_parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config), |
| 333 |
<<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">> |
| 334 |
= do_get_body("/filter_then_parse_cookies", |
| 335 |
[{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config), |
| 336 |
<<"[]">> |
| 337 |
= do_get_body("/filter_then_parse_cookies", |
| 338 |
[{<<"cookie">>, "bad name=strawberry"}], Config), |
| 339 |
<<"[{<<\"cake\">>,<<\"strawberry\">>}]">> |
| 340 |
= do_get_body("/filter_then_parse_cookies", |
| 341 |
[{<<"cookie">>, "bad name=strawberry; another bad name=strawberry; cake=strawberry"}], Config), |
| 342 |
<<"[]">> |
| 343 |
= do_get_body("/filter_then_parse_cookies", |
| 344 |
[{<<"cookie">>, "Blocked by http://www.example.com/upgrade-to-remove"}], Config), |
| 345 |
ok. |
| 346 |
|
| 347 |
parse_header(Config) -> |
| 348 |
doc("Parsed request header with/without default."), |
| 349 |
<<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">> |
| 350 |
= do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config), |
| 351 |
<<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">> |
| 352 |
= do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config), |
| 353 |
%% Header not in request but with default defined by Cowboy. |
| 354 |
<<"0">> = do_get_body("/args/parse_header/content-length", Config), |
| 355 |
%% Header not in request and no default from Cowboy. |
| 356 |
<<"undefined">> = do_get_body("/args/parse_header/upgrade", Config), |
| 357 |
%% Header in request and with default provided. |
| 358 |
<<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config), |
| 359 |
%% Ensure parse errors result in a 400 response. |
| 360 |
{400, _, _} = do_get("/args/parse_header/accept", |
| 361 |
[{<<"accept">>, "bad media type"}], Config), |
| 362 |
ok. |
| 363 |
|
| 364 |
parse_qs(Config) -> |
| 365 |
doc("Parsed request URI query string."), |
| 366 |
<<"[]">> = do_get_body("/parse_qs", Config), |
| 367 |
<<"[{<<\"abc\">>,true}]">> = do_get_body("/parse_qs?abc", Config), |
| 368 |
<<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> = do_get_body("/parse_qs?a=b&c=d+e", Config), |
| 369 |
%% Ensure parse errors result in a 400 response. |
| 370 |
{400, _, _} = do_get("/parse_qs?%%%%%%%", Config), |
| 371 |
%% Ensure too many keys result in a 400 response. |
| 372 |
{200, _, _} = do_get("/parse_qs_with_max_keys?a=b&c=d&e=f&g=h&i=j", Config), |
| 373 |
{400, _, _} = do_get("/parse_qs_with_max_keys?a=b&c=d&e=f&g=h&i=j&k=l", Config), |
| 374 |
ok. |
| 375 |
|
| 376 |
path(Config) -> |
| 377 |
doc("Request URI path."), |
| 378 |
do_path("/path", Config), |
| 379 |
do_path("/direct/path", Config). |
| 380 |
|
| 381 |
do_path(Path0, Config) -> |
| 382 |
Path = list_to_binary(Path0 ++ "/to/the/resource"), |
| 383 |
Path = do_get_body(Path, Config), |
| 384 |
Path = do_get_body([Path, "?query"], Config), |
| 385 |
ok. |
| 386 |
|
| 387 |
path_info(Config) -> |
| 388 |
doc("Request path_info."), |
| 389 |
<<"undefined">> = do_get_body("/no/path_info", Config), |
| 390 |
<<"[]">> = do_get_body("/path_info", Config), |
| 391 |
<<"[]">> = do_get_body("/path_info/", Config), |
| 392 |
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource", Config), |
| 393 |
<<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query", Config), |
| 394 |
ok. |
| 395 |
|
| 396 |
peer(Config) -> |
| 397 |
doc("Remote socket address."), |
| 398 |
<<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config), |
| 399 |
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config), |
| 400 |
ok. |
| 401 |
|
| 402 |
port(Config) -> |
| 403 |
doc("Request URI port."), |
| 404 |
Port = integer_to_binary(config(port, Config)), |
| 405 |
Port = do_get_body("/port", Config), |
| 406 |
Port = do_get_body("/direct/port", Config), |
| 407 |
ExpectedPort = case config(type, Config) of |
| 408 |
tcp -> <<"80">>; |
| 409 |
ssl -> <<"443">>; |
| 410 |
quic -> <<"443">> |
| 411 |
end, |
| 412 |
ExpectedPort = do_get_body("/port", [{<<"host">>, <<"localhost">>}], Config), |
| 413 |
ExpectedPort = do_get_body("/direct/port", [{<<"host">>, <<"localhost">>}], Config), |
| 414 |
ok. |
| 415 |
|
| 416 |
qs(Config) -> |
| 417 |
doc("Request URI query string."), |
| 418 |
do_qs("/qs", Config), |
| 419 |
do_qs("/direct/qs", Config). |
| 420 |
|
| 421 |
do_qs(Path, Config) -> |
| 422 |
<<>> = do_get_body(Path, Config), |
| 423 |
<<"abc">> = do_get_body(Path ++ "?abc", Config), |
| 424 |
<<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config), |
| 425 |
ok. |
| 426 |
|
| 427 |
scheme(Config) -> |
| 428 |
doc("Request URI scheme."), |
| 429 |
do_scheme("/scheme", Config), |
| 430 |
do_scheme("/direct/scheme", Config). |
| 431 |
|
| 432 |
do_scheme(Path, Config) -> |
| 433 |
Transport = config(type, Config), |
| 434 |
case do_get_body(Path, Config) of |
| 435 |
<<"http">> when Transport =:= tcp -> ok; |
| 436 |
<<"https">> when Transport =:= ssl -> ok; |
| 437 |
<<"https">> when Transport =:= quic -> ok |
| 438 |
end. |
| 439 |
|
| 440 |
sock(Config) -> |
| 441 |
doc("Local socket address."), |
| 442 |
<<"{{127,0,0,1},", _/bits >> = do_get_body("/sock", Config), |
| 443 |
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/sock", Config), |
| 444 |
ok. |
| 445 |
|
| 446 |
uri(Config) -> |
| 447 |
doc("Request URI building/modification."), |
| 448 |
Scheme = case config(type, Config) of |
| 449 |
tcp -> <<"http">>; |
| 450 |
ssl -> <<"https">>; |
| 451 |
quic -> <<"https">> |
| 452 |
end, |
| 453 |
SLen = byte_size(Scheme), |
| 454 |
Port = integer_to_binary(config(port, Config)), |
| 455 |
PLen = byte_size(Port), |
| 456 |
%% Absolute form. |
| 457 |
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >> |
| 458 |
= do_get_body("/uri?qs", Config), |
| 459 |
%% Origin form. |
| 460 |
<< "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config), |
| 461 |
%% Protocol relative. |
| 462 |
<< "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >> |
| 463 |
= do_get_body("/uri/protocol-relative?qs", Config), |
| 464 |
%% No query string. |
| 465 |
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >> |
| 466 |
= do_get_body("/uri/no-qs?qs", Config), |
| 467 |
%% No path or query string. |
| 468 |
<< Scheme:SLen/binary, "://localhost:", Port:PLen/binary >> |
| 469 |
= do_get_body("/uri/no-path?qs", Config), |
| 470 |
%% Changed port. |
| 471 |
<< Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >> |
| 472 |
= do_get_body("/uri/set-port?qs", Config), |
| 473 |
%% This function is tested more extensively through unit tests. |
| 474 |
ok. |
| 475 |
|
| 476 |
version(Config) -> |
| 477 |
doc("Request HTTP version."), |
| 478 |
do_version("/version", Config), |
| 479 |
do_version("/direct/version", Config). |
| 480 |
|
| 481 |
do_version(Path, Config) -> |
| 482 |
Protocol = config(protocol, Config), |
| 483 |
case do_get_body(Path, Config) of |
| 484 |
<<"HTTP/1.1">> when Protocol =:= http -> ok; |
| 485 |
<<"HTTP/2">> when Protocol =:= http2 -> ok; |
| 486 |
<<"HTTP/3">> when Protocol =:= http3 -> ok |
| 487 |
end. |
| 488 |
|
| 489 |
%% Tests: Request body. |
| 490 |
|
| 491 |
body_length(Config) -> |
| 492 |
doc("Request body length."), |
| 493 |
<<"0">> = do_get_body("/body_length", Config), |
| 494 |
<<"12">> = do_body("POST", "/body_length", [], "hello world!", Config), |
| 495 |
ok. |
| 496 |
|
| 497 |
has_body(Config) -> |
| 498 |
doc("Has a request body?"), |
| 499 |
<<"false">> = do_get_body("/has_body", Config), |
| 500 |
<<"true">> = do_body("POST", "/has_body", [], "hello world!", Config), |
| 501 |
ok. |
| 502 |
|
| 503 |
read_body(Config) -> |
| 504 |
doc("Request body."), |
| 505 |
<<>> = do_get_body("/read_body", Config), |
| 506 |
<<"hello world!">> = do_body("POST", "/read_body", [], "hello world!", Config), |
| 507 |
%% We expect to have read *at least* 1000 bytes. |
| 508 |
<<0:8000, _/bits>> = do_body("POST", "/opts/read_body/length", [], <<0:8000000>>, Config), |
| 509 |
%% The timeout value is set too low on purpose to ensure a crash occurs. |
| 510 |
ok = do_read_body_timeout("/opts/read_body/timeout", <<0:8000000>>, Config), |
| 511 |
%% 10MB body larger than default length. |
| 512 |
<<0:80000000>> = do_body("POST", "/full/read_body", [], <<0:80000000>>, Config), |
| 513 |
ok. |
| 514 |
|
| 515 |
read_body_mtu(Config) -> |
| 516 |
case os:type() of |
| 517 |
{win32, _} -> |
| 518 |
{skip, "Loopback MTU size is 0xFFFFFFFF on Windows."}; |
| 519 |
{unix, _} -> |
| 520 |
doc("Request body whose sizes are around the MTU."), |
| 521 |
MTU = ct_helper:get_loopback_mtu(), |
| 522 |
_ = [begin |
| 523 |
Body = <<0:Size/unit:8>>, |
| 524 |
Body = do_body("POST", "/full/read_body", [], Body, Config) |
| 525 |
end || Size <- lists:seq(MTU - 10, MTU + 10)], |
| 526 |
ok |
| 527 |
end. |
| 528 |
|
| 529 |
read_body_period(Config) -> |
| 530 |
doc("Read the request body for at most 2 seconds."), |
| 531 |
ConnPid = gun_open(Config), |
| 532 |
Body = <<0:8000000>>, |
| 533 |
Ref = gun:headers(ConnPid, "POST", "/opts/read_body/period", [ |
| 534 |
{<<"content-length">>, integer_to_binary(byte_size(Body) * 2)} |
| 535 |
]), |
| 536 |
%% The body is sent without fin. The server will read what it can |
| 537 |
%% for 2 seconds. The test succeeds if we get some of the data back |
| 538 |
%% (meaning the function will have returned after the period ends). |
| 539 |
gun:data(ConnPid, Ref, nofin, Body), |
| 540 |
Response = gun:await(ConnPid, Ref, infinity), |
| 541 |
case Response of |
| 542 |
{response, nofin, 200, _} -> |
| 543 |
{data, _, Data} = gun:await(ConnPid, Ref, infinity), |
| 544 |
%% We expect to read at least some data. |
| 545 |
true = Data =/= <<>>, |
| 546 |
gun:close(ConnPid); |
| 547 |
%% We got a crash, likely because the environment |
| 548 |
%% was overloaded and the timeout triggered. Try again. |
| 549 |
{response, _, 500, _} -> |
| 550 |
gun:close(ConnPid), |
| 551 |
read_body_period(Config) |
| 552 |
end. |
| 553 |
|
| 554 |
%% We expect a crash. |
| 555 |
do_read_body_timeout(Path, Body, Config) -> |
| 556 |
ConnPid = gun_open(Config), |
| 557 |
Ref = gun:headers(ConnPid, "POST", Path, [ |
| 558 |
{<<"content-length">>, integer_to_binary(byte_size(Body))} |
| 559 |
]), |
| 560 |
case gun:await(ConnPid, Ref, infinity) of |
| 561 |
{response, _, 500, _} -> |
| 562 |
ok; |
| 563 |
%% See do_maybe_h3_error comment for details. |
| 564 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} -> |
| 565 |
ok |
| 566 |
end, |
| 567 |
gun:close(ConnPid). |
| 568 |
|
| 569 |
read_body_auto(Config) -> |
| 570 |
doc("Read the request body using auto mode."), |
| 571 |
<<0:80000000>> = do_body("POST", "/auto-sync/read_body", [], <<0:80000000>>, Config), |
| 572 |
<<0:80000000>> = do_body("POST", "/auto-async/read_body", [], <<0:80000000>>, Config), |
| 573 |
ok. |
| 574 |
|
| 575 |
read_body_spawn(Config) -> |
| 576 |
doc("Confirm we can use cowboy_req:read_body/1,2 from another process."), |
| 577 |
<<"hello world!">> = do_body("POST", "/spawn/read_body", [], "hello world!", Config), |
| 578 |
ok. |
| 579 |
|
| 580 |
read_body_expect_100_continue(Config) -> |
| 581 |
doc("Request body with a 100-continue expect header."), |
| 582 |
do_read_body_expect_100_continue("/read_body", Config). |
| 583 |
|
| 584 |
read_body_expect_100_continue_user_sent(Config) -> |
| 585 |
doc("Request body with a 100-continue expect header, 100 response sent by handler."), |
| 586 |
do_read_body_expect_100_continue("/100-continue/read_body", Config). |
| 587 |
|
| 588 |
do_read_body_expect_100_continue(Path, Config) -> |
| 589 |
ConnPid = gun_open(Config), |
| 590 |
Body = <<0:8000000>>, |
| 591 |
Headers = [ |
| 592 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 593 |
{<<"expect">>, <<"100-continue">>}, |
| 594 |
{<<"content-length">>, integer_to_binary(byte_size(Body))} |
| 595 |
], |
| 596 |
Ref = gun:post(ConnPid, Path, Headers), |
| 597 |
{inform, 100, []} = gun:await(ConnPid, Ref, infinity), |
| 598 |
gun:data(ConnPid, Ref, fin, Body), |
| 599 |
{response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 600 |
{ok, RespBody} = case IsFin of |
| 601 |
nofin -> gun:await_body(ConnPid, Ref, infinity); |
| 602 |
fin -> {ok, <<>>} |
| 603 |
end, |
| 604 |
gun:close(ConnPid), |
| 605 |
do_decode(RespHeaders, RespBody), |
| 606 |
ok. |
| 607 |
|
| 608 |
read_urlencoded_body(Config) -> |
| 609 |
doc("application/x-www-form-urlencoded request body."), |
| 610 |
<<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config), |
| 611 |
<<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config), |
| 612 |
<<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> |
| 613 |
= do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config), |
| 614 |
%% The timeout value is set too low on purpose to ensure a crash occurs. |
| 615 |
ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config), |
| 616 |
%% Ensure parse errors result in a 400 response. |
| 617 |
{400, _} = do_body_error("POST", "/read_urlencoded_body", [], "%%%%%", Config), |
| 618 |
%% Ensure too many keys result in a 400 response. |
| 619 |
<<_/bits>> = do_body("POST", "/opts/read_urlencoded_body/max_keys", |
| 620 |
[], "a=b&c=d&e=f&g=h&i=j", Config), |
| 621 |
do_read_urlencoded_body_max_keys("/opts/read_urlencoded_body/max_keys", Config), |
| 622 |
ok. |
| 623 |
|
| 624 |
do_read_urlencoded_body_max_keys(Path, Config) -> |
| 625 |
ConnPid = gun_open(Config), |
| 626 |
Ref = gun:post(ConnPid, Path, [], |
| 627 |
"a=b&c=d&e=f&g=h&i=j&k=l"), |
| 628 |
{response, _, 400, _} = gun:await(ConnPid, Ref, infinity), |
| 629 |
gun:close(ConnPid). |
| 630 |
|
| 631 |
read_urlencoded_body_too_large(Config) -> |
| 632 |
doc("application/x-www-form-urlencoded request body too large. " |
| 633 |
"Send a 10MB body, larger than the default length, to ensure a crash occurs."), |
| 634 |
do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body", |
| 635 |
string:chars($a, 10000000), Config). |
| 636 |
|
| 637 |
%% We expect a crash. |
| 638 |
do_read_urlencoded_body_too_large(Path, Body, Config) -> |
| 639 |
ConnPid = gun_open(Config), |
| 640 |
Ref = gun:headers(ConnPid, "POST", Path, [ |
| 641 |
{<<"content-length">>, integer_to_binary(iolist_size(Body))} |
| 642 |
]), |
| 643 |
gun:data(ConnPid, Ref, fin, Body), |
| 644 |
Response = gun:await(ConnPid, Ref, infinity), |
| 645 |
gun:close(ConnPid), |
| 646 |
case Response of |
| 647 |
{response, _, 413, _} -> |
| 648 |
ok; |
| 649 |
%% We got the wrong crash, likely because the environment |
| 650 |
%% was overloaded and the timeout triggered. Try again. |
| 651 |
{response, _, 408, _} -> |
| 652 |
do_read_urlencoded_body_too_large(Path, Body, Config); |
| 653 |
%% Timing issues make it possible for the connection to be |
| 654 |
%% closed before the data went through. We retry. |
| 655 |
{error, {stream_error, {closed, {error,closed}}}} -> |
| 656 |
do_read_urlencoded_body_too_large(Path, Body, Config) |
| 657 |
end. |
| 658 |
|
| 659 |
read_urlencoded_body_too_long(Config) -> |
| 660 |
doc("application/x-www-form-urlencoded request body sent too slow. " |
| 661 |
"The body is simply not being sent fully. It is read by the handler " |
| 662 |
"for at most 1 second. A crash occurs because we don't have the full body."), |
| 663 |
do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config). |
| 664 |
|
| 665 |
%% We expect a crash. |
| 666 |
do_read_urlencoded_body_too_long(Path, Body, Config) -> |
| 667 |
ConnPid = gun_open(Config), |
| 668 |
Ref = gun:headers(ConnPid, "POST", Path, [ |
| 669 |
{<<"content-length">>, integer_to_binary(byte_size(Body) * 2)} |
| 670 |
]), |
| 671 |
gun:data(ConnPid, Ref, nofin, Body), |
| 672 |
Protocol = config(protocol, Config), |
| 673 |
case gun:await(ConnPid, Ref, infinity) of |
| 674 |
{response, _, 408, RespHeaders} when Protocol =:= http -> |
| 675 |
%% 408 error responses should close HTTP/1.1 connections. |
| 676 |
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders), |
| 677 |
gun:close(ConnPid); |
| 678 |
{response, _, 408, _} when Protocol =:= http2; Protocol =:= http3 -> |
| 679 |
gun:close(ConnPid); |
| 680 |
%% We must have hit the timeout due to busy CI environment. Retry. |
| 681 |
{response, _, 500, _} -> |
| 682 |
gun:close(ConnPid), |
| 683 |
do_read_urlencoded_body_too_long(Path, Body, Config) |
| 684 |
end. |
| 685 |
|
| 686 |
read_and_match_urlencoded_body(Config) -> |
| 687 |
doc("Read and match an application/x-www-form-urlencoded request body."), |
| 688 |
<<"#{}">> = do_body("POST", "/match/body_qs", [], "a=b&c=d", Config), |
| 689 |
<<"#{a => <<\"b\">>}">> = do_body("POST", "/match/body_qs/a", [], "a=b&c=d", Config), |
| 690 |
<<"#{c => <<\"d\">>}">> = do_body("POST", "/match/body_qs/c", [], "a=b&c=d", Config), |
| 691 |
case do_body("POST", "/match/body_qs/a/c", [], "a=b&c=d", Config) of |
| 692 |
<<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok; |
| 693 |
<<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok |
| 694 |
end, |
| 695 |
case do_body("POST", "/match/body_qs/a/c", [], "a=b&c", Config) of |
| 696 |
<<"#{a => <<\"b\">>,c => true}">> -> ok; |
| 697 |
<<"#{c => true,a => <<\"b\">>}">> -> ok |
| 698 |
end, |
| 699 |
case do_body("POST", "/match/body_qs/a/c", [], "a&c=d", Config) of |
| 700 |
<<"#{a => true,c => <<\"d\">>}">> -> ok; |
| 701 |
<<"#{c => <<\"d\">>,a => true}">> -> ok |
| 702 |
end, |
| 703 |
%% Ensure match errors result in a 400 response. |
| 704 |
{400, _} = do_body_error("POST", "/match/body_qs/a/c", [], "a=b", Config), |
| 705 |
%% Ensure parse errors result in a 400 response. |
| 706 |
{400, _} = do_body_error("POST", "/match/body_qs", [], "%%%%%", Config), |
| 707 |
%% Ensure too many keys result in a 400 response. |
| 708 |
<<_/bits>> = do_body("POST", "/opts/read_and_match_urlencoded_body/max_keys", |
| 709 |
[], "a=b&c=d&e=f&g=h&i=j", Config), |
| 710 |
do_read_urlencoded_body_max_keys("/opts/read_and_match_urlencoded_body/max_keys", Config), |
| 711 |
%% The timeout value is set too low on purpose to ensure a crash occurs. |
| 712 |
ok = do_read_body_timeout("/opts/read_and_match_urlencoded_body/timeout", <<"abc">>, Config), |
| 713 |
ok. |
| 714 |
|
| 715 |
read_and_match_urlencoded_body_too_large(Config) -> |
| 716 |
doc("Read and match an application/x-www-form-urlencoded request body too large. " |
| 717 |
"Send a 10MB body, larger than the default length, to ensure a crash occurs."), |
| 718 |
do_read_urlencoded_body_too_large( |
| 719 |
"/no-opts/read_and_match_urlencoded_body", |
| 720 |
string:chars($a, 10000000), Config). |
| 721 |
|
| 722 |
read_and_match_urlencoded_body_too_long(Config) -> |
| 723 |
doc("Read and match an application/x-www-form-urlencoded request body sent too slow. " |
| 724 |
"The body is simply not being sent fully. It is read by the handler " |
| 725 |
"for at most 1 second. A crash occurs because we don't have the full body."), |
| 726 |
do_read_urlencoded_body_too_long( |
| 727 |
"/crash/read_and_match_urlencoded_body/period", <<"abc">>, Config). |
| 728 |
|
| 729 |
multipart(Config) -> |
| 730 |
doc("Multipart request body."), |
| 731 |
do_multipart("/multipart", Config). |
| 732 |
|
| 733 |
do_multipart(Path, Config) -> |
| 734 |
LargeBody = iolist_to_binary(string:chars($a, 10000000)), |
| 735 |
ReqBody = [ |
| 736 |
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n" |
| 737 |
"--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n" |
| 738 |
"--deadbeef--" |
| 739 |
], |
| 740 |
RespBody = do_body("POST", Path, [ |
| 741 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 742 |
], ReqBody, Config), |
| 743 |
[ |
| 744 |
{#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>}, |
| 745 |
{LargeHeaders, LargeBody} |
| 746 |
] = binary_to_term(RespBody), |
| 747 |
#{ |
| 748 |
<<"content-type">> := <<"application/octet-stream">>, |
| 749 |
<<"x-custom">> := <<"value">> |
| 750 |
} = LargeHeaders, |
| 751 |
ok. |
| 752 |
|
| 753 |
multipart_error_empty(Config) -> |
| 754 |
doc("Multipart request body is empty."), |
| 755 |
%% We use an empty list as a body to make sure Gun knows |
| 756 |
%% we want to send an empty body. |
| 757 |
%% @todo This is a terrible hack. Improve Gun! |
| 758 |
Body = [], |
| 759 |
%% Ensure an empty body results in a 400 error. |
| 760 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 761 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 762 |
], Body, Config), |
| 763 |
ok. |
| 764 |
|
| 765 |
multipart_error_preamble_only(Config) -> |
| 766 |
doc("Multipart request body only contains a preamble."), |
| 767 |
%% Ensure an empty body results in a 400 error. |
| 768 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 769 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 770 |
], <<"Preamble.">>, Config), |
| 771 |
ok. |
| 772 |
|
| 773 |
multipart_error_headers(Config) -> |
| 774 |
doc("Multipart request body with invalid part headers."), |
| 775 |
ReqBody = [ |
| 776 |
"--deadbeef\r\nbad-header text/plain\r\n\r\nCowboy is an HTTP server.\r\n" |
| 777 |
"--deadbeef--" |
| 778 |
], |
| 779 |
%% Ensure parse errors result in a 400 response. |
| 780 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 781 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 782 |
], ReqBody, Config), |
| 783 |
ok. |
| 784 |
|
| 785 |
multipart_error_headers_nul_name(Config) -> |
| 786 |
doc("Multipart request body with invalid part headers."), |
| 787 |
ReqBody = [ |
| 788 |
"--deadbeef\r\nx-", 0, "bad: value\r\n\r\nbody\r\n" |
| 789 |
"--deadbeef--" |
| 790 |
], |
| 791 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 792 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 793 |
], ReqBody, Config), |
| 794 |
ok. |
| 795 |
|
| 796 |
multipart_error_headers_nul_value(Config) -> |
| 797 |
doc("Multipart request body with invalid part headers."), |
| 798 |
ReqBody = [ |
| 799 |
"--deadbeef\r\nx-bad: val", 0, "ue\r\n\r\nbody\r\n" |
| 800 |
"--deadbeef--" |
| 801 |
], |
| 802 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 803 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 804 |
], ReqBody, Config), |
| 805 |
ok. |
| 806 |
|
| 807 |
multipart_error_headers_nul_filename(Config) -> |
| 808 |
doc("Multipart request body with invalid part headers."), |
| 809 |
ReqBody = [ |
| 810 |
"--deadbeef\r\n" |
| 811 |
"content-disposition: form-data; name=\"f\"; filename=\"a", 0, "b.txt\"\r\n" |
| 812 |
"\r\nbody\r\n" |
| 813 |
"--deadbeef--" |
| 814 |
], |
| 815 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 816 |
{<<"content-type">>, <<"multipart/form-data; boundary=deadbeef">>} |
| 817 |
], ReqBody, Config), |
| 818 |
ok. |
| 819 |
|
| 820 |
multipart_error_headers_too_large(Config) -> |
| 821 |
doc("Multipart request body with part headers larger than 2048 bytes."), |
| 822 |
ReqBody = [ |
| 823 |
%% We use a very large header size because we are testing on |
| 824 |
%% localhost (large socket buffers) and this is a soft limit |
| 825 |
%% (we parse if we have the data in buffers already). |
| 826 |
"--deadbeef\r\ncontent-type: ", binary:copy(<<"a">>, 100000), "\r\n\r\nbody\r\n", |
| 827 |
"--deadbeef--" |
| 828 |
], |
| 829 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 830 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 831 |
], ReqBody, Config), |
| 832 |
ok. |
| 833 |
|
| 834 |
multipart_error_no_final_boundary(Config) -> |
| 835 |
doc("Multipart request body with no final boundary."), |
| 836 |
ReqBody = [ |
| 837 |
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n" |
| 838 |
], |
| 839 |
%% Ensure parse errors result in a 400 response. |
| 840 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 841 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 842 |
], ReqBody, Config), |
| 843 |
ok. |
| 844 |
|
| 845 |
multipart_missing_boundary(Config) -> |
| 846 |
doc("Multipart request body without a boundary in the media type."), |
| 847 |
ReqBody = [ |
| 848 |
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n" |
| 849 |
"--deadbeef--" |
| 850 |
], |
| 851 |
%% Ensure parse errors result in a 400 response. |
| 852 |
{400, _} = do_body_error("POST", "/multipart", [ |
| 853 |
{<<"content-type">>, <<"multipart/mixed">>} |
| 854 |
], ReqBody, Config), |
| 855 |
ok. |
| 856 |
|
| 857 |
read_part_skip_body(Config) -> |
| 858 |
doc("Multipart request body skipping part bodies."), |
| 859 |
LargeBody = iolist_to_binary(string:chars($a, 10000000)), |
| 860 |
ReqBody = [ |
| 861 |
"--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n" |
| 862 |
"--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n" |
| 863 |
"--deadbeef--" |
| 864 |
], |
| 865 |
RespBody = do_body("POST", "/multipart/skip_body", [ |
| 866 |
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>} |
| 867 |
], ReqBody, Config), |
| 868 |
[ |
| 869 |
#{<<"content-type">> := <<"text/plain">>}, |
| 870 |
LargeHeaders |
| 871 |
] = binary_to_term(RespBody), |
| 872 |
#{ |
| 873 |
<<"content-type">> := <<"application/octet-stream">>, |
| 874 |
<<"x-custom">> := <<"value">> |
| 875 |
} = LargeHeaders, |
| 876 |
ok. |
| 877 |
|
| 878 |
%% @todo When reading a multipart body, length and period |
| 879 |
%% only apply to a single read_body call. We may want a |
| 880 |
%% separate option to know how many reads we want to do |
| 881 |
%% before we give up. |
| 882 |
|
| 883 |
read_part2(Config) -> |
| 884 |
doc("Multipart request body using read_part/2."), |
| 885 |
%% Override the length and period values only, making |
| 886 |
%% the request process use more read_body calls. |
| 887 |
%% |
| 888 |
%% We do not try a custom timeout value since this would |
| 889 |
%% be the same test as read_body/2. |
| 890 |
do_multipart("/multipart/read_part2", Config). |
| 891 |
|
| 892 |
read_part_body2(Config) -> |
| 893 |
doc("Multipart request body using read_part_body/2."), |
| 894 |
%% Override the length and period values only, making |
| 895 |
%% the request process use more read_body calls. |
| 896 |
%% |
| 897 |
%% We do not try a custom timeout value since this would |
| 898 |
%% be the same test as read_body/2. |
| 899 |
do_multipart("/multipart/read_part_body2", Config). |
| 900 |
|
| 901 |
%% Tests: Response. |
| 902 |
|
| 903 |
%% @todo We want to crash when calling set_resp_* or related |
| 904 |
%% functions after the reply has been sent. |
| 905 |
|
| 906 |
set_resp_cookie(Config) -> |
| 907 |
doc("Response using set_resp_cookie."), |
| 908 |
%% Single cookie, no options. |
| 909 |
{200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config), |
| 910 |
{_, <<"mycookie=myvalue">>} |
| 911 |
= lists:keyfind(<<"set-cookie">>, 1, Headers1), |
| 912 |
%% Single cookie, with options. |
| 913 |
{200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config), |
| 914 |
{_, <<"mycookie=myvalue; Path=/resp/set_resp_cookie4">>} |
| 915 |
= lists:keyfind(<<"set-cookie">>, 1, Headers2), |
| 916 |
%% Multiple cookies. |
| 917 |
{200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config), |
| 918 |
[_, _] = [H || H={<<"set-cookie">>, _} <- Headers3], |
| 919 |
%% Overwrite previously set cookie. |
| 920 |
{200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config), |
| 921 |
{_, <<"mycookie=overwrite">>} |
| 922 |
= lists:keyfind(<<"set-cookie">>, 1, Headers4), |
| 923 |
ok. |
| 924 |
|
| 925 |
set_resp_header(Config) -> |
| 926 |
doc("Response using set_resp_header."), |
| 927 |
{200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config), |
| 928 |
true = lists:keymember(<<"content-type">>, 1, Headers), |
| 929 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 930 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/set_resp_header_cookie", Config)), |
| 931 |
ok. |
| 932 |
|
| 933 |
set_resp_headers(Config) -> |
| 934 |
doc("Response using set_resp_headers."), |
| 935 |
{200, Headers1, <<"OK">>} = do_get("/resp/set_resp_headers", Config), |
| 936 |
true = lists:keymember(<<"content-type">>, 1, Headers1), |
| 937 |
true = lists:keymember(<<"content-encoding">>, 1, Headers1), |
| 938 |
{200, Headers2, <<"OK">>} = do_get("/resp/set_resp_headers_list", Config), |
| 939 |
true = lists:keymember(<<"content-type">>, 1, Headers2), |
| 940 |
true = lists:keymember(<<"content-encoding">>, 1, Headers2), |
| 941 |
{_, <<"one, two">>} = lists:keyfind(<<"test-header">>, 1, Headers2), |
| 942 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 943 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/set_resp_headers_cookie", Config)), |
| 944 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/set_resp_headers_list_cookie", Config)), |
| 945 |
ok. |
| 946 |
|
| 947 |
resp_header(Config) -> |
| 948 |
doc("Response header with/without default."), |
| 949 |
{200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config), |
| 950 |
{200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config), |
| 951 |
ok. |
| 952 |
|
| 953 |
resp_headers(Config) -> |
| 954 |
doc("Get all response headers."), |
| 955 |
{200, _, <<"OK">>} = do_get("/resp/resp_headers", Config), |
| 956 |
{200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config), |
| 957 |
ok. |
| 958 |
|
| 959 |
set_resp_body(Config) -> |
| 960 |
doc("Response using set_resp_body."), |
| 961 |
{200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config), |
| 962 |
{200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config), |
| 963 |
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")), |
| 964 |
{200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config), |
| 965 |
ok. |
| 966 |
|
| 967 |
set_resp_body_sendfile0(Config) -> |
| 968 |
doc("Response using set_resp_body with a sendfile of length 0."), |
| 969 |
Path = "/resp/set_resp_body/sendfile0", |
| 970 |
ConnPid = gun_open(Config), |
| 971 |
%% First request. |
| 972 |
Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]), |
| 973 |
{response, IsFin, 200, _} = gun:await(ConnPid, Ref1, infinity), |
| 974 |
{ok, <<>>} = case IsFin of |
| 975 |
nofin -> gun:await_body(ConnPid, Ref1, infinity); |
| 976 |
fin -> {ok, <<>>} |
| 977 |
end, |
| 978 |
%% Second request will confirm everything works as intended. |
| 979 |
Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]), |
| 980 |
{response, IsFin, 200, _} = gun:await(ConnPid, Ref2, infinity), |
| 981 |
{ok, <<>>} = case IsFin of |
| 982 |
nofin -> gun:await_body(ConnPid, Ref2, infinity); |
| 983 |
fin -> {ok, <<>>} |
| 984 |
end, |
| 985 |
gun:close(ConnPid), |
| 986 |
ok. |
| 987 |
|
| 988 |
has_resp_header(Config) -> |
| 989 |
doc("Has response header?"), |
| 990 |
{200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config), |
| 991 |
true = lists:keymember(<<"content-type">>, 1, Headers), |
| 992 |
ok. |
| 993 |
|
| 994 |
has_resp_body(Config) -> |
| 995 |
doc("Has response body?"), |
| 996 |
{200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config), |
| 997 |
{200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config), |
| 998 |
ok. |
| 999 |
|
| 1000 |
delete_resp_header(Config) -> |
| 1001 |
doc("Delete response header."), |
| 1002 |
{200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config), |
| 1003 |
false = lists:keymember(<<"content-type">>, 1, Headers), |
| 1004 |
ok. |
| 1005 |
|
| 1006 |
%% Data may be lost due to how RESET_STREAM QUIC frame works. |
| 1007 |
%% Because there is ongoing work for a better way to reset streams |
| 1008 |
%% (https://www.ietf.org/archive/id/draft-ietf-quic-reliable-stream-reset-03.html) |
| 1009 |
%% we convert the error to a 500 to keep the tests more explicit |
| 1010 |
%% at what we expect. |
| 1011 |
%% @todo When RESET_STREAM_AT gets added we can remove this function. |
| 1012 |
do_maybe_h3_error2({stream_error, h3_internal_error, _}) -> {500, []}; |
| 1013 |
do_maybe_h3_error2(Result) -> Result. |
| 1014 |
|
| 1015 |
do_maybe_h3_error3({stream_error, h3_internal_error, _}) -> {500, [], <<>>}; |
| 1016 |
do_maybe_h3_error3(Result) -> Result. |
| 1017 |
|
| 1018 |
inform2(Config) -> |
| 1019 |
doc("Informational response(s) without headers, followed by the real response."), |
| 1020 |
{102, [], 200, _, _} = do_get_inform("/resp/inform2/102", Config), |
| 1021 |
{102, [], 200, _, _} = do_get_inform("/resp/inform2/binary", Config), |
| 1022 |
{500, _} = do_maybe_h3_error2(do_get_inform("/resp/inform2/error", Config)), |
| 1023 |
{102, [], 200, _, _} = do_get_inform("/resp/inform2/twice", Config), |
| 1024 |
%% With HTTP/1.1 and HTTP/2 we will not get an error. |
| 1025 |
%% With HTTP/3 however the stream will occasionally |
| 1026 |
%% be reset before Gun receives the response. |
| 1027 |
case do_get_inform("/resp/inform2/after_reply", Config) of |
| 1028 |
{200, _} -> |
| 1029 |
ok; |
| 1030 |
{stream_error, h3_internal_error, _} -> |
| 1031 |
ok |
| 1032 |
end. |
| 1033 |
|
| 1034 |
inform3(Config) -> |
| 1035 |
doc("Informational response(s) with headers, followed by the real response."), |
| 1036 |
Headers = [{<<"ext-header">>, <<"ext-value">>}], |
| 1037 |
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/102", Config), |
| 1038 |
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/binary", Config), |
| 1039 |
{500, _} = do_maybe_h3_error2(do_get_inform("/resp/inform3/error", Config)), |
| 1040 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 1041 |
{500, _} = do_maybe_h3_error2(do_get_inform("/resp/inform3/set_cookie", Config)), |
| 1042 |
{102, Headers, 200, _, _} = do_get_inform("/resp/inform3/twice", Config), |
| 1043 |
%% With HTTP/1.1 and HTTP/2 we will not get an error. |
| 1044 |
%% With HTTP/3 however the stream will occasionally |
| 1045 |
%% be reset before Gun receives the response. |
| 1046 |
case do_get_inform("/resp/inform3/after_reply", Config) of |
| 1047 |
{200, _} -> |
| 1048 |
ok; |
| 1049 |
{stream_error, h3_internal_error, _} -> |
| 1050 |
ok |
| 1051 |
end. |
| 1052 |
|
| 1053 |
reply2(Config) -> |
| 1054 |
doc("Response with default headers and no body."), |
| 1055 |
{200, _, _} = do_get("/resp/reply2/200", Config), |
| 1056 |
{201, _, _} = do_get("/resp/reply2/201", Config), |
| 1057 |
{404, _, _} = do_get("/resp/reply2/404", Config), |
| 1058 |
{200, _, _} = do_get("/resp/reply2/binary", Config), |
| 1059 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/reply2/error", Config)), |
| 1060 |
%% @todo How to test this properly? This isn't enough. |
| 1061 |
{200, _, _} = do_get("/resp/reply2/twice", Config), |
| 1062 |
ok. |
| 1063 |
|
| 1064 |
reply3(Config) -> |
| 1065 |
doc("Response with additional headers and no body."), |
| 1066 |
{200, Headers1, _} = do_get("/resp/reply3/200", Config), |
| 1067 |
true = lists:keymember(<<"content-type">>, 1, Headers1), |
| 1068 |
{201, Headers2, _} = do_get("/resp/reply3/201", Config), |
| 1069 |
true = lists:keymember(<<"content-type">>, 1, Headers2), |
| 1070 |
{404, Headers3, _} = do_get("/resp/reply3/404", Config), |
| 1071 |
true = lists:keymember(<<"content-type">>, 1, Headers3), |
| 1072 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/reply3/error", Config)), |
| 1073 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 1074 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/reply3/set_cookie", Config)), |
| 1075 |
ok. |
| 1076 |
|
| 1077 |
reply4(Config) -> |
| 1078 |
doc("Response with additional headers and body."), |
| 1079 |
{200, _, <<"OK">>} = do_get("/resp/reply4/200", Config), |
| 1080 |
{201, _, <<"OK">>} = do_get("/resp/reply4/201", Config), |
| 1081 |
{404, _, <<"OK">>} = do_get("/resp/reply4/404", Config), |
| 1082 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/reply4/error", Config)), |
| 1083 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 1084 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/reply4/set_cookie", Config)), |
| 1085 |
ok. |
| 1086 |
|
| 1087 |
stream_reply2(Config) -> |
| 1088 |
doc("Response with default headers and streamed body."), |
| 1089 |
Body = <<0:8000000>>, |
| 1090 |
{200, _, Body} = do_get("/resp/stream_reply2/200", Config), |
| 1091 |
{201, _, Body} = do_get("/resp/stream_reply2/201", Config), |
| 1092 |
{404, _, Body} = do_get("/resp/stream_reply2/404", Config), |
| 1093 |
{200, _, Body} = do_get("/resp/stream_reply2/binary", Config), |
| 1094 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/stream_reply2/error", Config)), |
| 1095 |
ok. |
| 1096 |
|
| 1097 |
stream_reply2_twice(Config) -> |
| 1098 |
doc("Attempting to stream a response twice results in a crash."), |
| 1099 |
ConnPid = gun_open(Config), |
| 1100 |
Ref = gun:get(ConnPid, "/resp/stream_reply2/twice", |
| 1101 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 1102 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1103 |
Protocol = config(protocol, Config), |
| 1104 |
Flavor = config(flavor, Config), |
| 1105 |
case {Protocol, Flavor, gun:await_body(ConnPid, Ref, infinity)} of |
| 1106 |
%% In HTTP/1.1 we cannot propagate an error at that point. |
| 1107 |
%% The response will simply not have a body. |
| 1108 |
{http, vanilla, {ok, <<>>}} -> |
| 1109 |
ok; |
| 1110 |
%% When compression was used we do get gzip headers. But |
| 1111 |
%% we do not have any data in the zlib stream. |
| 1112 |
{http, compress, {ok, Data}} -> |
| 1113 |
Z = zlib:open(), |
| 1114 |
zlib:inflateInit(Z, 31), |
| 1115 |
0 = iolist_size(zlib:inflate(Z, Data)), |
| 1116 |
ok; |
| 1117 |
%% In HTTP/2 and HTTP/3 the stream gets reset with an appropriate error. |
| 1118 |
{http2, _, {error, {stream_error, {stream_error, internal_error, _}}}} -> |
| 1119 |
ok; |
| 1120 |
{http3, _, {error, {stream_error, {stream_error, h3_internal_error, _}}}} -> |
| 1121 |
ok |
| 1122 |
end, |
| 1123 |
gun:close(ConnPid). |
| 1124 |
|
| 1125 |
stream_reply3(Config) -> |
| 1126 |
doc("Response with additional headers and streamed body."), |
| 1127 |
Body = <<0:8000000>>, |
| 1128 |
{200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config), |
| 1129 |
true = lists:keymember(<<"content-type">>, 1, Headers1), |
| 1130 |
{201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config), |
| 1131 |
true = lists:keymember(<<"content-type">>, 1, Headers2), |
| 1132 |
{404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config), |
| 1133 |
true = lists:keymember(<<"content-type">>, 1, Headers3), |
| 1134 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/stream_reply3/error", Config)), |
| 1135 |
%% The set-cookie header is special. set_resp_cookie must be used. |
| 1136 |
{500, _, _} = do_maybe_h3_error3(do_get("/resp/stream_reply3/set_cookie", Config)), |
| 1137 |
ok. |
| 1138 |
|
| 1139 |
stream_body_fin0(Config) -> |
| 1140 |
doc("Streamed body with last chunk of size 0."), |
| 1141 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/fin0", Config), |
| 1142 |
ok. |
| 1143 |
|
| 1144 |
stream_body_multiple(Config) -> |
| 1145 |
doc("Streamed body via multiple calls."), |
| 1146 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/multiple", Config), |
| 1147 |
ok. |
| 1148 |
|
| 1149 |
stream_body_loop(Config) -> |
| 1150 |
doc("Streamed body via a fast loop."), |
| 1151 |
{200, _, <<0:32000000/unit:8>>} = do_get("/resp/stream_body/loop", Config), |
| 1152 |
ok. |
| 1153 |
|
| 1154 |
stream_body_nofin(Config) -> |
| 1155 |
doc("Unfinished streamed body."), |
| 1156 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/nofin", Config), |
| 1157 |
ok. |
| 1158 |
|
| 1159 |
stream_body_sendfile(Config) -> |
| 1160 |
doc("Streamed body via multiple calls, including sendfile calls."), |
| 1161 |
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")), |
| 1162 |
ExpectedBody = iolist_to_binary([ |
| 1163 |
<<"Hello ">>, |
| 1164 |
AppFile, |
| 1165 |
<<" interspersed ">>, |
| 1166 |
AppFile, |
| 1167 |
<<" world!">> |
| 1168 |
]), |
| 1169 |
{200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile", Config), |
| 1170 |
ok. |
| 1171 |
|
| 1172 |
stream_body_sendfile_fin(Config) -> |
| 1173 |
doc("Streamed body via multiple calls, including a sendfile final call."), |
| 1174 |
{ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")), |
| 1175 |
ExpectedBody = iolist_to_binary([ |
| 1176 |
<<"Hello! ">>, |
| 1177 |
AppFile |
| 1178 |
]), |
| 1179 |
{200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile_fin", Config), |
| 1180 |
ok. |
| 1181 |
|
| 1182 |
stream_body_spawn(Config) -> |
| 1183 |
doc("Confirm we can use cowboy_req:stream_body/3 from another process."), |
| 1184 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/spawn", Config), |
| 1185 |
ok. |
| 1186 |
|
| 1187 |
stream_body_content_length_multiple(Config) -> |
| 1188 |
doc("Streamed body via multiple calls."), |
| 1189 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config), |
| 1190 |
ok. |
| 1191 |
|
| 1192 |
stream_body_content_length_fin0(Config) -> |
| 1193 |
doc("Streamed body with last chunk of size 0."), |
| 1194 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/fin0", Config), |
| 1195 |
ok. |
| 1196 |
|
| 1197 |
stream_body_content_length_nofin(Config) -> |
| 1198 |
doc("Unfinished streamed body."), |
| 1199 |
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/nofin", Config), |
| 1200 |
ok. |
| 1201 |
|
| 1202 |
stream_body_content_length_nofin_error(Config) -> |
| 1203 |
doc("Not all of the response body sent."), |
| 1204 |
case config(protocol, Config) of |
| 1205 |
http -> |
| 1206 |
case do_get_error("/resp/stream_body_content_length/nofin-error", Config) of |
| 1207 |
%% When compression is used content-length is not sent. |
| 1208 |
{200, Headers, <<"Hello">>} -> |
| 1209 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers); |
| 1210 |
%% The server closes the connection when the body couldn't be sent fully. |
| 1211 |
{error, {stream_error, closed}} -> |
| 1212 |
receive |
| 1213 |
{gun_down, ConnPid, _, _, _} -> |
| 1214 |
gun:close(ConnPid) |
| 1215 |
after 1000 -> |
| 1216 |
error(timeout) |
| 1217 |
end |
| 1218 |
end; |
| 1219 |
http2 -> |
| 1220 |
%% @todo HTTP/2 should have the same content-length checks. |
| 1221 |
{skip, "Implement the test for HTTP/2."}; |
| 1222 |
http3 -> |
| 1223 |
%% @todo HTTP/3 should have the same content-length checks. |
| 1224 |
{skip, "Implement the test for HTTP/3."} |
| 1225 |
end. |
| 1226 |
|
| 1227 |
stream_body_concurrent(Config) -> |
| 1228 |
ConnPid = gun_open(Config), |
| 1229 |
Ref1 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 1230 |
Ref2 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]), |
| 1231 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, infinity), |
| 1232 |
{ok, _} = gun:await_body(ConnPid, Ref1, infinity), |
| 1233 |
{response, nofin, 200, _} = gun:await(ConnPid, Ref2, infinity), |
| 1234 |
{ok, _} = gun:await_body(ConnPid, Ref2, infinity), |
| 1235 |
gun:close(ConnPid). |
| 1236 |
|
| 1237 |
%% @todo Crash when calling stream_body after the fin flag has been set. |
| 1238 |
%% @todo Crash when calling stream_body after calling reply. |
| 1239 |
%% @todo Crash when calling stream_body before calling stream_reply. |
| 1240 |
|
| 1241 |
stream_events_single(Config) -> |
| 1242 |
doc("Streamed event."), |
| 1243 |
{200, Headers, << |
| 1244 |
"event: add_comment\n" |
| 1245 |
"data: Comment text.\n" |
| 1246 |
"data: With many lines.\n" |
| 1247 |
"\n" |
| 1248 |
>>} = do_get("/resp/stream_events/single", Config), |
| 1249 |
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 1250 |
ok. |
| 1251 |
|
| 1252 |
stream_events_list(Config) -> |
| 1253 |
doc("Streamed list of events."), |
| 1254 |
{200, Headers, << |
| 1255 |
"event: add_comment\n" |
| 1256 |
"data: Comment text.\n" |
| 1257 |
"data: With many lines.\n" |
| 1258 |
"\n" |
| 1259 |
": Set retry higher\n" |
| 1260 |
": with many lines also.\n" |
| 1261 |
"retry: 10000\n" |
| 1262 |
"\n" |
| 1263 |
"id: 123\n" |
| 1264 |
"event: add_comment\n" |
| 1265 |
"data: Closing!\n" |
| 1266 |
"\n" |
| 1267 |
>>} = do_get("/resp/stream_events/list", Config), |
| 1268 |
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 1269 |
ok. |
| 1270 |
|
| 1271 |
stream_events_multiple(Config) -> |
| 1272 |
doc("Streamed events via multiple calls."), |
| 1273 |
{200, Headers, << |
| 1274 |
"event: add_comment\n" |
| 1275 |
"data: Comment text.\n" |
| 1276 |
"data: With many lines.\n" |
| 1277 |
"\n" |
| 1278 |
": Set retry higher\n" |
| 1279 |
": with many lines also.\n" |
| 1280 |
"retry: 10000\n" |
| 1281 |
"\n" |
| 1282 |
"id: 123\n" |
| 1283 |
"event: add_comment\n" |
| 1284 |
"data: Closing!\n" |
| 1285 |
"\n" |
| 1286 |
>>} = do_get("/resp/stream_events/multiple", Config), |
| 1287 |
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers), |
| 1288 |
ok. |
| 1289 |
|
| 1290 |
stream_trailers(Config) -> |
| 1291 |
doc("Stream body followed by trailer headers."), |
| 1292 |
{200, RespHeaders, <<"Hello world!">>, [ |
| 1293 |
{<<"grpc-status">>, <<"0">>} |
| 1294 |
]} = do_trailers("/resp/stream_trailers", Config), |
| 1295 |
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders), |
| 1296 |
ok. |
| 1297 |
|
| 1298 |
stream_trailers_large(Config) -> |
| 1299 |
doc("Stream large body followed by trailer headers."), |
| 1300 |
{200, RespHeaders, <<0:80000000>>, [ |
| 1301 |
{<<"grpc-status">>, <<"0">>} |
| 1302 |
]} = do_trailers("/resp/stream_trailers/large", Config), |
| 1303 |
{_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders), |
| 1304 |
ok. |
| 1305 |
|
| 1306 |
stream_trailers_no_te(Config) -> |
| 1307 |
doc("Stream body followed by trailer headers without a te header in the request."), |
| 1308 |
ConnPid = gun_open(Config), |
| 1309 |
Ref = gun:get(ConnPid, "/resp/stream_trailers", [ |
| 1310 |
{<<"accept-encoding">>, <<"gzip">>} |
| 1311 |
]), |
| 1312 |
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 1313 |
%% @todo Do we want to remove the trailer header automatically? |
| 1314 |
% false = lists:keyfind(<<"trailer">>, 1, RespHeaders), |
| 1315 |
{ok, RespBody} = gun:await_body(ConnPid, Ref, infinity), |
| 1316 |
<<"Hello world!">> = do_decode(RespHeaders, RespBody), |
| 1317 |
gun:close(ConnPid). |
| 1318 |
|
| 1319 |
stream_trailers_set_cookie(Config) -> |
| 1320 |
doc("Trying to send set-cookie in trailers should result in a crash."), |
| 1321 |
ConnPid = gun_open(Config), |
| 1322 |
Ref = gun:get(ConnPid, "/resp/stream_trailers/set_cookie", [ |
| 1323 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 1324 |
{<<"te">>, <<"trailers">>} |
| 1325 |
]), |
| 1326 |
Protocol = config(protocol, Config), |
| 1327 |
case gun:await(ConnPid, Ref, infinity) of |
| 1328 |
{response, nofin, 200, _} when Protocol =:= http -> |
| 1329 |
%% Trailers are not sent because of the stream error. |
| 1330 |
{ok, _Body} = gun:await_body(ConnPid, Ref, infinity), |
| 1331 |
{error, timeout} = gun:await_body(ConnPid, Ref, 1000), |
| 1332 |
ok; |
| 1333 |
{response, nofin, 200, _} when Protocol =:= http2 -> |
| 1334 |
{error, {stream_error, {stream_error, internal_error, _}}} |
| 1335 |
= gun:await_body(ConnPid, Ref, infinity), |
| 1336 |
ok; |
| 1337 |
{response, nofin, 200, _} when Protocol =:= http3 -> |
| 1338 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} |
| 1339 |
= gun:await_body(ConnPid, Ref, infinity), |
| 1340 |
ok; |
| 1341 |
%% The RST_STREAM arrived before the start of the response. |
| 1342 |
%% See maybe_h3_error comment for details. |
| 1343 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} when Protocol =:= http3 -> |
| 1344 |
ok |
| 1345 |
end, |
| 1346 |
gun:close(ConnPid). |
| 1347 |
|
| 1348 |
do_trailers(Path, Config) -> |
| 1349 |
ConnPid = gun_open(Config), |
| 1350 |
Ref = gun:get(ConnPid, Path, [ |
| 1351 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 1352 |
{<<"te">>, <<"trailers">>} |
| 1353 |
]), |
| 1354 |
{response, nofin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity), |
| 1355 |
{ok, RespBody, Trailers} = gun:await_body(ConnPid, Ref, infinity), |
| 1356 |
gun:close(ConnPid), |
| 1357 |
{Status, RespHeaders, do_decode(RespHeaders, RespBody), Trailers}. |
| 1358 |
|
| 1359 |
%% @todo Crash when calling stream_trailers twice. |
| 1360 |
%% @todo Crash when calling stream_trailers after the fin flag has been set. |
| 1361 |
%% @todo Crash when calling stream_trailers after calling reply. |
| 1362 |
%% @todo Crash when calling stream_trailers before calling stream_reply. |
| 1363 |
|
| 1364 |
%% Tests: Push. |
| 1365 |
|
| 1366 |
%% @todo We want to crash when push is called after reply has been initiated. |
| 1367 |
|
| 1368 |
push(Config) -> |
| 1369 |
case config(protocol, Config) of |
| 1370 |
http -> do_push_http("/resp/push", Config); |
| 1371 |
http2 -> do_push_http2(Config); |
| 1372 |
http3 -> {skip, "Implement server push for HTTP/3."} |
| 1373 |
end. |
| 1374 |
|
| 1375 |
push_after_reply(Config) -> |
| 1376 |
doc("Trying to push a response after the final response results in a crash."), |
| 1377 |
ConnPid = gun_open(Config), |
| 1378 |
Ref = gun:get(ConnPid, "/resp/push/after_reply", []), |
| 1379 |
%% With HTTP/1.1 and HTTP/2 we will not get an error. |
| 1380 |
%% With HTTP/3 however the stream will occasionally |
| 1381 |
%% be reset before Gun receives the response. |
| 1382 |
case gun:await(ConnPid, Ref, infinity) of |
| 1383 |
{response, fin, 200, _} -> |
| 1384 |
ok; |
| 1385 |
{error, {stream_error, {stream_error, h3_internal_error, _}}} -> |
| 1386 |
ok |
| 1387 |
end, |
| 1388 |
gun:close(ConnPid). |
| 1389 |
|
| 1390 |
push_method(Config) -> |
| 1391 |
case config(protocol, Config) of |
| 1392 |
http -> do_push_http("/resp/push/method", Config); |
| 1393 |
http2 -> do_push_http2_method(Config); |
| 1394 |
http3 -> {skip, "Implement server push for HTTP/3."} |
| 1395 |
end. |
| 1396 |
|
| 1397 |
%% @todo Using custom origin currently disabled in Gun. |
| 1398 |
%% Reenable when Gun supports multiple authoritative origins. |
| 1399 |
%push_origin(Config) -> |
| 1400 |
% case config(protocol, Config) of |
| 1401 |
% http -> do_push_http("/resp/push/origin", Config); |
| 1402 |
% http2 -> do_push_http2_origin(Config); |
| 1403 |
% http3 -> {skip, "Implement server push for HTTP/3."} |
| 1404 |
% end. |
| 1405 |
|
| 1406 |
push_qs(Config) -> |
| 1407 |
case config(protocol, Config) of |
| 1408 |
http -> do_push_http("/resp/push/qs", Config); |
| 1409 |
http2 -> do_push_http2_qs(Config); |
| 1410 |
http3 -> {skip, "Implement server push for HTTP/3."} |
| 1411 |
end. |
| 1412 |
|
| 1413 |
do_push_http(Path, Config) -> |
| 1414 |
doc("Ignore pushed responses when protocol is HTTP/1.1."), |
| 1415 |
ConnPid = gun_open(Config), |
| 1416 |
Ref = gun:get(ConnPid, Path, []), |
| 1417 |
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1418 |
gun:close(ConnPid). |
| 1419 |
|
| 1420 |
do_push_http2(Config) -> |
| 1421 |
doc("Pushed responses."), |
| 1422 |
ConnPid = gun_open(Config), |
| 1423 |
Ref = gun:get(ConnPid, "/resp/push", []), |
| 1424 |
%% We expect two pushed resources. |
| 1425 |
Origin = iolist_to_binary([ |
| 1426 |
case config(type, Config) of |
| 1427 |
tcp -> "http"; |
| 1428 |
ssl -> "https" |
| 1429 |
end, |
| 1430 |
"://localhost:", |
| 1431 |
integer_to_binary(config(port, Config)) |
| 1432 |
]), |
| 1433 |
OriginLen = byte_size(Origin), |
| 1434 |
{push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>, |
| 1435 |
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity), |
| 1436 |
{push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>, |
| 1437 |
[{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref, infinity), |
| 1438 |
%% Pushed CSS. |
| 1439 |
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity), |
| 1440 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS), |
| 1441 |
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity), |
| 1442 |
%% Pushed TXT is 406 because the pushed accept header uses an undefined type. |
| 1443 |
{response, fin, 406, _} = gun:await(ConnPid, PushTXT, infinity), |
| 1444 |
%% Let's not forget about the response to the client's request. |
| 1445 |
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1446 |
gun:close(ConnPid). |
| 1447 |
|
| 1448 |
do_push_http2_method(Config) -> |
| 1449 |
doc("Pushed response with non-GET method."), |
| 1450 |
ConnPid = gun_open(Config), |
| 1451 |
Ref = gun:get(ConnPid, "/resp/push/method", []), |
| 1452 |
%% Pushed CSS. |
| 1453 |
{push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity), |
| 1454 |
{response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity), |
| 1455 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS), |
| 1456 |
%% Let's not forget about the response to the client's request. |
| 1457 |
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1458 |
gun:close(ConnPid). |
| 1459 |
|
| 1460 |
do_push_http2_origin(Config) -> |
| 1461 |
doc("Pushed response with custom scheme/host/port."), |
| 1462 |
ConnPid = gun_open(Config), |
| 1463 |
Ref = gun:get(ConnPid, "/resp/push/origin", []), |
| 1464 |
%% Pushed CSS. |
| 1465 |
{push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>, |
| 1466 |
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity), |
| 1467 |
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity), |
| 1468 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS), |
| 1469 |
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity), |
| 1470 |
%% Let's not forget about the response to the client's request. |
| 1471 |
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1472 |
gun:close(ConnPid). |
| 1473 |
|
| 1474 |
do_push_http2_qs(Config) -> |
| 1475 |
doc("Pushed response with query string."), |
| 1476 |
ConnPid = gun_open(Config), |
| 1477 |
Ref = gun:get(ConnPid, "/resp/push/qs", []), |
| 1478 |
%% Pushed CSS. |
| 1479 |
Origin = iolist_to_binary([ |
| 1480 |
case config(type, Config) of |
| 1481 |
tcp -> "http"; |
| 1482 |
ssl -> "https" |
| 1483 |
end, |
| 1484 |
"://localhost:", |
| 1485 |
integer_to_binary(config(port, Config)) |
| 1486 |
]), |
| 1487 |
OriginLen = byte_size(Origin), |
| 1488 |
{push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>, |
| 1489 |
[{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity), |
| 1490 |
{response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity), |
| 1491 |
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS), |
| 1492 |
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity), |
| 1493 |
%% Let's not forget about the response to the client's request. |
| 1494 |
{response, fin, 200, _} = gun:await(ConnPid, Ref, infinity), |
| 1495 |
gun:close(ConnPid). |