req_SUITE.erl 44 KB

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