static_handler_SUITE.erl 35 KB

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