req_SUITE.erl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. {"/full/:key", echo_h, []},
  47. {"/no/:key", echo_h, []},
  48. {"/direct/:key/[...]", echo_h, []},
  49. {"/:key/[...]", echo_h, []}
  50. ]}]).
  51. %% Internal.
  52. do_body(Method, Path, Config) ->
  53. do_body(Method, Path, [], Config).
  54. do_body(Method, Path, Headers, Config) ->
  55. do_body(Method, Path, Headers, <<>>, Config).
  56. do_body(Method, Path, Headers0, Body, Config) ->
  57. ConnPid = gun_open(Config),
  58. Headers = [{<<"accept-encoding">>, <<"gzip">>}|Headers0],
  59. Ref = case Body of
  60. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  61. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  62. end,
  63. {response, IsFin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  64. {ok, RespBody} = case IsFin of
  65. nofin -> gun:await_body(ConnPid, Ref);
  66. fin -> {ok, <<>>}
  67. end,
  68. gun:close(ConnPid),
  69. do_decode(RespHeaders, RespBody).
  70. do_get(Path, Config) ->
  71. ConnPid = gun_open(Config),
  72. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  73. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  74. {ok, RespBody} = case IsFin of
  75. nofin -> gun:await_body(ConnPid, Ref);
  76. fin -> {ok, <<>>}
  77. end,
  78. gun:close(ConnPid),
  79. {Status, RespHeaders, do_decode(RespHeaders, RespBody)}.
  80. do_get_body(Path, Config) ->
  81. do_get_body(Path, [], Config).
  82. do_get_body(Path, Headers, Config) ->
  83. do_body("GET", Path, Headers, Config).
  84. do_decode(Headers, Body) ->
  85. case lists:keyfind(<<"content-encoding">>, 1, Headers) of
  86. {_, <<"gzip">>} -> zlib:gunzip(Body);
  87. _ -> Body
  88. end.
  89. %% Tests: Request.
  90. binding(Config) ->
  91. doc("Value bound from request URI path with/without default."),
  92. <<"binding">> = do_get_body("/args/binding/key", Config),
  93. <<"binding">> = do_get_body("/args/binding/key/default", Config),
  94. <<"default">> = do_get_body("/args/binding/undefined/default", Config),
  95. ok.
  96. bindings(Config) ->
  97. doc("Values bound from request URI path."),
  98. <<"#{key => <<\"bindings\">>}">> = do_get_body("/bindings", Config),
  99. ok.
  100. header(Config) ->
  101. doc("Request header with/without default."),
  102. <<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config),
  103. <<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config),
  104. <<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config),
  105. ok.
  106. headers(Config) ->
  107. doc("Request headers."),
  108. do_headers("/headers", Config),
  109. do_headers("/direct/headers", Config).
  110. do_headers(Path, Config) ->
  111. %% We always send accept-encoding with this test suite's requests.
  112. <<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
  113. = do_get_body(Path, [{<<"header">>, "value"}], Config),
  114. ok.
  115. host(Config) ->
  116. doc("Request URI host."),
  117. <<"localhost">> = do_get_body("/host", Config),
  118. <<"localhost">> = do_get_body("/direct/host", Config),
  119. ok.
  120. host_info(Config) ->
  121. doc("Request host_info."),
  122. <<"[<<\"localhost\">>]">> = do_get_body("/host_info", Config),
  123. ok.
  124. %% @todo Actually write the related unit tests.
  125. match_cookies(Config) ->
  126. doc("Matched request cookies."),
  127. <<"#{}">> = do_get_body("/match/cookies", [{<<"cookie">>, "a=b; c=d"}], Config),
  128. <<"#{a => <<\"b\">>}">> = do_get_body("/match/cookies/a", [{<<"cookie">>, "a=b; c=d"}], Config),
  129. <<"#{c => <<\"d\">>}">> = do_get_body("/match/cookies/c", [{<<"cookie">>, "a=b; c=d"}], Config),
  130. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/cookies/a/c",
  131. [{<<"cookie">>, "a=b; c=d"}], Config),
  132. %% This function is tested more extensively through unit tests.
  133. ok.
  134. %% @todo Actually write the related unit tests.
  135. match_qs(Config) ->
  136. doc("Matched request URI query string."),
  137. <<"#{}">> = do_get_body("/match/qs?a=b&c=d", Config),
  138. <<"#{a => <<\"b\">>}">> = do_get_body("/match/qs/a?a=b&c=d", Config),
  139. <<"#{c => <<\"d\">>}">> = do_get_body("/match/qs/c?a=b&c=d", Config),
  140. <<"#{a => <<\"b\">>,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a=b&c=d", Config),
  141. <<"#{a => <<\"b\">>,c => true}">> = do_get_body("/match/qs/a/c?a=b&c", Config),
  142. <<"#{a => true,c => <<\"d\">>}">> = do_get_body("/match/qs/a/c?a&c=d", Config),
  143. %% This function is tested more extensively through unit tests.
  144. ok.
  145. method(Config) ->
  146. doc("Request method."),
  147. do_method("/method", Config),
  148. do_method("/direct/method", Config).
  149. do_method(Path, Config) ->
  150. <<"GET">> = do_body("GET", "/method", Config),
  151. <<>> = do_body("HEAD", "/method", Config),
  152. <<"OPTIONS">> = do_body("OPTIONS", "/method", Config),
  153. <<"PATCH">> = do_body("PATCH", "/method", Config),
  154. <<"POST">> = do_body("POST", "/method", Config),
  155. <<"PUT">> = do_body("PUT", "/method", Config),
  156. <<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", "/method", Config),
  157. ok.
  158. parse_cookies(Config) ->
  159. doc("Request cookies."),
  160. <<"[]">> = do_get_body("/parse_cookies", Config),
  161. <<"[{<<\"cake\">>,<<\"strawberry\">>}]">>
  162. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config),
  163. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  164. = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config),
  165. <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">>
  166. = do_get_body("/parse_cookies",
  167. [{<<"cookie">>, "cake=strawberry"}, {<<"cookie">>, "color=blue"}], Config),
  168. ok.
  169. parse_header(Config) ->
  170. doc("Parsed request header with/without default."),
  171. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  172. = do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config),
  173. <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">>
  174. = do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config),
  175. %% Header not in request but with default defined by Cowboy.
  176. <<"0">> = do_get_body("/args/parse_header/content-length", Config),
  177. %% Header not in request and no default from Cowboy.
  178. <<"undefined">> = do_get_body("/args/parse_header/upgrade", Config),
  179. %% Header in request and with default provided.
  180. <<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config),
  181. ok.
  182. parse_qs(Config) ->
  183. doc("Parsed request URI query string."),
  184. <<"[]">> = do_get_body("/parse_qs", Config),
  185. <<"[{<<\"abc\">>,true}]">> = do_get_body("/parse_qs?abc", Config),
  186. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">> = do_get_body("/parse_qs?a=b&c=d+e", Config),
  187. ok.
  188. path(Config) ->
  189. doc("Request URI path."),
  190. do_path("/path", Config),
  191. do_path("/direct/path", Config).
  192. do_path(Path0, Config) ->
  193. Path = list_to_binary(Path0 ++ "/to/the/resource"),
  194. Path = do_get_body(Path, Config),
  195. Path = do_get_body([Path, "?query"], Config),
  196. Path = do_get_body([Path, "?query#fragment"], Config),
  197. Path = do_get_body([Path, "#fragment"], Config),
  198. ok.
  199. path_info(Config) ->
  200. doc("Request path_info."),
  201. <<"undefined">> = do_get_body("/no/path_info", Config),
  202. <<"[]">> = do_get_body("/path_info", Config),
  203. <<"[]">> = do_get_body("/path_info/", Config),
  204. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource", Config),
  205. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query", Config),
  206. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource?query#fragment", Config),
  207. <<"[<<\"to\">>,<<\"the\">>,<<\"resource\">>]">> = do_get_body("/path_info/to/the/resource#fragment", Config),
  208. ok.
  209. peer(Config) ->
  210. doc("Request peer."),
  211. <<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
  212. <<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
  213. ok.
  214. port(Config) ->
  215. doc("Request URI port."),
  216. Port = integer_to_binary(config(port, Config)),
  217. Port = do_get_body("/port", Config),
  218. Port = do_get_body("/direct/port", Config),
  219. ok.
  220. qs(Config) ->
  221. doc("Request URI query string."),
  222. do_qs("/qs", Config),
  223. do_qs("/direct/qs", Config).
  224. do_qs(Path, Config) ->
  225. <<>> = do_get_body(Path, Config),
  226. <<"abc">> = do_get_body(Path ++ "?abc", Config),
  227. <<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config),
  228. ok.
  229. scheme(Config) ->
  230. doc("Request URI scheme."),
  231. do_scheme("/scheme", Config),
  232. do_scheme("/direct/scheme", Config).
  233. do_scheme(Path, Config) ->
  234. Transport = config(type, Config),
  235. case do_get_body(Path, Config) of
  236. <<"http">> when Transport =:= tcp -> ok;
  237. <<"https">> when Transport =:= ssl -> ok
  238. end.
  239. uri(Config) ->
  240. doc("Request URI building/modification."),
  241. Scheme = case config(type, Config) of
  242. tcp -> <<"http">>;
  243. ssl -> <<"https">>
  244. end,
  245. SLen = byte_size(Scheme),
  246. Port = integer_to_binary(config(port, Config)),
  247. PLen = byte_size(Port),
  248. %% Absolute form.
  249. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >>
  250. = do_get_body("/uri?qs", Config),
  251. %% Origin form.
  252. << "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config),
  253. %% Protocol relative.
  254. << "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >>
  255. = do_get_body("/uri/protocol-relative?qs", Config),
  256. %% No query string.
  257. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >>
  258. = do_get_body("/uri/no-qs?qs", Config),
  259. %% No path or query string.
  260. << Scheme:SLen/binary, "://localhost:", Port:PLen/binary >>
  261. = do_get_body("/uri/no-path?qs", Config),
  262. %% Changed port.
  263. << Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >>
  264. = do_get_body("/uri/set-port?qs", Config),
  265. %% This function is tested more extensively through unit tests.
  266. ok.
  267. version(Config) ->
  268. doc("Request HTTP version."),
  269. do_version("/version", Config),
  270. do_version("/direct/version", Config).
  271. do_version(Path, Config) ->
  272. Protocol = config(protocol, Config),
  273. case do_get_body(Path, Config) of
  274. <<"HTTP/1.1">> when Protocol =:= http -> ok;
  275. <<"HTTP/2">> when Protocol =:= http2 -> ok
  276. end.
  277. %% Tests: Request body.
  278. body_length(Config) ->
  279. doc("Request body length."),
  280. <<"0">> = do_get_body("/body_length", Config),
  281. <<"12">> = do_body("POST", "/body_length", [], "hello world!", Config),
  282. ok.
  283. has_body(Config) ->
  284. doc("Has a request body?"),
  285. <<"false">> = do_get_body("/has_body", Config),
  286. <<"true">> = do_body("POST", "/has_body", [], "hello world!", Config),
  287. ok.
  288. read_body(Config) ->
  289. doc("Request body."),
  290. <<>> = do_get_body("/read_body", Config),
  291. <<"hello world!">> = do_body("POST", "/read_body", [], "hello world!", Config),
  292. %% We expect to have read *at least* 1000 bytes.
  293. <<0:8000, _/bits>> = do_body("POST", "/opts/read_body/length", [], <<0:8000000>>, Config),
  294. %% We read any length for at most 1 second.
  295. %%
  296. %% The body is sent twice, first with nofin, then wait 2 seconds, then again with fin.
  297. <<0:8000000>> = do_read_body_period("/opts/read_body/period", <<0:8000000>>, Config),
  298. %% The timeout value is set too low on purpose to ensure a crash occurs.
  299. ok = do_read_body_timeout("/opts/read_body/timeout", <<0:8000000>>, Config),
  300. %% 10MB body larger than default length.
  301. <<0:80000000>> = do_body("POST", "/full/read_body", [], <<0:80000000>>, Config),
  302. ok.
  303. do_read_body_period(Path, Body, Config) ->
  304. ConnPid = gun_open(Config),
  305. Ref = gun:request(ConnPid, "POST", Path, [
  306. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  307. ]),
  308. gun:data(ConnPid, Ref, nofin, Body),
  309. timer:sleep(2000),
  310. gun:data(ConnPid, Ref, fin, Body),
  311. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  312. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  313. gun:close(ConnPid),
  314. RespBody.
  315. %% We expect a crash.
  316. do_read_body_timeout(Path, Body, Config) ->
  317. ConnPid = gun_open(Config),
  318. Ref = gun:request(ConnPid, "POST", Path, [
  319. {<<"content-length">>, integer_to_binary(byte_size(Body))}
  320. ]),
  321. {response, _, 500, _} = gun:await(ConnPid, Ref),
  322. gun:close(ConnPid).
  323. read_urlencoded_body(Config) ->
  324. doc("application/x-www-form-urlencoded request body."),
  325. <<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
  326. <<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
  327. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
  328. = do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
  329. %% Send a 10MB body, larger than the default length, to ensure a crash occurs.
  330. ok = do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
  331. string:chars($a, 10000000), Config),
  332. %% We read any length for at most 1 second.
  333. %%
  334. %% The body is sent twice, first with nofin, then wait 1.1 second, then again with fin.
  335. %% We expect the handler to crash because read_urlencoded_body expects the full body.
  336. ok = do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config),
  337. %% The timeout value is set too low on purpose to ensure a crash occurs.
  338. ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
  339. ok.
  340. %% We expect a crash.
  341. do_read_urlencoded_body_too_large(Path, Body, Config) ->
  342. ConnPid = gun_open(Config),
  343. Ref = gun:request(ConnPid, "POST", Path, [
  344. {<<"content-length">>, integer_to_binary(iolist_size(Body))}
  345. ]),
  346. gun:data(ConnPid, Ref, fin, Body),
  347. {response, _, 500, _} = gun:await(ConnPid, Ref),
  348. gun:close(ConnPid).
  349. %% We expect a crash.
  350. do_read_urlencoded_body_too_long(Path, Body, Config) ->
  351. ConnPid = gun_open(Config),
  352. Ref = gun:request(ConnPid, "POST", Path, [
  353. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  354. ]),
  355. gun:data(ConnPid, Ref, nofin, Body),
  356. timer:sleep(1100),
  357. gun:data(ConnPid, Ref, fin, Body),
  358. {response, _, 500, _} = gun:await(ConnPid, Ref),
  359. gun:close(ConnPid).
  360. multipart(Config) ->
  361. doc("Multipart request body."),
  362. do_multipart("/multipart", Config).
  363. do_multipart(Path, Config) ->
  364. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  365. ReqBody = [
  366. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  367. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  368. "--deadbeef--"
  369. ],
  370. RespBody = do_body("POST", Path, [
  371. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  372. ], ReqBody, Config),
  373. [
  374. {#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
  375. {LargeHeaders, LargeBody}
  376. ] = binary_to_term(RespBody),
  377. #{
  378. <<"content-type">> := <<"application/octet-stream">>,
  379. <<"x-custom">> := <<"value">>
  380. } = LargeHeaders,
  381. ok.
  382. read_part_skip_body(Config) ->
  383. doc("Multipart request body skipping part bodies."),
  384. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  385. ReqBody = [
  386. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  387. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  388. "--deadbeef--"
  389. ],
  390. RespBody = do_body("POST", "/multipart/skip_body", [
  391. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  392. ], ReqBody, Config),
  393. [
  394. #{<<"content-type">> := <<"text/plain">>},
  395. LargeHeaders
  396. ] = binary_to_term(RespBody),
  397. #{
  398. <<"content-type">> := <<"application/octet-stream">>,
  399. <<"x-custom">> := <<"value">>
  400. } = LargeHeaders,
  401. ok.
  402. %% @todo When reading a multipart body, length and period
  403. %% only apply to a single read_body call. We may want a
  404. %% separate option to know how many reads we want to do
  405. %% before we give up.
  406. read_part2(Config) ->
  407. doc("Multipart request body using read_part/2."),
  408. %% Override the length and period values only, making
  409. %% the request process use more read_body calls.
  410. %%
  411. %% We do not try a custom timeout value since this would
  412. %% be the same test as read_body/2.
  413. do_multipart("/multipart/read_part2", Config).
  414. read_part_body2(Config) ->
  415. doc("Multipart request body using read_part_body/2."),
  416. %% Override the length and period values only, making
  417. %% the request process use more read_body calls.
  418. %%
  419. %% We do not try a custom timeout value since this would
  420. %% be the same test as read_body/2.
  421. do_multipart("/multipart/read_part_body2", Config).
  422. %% Tests: Response.
  423. %% @todo We want to crash when calling set_resp_* or related
  424. %% functions after the reply has been sent.
  425. set_resp_cookie(Config) ->
  426. doc("Response using set_resp_cookie."),
  427. %% Single cookie, no options.
  428. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  429. {_, <<"mycookie=myvalue; Version=1">>}
  430. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  431. %% Single cookie, with options.
  432. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  433. {_, <<"mycookie=myvalue; Version=1; Path=/resp/set_resp_cookie4">>}
  434. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  435. %% Multiple cookies.
  436. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  437. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  438. %% Overwrite previously set cookie.
  439. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  440. {_, <<"mycookie=overwrite; Version=1">>}
  441. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  442. ok.
  443. set_resp_header(Config) ->
  444. doc("Response using set_resp_header."),
  445. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  446. true = lists:keymember(<<"content-type">>, 1, Headers),
  447. ok.
  448. set_resp_headers(Config) ->
  449. doc("Response using set_resp_headers."),
  450. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
  451. true = lists:keymember(<<"content-type">>, 1, Headers),
  452. true = lists:keymember(<<"content-encoding">>, 1, Headers),
  453. ok.
  454. resp_header(Config) ->
  455. doc("Response header with/without default."),
  456. {200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
  457. {200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
  458. ok.
  459. resp_headers(Config) ->
  460. doc("Get all response headers."),
  461. {200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
  462. {200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
  463. ok.
  464. set_resp_body(Config) ->
  465. doc("Response using set_resp_body."),
  466. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  467. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  468. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  469. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  470. ok.
  471. set_resp_body_sendfile0(Config) ->
  472. doc("Response using set_resp_body with a sendfile of length 0."),
  473. Path = "/resp/set_resp_body/sendfile0",
  474. ConnPid = gun_open(Config),
  475. %% First request.
  476. Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  477. {response, IsFin, 200, _} = gun:await(ConnPid, Ref1),
  478. {ok, <<>>} = case IsFin of
  479. nofin -> gun:await_body(ConnPid, Ref1);
  480. fin -> {ok, <<>>}
  481. end,
  482. %% Second request will confirm everything works as intended.
  483. Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]),
  484. {response, IsFin, 200, _} = gun:await(ConnPid, Ref2),
  485. {ok, <<>>} = case IsFin of
  486. nofin -> gun:await_body(ConnPid, Ref2);
  487. fin -> {ok, <<>>}
  488. end,
  489. gun:close(ConnPid),
  490. ok.
  491. has_resp_header(Config) ->
  492. doc("Has response header?"),
  493. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  494. true = lists:keymember(<<"content-type">>, 1, Headers),
  495. ok.
  496. has_resp_body(Config) ->
  497. doc("Has response body?"),
  498. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  499. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  500. ok.
  501. delete_resp_header(Config) ->
  502. doc("Delete response header."),
  503. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  504. false = lists:keymember(<<"content-type">>, 1, Headers),
  505. ok.
  506. reply2(Config) ->
  507. doc("Response with default headers and no body."),
  508. {200, _, _} = do_get("/resp/reply2/200", Config),
  509. {201, _, _} = do_get("/resp/reply2/201", Config),
  510. {404, _, _} = do_get("/resp/reply2/404", Config),
  511. {200, _, _} = do_get("/resp/reply2/binary", Config),
  512. {500, _, _} = do_get("/resp/reply2/error", Config),
  513. %% @todo We want to crash when reply or stream_reply is called twice.
  514. %% How to test this properly? This isn't enough.
  515. {200, _, _} = do_get("/resp/reply2/twice", Config),
  516. ok.
  517. reply3(Config) ->
  518. doc("Response with additional headers and no body."),
  519. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  520. true = lists:keymember(<<"content-type">>, 1, Headers1),
  521. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  522. true = lists:keymember(<<"content-type">>, 1, Headers2),
  523. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  524. true = lists:keymember(<<"content-type">>, 1, Headers3),
  525. {500, _, _} = do_get("/resp/reply3/error", Config),
  526. ok.
  527. reply4(Config) ->
  528. doc("Response with additional headers and body."),
  529. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  530. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  531. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  532. {500, _, _} = do_get("/resp/reply4/error", Config),
  533. ok.
  534. %% @todo Crash when stream_reply is called twice.
  535. stream_reply2(Config) ->
  536. doc("Response with default headers and streamed body."),
  537. Body = <<0:8000000>>,
  538. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  539. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  540. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  541. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  542. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  543. ok.
  544. stream_reply3(Config) ->
  545. doc("Response with additional headers and streamed body."),
  546. Body = <<0:8000000>>,
  547. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  548. true = lists:keymember(<<"content-type">>, 1, Headers1),
  549. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  550. true = lists:keymember(<<"content-type">>, 1, Headers2),
  551. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  552. true = lists:keymember(<<"content-type">>, 1, Headers3),
  553. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  554. ok.
  555. %% @todo Crash when calling stream_body after the fin flag has been set.
  556. %% @todo Crash when calling stream_body after calling reply.
  557. %% @todo Crash when calling stream_body before calling stream_reply.
  558. %% Tests: Push.
  559. %% @todo We want to crash when push is called after reply has been initiated.
  560. push(Config) ->
  561. case config(protocol, Config) of
  562. http -> do_push_http("/resp/push", Config);
  563. http2 -> do_push_http2(Config)
  564. end.
  565. push_method(Config) ->
  566. case config(protocol, Config) of
  567. http -> do_push_http("/resp/push/method", Config);
  568. http2 -> do_push_http2_method(Config)
  569. end.
  570. push_origin(Config) ->
  571. case config(protocol, Config) of
  572. http -> do_push_http("/resp/push/origin", Config);
  573. http2 -> do_push_http2_origin(Config)
  574. end.
  575. push_qs(Config) ->
  576. case config(protocol, Config) of
  577. http -> do_push_http("/resp/push/qs", Config);
  578. http2 -> do_push_http2_qs(Config)
  579. end.
  580. do_push_http(Path, Config) ->
  581. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  582. ConnPid = gun_open(Config),
  583. Ref = gun:get(ConnPid, Path, []),
  584. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  585. ok.
  586. do_push_http2(Config) ->
  587. doc("Pushed responses."),
  588. ConnPid = gun_open(Config),
  589. Ref = gun:get(ConnPid, "/resp/push", []),
  590. %% We expect two pushed resources.
  591. Origin = iolist_to_binary([
  592. case config(type, Config) of
  593. tcp -> "http";
  594. ssl -> "https"
  595. end,
  596. "://localhost:",
  597. integer_to_binary(config(port, Config))
  598. ]),
  599. OriginLen = byte_size(Origin),
  600. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  601. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  602. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  603. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref),
  604. %% Pushed CSS.
  605. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  606. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  607. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  608. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  609. {response, fin, 406, _} = gun:await(ConnPid, PushTXT),
  610. %% Let's not forget about the response to the client's request.
  611. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  612. gun:close(ConnPid).
  613. do_push_http2_method(Config) ->
  614. doc("Pushed response with non-GET method."),
  615. ConnPid = gun_open(Config),
  616. Ref = gun:get(ConnPid, "/resp/push/method", []),
  617. %% Pushed CSS.
  618. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  619. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  620. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  621. %% Let's not forget about the response to the client's request.
  622. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  623. gun:close(ConnPid).
  624. do_push_http2_origin(Config) ->
  625. doc("Pushed response with custom scheme/host/port."),
  626. ConnPid = gun_open(Config),
  627. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  628. %% Pushed CSS.
  629. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  630. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  631. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  632. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  633. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  634. %% Let's not forget about the response to the client's request.
  635. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  636. gun:close(ConnPid).
  637. do_push_http2_qs(Config) ->
  638. doc("Pushed response with query string."),
  639. ConnPid = gun_open(Config),
  640. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  641. %% Pushed CSS.
  642. Origin = iolist_to_binary([
  643. case config(type, Config) of
  644. tcp -> "http";
  645. ssl -> "https"
  646. end,
  647. "://localhost:",
  648. integer_to_binary(config(port, Config))
  649. ]),
  650. OriginLen = byte_size(Origin),
  651. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  652. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  653. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  654. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  655. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  656. %% Let's not forget about the response to the client's request.
  657. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  658. gun:close(ConnPid).