req_SUITE.erl 41 KB

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