req_SUITE.erl 26 KB

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