http_SUITE.erl 43 KB

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