req_SUITE.erl 25 KB

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