static_handler_SUITE.erl 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. %% Copyright (c) 2016-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(static_handler_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. %% ct.
  21. all() ->
  22. cowboy_test:common_all() ++ [
  23. {group, http_no_sendfile},
  24. {group, h2c_no_sendfile}
  25. ].
  26. groups() ->
  27. AllTests = ct_helper:all(?MODULE),
  28. %% The directory tests are shared between dir and priv_dir options.
  29. DirTests = lists:usort([F || {F, 1} <- ?MODULE:module_info(exports),
  30. string:substr(atom_to_list(F), 1, 4) =:= "dir_"
  31. ]),
  32. OtherTests = AllTests -- DirTests,
  33. GroupTests = OtherTests ++ [
  34. {dir, [parallel], DirTests},
  35. {priv_dir, [parallel], DirTests}
  36. ],
  37. [
  38. {http, [parallel], GroupTests},
  39. {https, [parallel], GroupTests},
  40. {h2, [parallel], GroupTests},
  41. {h2c, [parallel], GroupTests},
  42. {http_compress, [parallel], GroupTests},
  43. {https_compress, [parallel], GroupTests},
  44. {h2_compress, [parallel], GroupTests},
  45. {h2c_compress, [parallel], GroupTests},
  46. %% No real need to test sendfile disabled against https or h2.
  47. {http_no_sendfile, [parallel], GroupTests},
  48. {h2c_no_sendfile, [parallel], GroupTests}
  49. ].
  50. init_per_suite(Config) ->
  51. %% Two static folders are created: one in ct_helper's private directory,
  52. %% and one in the test run private directory.
  53. PrivDir = code:priv_dir(ct_helper) ++ "/static",
  54. StaticDir = config(priv_dir, Config) ++ "/static",
  55. ct_helper:create_static_dir(PrivDir),
  56. ct_helper:create_static_dir(StaticDir),
  57. init_large_file(PrivDir ++ "/large.bin"),
  58. init_large_file(StaticDir ++ "/large.bin"),
  59. %% Add a simple Erlang application archive containing one file
  60. %% in its priv directory.
  61. true = code:add_pathz(filename:join(
  62. [config(data_dir, Config), "static_files_app", "ebin"])),
  63. ok = application:load(static_files_app),
  64. %% A special folder contains files of 1 character from 1 to 127
  65. %% excluding / and \ as they are always rejected.
  66. CharDir = config(priv_dir, Config) ++ "/char",
  67. ok = filelib:ensure_dir(CharDir ++ "/file"),
  68. Chars0 = lists:flatten([case file:write_file(CharDir ++ [$/, C], [C]) of
  69. ok -> C;
  70. {error, _} -> []
  71. end || C <- (lists:seq(1, 127) -- "/\\")]),
  72. %% Determine whether we are on a case insensitive filesystem and
  73. %% remove uppercase characters in that case. On case insensitive
  74. %% filesystems we end up overwriting the "A" file with the "a" contents.
  75. {CaseSensitive, Chars} = case file:read_file(CharDir ++ "/A") of
  76. {ok, <<"A">>} -> {true, Chars0};
  77. {ok, <<"a">>} -> {false, Chars0 -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
  78. end,
  79. [{static_dir, StaticDir}, {char_dir, CharDir},
  80. {chars, Chars}, {case_sensitive, CaseSensitive}|Config].
  81. end_per_suite(Config) ->
  82. %% Special directory.
  83. CharDir = config(char_dir, Config),
  84. _ = [file:delete(CharDir ++ [$/, C]) || C <- lists:seq(0, 127)],
  85. _ = file:del_dir(CharDir),
  86. %% Static directories.
  87. StaticDir = config(static_dir, Config),
  88. PrivDir = code:priv_dir(ct_helper) ++ "/static",
  89. %% This file is not created on Windows.
  90. _ = file:delete(StaticDir ++ "/large.bin"),
  91. _ = file:delete(PrivDir ++ "/large.bin"),
  92. ct_helper:delete_static_dir(StaticDir),
  93. ct_helper:delete_static_dir(PrivDir).
  94. init_per_group(dir, Config) ->
  95. [{prefix, "/dir"}|Config];
  96. init_per_group(priv_dir, Config) ->
  97. [{prefix, "/priv_dir"}|Config];
  98. init_per_group(Name=http_no_sendfile, Config) ->
  99. cowboy_test:init_http(Name, #{
  100. env => #{dispatch => init_dispatch(Config)},
  101. middlewares => [?MODULE, cowboy_router, cowboy_handler],
  102. sendfile => false
  103. }, [{flavor, vanilla}|Config]);
  104. init_per_group(Name=h2c_no_sendfile, Config) ->
  105. Config1 = cowboy_test:init_http(Name, #{
  106. env => #{dispatch => init_dispatch(Config)},
  107. middlewares => [?MODULE, cowboy_router, cowboy_handler],
  108. sendfile => false
  109. }, [{flavor, vanilla}|Config]),
  110. lists:keyreplace(protocol, 1, Config1, {protocol, http2});
  111. init_per_group(Name, Config) ->
  112. Config1 = cowboy_test:init_common_groups(Name, Config, ?MODULE),
  113. Opts = ranch:get_protocol_options(Name),
  114. ok = ranch:set_protocol_options(Name, Opts#{
  115. middlewares => [?MODULE, cowboy_router, cowboy_handler]
  116. }),
  117. Config1.
  118. end_per_group(Name, _) ->
  119. cowboy:stop_listener(Name).
  120. %% Large file.
  121. init_large_file(Filename) ->
  122. case os:type() of
  123. {unix, _} ->
  124. "" = os:cmd("truncate -s 32M " ++ Filename),
  125. ok;
  126. {win32, _} ->
  127. Size = 32*1024*1024,
  128. ok = file:write_file(Filename, <<0:Size/unit:8>>)
  129. end.
  130. %% Routes.
  131. init_dispatch(Config) ->
  132. cowboy_router:compile([{'_', [
  133. {"/priv_dir/[...]", cowboy_static, {priv_dir, ct_helper, "static"}},
  134. {"/dir/[...]", cowboy_static, {dir, config(static_dir, Config)}},
  135. {"/priv_file/style.css", cowboy_static, {priv_file, ct_helper, "static/style.css"}},
  136. {"/file/style.css", cowboy_static, {file, config(static_dir, Config) ++ "/style.css"}},
  137. {"/index", cowboy_static, {file, config(static_dir, Config) ++ "/index.html"}},
  138. {"/mime/all/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  139. [{mimetypes, cow_mimetypes, all}]}},
  140. {"/mime/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  141. [{mimetypes, ?MODULE, do_mime_custom}]}},
  142. {"/mime/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  143. [{mimetypes, ?MODULE, do_mime_crash}]}},
  144. {"/mime/hardcode/binary-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy",
  145. [{mimetypes, <<"application/vnd.ninenines.cowboy+xml;v=1">>}]}},
  146. {"/mime/hardcode/tuple-form", cowboy_static, {priv_file, ct_helper, "static/file.cowboy",
  147. [{mimetypes, {<<"application">>, <<"vnd.ninenines.cowboy+xml">>, [{<<"v">>, <<"1">>}]}}]}},
  148. {"/charset/custom/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  149. [{charset, ?MODULE, do_charset_custom}]}},
  150. {"/charset/crash/[...]", cowboy_static, {priv_dir, ct_helper, "static",
  151. [{charset, ?MODULE, do_charset_crash}]}},
  152. {"/charset/hardcode/[...]", cowboy_static, {priv_file, ct_helper, "static/index.html",
  153. [{charset, <<"utf-8">>}]}},
  154. {"/etag/custom", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  155. [{etag, ?MODULE, do_etag_custom}]}},
  156. {"/etag/crash", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  157. [{etag, ?MODULE, do_etag_crash}]}},
  158. {"/etag/disable", cowboy_static, {file, config(static_dir, Config) ++ "/style.css",
  159. [{etag, false}]}},
  160. {"/bad", cowboy_static, bad},
  161. {"/bad/priv_dir/app/[...]", cowboy_static, {priv_dir, bad_app, "static"}},
  162. {"/bad/priv_dir/no-priv/[...]", cowboy_static, {priv_dir, cowboy, "static"}},
  163. {"/bad/priv_dir/path/[...]", cowboy_static, {priv_dir, ct_helper, "bad"}},
  164. {"/bad/priv_dir/route", cowboy_static, {priv_dir, ct_helper, "static"}},
  165. {"/bad/dir/path/[...]", cowboy_static, {dir, "/bad/path"}},
  166. {"/bad/dir/route", cowboy_static, {dir, config(static_dir, Config)}},
  167. {"/bad/priv_file/app", cowboy_static, {priv_file, bad_app, "static/style.css"}},
  168. {"/bad/priv_file/no-priv", cowboy_static, {priv_file, cowboy, "static/style.css"}},
  169. {"/bad/priv_file/path", cowboy_static, {priv_file, ct_helper, "bad/style.css"}},
  170. {"/bad/file/path", cowboy_static, {file, "/bad/path/style.css"}},
  171. {"/bad/options", cowboy_static, {priv_file, ct_helper, "static/style.css", bad}},
  172. {"/bad/options/mime", cowboy_static, {priv_file, ct_helper, "static/style.css", [{mimetypes, bad}]}},
  173. {"/bad/options/charset", cowboy_static, {priv_file, ct_helper, "static/style.css", [{charset, bad}]}},
  174. {"/bad/options/etag", cowboy_static, {priv_file, ct_helper, "static/style.css", [{etag, true}]}},
  175. {"/unknown/option", cowboy_static, {priv_file, ct_helper, "static/style.css", [{bad, option}]}},
  176. {"/char/[...]", cowboy_static, {dir, config(char_dir, Config)}},
  177. {"/ez_priv_file/index.html", cowboy_static, {priv_file, static_files_app, "www/index.html"}},
  178. {"/bad/ez_priv_file/index.php", cowboy_static, {priv_file, static_files_app, "www/index.php"}},
  179. {"/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "www"}},
  180. {"/bad/ez_priv_dir/[...]", cowboy_static, {priv_dir, static_files_app, "cgi-bin"}}
  181. ]}]).
  182. %% Middleware interface to silence expected errors.
  183. execute(Req=#{path := Path}, Env) ->
  184. case Path of
  185. <<"/bad/priv_dir/app/", _/bits>> -> ct_helper:ignore(cowboy_static, priv_path, 2);
  186. <<"/bad/priv_file/app">> -> ct_helper:ignore(cowboy_static, priv_path, 2);
  187. <<"/bad/priv_dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1);
  188. <<"/bad/dir/route">> -> ct_helper:ignore(cowboy_static, escape_reserved, 1);
  189. <<"/bad">> -> ct_helper:ignore(cowboy_static, init_opts, 2);
  190. <<"/bad/options">> -> ct_helper:ignore(cowboy_static, content_types_provided, 2);
  191. <<"/bad/options/mime">> -> ct_helper:ignore(cowboy_rest, set_content_type, 2);
  192. <<"/bad/options/etag">> -> ct_helper:ignore(cowboy_static, generate_etag, 2);
  193. <<"/bad/options/charset">> -> ct_helper:ignore(cowboy_static, charsets_provided, 2);
  194. _ -> ok
  195. end,
  196. {ok, Req, Env}.
  197. %% Internal functions.
  198. -spec do_charset_crash(_) -> no_return().
  199. do_charset_crash(_) ->
  200. ct_helper_error_h:ignore(?MODULE, do_charset_crash, 1),
  201. exit(crash).
  202. do_charset_custom(Path) ->
  203. case filename:extension(Path) of
  204. <<".cowboy">> -> <<"utf-32">>;
  205. <<".html">> -> <<"utf-16">>;
  206. _ -> <<"utf-8">>
  207. end.
  208. -spec do_etag_crash(_, _, _) -> no_return().
  209. do_etag_crash(_, _, _) ->
  210. ct_helper_error_h:ignore(?MODULE, do_etag_crash, 3),
  211. exit(crash).
  212. do_etag_custom(_, _, _) ->
  213. {strong, <<"etag">>}.
  214. -spec do_mime_crash(_) -> no_return().
  215. do_mime_crash(_) ->
  216. ct_helper_error_h:ignore(?MODULE, do_mime_crash, 1),
  217. exit(crash).
  218. do_mime_custom(Path) ->
  219. case filename:extension(Path) of
  220. <<".cowboy">> -> <<"application/vnd.ninenines.cowboy+xml;v=1">>;
  221. <<".txt">> -> <<"text/plain">>;
  222. _ -> {<<"application">>, <<"octet-stream">>, []}
  223. end.
  224. do_get(Path, Config) ->
  225. do_get(Path, [], Config).
  226. do_get(Path, ReqHeaders, Config) ->
  227. ConnPid = gun_open(Config),
  228. Ref = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}|ReqHeaders]),
  229. {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
  230. {ok, Body} = case IsFin of
  231. nofin -> gun:await_body(ConnPid, Ref);
  232. fin -> {ok, <<>>}
  233. end,
  234. gun:close(ConnPid),
  235. {Status, RespHeaders, Body}.
  236. %% Tests.
  237. bad(Config) ->
  238. doc("Bad cowboy_static options: not a tuple."),
  239. {500, _, _} = do_get("/bad", Config),
  240. ok.
  241. bad_dir_path(Config) ->
  242. doc("Bad cowboy_static options: wrong path."),
  243. {404, _, _} = do_get("/bad/dir/path/style.css", Config),
  244. ok.
  245. bad_dir_route(Config) ->
  246. doc("Bad cowboy_static options: missing [...] in route."),
  247. {500, _, _} = do_get("/bad/dir/route", Config),
  248. ok.
  249. bad_file_in_priv_dir_in_ez_archive(Config) ->
  250. doc("Get a missing file from a priv_dir stored in Erlang application .ez archive."),
  251. {404, _, _} = do_get("/ez_priv_dir/index.php", Config),
  252. ok.
  253. bad_file_path(Config) ->
  254. doc("Bad cowboy_static options: wrong path."),
  255. {404, _, _} = do_get("/bad/file/path", Config),
  256. ok.
  257. bad_options(Config) ->
  258. doc("Bad cowboy_static extra options: not a list."),
  259. {500, _, _} = do_get("/bad/options", Config),
  260. ok.
  261. bad_options_charset(Config) ->
  262. doc("Bad cowboy_static extra options: invalid charset option."),
  263. {500, _, _} = do_get("/bad/options/charset", Config),
  264. ok.
  265. bad_options_etag(Config) ->
  266. doc("Bad cowboy_static extra options: invalid etag option."),
  267. {500, _, _} = do_get("/bad/options/etag", Config),
  268. ok.
  269. bad_options_mime(Config) ->
  270. doc("Bad cowboy_static extra options: invalid mimetypes option."),
  271. {500, _, _} = do_get("/bad/options/mime", Config),
  272. ok.
  273. bad_priv_dir_app(Config) ->
  274. doc("Bad cowboy_static options: wrong application name."),
  275. {500, _, _} = do_get("/bad/priv_dir/app/style.css", Config),
  276. ok.
  277. bad_priv_dir_in_ez_archive(Config) ->
  278. doc("Bad cowboy_static options: priv_dir path missing from Erlang application .ez archive."),
  279. {404, _, _} = do_get("/bad/ez_priv_dir/index.html", Config),
  280. ok.
  281. bad_priv_dir_no_priv(Config) ->
  282. doc("Bad cowboy_static options: application has no priv directory."),
  283. {404, _, _} = do_get("/bad/priv_dir/no-priv/style.css", Config),
  284. ok.
  285. bad_priv_dir_path(Config) ->
  286. doc("Bad cowboy_static options: wrong path."),
  287. {404, _, _} = do_get("/bad/priv_dir/path/style.css", Config),
  288. ok.
  289. bad_priv_dir_route(Config) ->
  290. doc("Bad cowboy_static options: missing [...] in route."),
  291. {500, _, _} = do_get("/bad/priv_dir/route", Config),
  292. ok.
  293. bad_priv_file_app(Config) ->
  294. doc("Bad cowboy_static options: wrong application name."),
  295. {500, _, _} = do_get("/bad/priv_file/app", Config),
  296. ok.
  297. bad_priv_file_in_ez_archive(Config) ->
  298. doc("Bad cowboy_static options: priv_file path missing from Erlang application .ez archive."),
  299. {404, _, _} = do_get("/bad/ez_priv_file/index.php", Config),
  300. ok.
  301. bad_priv_file_no_priv(Config) ->
  302. doc("Bad cowboy_static options: application has no priv directory."),
  303. {404, _, _} = do_get("/bad/priv_file/no-priv", Config),
  304. ok.
  305. bad_priv_file_path(Config) ->
  306. doc("Bad cowboy_static options: wrong path."),
  307. {404, _, _} = do_get("/bad/priv_file/path", Config),
  308. ok.
  309. dir_cowboy(Config) ->
  310. doc("Get a .cowboy file."),
  311. {200, Headers, <<"File with custom extension.\n">>}
  312. = do_get(config(prefix, Config) ++ "/file.cowboy", Config),
  313. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  314. ok.
  315. dir_css(Config) ->
  316. doc("Get a .css file."),
  317. {200, Headers, <<"body{color:red}\n">>}
  318. = do_get(config(prefix, Config) ++ "/style.css", Config),
  319. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  320. ok.
  321. dir_css_urlencoded(Config) ->
  322. doc("Get a .css file with the extension dot urlencoded."),
  323. {200, Headers, <<"body{color:red}\n">>}
  324. = do_get(config(prefix, Config) ++ "/style%2ecss", Config),
  325. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  326. ok.
  327. dir_dot_file(Config) ->
  328. doc("Get a file with extra dot segments in the path."),
  329. %% All these are equivalent.
  330. {200, _, _} = do_get(config(prefix, Config) ++ "/./style.css", Config),
  331. {200, _, _} = do_get(config(prefix, Config) ++ "/././style.css", Config),
  332. {200, _, _} = do_get(config(prefix, Config) ++ "/./././style.css", Config),
  333. {200, _, _} = do_get("/./priv_dir/style.css", Config),
  334. {200, _, _} = do_get("/././priv_dir/style.css", Config),
  335. {200, _, _} = do_get("/./././priv_dir/style.css", Config),
  336. ok.
  337. dir_dotdot_file(Config) ->
  338. doc("Get a file with extra dotdot segments in the path."),
  339. %% All these are equivalent.
  340. {200, _, _} = do_get("/../priv_dir/style.css", Config),
  341. {200, _, _} = do_get("/../../priv_dir/style.css", Config),
  342. {200, _, _} = do_get("/../../../priv_dir/style.css", Config),
  343. {200, _, _} = do_get(config(prefix, Config) ++ "/../priv_dir/style.css", Config),
  344. {200, _, _} = do_get(config(prefix, Config) ++ "/../../priv_dir/style.css", Config),
  345. {200, _, _} = do_get(config(prefix, Config) ++ "/../../../priv_dir/style.css", Config),
  346. {200, _, _} = do_get("/../priv_dir/../priv_dir/style.css", Config),
  347. {200, _, _} = do_get("/../../priv_dir/../../priv_dir/style.css", Config),
  348. {200, _, _} = do_get("/../../../priv_dir/../../../priv_dir/style.css", Config),
  349. %% Try with non-existing segments, which may correspond to real folders.
  350. {200, _, _} = do_get("/anything/../priv_dir/style.css", Config),
  351. {200, _, _} = do_get(config(prefix, Config) ++ "/anything/../style.css", Config),
  352. {200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config),
  353. {200, _, _} = do_get(config(prefix, Config) ++ "/static/../style.css", Config),
  354. %% Try with segments corresponding to real files. It works because
  355. %% URI normalization happens before looking at the filesystem.
  356. {200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../style.css", Config),
  357. {200, _, _} = do_get(config(prefix, Config) ++ "/style.css/../../priv_dir/style.css", Config),
  358. %% Try to fool the server to accept segments corresponding to real folders.
  359. {404, _, _} = do_get(config(prefix, Config) ++ "/../static/style.css", Config),
  360. {404, _, _} = do_get(config(prefix, Config) ++ "/directory/../../static/style.css", Config),
  361. ok.
  362. dir_empty_file(Config) ->
  363. doc("Get an empty .txt file."),
  364. {200, _, <<>>} = do_get(config(prefix, Config) ++ "/empty.txt", Config),
  365. ok.
  366. dir_error_directory(Config) ->
  367. doc("Try to get a directory."),
  368. {403, _, _} = do_get(config(prefix, Config) ++ "/directory", Config),
  369. ok.
  370. dir_error_directory_slash(Config) ->
  371. doc("Try to get a directory with an extra slash in the path."),
  372. {403, _, _} = do_get(config(prefix, Config) ++ "/directory/", Config),
  373. ok.
  374. dir_error_doesnt_exist(Config) ->
  375. doc("Try to get a file that does not exist."),
  376. {404, Headers, _} = do_get(config(prefix, Config) ++ "/not.found", Config),
  377. false = lists:keyfind(<<"content-type">>, 1, Headers),
  378. ok.
  379. dir_error_dot(Config) ->
  380. doc("Try to get a file named '.'."),
  381. {403, _, _} = do_get(config(prefix, Config) ++ "/.", Config),
  382. ok.
  383. dir_error_dot_urlencoded(Config) ->
  384. doc("Try to get a file named '.' percent encoded."),
  385. {403, _, _} = do_get(config(prefix, Config) ++ "/%2e", Config),
  386. ok.
  387. dir_error_dotdot(Config) ->
  388. doc("Try to get a file named '..'."),
  389. {404, _, _} = do_get(config(prefix, Config) ++ "/..", Config),
  390. ok.
  391. dir_error_dotdot_urlencoded(Config) ->
  392. doc("Try to get a file named '..' percent encoded."),
  393. {404, _, _} = do_get(config(prefix, Config) ++ "/%2e%2e", Config),
  394. ok.
  395. dir_error_empty(Config) ->
  396. doc("Try to get the configured directory."),
  397. {403, _, _} = do_get(config(prefix, Config) ++ "", Config),
  398. ok.
  399. dir_error_slash(Config) ->
  400. %% I know the description isn't that good considering / has a meaning in URIs.
  401. doc("Try to get a file named '/'."),
  402. {403, _, _} = do_get(config(prefix, Config) ++ "//", Config),
  403. ok.
  404. dir_error_reserved_urlencoded(Config) ->
  405. doc("Try to get a file named '/' or '\\' or 'NUL' percent encoded."),
  406. {400, _, _} = do_get(config(prefix, Config) ++ "/%2f", Config),
  407. {400, _, _} = do_get(config(prefix, Config) ++ "/%5c", Config),
  408. {400, _, _} = do_get(config(prefix, Config) ++ "/%00", Config),
  409. ok.
  410. dir_error_slash_urlencoded_dotdot_file(Config) ->
  411. doc("Try to use a percent encoded slash to access an existing file."),
  412. {200, _, _} = do_get(config(prefix, Config) ++ "/directory/../style.css", Config),
  413. {400, _, _} = do_get(config(prefix, Config) ++ "/directory%2f../style.css", Config),
  414. ok.
  415. dir_error_unreadable(Config) ->
  416. case os:type() of
  417. {win32, _} ->
  418. {skip, "ACL not enabled by default under MSYS2."};
  419. {unix, _} ->
  420. doc("Try to get a file that can't be read."),
  421. {403, _, _} = do_get(config(prefix, Config) ++ "/unreadable", Config),
  422. ok
  423. end.
  424. dir_html(Config) ->
  425. doc("Get a .html file."),
  426. {200, Headers, <<"<html><body>Hello!</body></html>\n">>}
  427. = do_get(config(prefix, Config) ++ "/index.html", Config),
  428. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  429. ok.
  430. dir_large_file(Config) ->
  431. doc("Get a large file."),
  432. ConnPid = gun_open(Config),
  433. Ref = gun:get(ConnPid, config(prefix, Config) ++ "/large.bin",
  434. [{<<"accept-encoding">>, <<"gzip">>}]),
  435. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  436. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, RespHeaders),
  437. Size = 32*1024*1024,
  438. {ok, Size} = do_dir_large_file(ConnPid, Ref, 0),
  439. ok.
  440. do_dir_large_file(ConnPid, Ref, N) ->
  441. receive
  442. {gun_data, ConnPid, Ref, nofin, Data} ->
  443. do_dir_large_file(ConnPid, Ref, N + byte_size(Data));
  444. {gun_data, ConnPid, Ref, fin, Data} ->
  445. {ok, N + byte_size(Data)};
  446. {gun_error, ConnPid, Ref, Reason} ->
  447. {error, Reason};
  448. {gun_error, ConnPid, Reason} ->
  449. {error, Reason}
  450. after 5000 ->
  451. {error, timeout}
  452. end.
  453. dir_text(Config) ->
  454. doc("Get a .txt file. The extension is unknown by default."),
  455. {200, Headers, <<"Timeless space.\n">>}
  456. = do_get(config(prefix, Config) ++ "/plain.txt", Config),
  457. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  458. ok.
  459. dir_unknown(Config) ->
  460. doc("Get a file with no extension."),
  461. {200, Headers, <<"File with no extension.\n">>}
  462. = do_get(config(prefix, Config) ++ "/unknown", Config),
  463. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  464. ok.
  465. etag_crash(Config) ->
  466. doc("Get a file with a crashing etag function."),
  467. {500, _, _} = do_get("/etag/crash", Config),
  468. ok.
  469. etag_custom(Config) ->
  470. doc("Get a file with custom Etag function and make sure it is used."),
  471. {200, Headers, _} = do_get("/etag/custom", Config),
  472. {_, <<"\"etag\"">>} = lists:keyfind(<<"etag">>, 1, Headers),
  473. ok.
  474. etag_default(Config) ->
  475. doc("Get a file twice and make sure the Etag matches."),
  476. {200, Headers1, _} = do_get("/dir/style.css", Config),
  477. {200, Headers2, _} = do_get("/dir/style.css", Config),
  478. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers1),
  479. {_, Etag} = lists:keyfind(<<"etag">>, 1, Headers2),
  480. ok.
  481. etag_default_change(Config) ->
  482. doc("Get a file, modify it, get it again and make sure the Etag doesn't match."),
  483. %% We set the file to the current time first, then to a time in the past.
  484. ok = file:change_time(config(static_dir, Config) ++ "/index.html",
  485. calendar:universal_time()),
  486. {200, Headers1, _} = do_get("/dir/index.html", Config),
  487. {_, Etag1} = lists:keyfind(<<"etag">>, 1, Headers1),
  488. ok = file:change_time(config(static_dir, Config) ++ "/index.html",
  489. {{2019, 1, 1}, {1, 1, 1}}),
  490. {200, Headers2, _} = do_get("/dir/index.html", Config),
  491. {_, Etag2} = lists:keyfind(<<"etag">>, 1, Headers2),
  492. true = Etag1 =/= Etag2,
  493. ok.
  494. etag_disable(Config) ->
  495. doc("Get a file with disabled Etag and make sure no Etag is provided."),
  496. {200, Headers, _} = do_get("/etag/disable", Config),
  497. false = lists:keyfind(<<"etag">>, 1, Headers),
  498. ok.
  499. file(Config) ->
  500. doc("Get a file with hardcoded route."),
  501. {200, Headers, <<"body{color:red}\n">>} = do_get("/file/style.css", Config),
  502. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  503. ok.
  504. if_match(Config) ->
  505. doc("Get a file with If-Match matching."),
  506. {200, _, _} = do_get("/etag/custom", [
  507. {<<"if-match">>, <<"\"etag\"">>}
  508. ], Config),
  509. ok.
  510. if_match_fail(Config) ->
  511. doc("Get a file with If-Match not matching."),
  512. {412, _, _} = do_get("/etag/custom", [
  513. {<<"if-match">>, <<"\"invalid\"">>}
  514. ], Config),
  515. ok.
  516. if_match_invalid(Config) ->
  517. doc("Try to get a file with an invalid If-Match header."),
  518. {400, _, _} = do_get("/etag/custom", [
  519. {<<"if-match">>, <<"bad input">>}
  520. ], Config),
  521. ok.
  522. if_match_list(Config) ->
  523. doc("Get a file with If-Match matching."),
  524. {200, _, _} = do_get("/etag/custom", [
  525. {<<"if-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  526. ], Config),
  527. ok.
  528. if_match_list_fail(Config) ->
  529. doc("Get a file with If-Match not matching."),
  530. {412, _, _} = do_get("/etag/custom", [
  531. {<<"if-match">>, <<"\"invalid\", W/\"etag\", \"cowboy\"">>}
  532. ], Config),
  533. ok.
  534. if_match_weak(Config) ->
  535. doc("Try to get a file with a weak If-Match header."),
  536. {412, _, _} = do_get("/etag/custom", [
  537. {<<"if-match">>, <<"W/\"etag\"">>}
  538. ], Config),
  539. ok.
  540. if_match_wildcard(Config) ->
  541. doc("Get a file with a wildcard If-Match."),
  542. {200, _, _} = do_get("/etag/custom", [
  543. {<<"if-match">>, <<"*">>}
  544. ], Config),
  545. ok.
  546. if_modified_since(Config) ->
  547. doc("Get a file with If-Modified-Since in the past."),
  548. {200, _, _} = do_get("/etag/custom", [
  549. {<<"if-modified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  550. ], Config),
  551. ok.
  552. if_modified_since_fail(Config) ->
  553. doc("Get a file with If-Modified-Since equal to file modification time."),
  554. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  555. {304, _, _} = do_get("/etag/custom", [
  556. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)}
  557. ], Config),
  558. ok.
  559. if_modified_since_future(Config) ->
  560. doc("Get a file with If-Modified-Since in the future."),
  561. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  562. {200, _, _} = do_get("/etag/custom", [
  563. {<<"if-modified-since">>, [
  564. <<"Sat, 29 Oct ">>,
  565. integer_to_binary(Year + 1),
  566. <<" 19:43:31 GMT">>]}
  567. ], Config),
  568. ok.
  569. if_modified_since_if_none_match(Config) ->
  570. doc("Get a file with both If-Modified-Since and If-None-Match headers."
  571. "If-None-Match takes precedence and If-Modified-Since is ignored. (RFC7232 3.3)"),
  572. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  573. {200, _, _} = do_get("/etag/custom", [
  574. {<<"if-modified-since">>, httpd_util:rfc1123_date(LastModified)},
  575. {<<"if-none-match">>, <<"\"not-etag\"">>}
  576. ], Config),
  577. ok.
  578. if_modified_since_invalid(Config) ->
  579. doc("Get a file with an invalid If-Modified-Since header."),
  580. {200, _, _} = do_get("/etag/custom", [
  581. {<<"if-modified-since">>, <<"\"not a date\"">>}
  582. ], Config),
  583. ok.
  584. if_none_match(Config) ->
  585. doc("Get a file with If-None-Match not matching."),
  586. {200, _, _} = do_get("/etag/custom", [
  587. {<<"if-none-match">>, <<"\"not-etag\"">>}
  588. ], Config),
  589. ok.
  590. if_none_match_fail(Config) ->
  591. doc("Get a file with If-None-Match matching."),
  592. {304, _, _} = do_get("/etag/custom", [
  593. {<<"if-none-match">>, <<"\"etag\"">>}
  594. ], Config),
  595. ok.
  596. if_none_match_invalid(Config) ->
  597. doc("Try to get a file with an invalid If-None-Match header."),
  598. {400, _, _} = do_get("/etag/custom", [
  599. {<<"if-none-match">>, <<"bad input">>}
  600. ], Config),
  601. ok.
  602. if_none_match_list(Config) ->
  603. doc("Get a file with If-None-Match not matching."),
  604. {200, _, _} = do_get("/etag/custom", [
  605. {<<"if-none-match">>, <<"\"invalid\", W/\"not-etag\", \"cowboy\"">>}
  606. ], Config),
  607. ok.
  608. if_none_match_list_fail(Config) ->
  609. doc("Get a file with If-None-Match matching."),
  610. {304, _, _} = do_get("/etag/custom", [
  611. {<<"if-none-match">>, <<"\"invalid\", \"etag\", \"cowboy\"">>}
  612. ], Config),
  613. ok.
  614. if_none_match_weak(Config) ->
  615. doc("Try to get a file with a weak If-None-Match header matching."),
  616. {304, _, _} = do_get("/etag/custom", [
  617. {<<"if-none-match">>, <<"W/\"etag\"">>}
  618. ], Config),
  619. ok.
  620. if_none_match_wildcard(Config) ->
  621. doc("Try to get a file with a wildcard If-None-Match."),
  622. {304, _, _} = do_get("/etag/custom", [
  623. {<<"if-none-match">>, <<"*">>}
  624. ], Config),
  625. ok.
  626. if_unmodified_since(Config) ->
  627. doc("Get a file with If-Unmodified-Since equal to file modification time."),
  628. LastModified = filelib:last_modified(config(static_dir, Config) ++ "/style.css"),
  629. {200, _, _} = do_get("/etag/custom", [
  630. {<<"if-unmodified-since">>, httpd_util:rfc1123_date(LastModified)}
  631. ], Config),
  632. ok.
  633. if_unmodified_since_fail(Config) ->
  634. doc("Get a file with If-Unmodified-Since in the past."),
  635. {412, _, _} = do_get("/etag/custom", [
  636. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>}
  637. ], Config),
  638. ok.
  639. if_unmodified_since_future(Config) ->
  640. doc("Get a file with If-Unmodified-Since in the future."),
  641. {{Year, _, _}, {_, _, _}} = calendar:universal_time(),
  642. {200, _, _} = do_get("/etag/custom", [
  643. {<<"if-unmodified-since">>, [
  644. <<"Sat, 29 Oct ">>,
  645. integer_to_binary(Year + 1),
  646. <<" 19:43:31 GMT">>]}
  647. ], Config),
  648. ok.
  649. if_unmodified_since_if_match(Config) ->
  650. doc("Get a file with both If-Unmodified-Since and If-Match headers."
  651. "If-Match takes precedence and If-Unmodified-Since is ignored. (RFC7232 3.4)"),
  652. {200, _, _} = do_get("/etag/custom", [
  653. {<<"if-unmodified-since">>, <<"Sat, 29 Oct 1994 19:43:31 GMT">>},
  654. {<<"if-match">>, <<"\"etag\"">>}
  655. ], Config),
  656. ok.
  657. if_unmodified_since_invalid(Config) ->
  658. doc("Get a file with an invalid If-Unmodified-Since header."),
  659. {200, _, _} = do_get("/etag/custom", [
  660. {<<"if-unmodified-since">>, <<"\"not a date\"">>}
  661. ], Config),
  662. ok.
  663. index_file(Config) ->
  664. doc("Get an index file."),
  665. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index", Config),
  666. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  667. ok.
  668. index_file_slash(Config) ->
  669. doc("Get an index file with extra slash."),
  670. {200, Headers, <<"<html><body>Hello!</body></html>\n">>} = do_get("/index/", Config),
  671. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  672. ok.
  673. last_modified(Config) ->
  674. doc("Get a file, modify it, get it again and make sure Last-Modified changes."),
  675. %% We set the file to the current time first, then to a time in the past.
  676. ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy",
  677. calendar:universal_time()),
  678. {200, Headers1, _} = do_get("/dir/file.cowboy", Config),
  679. {_, LastModified1} = lists:keyfind(<<"last-modified">>, 1, Headers1),
  680. ok = file:change_time(config(static_dir, Config) ++ "/file.cowboy",
  681. {{2019, 1, 1}, {1, 1, 1}}),
  682. {200, Headers2, _} = do_get("/dir/file.cowboy", Config),
  683. {_, LastModified2} = lists:keyfind(<<"last-modified">>, 1, Headers2),
  684. true = LastModified1 =/= LastModified2,
  685. ok.
  686. mime_all_cowboy(Config) ->
  687. doc("Get a .cowboy file. The extension is unknown."),
  688. {200, Headers, _} = do_get("/mime/all/file.cowboy", Config),
  689. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  690. ok.
  691. mime_all_css(Config) ->
  692. doc("Get a .css file."),
  693. {200, Headers, _} = do_get("/mime/all/style.css", Config),
  694. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  695. ok.
  696. mime_all_txt(Config) ->
  697. doc("Get a .txt file."),
  698. {200, Headers, _} = do_get("/mime/all/plain.txt", Config),
  699. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  700. ok.
  701. mime_all_uppercase(Config) ->
  702. doc("Get an uppercase .TXT file."),
  703. {200, Headers, _} = do_get("/mime/all/UPPER.TXT", Config),
  704. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  705. ok.
  706. mime_crash(Config) ->
  707. doc("Get a file with a crashing mimetype function."),
  708. {500, _, _} = do_get("/mime/crash/style.css", Config),
  709. ok.
  710. mime_custom_cowboy(Config) ->
  711. doc("Get a .cowboy file."),
  712. {200, Headers, _} = do_get("/mime/custom/file.cowboy", Config),
  713. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  714. ok.
  715. mime_custom_css(Config) ->
  716. doc("Get a .css file. The extension is unknown."),
  717. {200, Headers, _} = do_get("/mime/custom/style.css", Config),
  718. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  719. ok.
  720. mime_custom_txt(Config) ->
  721. doc("Get a .txt file."),
  722. {200, Headers, _} = do_get("/mime/custom/plain.txt", Config),
  723. {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  724. ok.
  725. mime_hardcode_binary(Config) ->
  726. doc("Get a .cowboy file with hardcoded route and media type in binary form."),
  727. {200, Headers, _} = do_get("/mime/hardcode/binary-form", Config),
  728. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  729. ok.
  730. mime_hardcode_tuple(Config) ->
  731. doc("Get a .cowboy file with hardcoded route and media type in tuple form."),
  732. {200, Headers, _} = do_get("/mime/hardcode/tuple-form", Config),
  733. {_, <<"application/vnd.ninenines.cowboy+xml;v=1">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  734. ok.
  735. charset_crash(Config) ->
  736. doc("Get a file with a crashing charset function."),
  737. {500, _, _} = do_get("/charset/crash/style.css", Config),
  738. ok.
  739. charset_custom_cowboy(Config) ->
  740. doc("Get a .cowboy file."),
  741. {200, Headers, _} = do_get("/charset/custom/file.cowboy", Config),
  742. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  743. ok.
  744. charset_custom_css(Config) ->
  745. doc("Get a .css file."),
  746. {200, Headers, _} = do_get("/charset/custom/style.css", Config),
  747. {_, <<"text/css; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  748. ok.
  749. charset_custom_html(Config) ->
  750. doc("Get a .html file."),
  751. {200, Headers, _} = do_get("/charset/custom/index.html", Config),
  752. {_, <<"text/html; charset=utf-16">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  753. ok.
  754. charset_hardcode_binary(Config) ->
  755. doc("Get a .html file with hardcoded route and charset."),
  756. {200, Headers, _} = do_get("/charset/hardcode", Config),
  757. {_, <<"text/html; charset=utf-8">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  758. ok.
  759. priv_dir_in_ez_archive(Config) ->
  760. doc("Get a file from a priv_dir stored in Erlang application .ez archive."),
  761. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_dir/index.html", Config),
  762. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  763. ok.
  764. priv_file(Config) ->
  765. doc("Get a file with hardcoded route."),
  766. {200, Headers, <<"body{color:red}\n">>} = do_get("/priv_file/style.css", Config),
  767. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  768. ok.
  769. priv_file_in_ez_archive(Config) ->
  770. doc("Get a file stored in Erlang application .ez archive."),
  771. {200, Headers, <<"<h1>It works!</h1>\n">>} = do_get("/ez_priv_file/index.html", Config),
  772. {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  773. ok.
  774. range_request(Config) ->
  775. doc("Confirm that range requests are enabled."),
  776. {206, Headers, <<"less space.\n">>} = do_get("/dir/plain.txt",
  777. [{<<"range">>, <<"bytes=4-">>}], Config),
  778. {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
  779. {_, <<"bytes 4-15/16">>} = lists:keyfind(<<"content-range">>, 1, Headers),
  780. {_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  781. ok.
  782. unicode_basic_latin(Config) ->
  783. doc("Get a file with non-urlencoded characters from Unicode Basic Latin block."),
  784. %% Excluding the dot which has a special meaning in URLs
  785. %% when they are the only content in a path segment,
  786. %% and is tested as part of filenames in other test cases.
  787. Chars0 =
  788. "abcdefghijklmnopqrstuvwxyz"
  789. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  790. "0123456789"
  791. ":@-_~!$&'()*+,;=",
  792. Chars1 = case config(case_sensitive, Config) of
  793. false -> Chars0 -- "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  794. true -> Chars0
  795. end,
  796. %% Remove the characters for which we have no corresponding file.
  797. Chars = Chars1 -- (Chars1 -- config(chars, Config)),
  798. _ = [case do_get("/char/" ++ [C], Config) of
  799. {200, _, << C >>} -> ok;
  800. Error -> exit({error, C, Error})
  801. end || C <- Chars],
  802. ok.
  803. unicode_basic_error(Config) ->
  804. doc("Try to get a file with invalid non-urlencoded characters from Unicode Basic Latin block."),
  805. Exclude = case config(protocol, Config) of
  806. %% Some characters trigger different errors in HTTP/1.1
  807. %% because they are used for the protocol.
  808. %%
  809. %% # and ? indicate fragment and query components
  810. %% and are therefore not part of the path.
  811. http -> "\r\s#?";
  812. http2 -> "#?"
  813. end,
  814. _ = [case do_get("/char/" ++ [C], Config) of
  815. {400, _, _} -> ok;
  816. Error -> exit({error, C, Error})
  817. end || C <- (config(chars, Config) -- Exclude) --
  818. "abcdefghijklmnopqrstuvwxyz"
  819. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  820. "0123456789"
  821. ":@-_~!$&'()*+,;="
  822. ],
  823. ok.
  824. unicode_basic_latin_urlencoded(Config) ->
  825. doc("Get a file with urlencoded characters from Unicode Basic Latin block."),
  826. _ = [case do_get(lists:flatten(["/char/%", io_lib:format("~2.16.0b", [C])]), Config) of
  827. {200, _, << C >>} -> ok;
  828. Error -> exit({error, C, Error})
  829. end || C <- config(chars, Config)],
  830. ok.
  831. unknown_option(Config) ->
  832. doc("Get a file configured with unknown extra options."),
  833. {200, Headers, <<"body{color:red}\n">>} = do_get("/unknown/option", Config),
  834. {_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
  835. ok.