req_SUITE.erl 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. ok.
  491. read_urlencoded_body(Config) ->
  492. doc("application/x-www-form-urlencoded request body."),
  493. <<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
  494. <<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
  495. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
  496. = do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
  497. %% The timeout value is set too low on purpose to ensure a crash occurs.
  498. ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
  499. %% Ensure parse errors result in a 400 response.
  500. {400, _} = do_body_error("POST", "/read_urlencoded_body", [], "%%%%%", Config),
  501. ok.
  502. read_urlencoded_body_too_large(Config) ->
  503. doc("application/x-www-form-urlencoded request body too large. "
  504. "Send a 10MB body, larger than the default length, to ensure a crash occurs."),
  505. do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
  506. string:chars($a, 10000000), Config).
  507. %% We expect a crash.
  508. do_read_urlencoded_body_too_large(Path, Body, Config) ->
  509. ConnPid = gun_open(Config),
  510. Ref = gun:headers(ConnPid, "POST", Path, [
  511. {<<"content-length">>, integer_to_binary(iolist_size(Body))}
  512. ]),
  513. gun:data(ConnPid, Ref, fin, Body),
  514. {response, _, 413, _} = gun:await(ConnPid, Ref, infinity),
  515. gun:close(ConnPid).
  516. read_urlencoded_body_too_long(Config) ->
  517. doc("application/x-www-form-urlencoded request body sent too slow. "
  518. "The body is simply not being sent fully. It is read by the handler "
  519. "for at most 1 second. A crash occurs because we don't have the full body."),
  520. do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config).
  521. %% We expect a crash.
  522. do_read_urlencoded_body_too_long(Path, Body, Config) ->
  523. ConnPid = gun_open(Config),
  524. Ref = gun:headers(ConnPid, "POST", Path, [
  525. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  526. ]),
  527. gun:data(ConnPid, Ref, nofin, Body),
  528. {response, _, 408, RespHeaders} = gun:await(ConnPid, Ref, infinity),
  529. _ = case config(protocol, Config) of
  530. http ->
  531. %% 408 error responses should close HTTP/1.1 connections.
  532. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders);
  533. http2 ->
  534. ok
  535. end,
  536. gun:close(ConnPid).
  537. read_and_match_urlencoded_body(Config) ->
  538. doc("Read and match an application/x-www-form-urlencoded request body."),
  539. <<"#{}">> = do_body("POST", "/match/body_qs", [], "a=b&c=d", Config),
  540. <<"#{a => <<\"b\">>}">> = do_body("POST", "/match/body_qs/a", [], "a=b&c=d", Config),
  541. <<"#{c => <<\"d\">>}">> = do_body("POST", "/match/body_qs/c", [], "a=b&c=d", Config),
  542. case do_body("POST", "/match/body_qs/a/c", [], "a=b&c=d", Config) of
  543. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> -> ok;
  544. <<"#{c => <<\"d\">>,a => <<\"b\">>}">> -> ok
  545. end,
  546. case do_body("POST", "/match/body_qs/a/c", [], "a=b&c", Config) of
  547. <<"#{a => <<\"b\">>,c => true}">> -> ok;
  548. <<"#{c => true,a => <<\"b\">>}">> -> ok
  549. end,
  550. case do_body("POST", "/match/body_qs/a/c", [], "a&c=d", Config) of
  551. <<"#{a => true,c => <<\"d\">>}">> -> ok;
  552. <<"#{c => <<\"d\">>,a => true}">> -> ok
  553. end,
  554. %% Ensure match errors result in a 400 response.
  555. {400, _} = do_body_error("POST", "/match/body_qs/a/c", [], "a=b", Config),
  556. %% Ensure parse errors result in a 400 response.
  557. {400, _} = do_body_error("POST", "/match/body_qs", [], "%%%%%", Config),
  558. %% The timeout value is set too low on purpose to ensure a crash occurs.
  559. ok = do_read_body_timeout("/opts/read_and_match_urlencoded_body/timeout", <<"abc">>, Config),
  560. ok.
  561. read_and_match_urlencoded_body_too_large(Config) ->
  562. doc("Read and match an application/x-www-form-urlencoded request body too large. "
  563. "Send a 10MB body, larger than the default length, to ensure a crash occurs."),
  564. do_read_urlencoded_body_too_large(
  565. "/no-opts/read_and_match_urlencoded_body",
  566. string:chars($a, 10000000), Config).
  567. read_and_match_urlencoded_body_too_long(Config) ->
  568. doc("Read and match an application/x-www-form-urlencoded request body sent too slow. "
  569. "The body is simply not being sent fully. It is read by the handler "
  570. "for at most 1 second. A crash occurs because we don't have the full body."),
  571. do_read_urlencoded_body_too_long(
  572. "/crash/read_and_match_urlencoded_body/period", <<"abc">>, Config).
  573. multipart(Config) ->
  574. doc("Multipart request body."),
  575. do_multipart("/multipart", Config).
  576. do_multipart(Path, Config) ->
  577. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  578. ReqBody = [
  579. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  580. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  581. "--deadbeef--"
  582. ],
  583. RespBody = do_body("POST", Path, [
  584. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  585. ], ReqBody, Config),
  586. [
  587. {#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
  588. {LargeHeaders, LargeBody}
  589. ] = binary_to_term(RespBody),
  590. #{
  591. <<"content-type">> := <<"application/octet-stream">>,
  592. <<"x-custom">> := <<"value">>
  593. } = LargeHeaders,
  594. ok.
  595. multipart_error_empty(Config) ->
  596. doc("Multipart request body is empty."),
  597. %% We use an empty list as a body to make sure Gun knows
  598. %% we want to send an empty body.
  599. %% @todo This is a terrible hack. Improve Gun!
  600. Body = [],
  601. %% Ensure an empty body results in a 400 error.
  602. {400, _} = do_body_error("POST", "/multipart", [
  603. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  604. ], Body, Config),
  605. ok.
  606. multipart_error_preamble_only(Config) ->
  607. doc("Multipart request body only contains a preamble."),
  608. %% Ensure an empty body results in a 400 error.
  609. {400, _} = do_body_error("POST", "/multipart", [
  610. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  611. ], <<"Preamble.">>, Config),
  612. ok.
  613. multipart_error_headers(Config) ->
  614. doc("Multipart request body with invalid part headers."),
  615. ReqBody = [
  616. "--deadbeef\r\nbad-header text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  617. "--deadbeef--"
  618. ],
  619. %% Ensure parse errors result in a 400 response.
  620. {400, _} = do_body_error("POST", "/multipart", [
  621. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  622. ], ReqBody, Config),
  623. ok.
  624. %% The function to parse the multipart body currently does not crash,
  625. %% as far as I can tell. There is therefore no test for it.
  626. multipart_error_no_final_boundary(Config) ->
  627. doc("Multipart request body with no final boundary."),
  628. ReqBody = [
  629. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  630. ],
  631. %% Ensure parse errors result in a 400 response.
  632. {400, _} = do_body_error("POST", "/multipart", [
  633. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  634. ], ReqBody, Config),
  635. ok.
  636. multipart_missing_boundary(Config) ->
  637. doc("Multipart request body without a boundary in the media type."),
  638. ReqBody = [
  639. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  640. "--deadbeef--"
  641. ],
  642. %% Ensure parse errors result in a 400 response.
  643. {400, _} = do_body_error("POST", "/multipart", [
  644. {<<"content-type">>, <<"multipart/mixed">>}
  645. ], ReqBody, Config),
  646. ok.
  647. read_part_skip_body(Config) ->
  648. doc("Multipart request body skipping part bodies."),
  649. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  650. ReqBody = [
  651. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  652. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  653. "--deadbeef--"
  654. ],
  655. RespBody = do_body("POST", "/multipart/skip_body", [
  656. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  657. ], ReqBody, Config),
  658. [
  659. #{<<"content-type">> := <<"text/plain">>},
  660. LargeHeaders
  661. ] = binary_to_term(RespBody),
  662. #{
  663. <<"content-type">> := <<"application/octet-stream">>,
  664. <<"x-custom">> := <<"value">>
  665. } = LargeHeaders,
  666. ok.
  667. %% @todo When reading a multipart body, length and period
  668. %% only apply to a single read_body call. We may want a
  669. %% separate option to know how many reads we want to do
  670. %% before we give up.
  671. read_part2(Config) ->
  672. doc("Multipart request body using read_part/2."),
  673. %% Override the length and period values only, making
  674. %% the request process use more read_body calls.
  675. %%
  676. %% We do not try a custom timeout value since this would
  677. %% be the same test as read_body/2.
  678. do_multipart("/multipart/read_part2", Config).
  679. read_part_body2(Config) ->
  680. doc("Multipart request body using read_part_body/2."),
  681. %% Override the length and period values only, making
  682. %% the request process use more read_body calls.
  683. %%
  684. %% We do not try a custom timeout value since this would
  685. %% be the same test as read_body/2.
  686. do_multipart("/multipart/read_part_body2", Config).
  687. %% Tests: Response.
  688. %% @todo We want to crash when calling set_resp_* or related
  689. %% functions after the reply has been sent.
  690. set_resp_cookie(Config) ->
  691. doc("Response using set_resp_cookie."),
  692. %% Single cookie, no options.
  693. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  694. {_, <<"mycookie=myvalue">>}
  695. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  696. %% Single cookie, with options.
  697. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  698. {_, <<"mycookie=myvalue; Path=/resp/set_resp_cookie4">>}
  699. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  700. %% Multiple cookies.
  701. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  702. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  703. %% Overwrite previously set cookie.
  704. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  705. {_, <<"mycookie=overwrite">>}
  706. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  707. ok.
  708. set_resp_header(Config) ->
  709. doc("Response using set_resp_header."),
  710. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  711. true = lists:keymember(<<"content-type">>, 1, Headers),
  712. ok.
  713. set_resp_headers(Config) ->
  714. doc("Response using set_resp_headers."),
  715. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
  716. true = lists:keymember(<<"content-type">>, 1, Headers),
  717. true = lists:keymember(<<"content-encoding">>, 1, Headers),
  718. ok.
  719. resp_header(Config) ->
  720. doc("Response header with/without default."),
  721. {200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
  722. {200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
  723. ok.
  724. resp_headers(Config) ->
  725. doc("Get all response headers."),
  726. {200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
  727. {200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
  728. ok.
  729. set_resp_body(Config) ->
  730. doc("Response using set_resp_body."),
  731. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  732. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  733. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  734. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  735. ok.
  736. set_resp_body_sendfile0(Config) ->
  737. doc("Response using set_resp_body with a sendfile of length 0."),
  738. Path = "/resp/set_resp_body/sendfile0",
  739. ConnPid = gun_open(Config),
  740. %% First request.
  741. Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  742. {response, IsFin, 200, _} = gun:await(ConnPid, Ref1, infinity),
  743. {ok, <<>>} = case IsFin of
  744. nofin -> gun:await_body(ConnPid, Ref1, infinity);
  745. fin -> {ok, <<>>}
  746. end,
  747. %% Second request will confirm everything works as intended.
  748. Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  749. {response, IsFin, 200, _} = gun:await(ConnPid, Ref2, infinity),
  750. {ok, <<>>} = case IsFin of
  751. nofin -> gun:await_body(ConnPid, Ref2, infinity);
  752. fin -> {ok, <<>>}
  753. end,
  754. gun:close(ConnPid),
  755. ok.
  756. has_resp_header(Config) ->
  757. doc("Has response header?"),
  758. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  759. true = lists:keymember(<<"content-type">>, 1, Headers),
  760. ok.
  761. has_resp_body(Config) ->
  762. doc("Has response body?"),
  763. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  764. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  765. ok.
  766. delete_resp_header(Config) ->
  767. doc("Delete response header."),
  768. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  769. false = lists:keymember(<<"content-type">>, 1, Headers),
  770. ok.
  771. inform2(Config) ->
  772. doc("Informational response(s) without headers, followed by the real response."),
  773. {102, [], 200, _, _} = do_get_inform("/resp/inform2/102", Config),
  774. {102, [], 200, _, _} = do_get_inform("/resp/inform2/binary", Config),
  775. {500, _} = do_get_inform("/resp/inform2/error", Config),
  776. {102, [], 200, _, _} = do_get_inform("/resp/inform2/twice", Config),
  777. ok.
  778. inform3(Config) ->
  779. doc("Informational response(s) with headers, followed by the real response."),
  780. Headers = [{<<"ext-header">>, <<"ext-value">>}],
  781. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/102", Config),
  782. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/binary", Config),
  783. {500, _} = do_get_inform("/resp/inform3/error", Config),
  784. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/twice", Config),
  785. ok.
  786. reply2(Config) ->
  787. doc("Response with default headers and no body."),
  788. {200, _, _} = do_get("/resp/reply2/200", Config),
  789. {201, _, _} = do_get("/resp/reply2/201", Config),
  790. {404, _, _} = do_get("/resp/reply2/404", Config),
  791. {200, _, _} = do_get("/resp/reply2/binary", Config),
  792. {500, _, _} = do_get("/resp/reply2/error", Config),
  793. %% @todo We want to crash when reply or stream_reply is called twice.
  794. %% How to test this properly? This isn't enough.
  795. {200, _, _} = do_get("/resp/reply2/twice", Config),
  796. ok.
  797. reply3(Config) ->
  798. doc("Response with additional headers and no body."),
  799. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  800. true = lists:keymember(<<"content-type">>, 1, Headers1),
  801. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  802. true = lists:keymember(<<"content-type">>, 1, Headers2),
  803. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  804. true = lists:keymember(<<"content-type">>, 1, Headers3),
  805. {500, _, _} = do_get("/resp/reply3/error", Config),
  806. ok.
  807. reply4(Config) ->
  808. doc("Response with additional headers and body."),
  809. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  810. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  811. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  812. {500, _, _} = do_get("/resp/reply4/error", Config),
  813. ok.
  814. %% @todo Crash when stream_reply is called twice.
  815. stream_reply2(Config) ->
  816. doc("Response with default headers and streamed body."),
  817. Body = <<0:8000000>>,
  818. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  819. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  820. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  821. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  822. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  823. ok.
  824. stream_reply3(Config) ->
  825. doc("Response with additional headers and streamed body."),
  826. Body = <<0:8000000>>,
  827. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  828. true = lists:keymember(<<"content-type">>, 1, Headers1),
  829. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  830. true = lists:keymember(<<"content-type">>, 1, Headers2),
  831. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  832. true = lists:keymember(<<"content-type">>, 1, Headers3),
  833. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  834. ok.
  835. stream_body_fin0(Config) ->
  836. doc("Streamed body with last chunk of size 0."),
  837. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/fin0", Config),
  838. ok.
  839. stream_body_multiple(Config) ->
  840. doc("Streamed body via multiple calls."),
  841. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/multiple", Config),
  842. ok.
  843. stream_body_loop(Config) ->
  844. doc("Streamed body via a fast loop."),
  845. {200, _, <<0:32000000/unit:8>>} = do_get("/resp/stream_body/loop", Config),
  846. ok.
  847. stream_body_nofin(Config) ->
  848. doc("Unfinished streamed body."),
  849. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/nofin", Config),
  850. ok.
  851. stream_body_sendfile(Config) ->
  852. doc("Streamed body via multiple calls, including sendfile calls."),
  853. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  854. ExpectedBody = iolist_to_binary([
  855. <<"Hello ">>,
  856. AppFile,
  857. <<" interspersed ">>,
  858. AppFile,
  859. <<" world!">>
  860. ]),
  861. {200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile", Config),
  862. ok.
  863. stream_body_sendfile_fin(Config) ->
  864. doc("Streamed body via multiple calls, including a sendfile final call."),
  865. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  866. ExpectedBody = iolist_to_binary([
  867. <<"Hello! ">>,
  868. AppFile
  869. ]),
  870. {200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile_fin", Config),
  871. ok.
  872. stream_body_spawn(Config) ->
  873. doc("Confirm we can use cowboy_req:stream_body/3 from another process."),
  874. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body/spawn", Config),
  875. ok.
  876. stream_body_content_length_multiple(Config) ->
  877. doc("Streamed body via multiple calls."),
  878. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config),
  879. ok.
  880. stream_body_content_length_fin0(Config) ->
  881. doc("Streamed body with last chunk of size 0."),
  882. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/fin0", Config),
  883. ok.
  884. stream_body_content_length_nofin(Config) ->
  885. doc("Unfinished streamed body."),
  886. {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/nofin", Config),
  887. ok.
  888. stream_body_content_length_nofin_error(Config) ->
  889. doc("Not all of the response body sent."),
  890. case config(protocol, Config) of
  891. http ->
  892. case do_get_error("/resp/stream_body_content_length/nofin-error", Config) of
  893. %% When compression is used content-length is not sent.
  894. {200, Headers, <<"Hello">>} ->
  895. {_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers);
  896. %% The server closes the connection when the body couldn't be sent fully.
  897. {error, {stream_error, closed}} ->
  898. receive
  899. {gun_down, ConnPid, _, _, _} ->
  900. gun:close(ConnPid)
  901. after 1000 ->
  902. error(timeout)
  903. end
  904. end;
  905. http2 ->
  906. %% @todo HTTP2 should have the same content-length checks
  907. ok
  908. end.
  909. stream_body_concurrent(Config) ->
  910. ConnPid = gun_open(Config),
  911. Ref1 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]),
  912. Ref2 = gun:get(ConnPid, "/resp/stream_body/loop", [{<<"accept-encoding">>, <<"gzip">>}]),
  913. {response, nofin, 200, _} = gun:await(ConnPid, Ref1, infinity),
  914. {ok, _} = gun:await_body(ConnPid, Ref1, infinity),
  915. {response, nofin, 200, _} = gun:await(ConnPid, Ref2, infinity),
  916. {ok, _} = gun:await_body(ConnPid, Ref2, infinity),
  917. gun:close(ConnPid).
  918. %% @todo Crash when calling stream_body after the fin flag has been set.
  919. %% @todo Crash when calling stream_body after calling reply.
  920. %% @todo Crash when calling stream_body before calling stream_reply.
  921. stream_events_single(Config) ->
  922. doc("Streamed event."),
  923. {200, Headers, <<
  924. "event: add_comment\n"
  925. "data: Comment text.\n"
  926. "data: With many lines.\n"
  927. "\n"
  928. >>} = do_get("/resp/stream_events/single", Config),
  929. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  930. ok.
  931. stream_events_list(Config) ->
  932. doc("Streamed list of events."),
  933. {200, Headers, <<
  934. "event: add_comment\n"
  935. "data: Comment text.\n"
  936. "data: With many lines.\n"
  937. "\n"
  938. ": Set retry higher\n"
  939. ": with many lines also.\n"
  940. "retry: 10000\n"
  941. "\n"
  942. "id: 123\n"
  943. "event: add_comment\n"
  944. "data: Closing!\n"
  945. "\n"
  946. >>} = do_get("/resp/stream_events/list", Config),
  947. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  948. ok.
  949. stream_events_multiple(Config) ->
  950. doc("Streamed events via multiple calls."),
  951. {200, Headers, <<
  952. "event: add_comment\n"
  953. "data: Comment text.\n"
  954. "data: With many lines.\n"
  955. "\n"
  956. ": Set retry higher\n"
  957. ": with many lines also.\n"
  958. "retry: 10000\n"
  959. "\n"
  960. "id: 123\n"
  961. "event: add_comment\n"
  962. "data: Closing!\n"
  963. "\n"
  964. >>} = do_get("/resp/stream_events/multiple", Config),
  965. {_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  966. ok.
  967. stream_trailers(Config) ->
  968. doc("Stream body followed by trailer headers."),
  969. {200, RespHeaders, <<"Hello world!">>, [
  970. {<<"grpc-status">>, <<"0">>}
  971. ]} = do_trailers("/resp/stream_trailers", Config),
  972. {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  973. ok.
  974. stream_trailers_large(Config) ->
  975. doc("Stream large body followed by trailer headers."),
  976. {200, RespHeaders, <<0:80000000>>, [
  977. {<<"grpc-status">>, <<"0">>}
  978. ]} = do_trailers("/resp/stream_trailers/large", Config),
  979. {_, <<"grpc-status">>} = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  980. ok.
  981. stream_trailers_no_te(Config) ->
  982. doc("Stream body followed by trailer headers without a te header in the request."),
  983. ConnPid = gun_open(Config),
  984. Ref = gun:get(ConnPid, "/resp/stream_trailers", [
  985. {<<"accept-encoding">>, <<"gzip">>}
  986. ]),
  987. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref, infinity),
  988. %% @todo Do we want to remove the trailer header automatically?
  989. % false = lists:keyfind(<<"trailer">>, 1, RespHeaders),
  990. {ok, RespBody} = gun:await_body(ConnPid, Ref, infinity),
  991. <<"Hello world!">> = do_decode(RespHeaders, RespBody),
  992. gun:close(ConnPid).
  993. do_trailers(Path, Config) ->
  994. ConnPid = gun_open(Config),
  995. Ref = gun:get(ConnPid, Path, [
  996. {<<"accept-encoding">>, <<"gzip">>},
  997. {<<"te">>, <<"trailers">>}
  998. ]),
  999. {response, nofin, Status, RespHeaders} = gun:await(ConnPid, Ref, infinity),
  1000. {ok, RespBody, Trailers} = gun:await_body(ConnPid, Ref, infinity),
  1001. gun:close(ConnPid),
  1002. {Status, RespHeaders, do_decode(RespHeaders, RespBody), Trailers}.
  1003. %% @todo Crash when calling stream_trailers twice.
  1004. %% @todo Crash when calling stream_trailers after the fin flag has been set.
  1005. %% @todo Crash when calling stream_trailers after calling reply.
  1006. %% @todo Crash when calling stream_trailers before calling stream_reply.
  1007. %% Tests: Push.
  1008. %% @todo We want to crash when push is called after reply has been initiated.
  1009. push(Config) ->
  1010. case config(protocol, Config) of
  1011. http -> do_push_http("/resp/push", Config);
  1012. http2 -> do_push_http2(Config)
  1013. end.
  1014. push_method(Config) ->
  1015. case config(protocol, Config) of
  1016. http -> do_push_http("/resp/push/method", Config);
  1017. http2 -> do_push_http2_method(Config)
  1018. end.
  1019. push_origin(Config) ->
  1020. case config(protocol, Config) of
  1021. http -> do_push_http("/resp/push/origin", Config);
  1022. http2 -> do_push_http2_origin(Config)
  1023. end.
  1024. push_qs(Config) ->
  1025. case config(protocol, Config) of
  1026. http -> do_push_http("/resp/push/qs", Config);
  1027. http2 -> do_push_http2_qs(Config)
  1028. end.
  1029. do_push_http(Path, Config) ->
  1030. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  1031. ConnPid = gun_open(Config),
  1032. Ref = gun:get(ConnPid, Path, []),
  1033. {response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
  1034. ok.
  1035. do_push_http2(Config) ->
  1036. doc("Pushed responses."),
  1037. ConnPid = gun_open(Config),
  1038. Ref = gun:get(ConnPid, "/resp/push", []),
  1039. %% We expect two pushed resources.
  1040. Origin = iolist_to_binary([
  1041. case config(type, Config) of
  1042. tcp -> "http";
  1043. ssl -> "https"
  1044. end,
  1045. "://localhost:",
  1046. integer_to_binary(config(port, Config))
  1047. ]),
  1048. OriginLen = byte_size(Origin),
  1049. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  1050. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
  1051. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  1052. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref, infinity),
  1053. %% Pushed CSS.
  1054. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
  1055. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  1056. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
  1057. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  1058. {response, fin, 406, _} = gun:await(ConnPid, PushTXT, infinity),
  1059. %% Let's not forget about the response to the client's request.
  1060. {response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
  1061. gun:close(ConnPid).
  1062. do_push_http2_method(Config) ->
  1063. doc("Pushed response with non-GET method."),
  1064. ConnPid = gun_open(Config),
  1065. Ref = gun:get(ConnPid, "/resp/push/method", []),
  1066. %% Pushed CSS.
  1067. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
  1068. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
  1069. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  1070. %% Let's not forget about the response to the client's request.
  1071. {response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
  1072. gun:close(ConnPid).
  1073. do_push_http2_origin(Config) ->
  1074. doc("Pushed response with custom scheme/host/port."),
  1075. ConnPid = gun_open(Config),
  1076. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  1077. %% Pushed CSS.
  1078. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  1079. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
  1080. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
  1081. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  1082. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
  1083. %% Let's not forget about the response to the client's request.
  1084. {response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
  1085. gun:close(ConnPid).
  1086. do_push_http2_qs(Config) ->
  1087. doc("Pushed response with query string."),
  1088. ConnPid = gun_open(Config),
  1089. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  1090. %% Pushed CSS.
  1091. Origin = iolist_to_binary([
  1092. case config(type, Config) of
  1093. tcp -> "http";
  1094. ssl -> "https"
  1095. end,
  1096. "://localhost:",
  1097. integer_to_binary(config(port, Config))
  1098. ]),
  1099. OriginLen = byte_size(Origin),
  1100. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  1101. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref, infinity),
  1102. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS, infinity),
  1103. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  1104. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS, infinity),
  1105. %% Let's not forget about the response to the client's request.
  1106. {response, fin, 200, _} = gun:await(ConnPid, Ref, infinity),
  1107. gun:close(ConnPid).