req_SUITE.erl 37 KB

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