req_SUITE.erl 26 KB

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