req_SUITE.erl 42 KB

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