req_SUITE.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. %% Copyright (c) 2016, 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. AllTests = ct_helper:all(?MODULE),
  24. [
  25. {http, [parallel], AllTests},
  26. {https, [parallel], AllTests},
  27. {h2, [parallel], AllTests},
  28. {h2c, [parallel], AllTests}
  29. %% @todo With compression enabled.
  30. ].
  31. init_per_suite(Config) ->
  32. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  33. Config.
  34. end_per_suite(Config) ->
  35. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  36. init_per_group(Name, Config) ->
  37. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  38. end_per_group(Name, _) ->
  39. cowboy:stop_listener(Name).
  40. %% Routes.
  41. init_dispatch(Config) ->
  42. cowboy_router:compile([{"[...]", [
  43. {"/static/[...]", cowboy_static, {dir, config(priv_dir, Config) ++ "/static"}},
  44. %% @todo Seriously InitialState should be optional.
  45. {"/resp/:key[/:arg]", resp_h, []},
  46. {"/multipart[/:key]", multipart_h, []},
  47. {"/args/:key/:arg[/:default]", echo_h, []},
  48. {"/crash/:key/period", echo_h, #{length => infinity, period => 1000, crash => true}},
  49. {"/no-opts/:key", echo_h, #{crash => true}},
  50. {"/opts/:key/length", echo_h, #{length => 1000}},
  51. {"/opts/:key/period", echo_h, #{length => infinity, period => 1000}},
  52. {"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
  53. {"/full/:key", echo_h, []},
  54. {"/no/:key", echo_h, []},
  55. {"/:key/[...]", echo_h, []}
  56. ]}]).
  57. %% Internal.
  58. do_body(Method, Path, Config) ->
  59. do_body(Method, Path, [], Config).
  60. do_body(Method, Path, Headers, Config) ->
  61. do_body(Method, Path, Headers, <<>>, Config).
  62. do_body(Method, Path, Headers, Body, Config) ->
  63. ConnPid = gun_open(Config),
  64. Ref = case Body of
  65. <<>> -> gun:request(ConnPid, Method, Path, Headers);
  66. _ -> gun:request(ConnPid, Method, Path, Headers, Body)
  67. end,
  68. {response, IsFin, 200, _} = gun:await(ConnPid, Ref),
  69. {ok, RespBody} = case IsFin of
  70. nofin -> gun:await_body(ConnPid, Ref);
  71. fin -> {ok, <<>>}
  72. end,
  73. gun:close(ConnPid),
  74. RespBody.
  75. do_get(Path, Config) ->
  76. ConnPid = gun_open(Config),
  77. Ref = gun:get(ConnPid, Path, []),
  78. {response, IsFin, Status, Headers} = gun:await(ConnPid, Ref),
  79. {ok, RespBody} = case IsFin of
  80. nofin -> gun:await_body(ConnPid, Ref);
  81. fin -> {ok, <<>>}
  82. end,
  83. gun:close(ConnPid),
  84. {Status, Headers, RespBody}.
  85. do_get_body(Path, Config) ->
  86. do_get_body(Path, [], Config).
  87. do_get_body(Path, Headers, Config) ->
  88. do_body("GET", Path, Headers, Config).
  89. %% Tests: Request.
  90. binding(Config) ->
  91. doc("Value bound from request URI path with/without default."),
  92. <<"binding">> = do_get_body("/args/binding/key", Config),
  93. <<"binding">> = do_get_body("/args/binding/key/default", Config),
  94. <<"default">> = do_get_body("/args/binding/undefined/default", Config),
  95. ok.
  96. %% @todo Do we really want a key/value list here instead of a map?
  97. bindings(Config) ->
  98. doc("Values bound from request URI path."),
  99. <<"[{key,<<\"bindings\">>}]">> = do_get_body("/bindings", Config),
  100. ok.
  101. header(Config) ->
  102. doc("Request header with/without default."),
  103. <<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config),
  104. <<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config),
  105. <<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config),
  106. ok.
  107. headers(Config) ->
  108. doc("Request headers."),
  109. << "#{<<\"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. %% This function is tested more extensively through unit tests.
  138. ok.
  139. method(Config) ->
  140. doc("Request method."),
  141. <<"GET">> = do_body("GET", "/method", Config),
  142. <<>> = do_body("HEAD", "/method", Config),
  143. <<"OPTIONS">> = do_body("OPTIONS", "/method", Config),
  144. <<"PATCH">> = do_body("PATCH", "/method", Config),
  145. <<"POST">> = do_body("POST", "/method", Config),
  146. <<"PUT">> = do_body("PUT", "/method", Config),
  147. <<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", "/method", Config),
  148. ok.
  149. %% @todo Do we really want a key/value list here instead of a map?
  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. %% @todo Do we really want a key/value list here instead of a map?
  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. %% @todo Do we really want a key/value list here instead of a map?
  302. read_urlencoded_body(Config) ->
  303. doc("application/x-www-form-urlencoded request body."),
  304. <<"[]">> = do_body("POST", "/read_urlencoded_body", [], <<>>, Config),
  305. <<"[{<<\"abc\">>,true}]">> = do_body("POST", "/read_urlencoded_body", [], "abc", Config),
  306. <<"[{<<\"a\">>,<<\"b\">>},{<<\"c\">>,<<\"d e\">>}]">>
  307. = do_body("POST", "/read_urlencoded_body", [], "a=b&c=d+e", Config),
  308. %% Send a 10MB body, larger than the default length, to ensure a crash occurs.
  309. ok = do_read_urlencoded_body_too_large("/no-opts/read_urlencoded_body",
  310. string:chars($a, 10000000), Config),
  311. %% We read any length for at most 1 second.
  312. %%
  313. %% The body is sent twice, first with nofin, then wait 1.1 second, then again with fin.
  314. %% We expect the handler to crash because read_urlencoded_body expects the full body.
  315. ok = do_read_urlencoded_body_too_long("/crash/read_urlencoded_body/period", <<"abc">>, Config),
  316. %% The timeout value is set too low on purpose to ensure a crash occurs.
  317. ok = do_read_body_timeout("/opts/read_urlencoded_body/timeout", <<"abc">>, Config),
  318. ok.
  319. %% We expect a crash.
  320. do_read_urlencoded_body_too_large(Path, Body, Config) ->
  321. ConnPid = gun_open(Config),
  322. Ref = gun:request(ConnPid, "POST", Path, [
  323. {<<"content-length">>, integer_to_binary(iolist_size(Body))}
  324. ]),
  325. gun:data(ConnPid, Ref, fin, Body),
  326. {response, _, 500, _} = gun:await(ConnPid, Ref),
  327. gun:close(ConnPid).
  328. %% We expect a crash.
  329. do_read_urlencoded_body_too_long(Path, Body, Config) ->
  330. ConnPid = gun_open(Config),
  331. Ref = gun:request(ConnPid, "POST", Path, [
  332. {<<"content-length">>, integer_to_binary(byte_size(Body) * 2)}
  333. ]),
  334. gun:data(ConnPid, Ref, nofin, Body),
  335. timer:sleep(1100),
  336. gun:data(ConnPid, Ref, fin, Body),
  337. {response, _, 500, _} = gun:await(ConnPid, Ref),
  338. gun:close(ConnPid).
  339. multipart(Config) ->
  340. doc("Multipart request body."),
  341. do_multipart("/multipart", Config).
  342. do_multipart(Path, Config) ->
  343. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  344. ReqBody = [
  345. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  346. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  347. "--deadbeef--"
  348. ],
  349. RespBody = do_body("POST", Path, [
  350. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  351. ], ReqBody, Config),
  352. [
  353. {[{<<"content-type">>, <<"text/plain">>}], <<"Cowboy is an HTTP server.">>},
  354. {LargeHeaders, LargeBody}
  355. ] = binary_to_term(RespBody),
  356. %% @todo Multipart header order is currently undefined.
  357. [
  358. {<<"content-type">>, <<"application/octet-stream">>},
  359. {<<"x-custom">>, <<"value">>}
  360. ] = lists:sort(LargeHeaders),
  361. ok.
  362. read_part_skip_body(Config) ->
  363. doc("Multipart request body skipping part bodies."),
  364. LargeBody = iolist_to_binary(string:chars($a, 10000000)),
  365. ReqBody = [
  366. "--deadbeef\r\nContent-Type: text/plain\r\n\r\nCowboy is an HTTP server.\r\n"
  367. "--deadbeef\r\nContent-Type: application/octet-stream\r\nX-Custom: value\r\n\r\n", LargeBody, "\r\n"
  368. "--deadbeef--"
  369. ],
  370. RespBody = do_body("POST", "/multipart/skip_body", [
  371. {<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
  372. ], ReqBody, Config),
  373. [
  374. [{<<"content-type">>, <<"text/plain">>}],
  375. LargeHeaders
  376. ] = binary_to_term(RespBody),
  377. %% @todo Multipart header order is currently undefined.
  378. [
  379. {<<"content-type">>, <<"application/octet-stream">>},
  380. {<<"x-custom">>, <<"value">>}
  381. ] = lists:sort(LargeHeaders),
  382. ok.
  383. %% @todo When reading a multipart body, length and period
  384. %% only apply to a single read_body call. We may want a
  385. %% separate option to know how many reads we want to do
  386. %% before we give up.
  387. read_part2(Config) ->
  388. doc("Multipart request body using read_part/2."),
  389. %% Override the length and period values only, making
  390. %% the request process use more read_body calls.
  391. %%
  392. %% We do not try a custom timeout value since this would
  393. %% be the same test as read_body/2.
  394. do_multipart("/multipart/read_part2", Config).
  395. read_part_body2(Config) ->
  396. doc("Multipart request body using read_part_body/2."),
  397. %% Override the length and period values only, making
  398. %% the request process use more read_body calls.
  399. %%
  400. %% We do not try a custom timeout value since this would
  401. %% be the same test as read_body/2.
  402. do_multipart("/multipart/read_part_body2", Config).
  403. %% Tests: Response.
  404. %% @todo We want to crash when calling set_resp_* or related
  405. %% functions after the reply has been sent.
  406. set_resp_cookie(Config) ->
  407. doc("Response using set_resp_cookie."),
  408. %% Single cookie, no options.
  409. {200, Headers1, _} = do_get("/resp/set_resp_cookie3", Config),
  410. {_, <<"mycookie=myvalue; Version=1">>}
  411. = lists:keyfind(<<"set-cookie">>, 1, Headers1),
  412. %% Single cookie, with options.
  413. {200, Headers2, _} = do_get("/resp/set_resp_cookie4", Config),
  414. {_, <<"mycookie=myvalue; Version=1; Path=/resp/set_resp_cookie4">>}
  415. = lists:keyfind(<<"set-cookie">>, 1, Headers2),
  416. %% Multiple cookies.
  417. {200, Headers3, _} = do_get("/resp/set_resp_cookie3/multiple", Config),
  418. [_, _] = [H || H={<<"set-cookie">>, _} <- Headers3],
  419. %% Overwrite previously set cookie.
  420. {200, Headers4, _} = do_get("/resp/set_resp_cookie3/overwrite", Config),
  421. {_, <<"mycookie=overwrite; Version=1">>}
  422. = lists:keyfind(<<"set-cookie">>, 1, Headers4),
  423. ok.
  424. set_resp_header(Config) ->
  425. doc("Response using set_resp_header."),
  426. {200, Headers, <<"OK">>} = do_get("/resp/set_resp_header", Config),
  427. true = lists:keymember(<<"content-type">>, 1, Headers),
  428. ok.
  429. set_resp_body(Config) ->
  430. doc("Response using set_resp_body."),
  431. {200, _, <<"OK">>} = do_get("/resp/set_resp_body", Config),
  432. {200, _, <<"OVERRIDE">>} = do_get("/resp/set_resp_body/override", Config),
  433. {ok, AppFile} = file:read_file(code:where_is_file("cowboy.app")),
  434. {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config),
  435. ok.
  436. has_resp_header(Config) ->
  437. doc("Has response header?"),
  438. {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config),
  439. true = lists:keymember(<<"content-type">>, 1, Headers),
  440. ok.
  441. has_resp_body(Config) ->
  442. doc("Has response body?"),
  443. {200, _, <<"OK">>} = do_get("/resp/has_resp_body", Config),
  444. {200, _, <<"OK">>} = do_get("/resp/has_resp_body/sendfile", Config),
  445. ok.
  446. delete_resp_header(Config) ->
  447. doc("Delete response header."),
  448. {200, Headers, <<"OK">>} = do_get("/resp/delete_resp_header", Config),
  449. false = lists:keymember(<<"content-type">>, 1, Headers),
  450. ok.
  451. reply2(Config) ->
  452. doc("Response with default headers and no body."),
  453. {200, _, _} = do_get("/resp/reply2/200", Config),
  454. {201, _, _} = do_get("/resp/reply2/201", Config),
  455. {404, _, _} = do_get("/resp/reply2/404", Config),
  456. {200, _, _} = do_get("/resp/reply2/binary", Config),
  457. {500, _, _} = do_get("/resp/reply2/error", Config),
  458. %% @todo We want to crash when reply or stream_reply is called twice.
  459. %% How to test this properly? This isn't enough.
  460. {200, _, _} = do_get("/resp/reply2/twice", Config),
  461. ok.
  462. reply3(Config) ->
  463. doc("Response with additional headers and no body."),
  464. {200, Headers1, _} = do_get("/resp/reply3/200", Config),
  465. true = lists:keymember(<<"content-type">>, 1, Headers1),
  466. {201, Headers2, _} = do_get("/resp/reply3/201", Config),
  467. true = lists:keymember(<<"content-type">>, 1, Headers2),
  468. {404, Headers3, _} = do_get("/resp/reply3/404", Config),
  469. true = lists:keymember(<<"content-type">>, 1, Headers3),
  470. {500, _, _} = do_get("/resp/reply3/error", Config),
  471. ok.
  472. reply4(Config) ->
  473. doc("Response with additional headers and body."),
  474. {200, _, <<"OK">>} = do_get("/resp/reply4/200", Config),
  475. {201, _, <<"OK">>} = do_get("/resp/reply4/201", Config),
  476. {404, _, <<"OK">>} = do_get("/resp/reply4/404", Config),
  477. {500, _, _} = do_get("/resp/reply4/error", Config),
  478. ok.
  479. %% @todo Crash when stream_reply is called twice.
  480. stream_reply2(Config) ->
  481. doc("Response with default headers and streamed body."),
  482. Body = <<0:8000000>>,
  483. {200, _, Body} = do_get("/resp/stream_reply2/200", Config),
  484. {201, _, Body} = do_get("/resp/stream_reply2/201", Config),
  485. {404, _, Body} = do_get("/resp/stream_reply2/404", Config),
  486. {200, _, Body} = do_get("/resp/stream_reply2/binary", Config),
  487. {500, _, _} = do_get("/resp/stream_reply2/error", Config),
  488. ok.
  489. stream_reply3(Config) ->
  490. doc("Response with additional headers and streamed body."),
  491. Body = <<0:8000000>>,
  492. {200, Headers1, Body} = do_get("/resp/stream_reply3/200", Config),
  493. true = lists:keymember(<<"content-type">>, 1, Headers1),
  494. {201, Headers2, Body} = do_get("/resp/stream_reply3/201", Config),
  495. true = lists:keymember(<<"content-type">>, 1, Headers2),
  496. {404, Headers3, Body} = do_get("/resp/stream_reply3/404", Config),
  497. true = lists:keymember(<<"content-type">>, 1, Headers3),
  498. {500, _, _} = do_get("/resp/stream_reply3/error", Config),
  499. ok.
  500. %% @todo Crash when calling stream_body after the fin flag has been set.
  501. %% @todo Crash when calling stream_body after calling reply.
  502. %% @todo Crash when calling stream_body before calling stream_reply.
  503. %% Tests: Push.
  504. %% @todo We want to crash when push is called after reply has been initiated.
  505. push(Config) ->
  506. case config(protocol, Config) of
  507. http -> do_push_http("/resp/push", Config);
  508. http2 -> do_push_http2(Config)
  509. end.
  510. push_method(Config) ->
  511. case config(protocol, Config) of
  512. http -> do_push_http("/resp/push/method", Config);
  513. http2 -> do_push_http2_method(Config)
  514. end.
  515. push_origin(Config) ->
  516. case config(protocol, Config) of
  517. http -> do_push_http("/resp/push/origin", Config);
  518. http2 -> do_push_http2_origin(Config)
  519. end.
  520. push_qs(Config) ->
  521. case config(protocol, Config) of
  522. http -> do_push_http("/resp/push/qs", Config);
  523. http2 -> do_push_http2_qs(Config)
  524. end.
  525. do_push_http(Path, Config) ->
  526. doc("Ignore pushed responses when protocol is HTTP/1.1."),
  527. ConnPid = gun_open(Config),
  528. Ref = gun:get(ConnPid, Path, []),
  529. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  530. ok.
  531. do_push_http2(Config) ->
  532. doc("Pushed responses."),
  533. ConnPid = gun_open(Config),
  534. Ref = gun:get(ConnPid, "/resp/push", []),
  535. %% We expect two pushed resources.
  536. Origin = iolist_to_binary([
  537. case config(type, Config) of
  538. tcp -> "http";
  539. ssl -> "https"
  540. end,
  541. "://localhost:",
  542. integer_to_binary(config(port, Config))
  543. ]),
  544. OriginLen = byte_size(Origin),
  545. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css">>,
  546. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  547. {push, PushTXT, <<"GET">>, <<Origin:OriginLen/binary, "/static/plain.txt">>,
  548. [{<<"accept">>,<<"text/plain">>}]} = gun:await(ConnPid, Ref),
  549. %% Pushed CSS.
  550. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  551. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  552. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  553. %% Pushed TXT is 406 because the pushed accept header uses an undefined type.
  554. {response, fin, 406, _} = gun:await(ConnPid, PushTXT),
  555. %% Let's not forget about the response to the client's request.
  556. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  557. gun:close(ConnPid).
  558. do_push_http2_method(Config) ->
  559. doc("Pushed response with non-GET method."),
  560. ConnPid = gun_open(Config),
  561. Ref = gun:get(ConnPid, "/resp/push/method", []),
  562. %% Pushed CSS.
  563. {push, PushCSS, <<"HEAD">>, _, [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  564. {response, fin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  565. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  566. %% Let's not forget about the response to the client's request.
  567. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  568. gun:close(ConnPid).
  569. do_push_http2_origin(Config) ->
  570. doc("Pushed response with custom scheme/host/port."),
  571. ConnPid = gun_open(Config),
  572. Ref = gun:get(ConnPid, "/resp/push/origin", []),
  573. %% Pushed CSS.
  574. {push, PushCSS, <<"GET">>, <<"ftp://127.0.0.1:21/static/style.css">>,
  575. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  576. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  577. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  578. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  579. %% Let's not forget about the response to the client's request.
  580. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  581. gun:close(ConnPid).
  582. do_push_http2_qs(Config) ->
  583. doc("Pushed response with query string."),
  584. ConnPid = gun_open(Config),
  585. Ref = gun:get(ConnPid, "/resp/push/qs", []),
  586. %% Pushed CSS.
  587. Origin = iolist_to_binary([
  588. case config(type, Config) of
  589. tcp -> "http";
  590. ssl -> "https"
  591. end,
  592. "://localhost:",
  593. integer_to_binary(config(port, Config))
  594. ]),
  595. OriginLen = byte_size(Origin),
  596. {push, PushCSS, <<"GET">>, <<Origin:OriginLen/binary, "/static/style.css?server=cowboy&version=2.0">>,
  597. [{<<"accept">>,<<"text/css">>}]} = gun:await(ConnPid, Ref),
  598. {response, nofin, 200, HeadersCSS} = gun:await(ConnPid, PushCSS),
  599. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, HeadersCSS),
  600. {ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, PushCSS),
  601. %% Let's not forget about the response to the client's request.
  602. {response, fin, 200, _} = gun:await(ConnPid, Ref),
  603. gun:close(ConnPid).