req_SUITE.erl 42 KB

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