req_SUITE.erl 45 KB

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