req_SUITE.erl 36 KB

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