http_SUITE.erl 38 KB

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