req_SUITE.erl 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. %% Copyright (c) 2016-2017, Loïc Hoguin <essen@ninenines.eu>
  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. -module(req_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. %% ct.
  21. all() ->
  22. cowboy_test:common_all().
  23. groups() ->
  24. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  25. init_per_suite(Config) ->
  26. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  27. Config.
  28. end_per_suite(Config) ->
  29. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  30. init_per_group(Name, Config) ->
  31. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  32. end_per_group(Name, _) ->
  33. cowboy:stop_listener(Name).
  34. %% Routes.
  35. init_dispatch(Config) ->
  36. cowboy_router:compile([{"[...]", [
  37. {"/static/[...]", cowboy_static, {dir, config(priv_dir, Config) ++ "/static"}},
  38. %% @todo Seriously InitialState should be optional.
  39. {"/resp/:key[/:arg]", resp_h, []},
  40. {"/multipart[/:key]", multipart_h, []},
  41. {"/args/:key/:arg[/:default]", echo_h, []},
  42. {"/crash/:key/period", echo_h, #{length => 999999999, period => 1000, crash => true}},
  43. {"/no-opts/:key", echo_h, #{crash => true}},
  44. {"/opts/:key/length", echo_h, #{length => 1000}},
  45. {"/opts/:key/period", echo_h, #{length => 999999999, period => 1000}},
  46. {"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
  47. {"/100-continue/:key", echo_h, []},
  48. {"/full/:key", echo_h, []},
  49. {"/no/:key", echo_h, []},
  50. {"/direct/:key/[...]", echo_h, []},
  51. {"/:key/[...]", echo_h, []}
  52. ]}]).
  53. %% Internal.
  54. do_body(Method, Path, Config) ->
  55. do_body(Method, Path, [], Config).
  56. do_body(Method, Path, Headers, Config) ->
  57. do_body(Method, Path, Headers, <<>>, Config).
  58. do_body(Method, Path, Headers0, Body, Config) ->
  59. ConnPid = gun_open(Config),
  60. Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
  61. Ref = case Body of
  62. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  63. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  64. end,
  65. {response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref, 10000),
  66. {ok, RespBody} = case IsFin of
  67. nofin -> gun:await_body(ConnPid, Ref);
  68. fin -> {ok, <<>>}
  69. end,
  70. gun:close(ConnPid),
  71. do_decode(RespHeaders, RespBody).
  72. do_body_error(Method, Path, Headers0, Body, Config) ->
  73. ConnPid = gun_open(Config),
  74. Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
  75. Ref = case Body of
  76. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  77. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  78. end,
  79. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  80. gun:close(ConnPid),
  81. {Status, RespHeaders}.
  82. do_get(Path, Config) ->
  83. do_get(Path, [], Config).
  84. do_get(Path, Headers, Config) ->
  85. ConnPid = gun_open(Config),
  86. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]),
  87. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  88. {ok, RespBody} = case IsFin of
  89. nofin -> gun:await_body(ConnPid, Ref);
  90. fin -> {ok, <<>>}
  91. end,
  92. gun:close(ConnPid),
  93. {Status, RespHeaders, do_decode(RespHeaders, RespBody)}.
  94. do_get_body(Path, Config) ->
  95. do_get_body(Path, [], Config).
  96. do_get_body(Path, Headers, Config) ->
  97. do_body("GET", Path, Headers, Config).
  98. do_get_inform(Path, Config) ->
  99. ConnPid = gun_open(Config),
  100. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  101. case gun:await(ConnPid, Ref) of
  102. {response, _, RespStatus, RespHeaders} ->
  103. %% We don't care about the body.
  104. gun:close(ConnPid),
  105. {RespStatus, RespHeaders};
  106. {inform, InfoStatus, InfoHeaders} ->
  107. {response, IsFin, RespStatus, RespHeaders}
  108. = case gun:await(ConnPid, Ref) of
  109. {inform, InfoStatus, InfoHeaders} ->
  110. gun:await(ConnPid, Ref);
  111. Response ->
  112. Response
  113. end,
  114. {ok, RespBody} = case IsFin of
  115. nofin -> gun:await_body(ConnPid, Ref);
  116. fin -> {ok, <<>>}
  117. end,
  118. gun:close(ConnPid),
  119. {InfoStatus, InfoHeaders, RespStatus, RespHeaders, do_decode(RespHeaders, RespBody)}
  120. end.
  121. do_decode(Headers, Body) ->
  122. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  123. {_, <<"gzip">>} -> zlib:gunzip(Body);
  124. _ -> Body
  125. end.
  126. do_get_error(Path, Config) ->
  127. do_get_error(Path, [], Config).
  128. do_get_error(Path, Headers, Config) ->
  129. ConnPid = gun_open(Config),
  130. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]),
  131. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  132. Result = case IsFin of
  133. nofin -> gun:await_body(ConnPid, Ref);
  134. fin -> {ok, <<>>}
  135. end,
  136. gun:close(ConnPid),
  137. case Result of
  138. {ok, RespBody} -> {Status, RespHeaders, do_decode(RespHeaders, RespBody)};
  139. _ -> Result
  140. end.
  141. %% Tests: Request.
  142. binding(Config) ->
  143. doc("Value bound from request URI path with/without default."),
  144. <<"binding">> = do_get_body("/args/binding/key", Config),
  145. <<"binding">> = do_get_body("/args/binding/key/default", Config),
  146. <<"default">> = do_get_body("/args/binding/undefined/default", Config),
  147. ok.
  148. bindings(Config) ->
  149. doc("Values bound from request URI path."),
  150. <<"#{key => <<\"bindings\">>}">> = do_get_body("/bindings", Config),
  151. ok.
  152. cert(Config) ->
  153. case config(type, Config) of
  154. tcp -> doc("TLS certificates can only be provided over TLS.");
  155. ssl -> do_cert(Config)
  156. end.
  157. do_cert(Config0) ->
  158. doc("A client TLS certificate was provided."),
  159. {CaCert, Cert, Key} = ct_helper:make_certs(),
  160. Config = [{transport_opts, [
  161. {cert, Cert},
  162. {key, Key},
  163. {cacerts, [CaCert]}
  164. ]}|Config0],
  165. Cert = do_get_body("/cert", Config),
  166. Cert = do_get_body("/direct/cert", Config),
  167. ok.
  168. cert_undefined(Config) ->
  169. doc("No client TLS certificate was provided."),
  170. <<"undefined">> = do_get_body("/cert", Config),
  171. <<"undefined">> = do_get_body("/direct/cert", Config),
  172. ok.
  173. header(Config) ->
  174. doc("Request header with/without default."),
  175. <<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config),
  176. <<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config),
  177. <<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config),
  178. ok.
  179. headers(Config) ->
  180. doc("Request headers."),
  181. do_headers("/headers", Config),
  182. do_headers("/direct/headers", Config).
  183. do_headers(Path, Config) ->
  184. %% We always send accept-encoding with this test suite's requests.
  185. <<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
  186. = do_get_body(Path, [{<<"header">>, "value"}], Config),
  187. ok.
  188. host(Config) ->
  189. doc("Request URI host."),
  190. <<"localhost">> = do_get_body("/host", Config),
  191. <<"localhost">> = do_get_body("/direct/host", Config),
  192. ok.
  193. host_info(Config) ->
  194. doc("Request host_info."),
  195. <<"[<<\"localhost\">>]">> = do_get_body("/host_info", Config),
  196. ok.
  197. %% @todo Actually write the related unit tests.
  198. match_cookies(Config) ->
  199. doc("Matched request cookies."),
  200. <<"#{}">> = do_get_body("/match/cookies", [{<<"cookie">>, "a=b; c=d"}], Config),
  201. <<"#{a => <<\"b\">>}">> = do_get_body("/match/cookies/a", [{<<"cookie">>, "a=b; c=d"}], Config),
  202. <<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
  203. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/cookies/a/c",
  204. [{<<"cookie">>, "a=b; c=d"}], Config),
  205. %% Ensure match errors result in a 400 response.
  206. {400, _, _} = do_get("/match/cookies/a/c",
  207. [{<<"cookie">>, "a=b"}], Config),
  208. %% This function is tested more extensively through unit tests.
  209. ok.
  210. %% @todo Actually write the related unit tests.
  211. match_qs(Config) ->
  212. doc("Matched request URI query string."),
  213. <<"#{}">> = do_get_body("/match/qs?a=b&c=d", Config),
  214. <<"#{a => <<\"b\">>}">> = do_get_body("/match/qs/a?a=b&c=d", Config),
  215. <<"#{c => <<\"d\">>}">> = do_get_body("/match/qs/c?a=b&c=d", Config),
  216. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a=b&c=d", Config),
  217. <<"#{a => <<\"b\">>,c => true}">> = do_get_body("/match/qs/a/c?a=b&c", Config),
  218. <<"#{a => true,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a&c=d", Config),
  219. %% Ensure match errors result in a 400 response.
  220. {400, _, _} = do_get("/match/qs/a/c?a=b", [], Config),
  221. %% This function is tested more extensively through unit tests.
  222. ok.
  223. method(Config) ->
  224. doc("Request method."),
  225. do_method("/method", Config),
  226. do_method("/direct/method", Config).
  227. do_method(Path, Config) ->
  228. <<"GET">> = do_body("GET", Path, Config),
  229. <<>> = do_body("HEAD", Path, Config),
  230. <<"OPTIONS">> = do_body("OPTIONS", Path, Config),
  231. <<"PATCH">> = do_body("PATCH", Path, Config),
  232. <<"POST">> = do_body("POST", Path, Config),
  233. <<"PUT">> = do_body("PUT", Path, Config),
  234. <<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", Path, Config),
  235. ok.
  236. parse_cookies(Config) ->
  237. doc("Request cookies."),
  238. <<"[]">> = do_get_body("/parse_cookies", Config),
  239. <<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
  240. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config),
  241. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  242. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config),
  243. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  244. = do_get_body("/parse_cookies",
  245. [{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config),
  246. %% Ensure parse errors result in a 400 response.
  247. {400, _, _} = do_get("/parse_cookies",
  248. [{<<"cookie">>, "bad name=strawberry"}], Config),
  249. {400, _, _} = do_get("/parse_cookies",
  250. [{<<"cookie">>, "goodname=strawberry\tmilkshake"}], Config),
  251. ok.
  252. parse_header(Config) ->
  253. doc("Parsed request header with/without default."),
  254. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  255. = do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config),
  256. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  257. = do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config),
  258. %% Header not in request but with default defined by Cowboy.
  259. <<"0">> = do_get_body("/args/parse_header/content-length", Config),
  260. %% Header not in request and no default from Cowboy.
  261. <<"undefined">> = do_get_body("/args/parse_header/upgrade", Config),
  262. %% Header in request and with default provided.
  263. <<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config),
  264. %% Ensure parse errors result in a 400 response.
  265. {400, _, _} = do_get("/args/parse_header/accept",
  266. [{<<"accept">>, "bad media type"}], Config),
  267. ok.
  268. parse_qs(Config) ->
  269. doc("Parsed request URI query string."),
  270. <<"[]">> = do_get_body("/parse_qs", Config),
  271. <<"[{<<\"abc\">>,true}]">> = do_get_body("/parse_qs?abc", Config),
  272. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> = do_get_body("/parse_qs?a=b&c=d+e", Config),
  273. %% Ensure parse errors result in a 400 response.
  274. {400, _, _} = do_get("/parse_qs?%%%%%%%", Config),
  275. ok.
  276. path(Config) ->
  277. doc("Request URI path."),
  278. do_path("/path", Config),
  279. do_path("/direct/path", Config).
  280. do_path(Path0, Config) ->
  281. Path = list_to_binary(Path0 ++ "/to/the/resource"),
  282. Path = do_get_body(Path, Config),
  283. Path = do_get_body([Path, "?query"], Config),
  284. Path = do_get_body([Path, "?query#fragment"], Config),
  285. Path = do_get_body([Path, "#fragment"], Config),
  286. ok.
  287. path_info(Config) ->
  288. doc("Request path_info."),
  289. <<"undefined">> = do_get_body("/no/path_info", Config),
  290. <<"[]">> = do_get_body("/path_info", Config),
  291. <<"[]">> = do_get_body("/path_info/", Config),
  292. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource", Config),
  293. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query", Config),
  294. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query#fragment", Config),
  295. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource#fragment", Config),
  296. ok.
  297. peer(Config) ->
  298. doc("Remote socket address."),
  299. <<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
  300. <<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
  301. ok.
  302. port(Config) ->
  303. doc("Request URI port."),
  304. Port = integer_to_binary(config(port, Config)),
  305. Port = do_get_body("/port", Config),
  306. Port = do_get_body("/direct/port", Config),
  307. ExpectedPort = case config(type, Config) of
  308. tcp -> <<"80">>;
  309. ssl -> <<"443">>
  310. end,
  311. ExpectedPort = do_get_body("/port", [{<<"host">>, <<"localhost">>}], Config),
  312. ExpectedPort = do_get_body("/direct/port", [{<<"host">>, <<"localhost">>}], Config),
  313. ok.
  314. qs(Config) ->
  315. doc("Request URI query string."),
  316. do_qs("/qs", Config),
  317. do_qs("/direct/qs", Config).
  318. do_qs(Path, Config) ->
  319. <<>> = do_get_body(Path, Config),
  320. <<"abc">> = do_get_body(Path ++ "?abc", Config),
  321. <<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config),
  322. ok.
  323. scheme(Config) ->
  324. doc("Request URI scheme."),
  325. do_scheme("/scheme", Config),
  326. do_scheme("/direct/scheme", Config).
  327. do_scheme(Path, Config) ->
  328. Transport = config(type, Config),
  329. case do_get_body(Path, Config) of
  330. <<"http">> when Transport =:= tcp -> ok;
  331. <<"https">> when Transport =:= ssl -> ok
  332. end.
  333. sock(Config) ->
  334. doc("Local socket address."),
  335. <<"{{127,0,0,1},", _/bits >> = do_get_body("/sock", Config),
  336. <<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/sock", Config),
  337. ok.
  338. uri(Config) ->
  339. doc("Request URI building/modification."),
  340. Scheme = case config(type, Config) of
  341. tcp -> <<"http">>;
  342. ssl -> <<"https">>
  343. end,
  344. SLen = byte_size(Scheme),
  345. Port = integer_to_binary(config(port, Config)),
  346. PLen = byte_size(Port),
  347. %% Absolute form.
  348. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >>
  349. = do_get_body("/uri?qs", Config),
  350. %% Origin form.
  351. << "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config),
  352. %% Protocol relative.
  353. << "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >>
  354. = do_get_body("/uri/protocol-relative?qs", Config),
  355. %% No query string.
  356. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >>
  357. = do_get_body("/uri/no-qs?qs", Config),
  358. %% No path or query string.
  359. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary >>
  360. = do_get_body("/uri/no-path?qs", Config),
  361. %% Changed port.
  362. << Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >>
  363. = do_get_body("/uri/set-port?qs", Config),
  364. %% This function is tested more extensively through unit tests.
  365. ok.
  366. version(Config) ->
  367. doc("Request HTTP version."),
  368. do_version("/version", Config),
  369. do_version("/direct/version", Config).
  370. do_version(Path, Config) ->
  371. Protocol = config(protocol, Config),
  372. case do_get_body(Path, Config) of
  373. <<"HTTP/1.1">> when Protocol =:= http -> ok;
  374. <<"HTTP/2">> when Protocol =:= http2 -> ok
  375. end.
  376. %% Tests: Request body.
  377. body_length(Config) ->
  378. doc("Request body length."),
  379. <<"0">> = do_get_body("/body_length", Config),
  380. <<"12">> = do_body("POST", "/body_length", [], "hello world!", Config),
  381. ok.
  382. has_body(Config) ->
  383. doc("Has a request body?"),
  384. <<"false">> = do_get_body("/has_body", Config),
  385. <<"true">> = do_body("POST", "/has_body", [], "hello world!", Config),
  386. ok.
  387. read_body(Config) ->
  388. doc("Request body."),
  389. <<>> = do_get_body("/read_body", Config),
  390. <<"hello world!">> = do_body("POST", "/read_body", [], "hello world!", Config),
  391. %% We expect to have read *at least* 1000 bytes.
  392. <<0:8000, _/bits>> = do_body("POST", "/opts/read_body/length", [], <<0:8000000>>, Config),
  393. %% The timeout value is set too low on purpose to ensure a crash occurs.
  394. ok = do_read_body_timeout("/opts/read_body/timeout", <<0:8000000>>, Config),
  395. %% 10MB body larger than default length.
  396. <<0:80000000>> = do_body("POST", "/full/read_body", [], <<0:80000000>>, Config),
  397. ok.
  398. read_body_mtu(Config) ->
  399. doc("Request body whose sizes are around the MTU."),
  400. MTU = ct_helper:get_loopback_mtu(),
  401. _ = [begin
  402. Body = <<0:Size/unit:8>>,
  403. Body = do_body("POST", "/full/read_body", [], Body, Config)
  404. end || Size <- lists:seq(MTU - 10, MTU + 10)],
  405. ok.
  406. read_body_period(Config) ->
  407. doc("Read the request body for at most 1 second."),
  408. ConnPid = gun_open(Config),
  409. Body = <<0:8000000>>,
  410. Ref = gun:request(ConnPid, "POST", "/opts/read_body/period", [
  411. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  412. ]),
  413. %% The body is sent twice, first with nofin, then wait 2 seconds, then again with fin.
  414. gun:data(ConnPid, Ref, nofin, Body),
  415. timer:sleep(2000),
  416. gun:data(ConnPid, Ref, fin, Body),
  417. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  418. {ok, Body} = gun:await_body(ConnPid, Ref),
  419. gun:close(ConnPid).
  420. %% We expect a crash.
  421. do_read_body_timeout(Path, Body, Config) ->
  422. ConnPid = gun_open(Config),
  423. Ref = gun:request(ConnPid, "POST", Path, [
  424. {<<"content-length">>, integer_to_binary(byte_size(Body))}
  425. ]),
  426. {response, _, 500, _} = gun:await(ConnPid, Ref),
  427. gun:close(ConnPid).
  428. read_body_expect_100_continue(Config) ->
  429. doc("Request body with a 100-continue expect header."),
  430. do_read_body_expect_100_continue("/read_body", Config).
  431. read_body_expect_100_continue_user_sent(Config) ->
  432. doc("Request body with a 100-continue expect header, 100 response sent by handler."),
  433. do_read_body_expect_100_continue("/100-continue/read_body", Config).
  434. do_read_body_expect_100_continue(Path, Config) ->
  435. ConnPid = gun_open(Config),
  436. Body = <<0:8000000>>,
  437. Headers = [
  438. {<<"accept-encoding">>, <<"gzip">>},
  439. {<<"expect">>, <<"100-continue">>},
  440. {<<"content-length">>, integer_to_binary(byte_size(Body))}
  441. ],
  442. Ref = gun:post(ConnPid, Path, Headers),
  443. {inform, 100, []} = gun:await(ConnPid, Ref),
  444. gun:data(ConnPid, Ref, fin, Body),
  445. {response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  446. {ok, RespBody} = case IsFin of
  447. nofin -> gun:await_body(ConnPid, Ref);
  448. fin -> {ok, <<>>}
  449. end,
  450. gun:close(ConnPid),
  451. do_decode(RespHeaders, RespBody).
  452. read_urlencoded_body(Config) ->
  453. doc("application/x-www-form-urlencoded request body."),
  454. <<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
  455. <<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
  456. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
  457. = do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
  458. %% Send a 10MB body, larger than the default length, to ensure a crash occurs.
  459. ok = do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
  460. string:chars($a, 10000000), Config),
  461. %% We read any length for at most 1 second.
  462. %%
  463. %% The body is sent twice, first with nofin, then wait 1.1 second, then again with fin.
  464. %% We expect the handler to crash because read_urlencoded_body expects the full body.
  465. ok = do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config),
  466. %% The timeout value is set too low on purpose to ensure a crash occurs.
  467. ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
  468. %% Ensure parse errors result in a 400 response.
  469. {400, _} = do_body_error("POST", "/read_urlencoded_body", [], "%%%%%", Config),
  470. ok.
  471. %% We expect a crash.
  472. do_read_urlencoded_body_too_large(Path, Body, Config) ->
  473. ConnPid = gun_open(Config),
  474. Ref = gun:request(ConnPid, "POST", Path, [
  475. {<<"content-length">>, integer_to_binary(iolist_size(Body))}
  476. ]),
  477. gun:data(ConnPid, Ref, fin, Body),
  478. {response, _, 413, _} = gun:await(ConnPid, Ref),
  479. gun:close(ConnPid).
  480. %% We expect a crash.
  481. do_read_urlencoded_body_too_long(Path, Body, Config) ->
  482. ConnPid = gun_open(Config),
  483. Ref = gun:request(ConnPid, "POST", Path, [
  484. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  485. ]),
  486. gun:data(ConnPid, Ref, nofin, Body),
  487. timer:sleep(1100),
  488. gun:data(ConnPid, Ref, fin, Body),
  489. {response, _, 408, RespHeaders} = gun:await(ConnPid, Ref),
  490. _ = case config(protocol, Config) of
  491. http ->
  492. %% 408 error responses should close HTTP/1.1 connections.
  493. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders);
  494. http2 ->
  495. ok
  496. end,
  497. gun:close(ConnPid).
  498. multipart(Config) ->
  499. doc("Multipart request body."),
  500. do_multipart("/multipart", Config).
  501. do_multipart(Path, Config) ->
  502. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  503. ReqBody = [
  504. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  505. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  506. "--deadbeef--"
  507. ],
  508. RespBody = do_body("POST", Path, [
  509. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  510. ], ReqBody, Config),
  511. [
  512. {#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
  513. {LargeHeaders, LargeBody}
  514. ] = binary_to_term(RespBody),
  515. #{
  516. <<"content-type">> := <<"application/octet-stream">>,
  517. <<"x-custom">> := <<"value">>
  518. } = LargeHeaders,
  519. ok.
  520. multipart_error_empty(Config) ->
  521. doc("Multipart request body is empty."),
  522. %% We use an empty list as a body to make sure Gun knows
  523. %% we want to send an empty body.
  524. %% @todo This is a terrible hack. Improve Gun!
  525. Body = [],
  526. %% Ensure an empty body results in a 400 error.
  527. {400, _} = do_body_error("POST", "/multipart", [
  528. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  529. ], Body, Config),
  530. ok.
  531. multipart_error_preamble_only(Config) ->
  532. doc("Multipart request body only contains a preamble."),
  533. %% Ensure an empty body results in a 400 error.
  534. {400, _} = do_body_error("POST", "/multipart", [
  535. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  536. ], <<"Preamble.">>, Config),
  537. ok.
  538. multipart_error_headers(Config) ->
  539. doc("Multipart request body with invalid part headers."),
  540. ReqBody = [
  541. "--deadbeef\r\nbad-header text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  542. "--deadbeef--"
  543. ],
  544. %% Ensure parse errors result in a 400 response.
  545. {400, _} = do_body_error("POST", "/multipart", [
  546. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  547. ], ReqBody, Config),
  548. ok.
  549. %% The function to parse the multipart body currently does not crash,
  550. %% as far as I can tell. There is therefore no test for it.
  551. multipart_error_no_final_boundary(Config) ->
  552. doc("Multipart request body with no final boundary."),
  553. ReqBody = [
  554. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  555. ],
  556. %% Ensure parse errors result in a 400 response.
  557. {400, _} = do_body_error("POST", "/multipart", [
  558. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  559. ], ReqBody, Config),
  560. ok.
  561. multipart_missing_boundary(Config) ->
  562. doc("Multipart request body without a boundary in the media type."),
  563. ReqBody = [
  564. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  565. "--deadbeef--"
  566. ],
  567. %% Ensure parse errors result in a 400 response.
  568. {400, _} = do_body_error("POST", "/multipart", [
  569. {<<"content-type">>, <<"multipart/mixed">>}
  570. ], ReqBody, Config),
  571. ok.
  572. read_part_skip_body(Config) ->
  573. doc("Multipart request body skipping part bodies."),
  574. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  575. ReqBody = [
  576. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  577. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  578. "--deadbeef--"
  579. ],
  580. RespBody = do_body("POST", "/multipart/skip_body", [
  581. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  582. ], ReqBody, Config),
  583. [
  584. #{<<"content-type">> := <<"text/plain">>},
  585. LargeHeaders
  586. ] = binary_to_term(RespBody),
  587. #{
  588. <<"content-type">> := <<"application/octet-stream">>,
  589. <<"x-custom">> := <<"value">>
  590. } = LargeHeaders,
  591. ok.
  592. %% @todo When reading a multipart body, length and period
  593. %% only apply to a single read_body call. We may want a
  594. %% separate option to know how many reads we want to do
  595. %% before we give up.
  596. read_part2(Config) ->
  597. doc("Multipart request body using read_part/2."),
  598. %% Override the length and period values only, making
  599. %% the request process use more read_body calls.
  600. %%
  601. %% We do not try a custom timeout value since this would
  602. %% be the same test as read_body/2.
  603. do_multipart("/multipart/read_part2", Config).
  604. read_part_body2(Config) ->
  605. doc("Multipart request body using read_part_body/2."),
  606. %% Override the length and period values only, making
  607. %% the request process use more read_body calls.
  608. %%
  609. %% We do not try a custom timeout value since this would
  610. %% be the same test as read_body/2.
  611. do_multipart("/multipart/read_part_body2", Config).
  612. %% Tests: Response.
  613. %% @todo We want to crash when calling set_resp_* or related
  614. %% functions after the reply has been sent.
  615. set_resp_cookie(Config) ->
  616. doc("Response using set_resp_cookie."),
  617. %% Single cookie, no options.
  618. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  619. {_, <<"mycookie=myvalue; Version=1">>}
  620. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  621. %% Single cookie, with options.
  622. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  623. {_, <<"mycookie=myvalue; Version=1; Path=/resp/set_resp_cookie4">>}
  624. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  625. %% Multiple cookies.
  626. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  627. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  628. %% Overwrite previously set cookie.
  629. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  630. {_, <<"mycookie=overwrite; Version=1">>}
  631. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  632. ok.
  633. set_resp_header(Config) ->
  634. doc("Response using set_resp_header."),
  635. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  636. true = lists:keymember(<<"content-type">>, 1, Headers),
  637. ok.
  638. set_resp_headers(Config) ->
  639. doc("Response using set_resp_headers."),
  640. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
  641. true = lists:keymember(<<"content-type">>, 1, Headers),
  642. true = lists:keymember(<<"content-encoding">>, 1, Headers),
  643. ok.
  644. resp_header(Config) ->
  645. doc("Response header with/without default."),
  646. {200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
  647. {200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
  648. ok.
  649. resp_headers(Config) ->
  650. doc("Get all response headers."),
  651. {200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
  652. {200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
  653. ok.
  654. set_resp_body(Config) ->
  655. doc("Response using set_resp_body."),
  656. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  657. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  658. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  659. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  660. ok.
  661. set_resp_body_sendfile0(Config) ->
  662. doc("Response using set_resp_body with a sendfile of length 0."),
  663. Path = "/resp/set_resp_body/sendfile0",
  664. ConnPid = gun_open(Config),
  665. %% First request.
  666. Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  667. {response, IsFin, 200, _} = gun:await(ConnPid, Ref1),
  668. {ok, <<>>} = case IsFin of
  669. nofin -> gun:await_body(ConnPid, Ref1);
  670. fin -> {ok, <<>>}
  671. end,
  672. %% Second request will confirm everything works as intended.
  673. Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  674. {response, IsFin, 200, _} = gun:await(ConnPid, Ref2),
  675. {ok, <<>>} = case IsFin of
  676. nofin -> gun:await_body(ConnPid, Ref2);
  677. fin -> {ok, <<>>}
  678. end,
  679. gun:close(ConnPid),
  680. ok.
  681. has_resp_header(Config) ->
  682. doc("Has response header?"),
  683. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  684. true = lists:keymember(<<"content-type">>, 1, Headers),
  685. ok.
  686. has_resp_body(Config) ->
  687. doc("Has response body?"),
  688. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  689. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  690. ok.
  691. delete_resp_header(Config) ->
  692. doc("Delete response header."),
  693. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  694. false = lists:keymember(<<"content-type">>, 1, Headers),
  695. ok.
  696. inform2(Config) ->
  697. doc("Informational response(s) without headers, followed by the real response."),
  698. {102, [], 200, _, _} = do_get_inform("/resp/inform2/102", Config),
  699. {102, [], 200, _, _} = do_get_inform("/resp/inform2/binary", Config),
  700. {500, _} = do_get_inform("/resp/inform2/error", Config),
  701. {102, [], 200, _, _} = do_get_inform("/resp/inform2/twice", Config),
  702. ok.
  703. inform3(Config) ->
  704. doc("Informational response(s) with headers, followed by the real response."),
  705. Headers = [{<<"ext-header">>, <<"ext-value">>}],
  706. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/102", Config),
  707. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/binary", Config),
  708. {500, _} = do_get_inform("/resp/inform3/error", Config),
  709. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/twice", Config),
  710. ok.
  711. reply2(Config) ->
  712. doc("Response with default headers and no body."),
  713. {200, _, _} = do_get("/resp/reply2/200", Config),
  714. {201, _, _} = do_get("/resp/reply2/201", Config),
  715. {404, _, _} = do_get("/resp/reply2/404", Config),
  716. {200, _, _} = do_get("/resp/reply2/binary", Config),
  717. {500, _, _} = do_get("/resp/reply2/error", Config),
  718. %% @todo We want to crash when reply or stream_reply is called twice.
  719. %% How to test this properly? This isn't enough.
  720. {200, _, _} = do_get("/resp/reply2/twice", Config),
  721. ok.
  722. reply3(Config) ->
  723. doc("Response with additional headers and no body."),
  724. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  725. true = lists:keymember(<<"content-type">>, 1, Headers1),
  726. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  727. true = lists:keymember(<<"content-type">>, 1, Headers2),
  728. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  729. true = lists:keymember(<<"content-type">>, 1, Headers3),
  730. {500, _, _} = do_get("/resp/reply3/error", Config),
  731. ok.
  732. reply4(Config) ->
  733. doc("Response with additional headers and body."),
  734. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  735. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  736. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  737. {500, _, _} = do_get("/resp/reply4/error", Config),
  738. ok.
  739. %% @todo Crash when stream_reply is called twice.
  740. stream_reply2(Config) ->
  741. doc("Response with default headers and streamed body."),
  742. Body = <<0:8000000>>,
  743. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  744. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  745. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  746. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  747. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  748. ok.
  749. stream_reply3(Config) ->
  750. doc("Response with additional headers and streamed body."),
  751. Body = <<0:8000000>>,
  752. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  753. true = lists:keymember(<<"content-type">>, 1, Headers1),
  754. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  755. true = lists:keymember(<<"content-type">>, 1, Headers2),
  756. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  757. true = lists:keymember(<<"content-type">>, 1, Headers3),
  758. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  759. ok.
  760. stream_body_multiple(Config) ->
  761. doc("Streamed body via multiple calls."),
  762. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/multiple", Config),
  763. ok.
  764. stream_body_fin0(Config) ->
  765. doc("Streamed body with last chunk of size 0."),
  766. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/fin0", Config),
  767. ok.
  768. stream_body_nofin(Config) ->
  769. doc("Unfinished streamed body."),
  770. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/nofin", Config),
  771. ok.
  772. stream_body_content_length_multiple(Config) ->
  773. doc("Streamed body via multiple calls."),
  774. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config),
  775. ok.
  776. stream_body_content_length_fin0(Config) ->
  777. doc("Streamed body with last chunk of size 0."),
  778. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/fin0", Config),
  779. ok.
  780. stream_body_content_length_nofin(Config) ->
  781. doc("Unfinished streamed body."),
  782. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/nofin", Config),
  783. ok.
  784. stream_body_content_length_nofin_error(Config) ->
  785. doc("Not all of body sent."),
  786. case config(protocol, Config) of
  787. http ->
  788. case do_get_error("/resp/stream_body_content_length/nofin-error", Config) of
  789. {200, Headers, <<"Hello">>} ->
  790. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
  791. {error, {closed, "The connection was lost."}} ->
  792. ok;
  793. {error, timeout} ->
  794. ok
  795. end;
  796. http2 ->
  797. %% @todo HTTP2 should have the same content-length checks
  798. ok
  799. end.
  800. %% @todo Crash when calling stream_body after the fin flag has been set.
  801. %% @todo Crash when calling stream_body after calling reply.
  802. %% @todo Crash when calling stream_body before calling stream_reply.
  803. stream_events_single(Config) ->
  804. doc("Streamed event."),
  805. {200, Headers, <<
  806. "event: add_comment\n"
  807. "data: Comment text.\n"
  808. "data: With many lines.\n"
  809. "\n"
  810. >>} = do_get("/resp/stream_events/single", Config),
  811. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  812. ok.
  813. stream_events_list(Config) ->
  814. doc("Streamed list of events."),
  815. {200, Headers, <<
  816. "event: add_comment\n"
  817. "data: Comment text.\n"
  818. "data: With many lines.\n"
  819. "\n"
  820. ": Set retry higher\n"
  821. ": with many lines also.\n"
  822. "retry: 10000\n"
  823. "\n"
  824. "id: 123\n"
  825. "event: add_comment\n"
  826. "data: Closing!\n"
  827. "\n"
  828. >>} = do_get("/resp/stream_events/list", Config),
  829. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  830. ok.
  831. stream_events_multiple(Config) ->
  832. doc("Streamed events via multiple calls."),
  833. {200, Headers, <<
  834. "event: add_comment\n"
  835. "data: Comment text.\n"
  836. "data: With many lines.\n"
  837. "\n"
  838. ": Set retry higher\n"
  839. ": with many lines also.\n"
  840. "retry: 10000\n"
  841. "\n"
  842. "id: 123\n"
  843. "event: add_comment\n"
  844. "data: Closing!\n"
  845. "\n"
  846. >>} = do_get("/resp/stream_events/multiple", Config),
  847. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  848. ok.
  849. stream_trailers(Config) ->
  850. doc("Stream body followed by trailer headers."),
  851. {200, RespHeaders, <<"Hello world!">>, [
  852. {<<"grpc-status">>, <<"0">>}
  853. ]} = do_trailers("/resp/stream_trailers", Config),
  854. {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  855. ok.
  856. stream_trailers_large(Config) ->
  857. doc("Stream large body followed by trailer headers."),
  858. {200, RespHeaders, <<0:800000>>, [
  859. {<<"grpc-status">>, <<"0">>}
  860. ]} = do_trailers("/resp/stream_trailers/large", Config),
  861. {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  862. ok.
  863. stream_trailers_no_te(Config) ->
  864. doc("Stream body followed by trailer headers without a te header in the request."),
  865. ConnPid = gun_open(Config),
  866. Ref = gun:get(ConnPid, "/resp/stream_trailers", [
  867. {<<"accept-encoding">>, <<"gzip">>}
  868. ]),
  869. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  870. %% @todo Do we want to remove the trailer header automatically?
  871. % false = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  872. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  873. <<"Hello world!">> = do_decode(RespHeaders, RespBody),
  874. gun:close(ConnPid).
  875. do_trailers(Path, Config) ->
  876. ConnPid = gun_open(Config),
  877. Ref = gun:get(ConnPid, Path, [
  878. {<<"accept-encoding">>, <<"gzip">>},
  879. {<<"te">>, <<"trailers">>}
  880. ]),
  881. {response, nofin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  882. {ok, RespBody, Trailers} = gun:await_body(ConnPid, Ref),
  883. gun:close(ConnPid),
  884. {Status, RespHeaders, do_decode(RespHeaders, RespBody), Trailers}.
  885. %% @todo Crash when calling stream_trailers twice.
  886. %% @todo Crash when calling stream_trailers after the fin flag has been set.
  887. %% @todo Crash when calling stream_trailers after calling reply.
  888. %% @todo Crash when calling stream_trailers before calling stream_reply.
  889. %% Tests: Push.
  890. %% @todo We want to crash when push is called after reply has been initiated.
  891. push(Config) ->
  892. case config(protocol, Config) of
  893. http -> do_push_http("/resp/push", Config);
  894. http2 -> do_push_http2(Config)
  895. end.
  896. push_method(Config) ->
  897. case config(protocol, Config) of
  898. http -> do_push_http("/resp/push/method", Config);
  899. http2 -> do_push_http2_method(Config)
  900. end.
  901. push_origin(Config) ->
  902. case config(protocol, Config) of
  903. http -> do_push_http("/resp/push/origin", Config);
  904. http2 -> do_push_http2_origin(Config)
  905. end.
  906. push_qs(Config) ->
  907. case config(protocol, Config) of
  908. http -> do_push_http("/resp/push/qs", Config);
  909. http2 -> do_push_http2_qs(Config)
  910. end.
  911. do_push_http(Path, Config) ->
  912. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  913. ConnPid = gun_open(Config),
  914. Ref = gun:get(ConnPid, Path, []),
  915. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  916. ok.
  917. do_push_http2(Config) ->
  918. doc("Pushed responses."),
  919. ConnPid = gun_open(Config),
  920. Ref = gun:get(ConnPid, "/resp/push", []),
  921. %% We expect two pushed resources.
  922. Origin = iolist_to_binary([
  923. case config(type, Config) of
  924. tcp -> "http";
  925. ssl -> "https"
  926. end,
  927. "://localhost:",
  928. integer_to_binary(config(port, Config))
  929. ]),
  930. OriginLen = byte_size(Origin),
  931. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  932. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  933. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  934. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref),
  935. %% Pushed CSS.
  936. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  937. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  938. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  939. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  940. {response, fin, 406, _} = gun:await(ConnPid, PushTXT),
  941. %% Let's not forget about the response to the client's request.
  942. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  943. gun:close(ConnPid).
  944. do_push_http2_method(Config) ->
  945. doc("Pushed response with non-GET method."),
  946. ConnPid = gun_open(Config),
  947. Ref = gun:get(ConnPid, "/resp/push/method", []),
  948. %% Pushed CSS.
  949. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  950. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  951. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  952. %% Let's not forget about the response to the client's request.
  953. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  954. gun:close(ConnPid).
  955. do_push_http2_origin(Config) ->
  956. doc("Pushed response with custom scheme/host/port."),
  957. ConnPid = gun_open(Config),
  958. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  959. %% Pushed CSS.
  960. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  961. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  962. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  963. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  964. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  965. %% Let's not forget about the response to the client's request.
  966. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  967. gun:close(ConnPid).
  968. do_push_http2_qs(Config) ->
  969. doc("Pushed response with query string."),
  970. ConnPid = gun_open(Config),
  971. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  972. %% Pushed CSS.
  973. Origin = iolist_to_binary([
  974. case config(type, Config) of
  975. tcp -> "http";
  976. ssl -> "https"
  977. end,
  978. "://localhost:",
  979. integer_to_binary(config(port, Config))
  980. ]),
  981. OriginLen = byte_size(Origin),
  982. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  983. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  984. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  985. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  986. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  987. %% Let's not forget about the response to the client's request.
  988. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  989. gun:close(ConnPid).