req_SUITE.erl 47 KB

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