req_SUITE.erl 44 KB

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