req_SUITE.erl 36 KB

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