http_SUITE.erl 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. %% Copyright (c) 2011-2013, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(http_SUITE).
  16. -include_lib("common_test/include/ct.hrl").
  17. %% ct.
  18. -export([all/0]).
  19. -export([groups/0]).
  20. -export([init_per_suite/1]).
  21. -export([end_per_suite/1]).
  22. -export([init_per_group/2]).
  23. -export([end_per_group/2]).
  24. %% Tests.
  25. -export([check_raw_status/1]).
  26. -export([check_status/1]).
  27. -export([chunked_response/1]).
  28. -export([echo_body/1]).
  29. -export([error_chain_handle_after_reply/1]).
  30. -export([error_chain_handle_before_reply/1]).
  31. -export([error_handle_after_reply/1]).
  32. -export([error_init_after_reply/1]).
  33. -export([error_init_reply_handle_error/1]).
  34. -export([headers_dupe/1]).
  35. -export([http10_chunkless/1]).
  36. -export([http10_hostless/1]).
  37. -export([keepalive_max/1]).
  38. -export([keepalive_nl/1]).
  39. -export([multipart/1]).
  40. -export([nc_rand/1]).
  41. -export([nc_zero/1]).
  42. -export([onrequest/1]).
  43. -export([onrequest_reply/1]).
  44. -export([onresponse_capitalize/1]).
  45. -export([onresponse_crash/1]).
  46. -export([onresponse_reply/1]).
  47. -export([pipeline/1]).
  48. -export([rest_bad_accept/1]).
  49. -export([rest_expires/1]).
  50. -export([rest_keepalive/1]).
  51. -export([rest_keepalive_post/1]).
  52. -export([rest_missing_get_callbacks/1]).
  53. -export([rest_missing_put_callbacks/1]).
  54. -export([rest_nodelete/1]).
  55. -export([rest_patch/1]).
  56. -export([rest_resource_etags/1]).
  57. -export([rest_resource_etags_if_none_match/1]).
  58. -export([set_resp_body/1]).
  59. -export([set_resp_header/1]).
  60. -export([set_resp_overwrite/1]).
  61. -export([slowloris/1]).
  62. -export([slowloris2/1]).
  63. -export([static_attribute_etag/1]).
  64. -export([static_function_etag/1]).
  65. -export([static_mimetypes_function/1]).
  66. -export([static_specify_file/1]).
  67. -export([static_specify_file_catchall/1]).
  68. -export([static_test_file/1]).
  69. -export([static_test_file_css/1]).
  70. -export([stream_body_set_resp/1]).
  71. -export([stream_body_set_resp_close/1]).
  72. -export([te_chunked/1]).
  73. -export([te_chunked_chopped/1]).
  74. -export([te_chunked_delayed/1]).
  75. -export([te_identity/1]).
  76. %% ct.
  77. all() ->
  78. [
  79. {group, http},
  80. {group, https},
  81. {group, http_compress},
  82. {group, https_compress},
  83. {group, onrequest},
  84. {group, onresponse},
  85. {group, onresponse_capitalize}
  86. ].
  87. groups() ->
  88. Tests = [
  89. check_raw_status,
  90. check_status,
  91. chunked_response,
  92. echo_body,
  93. error_chain_handle_after_reply,
  94. error_chain_handle_before_reply,
  95. error_handle_after_reply,
  96. error_init_after_reply,
  97. error_init_reply_handle_error,
  98. headers_dupe,
  99. http10_chunkless,
  100. http10_hostless,
  101. keepalive_max,
  102. keepalive_nl,
  103. multipart,
  104. nc_rand,
  105. nc_zero,
  106. pipeline,
  107. rest_bad_accept,
  108. rest_expires,
  109. rest_keepalive,
  110. rest_keepalive_post,
  111. rest_missing_get_callbacks,
  112. rest_missing_put_callbacks,
  113. rest_nodelete,
  114. rest_patch,
  115. rest_resource_etags,
  116. rest_resource_etags_if_none_match,
  117. set_resp_body,
  118. set_resp_header,
  119. set_resp_overwrite,
  120. slowloris,
  121. slowloris2,
  122. static_attribute_etag,
  123. static_function_etag,
  124. static_mimetypes_function,
  125. static_specify_file,
  126. static_specify_file_catchall,
  127. static_test_file,
  128. static_test_file_css,
  129. stream_body_set_resp,
  130. stream_body_set_resp_close,
  131. te_chunked,
  132. te_chunked_chopped,
  133. te_chunked_delayed,
  134. te_identity
  135. ],
  136. [
  137. {http, [], Tests},
  138. {https, [], Tests},
  139. {http_compress, [], Tests},
  140. {https_compress, [], Tests},
  141. {onrequest, [], [
  142. onrequest,
  143. onrequest_reply
  144. ]},
  145. {onresponse, [], [
  146. onresponse_crash,
  147. onresponse_reply
  148. ]},
  149. {onresponse_capitalize, [], [
  150. onresponse_capitalize
  151. ]}
  152. ].
  153. init_per_suite(Config) ->
  154. application:start(crypto),
  155. application:start(ranch),
  156. application:start(cowboy),
  157. Config.
  158. end_per_suite(_Config) ->
  159. application:stop(cowboy),
  160. application:stop(ranch),
  161. application:stop(crypto),
  162. ok.
  163. init_per_group(http, Config) ->
  164. Port = 33080,
  165. Transport = ranch_tcp,
  166. Config1 = init_static_dir(Config),
  167. {ok, _} = cowboy:start_http(http, 100, [{port, Port}], [
  168. {env, [{dispatch, init_dispatch(Config1)}]},
  169. {max_keepalive, 50},
  170. {timeout, 500}
  171. ]),
  172. {ok, Client} = cowboy_client:init([]),
  173. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  174. {transport, Transport}, {client, Client}|Config1];
  175. init_per_group(https, Config) ->
  176. Port = 33081,
  177. Transport = ranch_ssl,
  178. Opts = [
  179. {certfile, ?config(data_dir, Config) ++ "cert.pem"},
  180. {keyfile, ?config(data_dir, Config) ++ "key.pem"},
  181. {password, "cowboy"}
  182. ],
  183. Config1 = init_static_dir(Config),
  184. application:start(public_key),
  185. application:start(ssl),
  186. {ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, Port}], [
  187. {env, [{dispatch, init_dispatch(Config1)}]},
  188. {max_keepalive, 50},
  189. {timeout, 500}
  190. ]),
  191. {ok, Client} = cowboy_client:init(Opts),
  192. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  193. {transport, Transport}, {client, Client}|Config1];
  194. init_per_group(http_compress, Config) ->
  195. Port = 33082,
  196. Transport = ranch_tcp,
  197. Config1 = init_static_dir(Config),
  198. {ok, _} = cowboy:start_http(http_compress, 100, [{port, Port}], [
  199. {compress, true},
  200. {env, [{dispatch, init_dispatch(Config1)}]},
  201. {max_keepalive, 50},
  202. {timeout, 500}
  203. ]),
  204. {ok, Client} = cowboy_client:init([]),
  205. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  206. {transport, Transport}, {client, Client}|Config1];
  207. init_per_group(https_compress, Config) ->
  208. Port = 33083,
  209. Transport = ranch_ssl,
  210. Opts = [
  211. {certfile, ?config(data_dir, Config) ++ "cert.pem"},
  212. {keyfile, ?config(data_dir, Config) ++ "key.pem"},
  213. {password, "cowboy"}
  214. ],
  215. Config1 = init_static_dir(Config),
  216. application:start(public_key),
  217. application:start(ssl),
  218. {ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, Port}], [
  219. {compress, true},
  220. {env, [{dispatch, init_dispatch(Config1)}]},
  221. {max_keepalive, 50},
  222. {timeout, 500}
  223. ]),
  224. {ok, Client} = cowboy_client:init(Opts),
  225. [{scheme, <<"https">>}, {port, Port}, {opts, Opts},
  226. {transport, Transport}, {client, Client}|Config1];
  227. init_per_group(onrequest, Config) ->
  228. Port = 33084,
  229. Transport = ranch_tcp,
  230. {ok, _} = cowboy:start_http(onrequest, 100, [{port, Port}], [
  231. {env, [{dispatch, init_dispatch(Config)}]},
  232. {max_keepalive, 50},
  233. {onrequest, fun onrequest_hook/1},
  234. {timeout, 500}
  235. ]),
  236. {ok, Client} = cowboy_client:init([]),
  237. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  238. {transport, Transport}, {client, Client}|Config];
  239. init_per_group(onresponse, Config) ->
  240. Port = 33085,
  241. Transport = ranch_tcp,
  242. {ok, _} = cowboy:start_http(onresponse, 100, [{port, Port}], [
  243. {env, [{dispatch, init_dispatch(Config)}]},
  244. {max_keepalive, 50},
  245. {onresponse, fun onresponse_hook/4},
  246. {timeout, 500}
  247. ]),
  248. {ok, Client} = cowboy_client:init([]),
  249. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  250. {transport, Transport}, {client, Client}|Config];
  251. init_per_group(onresponse_capitalize, Config) ->
  252. Port = 33086,
  253. Transport = ranch_tcp,
  254. {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, Port}], [
  255. {env, [{dispatch, init_dispatch(Config)}]},
  256. {max_keepalive, 50},
  257. {onresponse, fun onresponse_capitalize_hook/4},
  258. {timeout, 500}
  259. ]),
  260. {ok, Client} = cowboy_client:init([]),
  261. [{scheme, <<"http">>}, {port, Port}, {opts, []},
  262. {transport, Transport}, {client, Client}|Config].
  263. end_per_group(Group, Config) when Group =:= https; Group =:= https_compress ->
  264. cowboy:stop_listener(https),
  265. application:stop(ssl),
  266. application:stop(public_key),
  267. end_static_dir(Config),
  268. ok;
  269. end_per_group(Group, Config) when Group =:= http; Group =:= http_compress ->
  270. cowboy:stop_listener(http),
  271. end_static_dir(Config);
  272. end_per_group(Name, _) ->
  273. cowboy:stop_listener(Name),
  274. ok.
  275. %% Dispatch configuration.
  276. init_dispatch(Config) ->
  277. cowboy_router:compile([
  278. {"localhost", [
  279. {"/chunked_response", chunked_handler, []},
  280. {"/init_shutdown", http_handler_init_shutdown, []},
  281. {"/long_polling", http_handler_long_polling, []},
  282. {"/headers/dupe", http_handler,
  283. [{headers, [{<<"connection">>, <<"close">>}]}]},
  284. {"/set_resp/header", http_handler_set_resp,
  285. [{headers, [{<<"vary">>, <<"Accept">>}]}]},
  286. {"/set_resp/overwrite", http_handler_set_resp,
  287. [{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
  288. {"/set_resp/body", http_handler_set_resp,
  289. [{body, <<"A flameless dance does not equal a cycle">>}]},
  290. {"/stream_body/set_resp", http_handler_stream_body,
  291. [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
  292. {"/stream_body/set_resp_close",
  293. http_handler_stream_body, [
  294. {reply, set_resp_close},
  295. {body, <<"stream_body_set_resp_close">>}]},
  296. {"/static/[...]", cowboy_static,
  297. [{directory, ?config(static_dir, Config)},
  298. {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
  299. {"/static_mimetypes_function/[...]", cowboy_static,
  300. [{directory, ?config(static_dir, Config)},
  301. {mimetypes, {fun(Path, data) when is_binary(Path) ->
  302. [<<"text/html">>] end, data}}]},
  303. {"/handler_errors", http_handler_errors, []},
  304. {"/static_attribute_etag/[...]", cowboy_static,
  305. [{directory, ?config(static_dir, Config)},
  306. {etag, {attributes, [filepath, filesize, inode, mtime]}}]},
  307. {"/static_function_etag/[...]", cowboy_static,
  308. [{directory, ?config(static_dir, Config)},
  309. {etag, {fun static_function_etag/2, etag_data}}]},
  310. {"/static_specify_file/[...]", cowboy_static,
  311. [{directory, ?config(static_dir, Config)},
  312. {mimetypes, [{<<".css">>, [<<"text/css">>]}]},
  313. {file, <<"test_file.css">>}]},
  314. {"/multipart", http_handler_multipart, []},
  315. {"/echo/body", http_handler_echo_body, []},
  316. {"/bad_accept", rest_simple_resource, []},
  317. {"/simple", rest_simple_resource, []},
  318. {"/forbidden_post", rest_forbidden_resource, [true]},
  319. {"/simple_post", rest_forbidden_resource, [false]},
  320. {"/missing_get_callbacks", rest_missing_callbacks, []},
  321. {"/missing_put_callbacks", rest_missing_callbacks, []},
  322. {"/nodelete", rest_nodelete_resource, []},
  323. {"/patch", rest_patch_resource, []},
  324. {"/resetags", rest_resource_etags, []},
  325. {"/rest_expires", rest_expires, []},
  326. {"/loop_timeout", http_handler_loop_timeout, []},
  327. {"/", http_handler, []}
  328. ]}
  329. ]).
  330. init_static_dir(Config) ->
  331. Dir = filename:join(?config(priv_dir, Config), "static"),
  332. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  333. ok = file:make_dir(Dir),
  334. ok = file:write_file(Level1("test_file"), "test_file\n"),
  335. ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
  336. ok = file:write_file(Level1("test_noread"), "test_noread\n"),
  337. ok = file:change_mode(Level1("test_noread"), 8#0333),
  338. ok = file:write_file(Level1("test.html"), "test.html\n"),
  339. ok = file:make_dir(Level1("test_dir")),
  340. [{static_dir, Dir}|Config].
  341. end_static_dir(Config) ->
  342. Dir = ?config(static_dir, Config),
  343. Level1 = fun(Name) -> filename:join(Dir, Name) end,
  344. ok = file:delete(Level1("test_file")),
  345. ok = file:delete(Level1("test_file.css")),
  346. ok = file:delete(Level1("test_noread")),
  347. ok = file:delete(Level1("test.html")),
  348. ok = file:del_dir(Level1("test_dir")),
  349. ok = file:del_dir(Dir),
  350. Config.
  351. %% Convenience functions.
  352. quick_raw(Data, Config) ->
  353. Client = ?config(client, Config),
  354. Transport = ?config(transport, Config),
  355. {ok, Client2} = cowboy_client:connect(
  356. Transport, "localhost", ?config(port, Config), Client),
  357. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  358. case cowboy_client:response(Client3) of
  359. {ok, Status, _, _} -> Status;
  360. {error, _} -> closed
  361. end.
  362. build_url(Path, Config) ->
  363. {scheme, Scheme} = lists:keyfind(scheme, 1, Config),
  364. {port, Port} = lists:keyfind(port, 1, Config),
  365. PortBin = list_to_binary(integer_to_list(Port)),
  366. PathBin = list_to_binary(Path),
  367. << Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
  368. quick_get(URL, Config) ->
  369. Client = ?config(client, Config),
  370. {ok, Client2} = cowboy_client:request(<<"GET">>,
  371. build_url(URL, Config), Client),
  372. {ok, Status, _, _} = cowboy_client:response(Client2),
  373. Status.
  374. body_to_chunks(_, <<>>, Acc) ->
  375. lists:reverse([<<"0\r\n\r\n">>|Acc]);
  376. body_to_chunks(ChunkSize, Body, Acc) ->
  377. BodySize = byte_size(Body),
  378. ChunkSize2 = case BodySize < ChunkSize of
  379. true -> BodySize;
  380. false -> ChunkSize
  381. end,
  382. << Chunk:ChunkSize2/binary, Rest/binary >> = Body,
  383. ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
  384. body_to_chunks(ChunkSize, Rest,
  385. [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
  386. %% Tests.
  387. check_raw_status(Config) ->
  388. Huge = [$0 || _ <- lists:seq(1, 5000)],
  389. HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
  390. "Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
  391. ResponsePacket =
  392. "HTTP/1.0 302 Found\r
  393. Location: http://www.google.co.il/\r
  394. Cache-Control: private\r
  395. Content-Type: text/html; charset=UTF-8\r
  396. Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com\r
  397. Date: Sun, 04 Dec 2011 15:55:01 GMT\r
  398. Server: gws\r
  399. Content-Length: 221\r
  400. X-XSS-Protection: 1; mode=block\r
  401. X-Frame-Options: SAMEORIGIN\r
  402. \r
  403. <HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
  404. <TITLE>302 Moved</TITLE></HEAD><BODY>
  405. <H1>302 Moved</H1>
  406. The document has moved
  407. <A HREF=\"http://www.google.co.il/\">here</A>.
  408. </BODY></HTML>",
  409. Tests = [
  410. {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
  411. "Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
  412. {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  413. {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
  414. {400, "\n"},
  415. {400, "Garbage\r\n\r\n"},
  416. {400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
  417. {400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
  418. {400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
  419. {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
  420. {505, ResponsePacket},
  421. {408, "GET / HTTP/1.1\r\n"},
  422. {408, "GET / HTTP/1.1\r\nHost: localhost"},
  423. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
  424. {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
  425. {414, Huge},
  426. {400, "GET / HTTP/1.1\r\n" ++ Huge},
  427. {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
  428. {closed, ""},
  429. {closed, "\r\n"},
  430. {closed, "\r\n\r\n"},
  431. {closed, "GET / HTTP/1.1"}
  432. ],
  433. _ = [{Status, Packet} = begin
  434. Ret = quick_raw(Packet, Config),
  435. {Ret, Packet}
  436. end || {Status, Packet} <- Tests].
  437. check_status(Config) ->
  438. Tests = [
  439. {102, "/long_polling"},
  440. {200, "/"},
  441. {200, "/simple"},
  442. {204, "/loop_timeout"},
  443. {400, "/static/%2f"},
  444. {400, "/static/%2e"},
  445. {400, "/static/%2e%2e"},
  446. {403, "/static/test_dir"},
  447. {403, "/static/test_dir/"},
  448. {403, "/static/test_noread"},
  449. {404, "/not/found"},
  450. {404, "/static/not_found"},
  451. {500, "/handler_errors?case=handler_before_reply"},
  452. {500, "/handler_errors?case=init_before_reply"},
  453. {666, "/init_shutdown"}
  454. ],
  455. _ = [{Status, URL} = begin
  456. Ret = quick_get(URL, Config),
  457. {Ret, URL}
  458. end || {Status, URL} <- Tests].
  459. chunked_response(Config) ->
  460. Client = ?config(client, Config),
  461. {ok, Client2} = cowboy_client:request(<<"GET">>,
  462. build_url("/chunked_response", Config), Client),
  463. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  464. true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
  465. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  466. {ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
  467. = Transport:recv(Socket, 44, 1000),
  468. {error, closed} = cowboy_client:response(Client3).
  469. %% Check if sending requests whose size is around the MTU breaks something.
  470. echo_body(Config) ->
  471. Client = ?config(client, Config),
  472. {ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
  473. [begin
  474. Body = list_to_binary(lists:duplicate(Size, $a)),
  475. {ok, Client2} = cowboy_client:request(<<"POST">>,
  476. build_url("/echo/body", Config),
  477. [{<<"connection">>, <<"close">>}],
  478. Body, Client),
  479. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  480. {ok, Body, _} = cowboy_client:response_body(Client3)
  481. end || Size <- lists:seq(MTU - 500, MTU)].
  482. error_chain_handle_after_reply(Config) ->
  483. Client = ?config(client, Config),
  484. {ok, Client2} = cowboy_client:request(<<"GET">>,
  485. build_url("/", Config), Client),
  486. {ok, Client3} = cowboy_client:request(<<"GET">>,
  487. build_url("/handler_errors?case=handle_after_reply", Config), Client2),
  488. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  489. {ok, 200, _, Client5} = cowboy_client:response(Client4),
  490. {error, closed} = cowboy_client:response(Client5).
  491. error_chain_handle_before_reply(Config) ->
  492. Client = ?config(client, Config),
  493. {ok, Client2} = cowboy_client:request(<<"GET">>,
  494. build_url("/", Config), Client),
  495. {ok, Client3} = cowboy_client:request(<<"GET">>,
  496. build_url("/handler_errors?case=handle_before_reply", Config), Client2),
  497. {ok, 200, _, Client4} = cowboy_client:response(Client3),
  498. {ok, 500, _, Client5} = cowboy_client:response(Client4),
  499. {error, closed} = cowboy_client:response(Client5).
  500. error_handle_after_reply(Config) ->
  501. Client = ?config(client, Config),
  502. {ok, Client2} = cowboy_client:request(<<"GET">>,
  503. build_url("/handler_errors?case=handle_after_reply", Config), Client),
  504. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  505. {error, closed} = cowboy_client:response(Client3).
  506. error_init_after_reply(Config) ->
  507. Client = ?config(client, Config),
  508. {ok, Client2} = cowboy_client:request(<<"GET">>,
  509. build_url("/handler_errors?case=init_after_reply", Config), Client),
  510. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  511. {error, closed} = cowboy_client:response(Client3).
  512. error_init_reply_handle_error(Config) ->
  513. Client = ?config(client, Config),
  514. {ok, Client2} = cowboy_client:request(<<"GET">>,
  515. build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
  516. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  517. {error, closed} = cowboy_client:response(Client3).
  518. headers_dupe(Config) ->
  519. Client = ?config(client, Config),
  520. {ok, Client2} = cowboy_client:request(<<"GET">>,
  521. build_url("/headers/dupe", Config), Client),
  522. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  523. {<<"connection">>, <<"close">>}
  524. = lists:keyfind(<<"connection">>, 1, Headers),
  525. Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
  526. 1 = length(Connections),
  527. {error, closed} = cowboy_client:response(Client3).
  528. http10_chunkless(Config) ->
  529. Client = ?config(client, Config),
  530. Transport = ?config(transport, Config),
  531. {ok, Client2} = cowboy_client:connect(
  532. Transport, "localhost", ?config(port, Config), Client),
  533. Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
  534. {ok, Client3} = cowboy_client:raw_request(Data, Client2),
  535. {ok, 200, Headers, Client4} = cowboy_client:response(Client3),
  536. false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
  537. %% Hack: we just try to get 28 bytes and compare.
  538. {ok, Transport, Socket} = cowboy_client:transport(Client4),
  539. Buffer = element(7, Client4),
  540. Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
  541. {ok, Recv} -> << Buffer/binary, Recv/binary >>;
  542. _ -> Buffer
  543. end,
  544. <<"chunked_handler\r\nworks fine!">> = Buffer2.
  545. http10_hostless(Config) ->
  546. Port10 = ?config(port, Config) + 10,
  547. Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
  548. ranch:start_listener(Name, 5,
  549. ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
  550. cowboy_protocol, [
  551. {env, [{dispatch, cowboy_router:compile([
  552. {'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
  553. {max_keepalive, 50},
  554. {timeout, 500}]
  555. ),
  556. 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
  557. [{port, Port10}|Config]),
  558. cowboy:stop_listener(http10).
  559. keepalive_max(Config) ->
  560. Client = ?config(client, Config),
  561. URL = build_url("/", Config),
  562. ok = keepalive_max_loop(Client, URL, 50).
  563. keepalive_max_loop(Client, _, 0) ->
  564. {error, closed} = cowboy_client:response(Client),
  565. ok;
  566. keepalive_max_loop(Client, URL, N) ->
  567. Headers = [{<<"connection">>, <<"keep-alive">>}],
  568. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  569. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  570. Expected = case N of
  571. 1 -> <<"close">>;
  572. N -> <<"keep-alive">>
  573. end,
  574. {<<"connection">>, Expected}
  575. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  576. keepalive_max_loop(Client3, URL, N - 1).
  577. keepalive_nl(Config) ->
  578. Client = ?config(client, Config),
  579. URL = build_url("/", Config),
  580. ok = keepalive_nl_loop(Client, URL, 10).
  581. keepalive_nl_loop(Client, _, 0) ->
  582. {error, closed} = cowboy_client:response(Client),
  583. ok;
  584. keepalive_nl_loop(Client, URL, N) ->
  585. Headers = [{<<"connection">>, <<"keep-alive">>}],
  586. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  587. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  588. {<<"connection">>, <<"keep-alive">>}
  589. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  590. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  591. ok = Transport:send(Socket, <<"\r\n">>), %% empty line
  592. keepalive_nl_loop(Client3, URL, N - 1).
  593. multipart(Config) ->
  594. Client = ?config(client, Config),
  595. Body = <<
  596. "This is a preamble."
  597. "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
  598. "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
  599. "\r\n--OHai--"
  600. "This is an epiloque."
  601. >>,
  602. {ok, Client2} = cowboy_client:request(<<"POST">>,
  603. build_url("/multipart", Config),
  604. [{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
  605. Body, Client),
  606. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  607. {ok, RespBody, _} = cowboy_client:response_body(Client3),
  608. Parts = binary_to_term(RespBody),
  609. Parts = [
  610. {[{<<"x-name">>, <<"answer">>}], <<"42">>},
  611. {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
  612. ].
  613. nc_reqs(Config, Input) ->
  614. Cat = os:find_executable("cat"),
  615. Nc = os:find_executable("nc"),
  616. case {Cat, Nc} of
  617. {false, _} ->
  618. {skip, {notfound, cat}};
  619. {_, false} ->
  620. {skip, {notfound, nc}};
  621. _Good ->
  622. %% Throw garbage at the server then check if it's still up.
  623. {port, Port} = lists:keyfind(port, 1, Config),
  624. StrPort = integer_to_list(Port),
  625. [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  626. || _ <- lists:seq(1, 100)],
  627. 200 = quick_get("/", Config)
  628. end.
  629. nc_rand(Config) ->
  630. nc_reqs(Config, "/dev/urandom").
  631. nc_zero(Config) ->
  632. nc_reqs(Config, "/dev/zero").
  633. onrequest(Config) ->
  634. Client = ?config(client, Config),
  635. {ok, Client2} = cowboy_client:request(<<"GET">>,
  636. build_url("/", Config), Client),
  637. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  638. {<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
  639. {ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
  640. onrequest_reply(Config) ->
  641. Client = ?config(client, Config),
  642. {ok, Client2} = cowboy_client:request(<<"GET">>,
  643. build_url("/?reply=1", Config), Client),
  644. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  645. {<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
  646. {ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
  647. %% Hook for the above onrequest tests.
  648. onrequest_hook(Req) ->
  649. case cowboy_req:qs_val(<<"reply">>, Req) of
  650. {undefined, Req2} ->
  651. cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
  652. {_, Req2} ->
  653. {ok, Req3} = cowboy_req:reply(
  654. 200, [], <<"replied!">>, Req2),
  655. Req3
  656. end.
  657. onresponse_capitalize(Config) ->
  658. Client = ?config(client, Config),
  659. {ok, Client2} = cowboy_client:request(<<"GET">>,
  660. build_url("/", Config), Client),
  661. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  662. {ok, Data} = Transport:recv(Socket, 0, 1000),
  663. false = nomatch =:= binary:match(Data, <<"Content-Length">>).
  664. %% Hook for the above onresponse_capitalize test.
  665. onresponse_capitalize_hook(Status, Headers, Body, Req) ->
  666. Headers2 = [{cowboy_bstr:capitalize_token(N), V}
  667. || {N, V} <- Headers],
  668. {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
  669. Req2.
  670. onresponse_crash(Config) ->
  671. Client = ?config(client, Config),
  672. {ok, Client2} = cowboy_client:request(<<"GET">>,
  673. build_url("/handler_errors?case=init_before_reply", Config), Client),
  674. {ok, 777, Headers, _} = cowboy_client:response(Client2),
  675. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
  676. onresponse_reply(Config) ->
  677. Client = ?config(client, Config),
  678. {ok, Client2} = cowboy_client:request(<<"GET">>,
  679. build_url("/", Config), Client),
  680. {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
  681. {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
  682. %% Make sure we don't get the body initially sent.
  683. {error, closed} = cowboy_client:response_body(Client3).
  684. %% Hook for the above onresponse tests.
  685. onresponse_hook(_, Headers, _, Req) ->
  686. {ok, Req2} = cowboy_req:reply(
  687. <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
  688. Req2.
  689. pipeline(Config) ->
  690. Client = ?config(client, Config),
  691. {ok, Client2} = cowboy_client:request(<<"GET">>,
  692. build_url("/", Config), Client),
  693. {ok, Client3} = cowboy_client:request(<<"GET">>,
  694. build_url("/", Config), Client2),
  695. {ok, Client4} = cowboy_client:request(<<"GET">>,
  696. build_url("/", Config), Client3),
  697. {ok, Client5} = cowboy_client:request(<<"GET">>,
  698. build_url("/", Config), Client4),
  699. {ok, Client6} = cowboy_client:request(<<"GET">>,
  700. build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
  701. {ok, 200, _, Client7} = cowboy_client:response(Client6),
  702. {ok, 200, _, Client8} = cowboy_client:response(Client7),
  703. {ok, 200, _, Client9} = cowboy_client:response(Client8),
  704. {ok, 200, _, Client10} = cowboy_client:response(Client9),
  705. {ok, 200, _, Client11} = cowboy_client:response(Client10),
  706. {error, closed} = cowboy_client:response(Client11).
  707. rest_bad_accept(Config) ->
  708. Client = ?config(client, Config),
  709. {ok, Client2} = cowboy_client:request(<<"GET">>,
  710. build_url("/bad_accept", Config),
  711. [{<<"accept">>, <<"1">>}],
  712. Client),
  713. {ok, 400, _, _} = cowboy_client:response(Client2).
  714. rest_expires(Config) ->
  715. Client = ?config(client, Config),
  716. {ok, Client2} = cowboy_client:request(<<"GET">>,
  717. build_url("/rest_expires", Config), Client),
  718. {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
  719. {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
  720. {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
  721. Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
  722. ok.
  723. rest_keepalive(Config) ->
  724. Client = ?config(client, Config),
  725. URL = build_url("/simple", Config),
  726. ok = rest_keepalive_loop(Client, URL, 10).
  727. rest_keepalive_loop(_, _, 0) ->
  728. ok;
  729. rest_keepalive_loop(Client, URL, N) ->
  730. Headers = [{<<"connection">>, <<"keep-alive">>}],
  731. {ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
  732. {ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
  733. {<<"connection">>, <<"keep-alive">>}
  734. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  735. rest_keepalive_loop(Client3, URL, N - 1).
  736. rest_keepalive_post(Config) ->
  737. Client = ?config(client, Config),
  738. ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
  739. rest_keepalive_post_loop(_, _, _, 0) ->
  740. ok;
  741. rest_keepalive_post_loop(Config, Client, simple_post, N) ->
  742. Headers = [
  743. {<<"connection">>, <<"keep-alive">>},
  744. {<<"content-type">>, <<"text/plain">>}
  745. ],
  746. {ok, Client2} = cowboy_client:request(<<"POST">>,
  747. build_url("/simple_post", Config), Headers, "12345", Client),
  748. {ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
  749. {<<"connection">>, <<"keep-alive">>}
  750. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  751. rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
  752. rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
  753. Headers = [
  754. {<<"connection">>, <<"keep-alive">>},
  755. {<<"content-type">>, <<"text/plain">>}
  756. ],
  757. {ok, Client2} = cowboy_client:request(<<"POST">>,
  758. build_url("/forbidden_post", Config), Headers, "12345", Client),
  759. {ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
  760. {<<"connection">>, <<"keep-alive">>}
  761. = lists:keyfind(<<"connection">>, 1, RespHeaders),
  762. rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
  763. rest_missing_get_callbacks(Config) ->
  764. Client = ?config(client, Config),
  765. {ok, Client2} = cowboy_client:request(<<"GET">>,
  766. build_url("/missing_get_callbacks", Config), Client),
  767. {ok, 500, _, _} = cowboy_client:response(Client2).
  768. rest_missing_put_callbacks(Config) ->
  769. Client = ?config(client, Config),
  770. {ok, Client2} = cowboy_client:request(<<"PUT">>,
  771. build_url("/missing_put_callbacks", Config),
  772. [{<<"content-type">>, <<"application/json">>}],
  773. <<"{}">>, Client),
  774. {ok, 500, _, _} = cowboy_client:response(Client2).
  775. rest_nodelete(Config) ->
  776. Client = ?config(client, Config),
  777. {ok, Client2} = cowboy_client:request(<<"DELETE">>,
  778. build_url("/nodelete", Config), Client),
  779. {ok, 500, _, _} = cowboy_client:response(Client2).
  780. rest_patch(Config) ->
  781. Tests = [
  782. {204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
  783. {500, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
  784. {400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
  785. {415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
  786. ],
  787. Client = ?config(client, Config),
  788. _ = [begin
  789. {ok, Client2} = cowboy_client:request(<<"PATCH">>,
  790. build_url("/patch", Config), Headers, Body, Client),
  791. {ok, Status, _, _} = cowboy_client:response(Client2),
  792. ok
  793. end || {Status, Headers, Body} <- Tests].
  794. rest_resource_get_etag(Config, Type) ->
  795. rest_resource_get_etag(Config, Type, []).
  796. rest_resource_get_etag(Config, Type, Headers) ->
  797. Client = ?config(client, Config),
  798. {ok, Client2} = cowboy_client:request(<<"GET">>,
  799. build_url("/resetags?type=" ++ Type, Config), Headers, Client),
  800. {ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
  801. case lists:keyfind(<<"etag">>, 1, RespHeaders) of
  802. false -> {Status, false};
  803. {<<"etag">>, ETag} -> {Status, ETag}
  804. end.
  805. rest_resource_etags(Config) ->
  806. Tests = [
  807. {200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  808. {200, <<"\"etag-header-value\"">>, "tuple-strong"},
  809. {200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  810. {200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
  811. {500, false, "binary-strong-unquoted"},
  812. {500, false, "binary-weak-unquoted"}
  813. ],
  814. _ = [{Status, ETag, Type} = begin
  815. {Ret, RespETag} = rest_resource_get_etag(Config, Type),
  816. {Ret, RespETag, Type}
  817. end || {Status, ETag, Type} <- Tests].
  818. rest_resource_etags_if_none_match(Config) ->
  819. Tests = [
  820. {304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
  821. {304, <<"\"etag-header-value\"">>, "tuple-strong"},
  822. {304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
  823. {304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
  824. ],
  825. _ = [{Status, Type} = begin
  826. {Ret, _} = rest_resource_get_etag(Config, Type,
  827. [{<<"if-none-match">>, ETag}]),
  828. {Ret, Type}
  829. end || {Status, ETag, Type} <- Tests].
  830. set_resp_body(Config) ->
  831. Client = ?config(client, Config),
  832. {ok, Client2} = cowboy_client:request(<<"GET">>,
  833. build_url("/set_resp/body", Config), Client),
  834. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  835. {ok, <<"A flameless dance does not equal a cycle">>, _}
  836. = cowboy_client:response_body(Client3).
  837. set_resp_header(Config) ->
  838. Client = ?config(client, Config),
  839. {ok, Client2} = cowboy_client:request(<<"GET">>,
  840. build_url("/set_resp/header", Config), Client),
  841. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  842. {<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
  843. {<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
  844. set_resp_overwrite(Config) ->
  845. Client = ?config(client, Config),
  846. {ok, Client2} = cowboy_client:request(<<"GET">>,
  847. build_url("/set_resp/overwrite", Config), Client),
  848. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  849. {<<"server">>, <<"DesireDrive/1.0">>}
  850. = lists:keyfind(<<"server">>, 1, Headers).
  851. slowloris(Config) ->
  852. Client = ?config(client, Config),
  853. Transport = ?config(transport, Config),
  854. {ok, Client2} = cowboy_client:connect(
  855. Transport, "localhost", ?config(port, Config), Client),
  856. try
  857. [begin
  858. {ok, _} = cowboy_client:raw_request([C], Client2),
  859. receive after 25 -> ok end
  860. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  861. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  862. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  863. error(failure)
  864. catch error:{badmatch, _} ->
  865. ok
  866. end.
  867. slowloris2(Config) ->
  868. Client = ?config(client, Config),
  869. Transport = ?config(transport, Config),
  870. {ok, Client2} = cowboy_client:connect(
  871. Transport, "localhost", ?config(port, Config), Client),
  872. {ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
  873. receive after 300 -> ok end,
  874. {ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
  875. receive after 300 -> ok end,
  876. {ok, 408, _, _} = cowboy_client:response(Client2).
  877. static_attribute_etag(Config) ->
  878. Client = ?config(client, Config),
  879. {ok, Client2} = cowboy_client:request(<<"GET">>,
  880. build_url("/static_attribute_etag/test.html", Config), Client),
  881. {ok, Client3} = cowboy_client:request(<<"GET">>,
  882. build_url("/static_attribute_etag/test.html", Config), Client2),
  883. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  884. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  885. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  886. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  887. false = ETag1 =:= undefined,
  888. ETag1 = ETag2.
  889. static_function_etag(Config) ->
  890. Client = ?config(client, Config),
  891. {ok, Client2} = cowboy_client:request(<<"GET">>,
  892. build_url("/static_function_etag/test.html", Config), Client),
  893. {ok, Client3} = cowboy_client:request(<<"GET">>,
  894. build_url("/static_function_etag/test.html", Config), Client2),
  895. {ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
  896. {ok, 200, Headers2, _} = cowboy_client:response(Client4),
  897. {<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  898. {<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  899. false = ETag1 =:= undefined,
  900. ETag1 = ETag2.
  901. %% Callback function for generating the ETag for the above test.
  902. static_function_etag(Arguments, etag_data) ->
  903. {_, Filepath} = lists:keyfind(filepath, 1, Arguments),
  904. {_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
  905. {_, _INode} = lists:keyfind(inode, 1, Arguments),
  906. {_, _Modified} = lists:keyfind(mtime, 1, Arguments),
  907. ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
  908. [Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
  909. {strong, iolist_to_binary(Checksum)}.
  910. static_mimetypes_function(Config) ->
  911. Client = ?config(client, Config),
  912. {ok, Client2} = cowboy_client:request(<<"GET">>,
  913. build_url("/static_mimetypes_function/test.html", Config), Client),
  914. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  915. {<<"content-type">>, <<"text/html">>}
  916. = lists:keyfind(<<"content-type">>, 1, Headers).
  917. static_specify_file(Config) ->
  918. Client = ?config(client, Config),
  919. {ok, Client2} = cowboy_client:request(<<"GET">>,
  920. build_url("/static_specify_file", Config), Client),
  921. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  922. {<<"content-type">>, <<"text/css">>}
  923. = lists:keyfind(<<"content-type">>, 1, Headers),
  924. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  925. static_specify_file_catchall(Config) ->
  926. Client = ?config(client, Config),
  927. {ok, Client2} = cowboy_client:request(<<"GET">>,
  928. build_url("/static_specify_file/none", Config), Client),
  929. {ok, 200, Headers, Client3} = cowboy_client:response(Client2),
  930. {<<"content-type">>, <<"text/css">>}
  931. = lists:keyfind(<<"content-type">>, 1, Headers),
  932. {ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
  933. static_test_file(Config) ->
  934. Client = ?config(client, Config),
  935. {ok, Client2} = cowboy_client:request(<<"GET">>,
  936. build_url("/static/test_file", Config), Client),
  937. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  938. {<<"content-type">>, <<"application/octet-stream">>}
  939. = lists:keyfind(<<"content-type">>, 1, Headers).
  940. static_test_file_css(Config) ->
  941. Client = ?config(client, Config),
  942. {ok, Client2} = cowboy_client:request(<<"GET">>,
  943. build_url("/static/test_file.css", Config), Client),
  944. {ok, 200, Headers, _} = cowboy_client:response(Client2),
  945. {<<"content-type">>, <<"text/css">>}
  946. = lists:keyfind(<<"content-type">>, 1, Headers).
  947. stream_body_set_resp(Config) ->
  948. Client = ?config(client, Config),
  949. {ok, Client2} = cowboy_client:request(<<"GET">>,
  950. build_url("/stream_body/set_resp", Config), Client),
  951. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  952. {ok, <<"stream_body_set_resp">>, _}
  953. = cowboy_client:response_body(Client3).
  954. stream_body_set_resp_close(Config) ->
  955. Client = ?config(client, Config),
  956. {ok, Client2} = cowboy_client:request(<<"GET">>,
  957. build_url("/stream_body/set_resp_close", Config), Client),
  958. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  959. {ok, Transport, Socket} = cowboy_client:transport(Client3),
  960. case element(7, Client3) of
  961. <<"stream_body_set_resp_close">> ->
  962. ok;
  963. Buffer ->
  964. {ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
  965. <<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
  966. ok
  967. end,
  968. {error, closed} = Transport:recv(Socket, 0, 1000).
  969. te_chunked(Config) ->
  970. Client = ?config(client, Config),
  971. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  972. Chunks = body_to_chunks(50, Body, []),
  973. {ok, Client2} = cowboy_client:request(<<"GET">>,
  974. build_url("/echo/body", Config),
  975. [{<<"transfer-encoding">>, <<"chunked">>}],
  976. Chunks, Client),
  977. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  978. {ok, Body, _} = cowboy_client:response_body(Client3).
  979. te_chunked_chopped(Config) ->
  980. Client = ?config(client, Config),
  981. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  982. Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
  983. {ok, Client2} = cowboy_client:request(<<"GET">>,
  984. build_url("/echo/body", Config),
  985. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  986. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  987. _ = [begin
  988. ok = Transport:send(Socket, << C >>),
  989. ok = timer:sleep(10)
  990. end || << C >> <= Body2],
  991. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  992. {ok, Body, _} = cowboy_client:response_body(Client3).
  993. te_chunked_delayed(Config) ->
  994. Client = ?config(client, Config),
  995. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  996. Chunks = body_to_chunks(50, Body, []),
  997. {ok, Client2} = cowboy_client:request(<<"GET">>,
  998. build_url("/echo/body", Config),
  999. [{<<"transfer-encoding">>, <<"chunked">>}], Client),
  1000. {ok, Transport, Socket} = cowboy_client:transport(Client2),
  1001. _ = [begin
  1002. ok = Transport:send(Socket, Chunk),
  1003. ok = timer:sleep(10)
  1004. end || Chunk <- Chunks],
  1005. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1006. {ok, Body, _} = cowboy_client:response_body(Client3).
  1007. te_identity(Config) ->
  1008. Client = ?config(client, Config),
  1009. Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
  1010. {ok, Client2} = cowboy_client:request(<<"GET">>,
  1011. build_url("/echo/body", Config), [], Body, Client),
  1012. {ok, 200, _, Client3} = cowboy_client:response(Client2),
  1013. {ok, Body, _} = cowboy_client:response_body(Client3).