req_SUITE.erl 45 KB

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