req_SUITE.erl 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. %% ct.
  20. all() ->
  21. cowboy_test:common_all().
  22. groups() ->
  23. cowboy_test:common_groups(ct_helper:all(?MODULE)).
  24. init_per_suite(Config) ->
  25. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  26. Config.
  27. end_per_suite(Config) ->
  28. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  29. init_per_group(Name, Config) ->
  30. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  31. end_per_group(Name, _) ->
  32. cowboy:stop_listener(Name).
  33. %% Routes.
  34. init_dispatch(Config) ->
  35. cowboy_router:compile([{"[...]", [
  36. {"/static/[...]", cowboy_static, {dir, config(priv_dir, Config) ++ "/static"}},
  37. %% @todo Seriously InitialState should be optional.
  38. {"/resp/:key[/:arg]", resp_h, []},
  39. {"/multipart[/:key]", multipart_h, []},
  40. {"/args/:key/:arg[/:default]", echo_h, []},
  41. {"/crash/:key/period", echo_h, #{length => 999999999, period => 1000, crash => true}},
  42. {"/no-opts/:key", echo_h, #{crash => true}},
  43. {"/opts/:key/length", echo_h, #{length => 1000}},
  44. {"/opts/:key/period", echo_h, #{length => 999999999, period => 1000}},
  45. {"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
  46. {"/100-continue/:key", echo_h, []},
  47. {"/full/:key", echo_h, []},
  48. {"/no/:key", echo_h, []},
  49. {"/direct/:key/[...]", echo_h, []},
  50. {"/:key/[...]", echo_h, []}
  51. ]}]).
  52. %% Internal.
  53. do_body(Method, Path, Config) ->
  54. do_body(Method, Path, [], Config).
  55. do_body(Method, Path, Headers, Config) ->
  56. do_body(Method, Path, Headers, <<>>, Config).
  57. do_body(Method, Path, Headers0, Body, Config) ->
  58. ConnPid = gun_open(Config),
  59. Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
  60. Ref = case Body of
  61. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  62. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  63. end,
  64. {response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  65. {ok, RespBody} = case IsFin of
  66. nofin -> gun:await_body(ConnPid, Ref);
  67. fin -> {ok, <<>>}
  68. end,
  69. gun:close(ConnPid),
  70. do_decode(RespHeaders, RespBody).
  71. do_body_error(Method, Path, Headers0, Body, Config) ->
  72. ConnPid = gun_open(Config),
  73. Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
  74. Ref = case Body of
  75. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  76. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  77. end,
  78. {response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
  79. gun:close(ConnPid),
  80. {Status, RespHeaders}.
  81. do_get(Path, Config) ->
  82. do_get(Path, [], Config).
  83. do_get(Path, Headers, Config) ->
  84. ConnPid = gun_open(Config),
  85. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|Headers]),
  86. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  87. {ok, RespBody} = case IsFin of
  88. nofin -> gun:await_body(ConnPid, Ref);
  89. fin -> {ok, <<>>}
  90. end,
  91. gun:close(ConnPid),
  92. {Status, RespHeaders, do_decode(RespHeaders, RespBody)}.
  93. do_get_body(Path, Config) ->
  94. do_get_body(Path, [], Config).
  95. do_get_body(Path, Headers, Config) ->
  96. do_body("GET", Path, Headers, Config).
  97. do_get_inform(Path, Config) ->
  98. ConnPid = gun_open(Config),
  99. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  100. case gun:await(ConnPid, Ref) of
  101. {response, _, RespStatus, RespHeaders} ->
  102. %% We don't care about the body.
  103. gun:close(ConnPid),
  104. {RespStatus, RespHeaders};
  105. {inform, InfoStatus, InfoHeaders} ->
  106. {response, IsFin, RespStatus, RespHeaders}
  107. = case gun:await(ConnPid, Ref) of
  108. {inform, InfoStatus, InfoHeaders} ->
  109. gun:await(ConnPid, Ref);
  110. Response ->
  111. Response
  112. end,
  113. {ok, RespBody} = case IsFin of
  114. nofin -> gun:await_body(ConnPid, Ref);
  115. fin -> {ok, <<>>}
  116. end,
  117. gun:close(ConnPid),
  118. {InfoStatus, InfoHeaders, RespStatus, RespHeaders, do_decode(RespHeaders, RespBody)}
  119. end.
  120. do_decode(Headers, Body) ->
  121. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  122. {_, <<"gzip">>} -> zlib:gunzip(Body);
  123. _ -> Body
  124. end.
  125. %% Tests: Request.
  126. binding(Config) ->
  127. doc("Value bound from request URI path with/without default."),
  128. <<"binding">> = do_get_body("/args/binding/key", Config),
  129. <<"binding">> = do_get_body("/args/binding/key/default", Config),
  130. <<"default">> = do_get_body("/args/binding/undefined/default", Config),
  131. ok.
  132. bindings(Config) ->
  133. doc("Values bound from request URI path."),
  134. <<"#{key => <<\"bindings\">>}">> = do_get_body("/bindings", Config),
  135. ok.
  136. cert(Config) ->
  137. case config(type, Config) of
  138. tcp -> doc("TLS certificates can only be provided over TLS.");
  139. ssl -> do_cert(Config)
  140. end.
  141. do_cert(Config0) ->
  142. doc("A client TLS certificate was provided."),
  143. {CaCert, Cert, Key} = ct_helper:make_certs(),
  144. Config = [{transport_opts, [
  145. {cert, Cert},
  146. {key, Key},
  147. {cacerts, [CaCert]}
  148. ]}|Config0],
  149. Cert = do_get_body("/cert", Config),
  150. Cert = do_get_body("/direct/cert", Config),
  151. ok.
  152. cert_undefined(Config) ->
  153. doc("No client TLS certificate was provided."),
  154. <<"undefined">> = do_get_body("/cert", Config),
  155. <<"undefined">> = do_get_body("/direct/cert", Config),
  156. ok.
  157. header(Config) ->
  158. doc("Request header with/without default."),
  159. <<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config),
  160. <<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config),
  161. <<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config),
  162. ok.
  163. headers(Config) ->
  164. doc("Request headers."),
  165. do_headers("/headers", Config),
  166. do_headers("/direct/headers", Config).
  167. do_headers(Path, Config) ->
  168. %% We always send accept-encoding with this test suite's requests.
  169. <<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
  170. = do_get_body(Path, [{<<"header">>, "value"}], Config),
  171. ok.
  172. host(Config) ->
  173. doc("Request URI host."),
  174. <<"localhost">> = do_get_body("/host", Config),
  175. <<"localhost">> = do_get_body("/direct/host", Config),
  176. ok.
  177. host_info(Config) ->
  178. doc("Request host_info."),
  179. <<"[<<\"localhost\">>]">> = do_get_body("/host_info", Config),
  180. ok.
  181. %% @todo Actually write the related unit tests.
  182. match_cookies(Config) ->
  183. doc("Matched request cookies."),
  184. <<"#{}">> = do_get_body("/match/cookies", [{<<"cookie">>, "a=b; c=d"}], Config),
  185. <<"#{a => <<\"b\">>}">> = do_get_body("/match/cookies/a", [{<<"cookie">>, "a=b; c=d"}], Config),
  186. <<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
  187. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/cookies/a/c",
  188. [{<<"cookie">>, "a=b; c=d"}], Config),
  189. %% Ensure match errors result in a 400 response.
  190. {400, _, _} = do_get("/match/cookies/a/c",
  191. [{<<"cookie">>, "a=b"}], Config),
  192. %% This function is tested more extensively through unit tests.
  193. ok.
  194. %% @todo Actually write the related unit tests.
  195. match_qs(Config) ->
  196. doc("Matched request URI query string."),
  197. <<"#{}">> = do_get_body("/match/qs?a=b&c=d", Config),
  198. <<"#{a => <<\"b\">>}">> = do_get_body("/match/qs/a?a=b&c=d", Config),
  199. <<"#{c => <<\"d\">>}">> = do_get_body("/match/qs/c?a=b&c=d", Config),
  200. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a=b&c=d", Config),
  201. <<"#{a => <<\"b\">>,c => true}">> = do_get_body("/match/qs/a/c?a=b&c", Config),
  202. <<"#{a => true,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a&c=d", Config),
  203. %% Ensure match errors result in a 400 response.
  204. {400, _, _} = do_get("/match/qs/a/c?a=b", [], Config),
  205. %% This function is tested more extensively through unit tests.
  206. ok.
  207. method(Config) ->
  208. doc("Request method."),
  209. do_method("/method", Config),
  210. do_method("/direct/method", Config).
  211. do_method(Path, Config) ->
  212. <<"GET">> = do_body("GET", Path, Config),
  213. <<>> = do_body("HEAD", Path, Config),
  214. <<"OPTIONS">> = do_body("OPTIONS", Path, Config),
  215. <<"PATCH">> = do_body("PATCH", Path, Config),
  216. <<"POST">> = do_body("POST", Path, Config),
  217. <<"PUT">> = do_body("PUT", Path, Config),
  218. <<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", Path, Config),
  219. ok.
  220. parse_cookies(Config) ->
  221. doc("Request cookies."),
  222. <<"[]">> = do_get_body("/parse_cookies", Config),
  223. <<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
  224. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config),
  225. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  226. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config),
  227. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  228. = do_get_body("/parse_cookies",
  229. [{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config),
  230. %% Ensure parse errors result in a 400 response.
  231. {400, _, _} = do_get("/parse_cookies",
  232. [{<<"cookie">>, "bad name=strawberry"}], Config),
  233. {400, _, _} = do_get("/parse_cookies",
  234. [{<<"cookie">>, "goodname=strawberry\tmilkshake"}], Config),
  235. ok.
  236. parse_header(Config) ->
  237. doc("Parsed request header with/without default."),
  238. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  239. = do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config),
  240. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  241. = do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config),
  242. %% Header not in request but with default defined by Cowboy.
  243. <<"0">> = do_get_body("/args/parse_header/content-length", Config),
  244. %% Header not in request and no default from Cowboy.
  245. <<"undefined">> = do_get_body("/args/parse_header/upgrade", Config),
  246. %% Header in request and with default provided.
  247. <<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config),
  248. %% Ensure parse errors result in a 400 response.
  249. {400, _, _} = do_get("/args/parse_header/accept",
  250. [{<<"accept">>, "bad media type"}], Config),
  251. ok.
  252. parse_qs(Config) ->
  253. doc("Parsed request URI query string."),
  254. <<"[]">> = do_get_body("/parse_qs", Config),
  255. <<"[{<<\"abc\">>,true}]">> = do_get_body("/parse_qs?abc", Config),
  256. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> = do_get_body("/parse_qs?a=b&c=d+e", Config),
  257. %% Ensure parse errors result in a 400 response.
  258. {400, _, _} = do_get("/parse_qs?%%%%%%%", Config),
  259. ok.
  260. path(Config) ->
  261. doc("Request URI path."),
  262. do_path("/path", Config),
  263. do_path("/direct/path", Config).
  264. do_path(Path0, Config) ->
  265. Path = list_to_binary(Path0 ++ "/to/the/resource"),
  266. Path = do_get_body(Path, Config),
  267. Path = do_get_body([Path, "?query"], Config),
  268. Path = do_get_body([Path, "?query#fragment"], Config),
  269. Path = do_get_body([Path, "#fragment"], Config),
  270. ok.
  271. path_info(Config) ->
  272. doc("Request path_info."),
  273. <<"undefined">> = do_get_body("/no/path_info", Config),
  274. <<"[]">> = do_get_body("/path_info", Config),
  275. <<"[]">> = do_get_body("/path_info/", Config),
  276. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource", Config),
  277. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query", Config),
  278. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query#fragment", Config),
  279. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource#fragment", Config),
  280. ok.
  281. peer(Config) ->
  282. doc("Remote socket address."),
  283. <<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
  284. <<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
  285. ok.
  286. port(Config) ->
  287. doc("Request URI port."),
  288. Port = integer_to_binary(config(port, Config)),
  289. Port = do_get_body("/port", Config),
  290. Port = do_get_body("/direct/port", Config),
  291. ok.
  292. qs(Config) ->
  293. doc("Request URI query string."),
  294. do_qs("/qs", Config),
  295. do_qs("/direct/qs", Config).
  296. do_qs(Path, Config) ->
  297. <<>> = do_get_body(Path, Config),
  298. <<"abc">> = do_get_body(Path ++ "?abc", Config),
  299. <<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config),
  300. ok.
  301. scheme(Config) ->
  302. doc("Request URI scheme."),
  303. do_scheme("/scheme", Config),
  304. do_scheme("/direct/scheme", Config).
  305. do_scheme(Path, Config) ->
  306. Transport = config(type, Config),
  307. case do_get_body(Path, Config) of
  308. <<"http">> when Transport =:= tcp -> ok;
  309. <<"https">> when Transport =:= ssl -> ok
  310. end.
  311. sock(Config) ->
  312. doc("Local socket address."),
  313. <<"{{127,0,0,1},", _/bits >> = do_get_body("/sock", Config),
  314. <<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/sock", Config),
  315. ok.
  316. uri(Config) ->
  317. doc("Request URI building/modification."),
  318. Scheme = case config(type, Config) of
  319. tcp -> <<"http">>;
  320. ssl -> <<"https">>
  321. end,
  322. SLen = byte_size(Scheme),
  323. Port = integer_to_binary(config(port, Config)),
  324. PLen = byte_size(Port),
  325. %% Absolute form.
  326. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >>
  327. = do_get_body("/uri?qs", Config),
  328. %% Origin form.
  329. << "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config),
  330. %% Protocol relative.
  331. << "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >>
  332. = do_get_body("/uri/protocol-relative?qs", Config),
  333. %% No query string.
  334. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >>
  335. = do_get_body("/uri/no-qs?qs", Config),
  336. %% No path or query string.
  337. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary >>
  338. = do_get_body("/uri/no-path?qs", Config),
  339. %% Changed port.
  340. << Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >>
  341. = do_get_body("/uri/set-port?qs", Config),
  342. %% This function is tested more extensively through unit tests.
  343. ok.
  344. version(Config) ->
  345. doc("Request HTTP version."),
  346. do_version("/version", Config),
  347. do_version("/direct/version", Config).
  348. do_version(Path, Config) ->
  349. Protocol = config(protocol, Config),
  350. case do_get_body(Path, Config) of
  351. <<"HTTP/1.1">> when Protocol =:= http -> ok;
  352. <<"HTTP/2">> when Protocol =:= http2 -> ok
  353. end.
  354. %% Tests: Request body.
  355. body_length(Config) ->
  356. doc("Request body length."),
  357. <<"0">> = do_get_body("/body_length", Config),
  358. <<"12">> = do_body("POST", "/body_length", [], "hello world!", Config),
  359. ok.
  360. has_body(Config) ->
  361. doc("Has a request body?"),
  362. <<"false">> = do_get_body("/has_body", Config),
  363. <<"true">> = do_body("POST", "/has_body", [], "hello world!", Config),
  364. ok.
  365. read_body(Config) ->
  366. doc("Request body."),
  367. <<>> = do_get_body("/read_body", Config),
  368. <<"hello world!">> = do_body("POST", "/read_body", [], "hello world!", Config),
  369. %% We expect to have read *at least* 1000 bytes.
  370. <<0:8000, _/bits>> = do_body("POST", "/opts/read_body/length", [], <<0:8000000>>, Config),
  371. %% We read any length for at most 1 second.
  372. %%
  373. %% The body is sent twice, first with nofin, then wait 2 seconds, then again with fin.
  374. <<0:8000000>> = do_read_body_period("/opts/read_body/period", <<0:8000000>>, Config),
  375. %% The timeout value is set too low on purpose to ensure a crash occurs.
  376. ok = do_read_body_timeout("/opts/read_body/timeout", <<0:8000000>>, Config),
  377. %% 10MB body larger than default length.
  378. <<0:80000000>> = do_body("POST", "/full/read_body", [], <<0:80000000>>, Config),
  379. ok.
  380. do_read_body_period(Path, Body, Config) ->
  381. ConnPid = gun_open(Config),
  382. Ref = gun:request(ConnPid, "POST", Path, [
  383. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  384. ]),
  385. gun:data(ConnPid, Ref, nofin, Body),
  386. timer:sleep(2000),
  387. gun:data(ConnPid, Ref, fin, Body),
  388. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  389. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  390. gun:close(ConnPid),
  391. RespBody.
  392. %% We expect a crash.
  393. do_read_body_timeout(Path, Body, Config) ->
  394. ConnPid = gun_open(Config),
  395. Ref = gun:request(ConnPid, "POST", Path, [
  396. {<<"content-length">>, integer_to_binary(byte_size(Body))}
  397. ]),
  398. {response, _, 500, _} = gun:await(ConnPid, Ref),
  399. gun:close(ConnPid).
  400. read_body_expect_100_continue(Config) ->
  401. doc("Request body with a 100-continue expect header."),
  402. do_read_body_expect_100_continue("/read_body", Config).
  403. read_body_expect_100_continue_user_sent(Config) ->
  404. doc("Request body with a 100-continue expect header, 100 response sent by handler."),
  405. do_read_body_expect_100_continue("/100-continue/read_body", Config).
  406. do_read_body_expect_100_continue(Path, Config) ->
  407. ConnPid = gun_open(Config),
  408. Body = <<0:8000000>>,
  409. Headers = [
  410. {<<"accept-encoding">>, <<"gzip">>},
  411. {<<"expect">>, <<"100-continue">>},
  412. {<<"content-length">>, integer_to_binary(byte_size(Body))}
  413. ],
  414. Ref = gun:post(ConnPid, Path, Headers),
  415. {inform, 100, []} = gun:await(ConnPid, Ref),
  416. gun:data(ConnPid, Ref, fin, Body),
  417. {response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  418. {ok, RespBody} = case IsFin of
  419. nofin -> gun:await_body(ConnPid, Ref);
  420. fin -> {ok, <<>>}
  421. end,
  422. gun:close(ConnPid),
  423. do_decode(RespHeaders, RespBody).
  424. read_urlencoded_body(Config) ->
  425. doc("application/x-www-form-urlencoded request body."),
  426. <<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
  427. <<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
  428. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
  429. = do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
  430. %% Send a 10MB body, larger than the default length, to ensure a crash occurs.
  431. ok = do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
  432. string:chars($a, 10000000), Config),
  433. %% We read any length for at most 1 second.
  434. %%
  435. %% The body is sent twice, first with nofin, then wait 1.1 second, then again with fin.
  436. %% We expect the handler to crash because read_urlencoded_body expects the full body.
  437. ok = do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config),
  438. %% The timeout value is set too low on purpose to ensure a crash occurs.
  439. ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
  440. %% Ensure parse errors result in a 400 response.
  441. {400, _} = do_body_error("POST", "/read_urlencoded_body", [], "%%%%%", Config),
  442. ok.
  443. %% We expect a crash.
  444. do_read_urlencoded_body_too_large(Path, Body, Config) ->
  445. ConnPid = gun_open(Config),
  446. Ref = gun:request(ConnPid, "POST", Path, [
  447. {<<"content-length">>, integer_to_binary(iolist_size(Body))}
  448. ]),
  449. gun:data(ConnPid, Ref, fin, Body),
  450. {response, _, 413, _} = gun:await(ConnPid, Ref),
  451. gun:close(ConnPid).
  452. %% We expect a crash.
  453. do_read_urlencoded_body_too_long(Path, Body, Config) ->
  454. ConnPid = gun_open(Config),
  455. Ref = gun:request(ConnPid, "POST", Path, [
  456. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  457. ]),
  458. gun:data(ConnPid, Ref, nofin, Body),
  459. timer:sleep(1100),
  460. gun:data(ConnPid, Ref, fin, Body),
  461. {response, _, 408, RespHeaders} = gun:await(ConnPid, Ref),
  462. _ = case config(protocol, Config) of
  463. http ->
  464. %% 408 error responses should close HTTP/1.1 connections.
  465. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, RespHeaders);
  466. http2 ->
  467. ok
  468. end,
  469. gun:close(ConnPid).
  470. multipart(Config) ->
  471. doc("Multipart request body."),
  472. do_multipart("/multipart", Config).
  473. do_multipart(Path, Config) ->
  474. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  475. ReqBody = [
  476. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  477. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  478. "--deadbeef--"
  479. ],
  480. RespBody = do_body("POST", Path, [
  481. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  482. ], ReqBody, Config),
  483. [
  484. {#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
  485. {LargeHeaders, LargeBody}
  486. ] = binary_to_term(RespBody),
  487. #{
  488. <<"content-type">> := <<"application/octet-stream">>,
  489. <<"x-custom">> := <<"value">>
  490. } = LargeHeaders,
  491. ok.
  492. multipart_error_empty(Config) ->
  493. doc("Multipart request body is empty."),
  494. %% We use an empty list as a body to make sure Gun knows
  495. %% we want to send an empty body.
  496. %% @todo This is a terrible hack. Improve Gun!
  497. Body = [],
  498. %% Ensure an empty body results in a 400 error.
  499. {400, _} = do_body_error("POST", "/multipart", [
  500. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  501. ], Body, Config),
  502. ok.
  503. multipart_error_preamble_only(Config) ->
  504. doc("Multipart request body only contains a preamble."),
  505. %% Ensure an empty body results in a 400 error.
  506. {400, _} = do_body_error("POST", "/multipart", [
  507. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  508. ], <<"Preamble.">>, Config),
  509. ok.
  510. multipart_error_headers(Config) ->
  511. doc("Multipart request body with invalid part headers."),
  512. ReqBody = [
  513. "--deadbeef\r\nbad-header text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  514. "--deadbeef--"
  515. ],
  516. %% Ensure parse errors result in a 400 response.
  517. {400, _} = do_body_error("POST", "/multipart", [
  518. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  519. ], ReqBody, Config),
  520. ok.
  521. %% The function to parse the multipart body currently does not crash,
  522. %% as far as I can tell. There is therefore no test for it.
  523. multipart_error_no_final_boundary(Config) ->
  524. doc("Multipart request body with no final boundary."),
  525. ReqBody = [
  526. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  527. ],
  528. %% Ensure parse errors result in a 400 response.
  529. {400, _} = do_body_error("POST", "/multipart", [
  530. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  531. ], ReqBody, Config),
  532. ok.
  533. multipart_missing_boundary(Config) ->
  534. doc("Multipart request body without a boundary in the media type."),
  535. ReqBody = [
  536. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  537. "--deadbeef--"
  538. ],
  539. %% Ensure parse errors result in a 400 response.
  540. {400, _} = do_body_error("POST", "/multipart", [
  541. {<<"content-type">>, <<"multipart/mixed">>}
  542. ], ReqBody, Config),
  543. ok.
  544. read_part_skip_body(Config) ->
  545. doc("Multipart request body skipping part bodies."),
  546. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  547. ReqBody = [
  548. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  549. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  550. "--deadbeef--"
  551. ],
  552. RespBody = do_body("POST", "/multipart/skip_body", [
  553. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  554. ], ReqBody, Config),
  555. [
  556. #{<<"content-type">> := <<"text/plain">>},
  557. LargeHeaders
  558. ] = binary_to_term(RespBody),
  559. #{
  560. <<"content-type">> := <<"application/octet-stream">>,
  561. <<"x-custom">> := <<"value">>
  562. } = LargeHeaders,
  563. ok.
  564. %% @todo When reading a multipart body, length and period
  565. %% only apply to a single read_body call. We may want a
  566. %% separate option to know how many reads we want to do
  567. %% before we give up.
  568. read_part2(Config) ->
  569. doc("Multipart request body using read_part/2."),
  570. %% Override the length and period values only, making
  571. %% the request process use more read_body calls.
  572. %%
  573. %% We do not try a custom timeout value since this would
  574. %% be the same test as read_body/2.
  575. do_multipart("/multipart/read_part2", Config).
  576. read_part_body2(Config) ->
  577. doc("Multipart request body using read_part_body/2."),
  578. %% Override the length and period values only, making
  579. %% the request process use more read_body calls.
  580. %%
  581. %% We do not try a custom timeout value since this would
  582. %% be the same test as read_body/2.
  583. do_multipart("/multipart/read_part_body2", Config).
  584. %% Tests: Response.
  585. %% @todo We want to crash when calling set_resp_* or related
  586. %% functions after the reply has been sent.
  587. set_resp_cookie(Config) ->
  588. doc("Response using set_resp_cookie."),
  589. %% Single cookie, no options.
  590. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  591. {_, <<"mycookie=myvalue; Version=1">>}
  592. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  593. %% Single cookie, with options.
  594. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  595. {_, <<"mycookie=myvalue; Version=1; Path=/resp/set_resp_cookie4">>}
  596. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  597. %% Multiple cookies.
  598. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  599. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  600. %% Overwrite previously set cookie.
  601. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  602. {_, <<"mycookie=overwrite; Version=1">>}
  603. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  604. ok.
  605. set_resp_header(Config) ->
  606. doc("Response using set_resp_header."),
  607. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  608. true = lists:keymember(<<"content-type">>, 1, Headers),
  609. ok.
  610. set_resp_headers(Config) ->
  611. doc("Response using set_resp_headers."),
  612. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
  613. true = lists:keymember(<<"content-type">>, 1, Headers),
  614. true = lists:keymember(<<"content-encoding">>, 1, Headers),
  615. ok.
  616. resp_header(Config) ->
  617. doc("Response header with/without default."),
  618. {200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
  619. {200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
  620. ok.
  621. resp_headers(Config) ->
  622. doc("Get all response headers."),
  623. {200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
  624. {200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
  625. ok.
  626. set_resp_body(Config) ->
  627. doc("Response using set_resp_body."),
  628. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  629. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  630. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  631. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  632. ok.
  633. set_resp_body_sendfile0(Config) ->
  634. doc("Response using set_resp_body with a sendfile of length 0."),
  635. Path = "/resp/set_resp_body/sendfile0",
  636. ConnPid = gun_open(Config),
  637. %% First request.
  638. Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  639. {response, IsFin, 200, _} = gun:await(ConnPid, Ref1),
  640. {ok, <<>>} = case IsFin of
  641. nofin -> gun:await_body(ConnPid, Ref1);
  642. fin -> {ok, <<>>}
  643. end,
  644. %% Second request will confirm everything works as intended.
  645. Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  646. {response, IsFin, 200, _} = gun:await(ConnPid, Ref2),
  647. {ok, <<>>} = case IsFin of
  648. nofin -> gun:await_body(ConnPid, Ref2);
  649. fin -> {ok, <<>>}
  650. end,
  651. gun:close(ConnPid),
  652. ok.
  653. has_resp_header(Config) ->
  654. doc("Has response header?"),
  655. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  656. true = lists:keymember(<<"content-type">>, 1, Headers),
  657. ok.
  658. has_resp_body(Config) ->
  659. doc("Has response body?"),
  660. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  661. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  662. ok.
  663. delete_resp_header(Config) ->
  664. doc("Delete response header."),
  665. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  666. false = lists:keymember(<<"content-type">>, 1, Headers),
  667. ok.
  668. inform2(Config) ->
  669. doc("Informational response(s) without headers, followed by the real response."),
  670. {102, [], 200, _, _} = do_get_inform("/resp/inform2/102", Config),
  671. {102, [], 200, _, _} = do_get_inform("/resp/inform2/binary", Config),
  672. {500, _} = do_get_inform("/resp/inform2/error", Config),
  673. {102, [], 200, _, _} = do_get_inform("/resp/inform2/twice", Config),
  674. ok.
  675. inform3(Config) ->
  676. doc("Informational response(s) with headers, followed by the real response."),
  677. Headers = [{<<"ext-header">>, <<"ext-value">>}],
  678. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/102", Config),
  679. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/binary", Config),
  680. {500, _} = do_get_inform("/resp/inform3/error", Config),
  681. {102, Headers, 200, _, _} = do_get_inform("/resp/inform3/twice", Config),
  682. ok.
  683. reply2(Config) ->
  684. doc("Response with default headers and no body."),
  685. {200, _, _} = do_get("/resp/reply2/200", Config),
  686. {201, _, _} = do_get("/resp/reply2/201", Config),
  687. {404, _, _} = do_get("/resp/reply2/404", Config),
  688. {200, _, _} = do_get("/resp/reply2/binary", Config),
  689. {500, _, _} = do_get("/resp/reply2/error", Config),
  690. %% @todo We want to crash when reply or stream_reply is called twice.
  691. %% How to test this properly? This isn't enough.
  692. {200, _, _} = do_get("/resp/reply2/twice", Config),
  693. ok.
  694. reply3(Config) ->
  695. doc("Response with additional headers and no body."),
  696. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  697. true = lists:keymember(<<"content-type">>, 1, Headers1),
  698. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  699. true = lists:keymember(<<"content-type">>, 1, Headers2),
  700. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  701. true = lists:keymember(<<"content-type">>, 1, Headers3),
  702. {500, _, _} = do_get("/resp/reply3/error", Config),
  703. ok.
  704. reply4(Config) ->
  705. doc("Response with additional headers and body."),
  706. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  707. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  708. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  709. {500, _, _} = do_get("/resp/reply4/error", Config),
  710. ok.
  711. %% @todo Crash when stream_reply is called twice.
  712. stream_reply2(Config) ->
  713. doc("Response with default headers and streamed body."),
  714. Body = <<0:8000000>>,
  715. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  716. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  717. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  718. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  719. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  720. ok.
  721. stream_reply3(Config) ->
  722. doc("Response with additional headers and streamed body."),
  723. Body = <<0:8000000>>,
  724. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  725. true = lists:keymember(<<"content-type">>, 1, Headers1),
  726. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  727. true = lists:keymember(<<"content-type">>, 1, Headers2),
  728. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  729. true = lists:keymember(<<"content-type">>, 1, Headers3),
  730. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  731. ok.
  732. %% @todo Crash when calling stream_body after the fin flag has been set.
  733. %% @todo Crash when calling stream_body after calling reply.
  734. %% @todo Crash when calling stream_body before calling stream_reply.
  735. %% Tests: Push.
  736. %% @todo We want to crash when push is called after reply has been initiated.
  737. push(Config) ->
  738. case config(protocol, Config) of
  739. http -> do_push_http("/resp/push", Config);
  740. http2 -> do_push_http2(Config)
  741. end.
  742. push_method(Config) ->
  743. case config(protocol, Config) of
  744. http -> do_push_http("/resp/push/method", Config);
  745. http2 -> do_push_http2_method(Config)
  746. end.
  747. push_origin(Config) ->
  748. case config(protocol, Config) of
  749. http -> do_push_http("/resp/push/origin", Config);
  750. http2 -> do_push_http2_origin(Config)
  751. end.
  752. push_qs(Config) ->
  753. case config(protocol, Config) of
  754. http -> do_push_http("/resp/push/qs", Config);
  755. http2 -> do_push_http2_qs(Config)
  756. end.
  757. do_push_http(Path, Config) ->
  758. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  759. ConnPid = gun_open(Config),
  760. Ref = gun:get(ConnPid, Path, []),
  761. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  762. ok.
  763. do_push_http2(Config) ->
  764. doc("Pushed responses."),
  765. ConnPid = gun_open(Config),
  766. Ref = gun:get(ConnPid, "/resp/push", []),
  767. %% We expect two pushed resources.
  768. Origin = iolist_to_binary([
  769. case config(type, Config) of
  770. tcp -> "http";
  771. ssl -> "https"
  772. end,
  773. "://localhost:",
  774. integer_to_binary(config(port, Config))
  775. ]),
  776. OriginLen = byte_size(Origin),
  777. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  778. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  779. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  780. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref),
  781. %% Pushed CSS.
  782. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  783. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  784. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  785. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  786. {response, fin, 406, _} = gun:await(ConnPid, PushTXT),
  787. %% Let's not forget about the response to the client's request.
  788. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  789. gun:close(ConnPid).
  790. do_push_http2_method(Config) ->
  791. doc("Pushed response with non-GET method."),
  792. ConnPid = gun_open(Config),
  793. Ref = gun:get(ConnPid, "/resp/push/method", []),
  794. %% Pushed CSS.
  795. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  796. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  797. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  798. %% Let's not forget about the response to the client's request.
  799. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  800. gun:close(ConnPid).
  801. do_push_http2_origin(Config) ->
  802. doc("Pushed response with custom scheme/host/port."),
  803. ConnPid = gun_open(Config),
  804. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  805. %% Pushed CSS.
  806. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  807. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  808. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  809. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  810. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  811. %% Let's not forget about the response to the client's request.
  812. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  813. gun:close(ConnPid).
  814. do_push_http2_qs(Config) ->
  815. doc("Pushed response with query string."),
  816. ConnPid = gun_open(Config),
  817. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  818. %% Pushed CSS.
  819. Origin = iolist_to_binary([
  820. case config(type, Config) of
  821. tcp -> "http";
  822. ssl -> "https"
  823. end,
  824. "://localhost:",
  825. integer_to_binary(config(port, Config))
  826. ]),
  827. OriginLen = byte_size(Origin),
  828. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  829. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  830. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  831. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  832. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  833. %% Let's not forget about the response to the client's request.
  834. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  835. gun:close(ConnPid).