req_SUITE.erl 44 KB

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