http_SUITE.erl 37 KB

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