req_SUITE.erl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. {"/: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. #{
  355. <<"content-type">> := <<"application/octet-stream">>,
  356. <<"x-custom">> := <<"value">>
  357. } = LargeHeaders,
  358. ok.
  359. read_part_skip_body(Config) ->
  360. doc("Multipart request body skipping part bodies."),
  361. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  362. ReqBody = [
  363. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  364. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  365. "--deadbeef--"
  366. ],
  367. RespBody = do_body("POST", "/multipart/skip_body", [
  368. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  369. ], ReqBody, Config),
  370. [
  371. #{<<"content-type">> := <<"text/plain">>},
  372. LargeHeaders
  373. ] = binary_to_term(RespBody),
  374. #{
  375. <<"content-type">> := <<"application/octet-stream">>,
  376. <<"x-custom">> := <<"value">>
  377. } = LargeHeaders,
  378. ok.
  379. %% @todo When reading a multipart body, length and period
  380. %% only apply to a single read_body call. We may want a
  381. %% separate option to know how many reads we want to do
  382. %% before we give up.
  383. read_part2(Config) ->
  384. doc("Multipart request body using read_part/2."),
  385. %% Override the length and period values only, making
  386. %% the request process use more read_body calls.
  387. %%
  388. %% We do not try a custom timeout value since this would
  389. %% be the same test as read_body/2.
  390. do_multipart("/multipart/read_part2", Config).
  391. read_part_body2(Config) ->
  392. doc("Multipart request body using read_part_body/2."),
  393. %% Override the length and period values only, making
  394. %% the request process use more read_body calls.
  395. %%
  396. %% We do not try a custom timeout value since this would
  397. %% be the same test as read_body/2.
  398. do_multipart("/multipart/read_part_body2", Config).
  399. %% Tests: Response.
  400. %% @todo We want to crash when calling set_resp_* or related
  401. %% functions after the reply has been sent.
  402. set_resp_cookie(Config) ->
  403. doc("Response using set_resp_cookie."),
  404. %% Single cookie, no options.
  405. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  406. {_, <<"mycookie=myvalue; Version=1">>}
  407. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  408. %% Single cookie, with options.
  409. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  410. {_, <<"mycookie=myvalue; Version=1; Path=/resp/set_resp_cookie4">>}
  411. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  412. %% Multiple cookies.
  413. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  414. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  415. %% Overwrite previously set cookie.
  416. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  417. {_, <<"mycookie=overwrite; Version=1">>}
  418. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  419. ok.
  420. set_resp_header(Config) ->
  421. doc("Response using set_resp_header."),
  422. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  423. true = lists:keymember(<<"content-type">>, 1, Headers),
  424. ok.
  425. set_resp_headers(Config) ->
  426. doc("Response using set_resp_headers."),
  427. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
  428. true = lists:keymember(<<"content-type">>, 1, Headers),
  429. true = lists:keymember(<<"content-encoding">>, 1, Headers),
  430. ok.
  431. resp_header(Config) ->
  432. doc("Response header with/without default."),
  433. {200, _, <<"OK">>} = do_get("/resp/resp_header_defined", Config),
  434. {200, _, <<"OK">>} = do_get("/resp/resp_header_default", Config),
  435. ok.
  436. resp_headers(Config) ->
  437. doc("Get all response headers."),
  438. {200, _, <<"OK">>} = do_get("/resp/resp_headers", Config),
  439. {200, _, <<"OK">>} = do_get("/resp/resp_headers_empty", Config),
  440. ok.
  441. set_resp_body(Config) ->
  442. doc("Response using set_resp_body."),
  443. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  444. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  445. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  446. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  447. ok.
  448. has_resp_header(Config) ->
  449. doc("Has response header?"),
  450. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  451. true = lists:keymember(<<"content-type">>, 1, Headers),
  452. ok.
  453. has_resp_body(Config) ->
  454. doc("Has response body?"),
  455. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  456. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  457. ok.
  458. delete_resp_header(Config) ->
  459. doc("Delete response header."),
  460. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  461. false = lists:keymember(<<"content-type">>, 1, Headers),
  462. ok.
  463. reply2(Config) ->
  464. doc("Response with default headers and no body."),
  465. {200, _, _} = do_get("/resp/reply2/200", Config),
  466. {201, _, _} = do_get("/resp/reply2/201", Config),
  467. {404, _, _} = do_get("/resp/reply2/404", Config),
  468. {200, _, _} = do_get("/resp/reply2/binary", Config),
  469. {500, _, _} = do_get("/resp/reply2/error", Config),
  470. %% @todo We want to crash when reply or stream_reply is called twice.
  471. %% How to test this properly? This isn't enough.
  472. {200, _, _} = do_get("/resp/reply2/twice", Config),
  473. ok.
  474. reply3(Config) ->
  475. doc("Response with additional headers and no body."),
  476. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  477. true = lists:keymember(<<"content-type">>, 1, Headers1),
  478. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  479. true = lists:keymember(<<"content-type">>, 1, Headers2),
  480. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  481. true = lists:keymember(<<"content-type">>, 1, Headers3),
  482. {500, _, _} = do_get("/resp/reply3/error", Config),
  483. ok.
  484. reply4(Config) ->
  485. doc("Response with additional headers and body."),
  486. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  487. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  488. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  489. {500, _, _} = do_get("/resp/reply4/error", Config),
  490. ok.
  491. %% @todo Crash when stream_reply is called twice.
  492. stream_reply2(Config) ->
  493. doc("Response with default headers and streamed body."),
  494. Body = <<0:8000000>>,
  495. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  496. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  497. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  498. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  499. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  500. ok.
  501. stream_reply3(Config) ->
  502. doc("Response with additional headers and streamed body."),
  503. Body = <<0:8000000>>,
  504. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  505. true = lists:keymember(<<"content-type">>, 1, Headers1),
  506. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  507. true = lists:keymember(<<"content-type">>, 1, Headers2),
  508. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  509. true = lists:keymember(<<"content-type">>, 1, Headers3),
  510. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  511. ok.
  512. %% @todo Crash when calling stream_body after the fin flag has been set.
  513. %% @todo Crash when calling stream_body after calling reply.
  514. %% @todo Crash when calling stream_body before calling stream_reply.
  515. %% Tests: Push.
  516. %% @todo We want to crash when push is called after reply has been initiated.
  517. push(Config) ->
  518. case config(protocol, Config) of
  519. http -> do_push_http("/resp/push", Config);
  520. http2 -> do_push_http2(Config)
  521. end.
  522. push_method(Config) ->
  523. case config(protocol, Config) of
  524. http -> do_push_http("/resp/push/method", Config);
  525. http2 -> do_push_http2_method(Config)
  526. end.
  527. push_origin(Config) ->
  528. case config(protocol, Config) of
  529. http -> do_push_http("/resp/push/origin", Config);
  530. http2 -> do_push_http2_origin(Config)
  531. end.
  532. push_qs(Config) ->
  533. case config(protocol, Config) of
  534. http -> do_push_http("/resp/push/qs", Config);
  535. http2 -> do_push_http2_qs(Config)
  536. end.
  537. do_push_http(Path, Config) ->
  538. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  539. ConnPid = gun_open(Config),
  540. Ref = gun:get(ConnPid, Path, []),
  541. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  542. ok.
  543. do_push_http2(Config) ->
  544. doc("Pushed responses."),
  545. ConnPid = gun_open(Config),
  546. Ref = gun:get(ConnPid, "/resp/push", []),
  547. %% We expect two pushed resources.
  548. Origin = iolist_to_binary([
  549. case config(type, Config) of
  550. tcp -> "http";
  551. ssl -> "https"
  552. end,
  553. "://localhost:",
  554. integer_to_binary(config(port, Config))
  555. ]),
  556. OriginLen = byte_size(Origin),
  557. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  558. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  559. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  560. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref),
  561. %% Pushed CSS.
  562. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  563. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  564. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  565. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  566. {response, fin, 406, _} = gun:await(ConnPid, PushTXT),
  567. %% Let's not forget about the response to the client's request.
  568. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  569. gun:close(ConnPid).
  570. do_push_http2_method(Config) ->
  571. doc("Pushed response with non-GET method."),
  572. ConnPid = gun_open(Config),
  573. Ref = gun:get(ConnPid, "/resp/push/method", []),
  574. %% Pushed CSS.
  575. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  576. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  577. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  578. %% Let's not forget about the response to the client's request.
  579. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  580. gun:close(ConnPid).
  581. do_push_http2_origin(Config) ->
  582. doc("Pushed response with custom scheme/host/port."),
  583. ConnPid = gun_open(Config),
  584. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  585. %% Pushed CSS.
  586. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  587. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  588. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  589. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  590. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  591. %% Let's not forget about the response to the client's request.
  592. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  593. gun:close(ConnPid).
  594. do_push_http2_qs(Config) ->
  595. doc("Pushed response with query string."),
  596. ConnPid = gun_open(Config),
  597. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  598. %% Pushed CSS.
  599. Origin = iolist_to_binary([
  600. case config(type, Config) of
  601. tcp -> "http";
  602. ssl -> "https"
  603. end,
  604. "://localhost:",
  605. integer_to_binary(config(port, Config))
  606. ]),
  607. OriginLen = byte_size(Origin),
  608. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  609. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  610. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  611. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  612. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  613. %% Let's not forget about the response to the client's request.
  614. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  615. gun:close(ConnPid).