mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Add test and doc for content types function.
This commit is contained in:
parent
8ab05058e6
commit
54d16c14ad
2 changed files with 28 additions and 2 deletions
|
@ -65,13 +65,25 @@
|
||||||
%% The mimetypes must be of a type that the `cowboy_http_rest' protocol can
|
%% The mimetypes must be of a type that the `cowboy_http_rest' protocol can
|
||||||
%% handle.
|
%% handle.
|
||||||
%%
|
%%
|
||||||
|
%% The <a href="https://github.com/spawngrid/mimetypes">spawngrid/mimetypes</a>
|
||||||
|
%% application, or an arbitrary function accepting the path to the file being
|
||||||
|
%% served, can also be used to generate the list of content types for a static
|
||||||
|
%% file resource. The function used must accept an additional argument after
|
||||||
|
%% the file path argument.
|
||||||
|
%%
|
||||||
%% ==== Example ====
|
%% ==== Example ====
|
||||||
%% ```
|
%% ```
|
||||||
|
%% %% Use a static list of content types.
|
||||||
%% {[<<"static">>, '...'], cowboy_http_static,
|
%% {[<<"static">>, '...'], cowboy_http_static,
|
||||||
%% [{directory, {priv_dir, cowboy, []}},
|
%% [{directory, {priv_dir, cowboy, []}},
|
||||||
%% {mimetypes, [
|
%% {mimetypes, [
|
||||||
%% {<<".css">>, [<<"text/css">>]},
|
%% {<<".css">>, [<<"text/css">>]},
|
||||||
%% {<<".js">>, [<<"application/javascript">>]}]}]}
|
%% {<<".js">>, [<<"application/javascript">>]}]}]}
|
||||||
|
%%
|
||||||
|
%% %% Use the default database in the mimetypes application.
|
||||||
|
%% {[<<"static">>, '...', cowboy_http_static,
|
||||||
|
%% [{directory, {priv_dir, cowboy, []}},
|
||||||
|
%% {mimetypes, {fun mimetypes:path_to_mimes/2, default}}]]}
|
||||||
%% '''
|
%% '''
|
||||||
-module(cowboy_http_static).
|
-module(cowboy_http_static).
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
|
||||||
keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
|
keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
|
||||||
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
|
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
|
||||||
set_resp_body/1, stream_body_set_resp/1, response_as_req/1]). %% http.
|
set_resp_body/1, stream_body_set_resp/1, response_as_req/1,
|
||||||
|
static_mimetypes_function/1]). %% http.
|
||||||
-export([http_200/1, http_404/1, file_200/1, file_403/1,
|
-export([http_200/1, http_404/1, file_200/1, file_403/1,
|
||||||
dir_403/1, file_404/1, file_400/1]). %% http and https.
|
dir_403/1, file_404/1, file_400/1]). %% http and https.
|
||||||
-export([http_10_hostless/1]). %% misc.
|
-export([http_10_hostless/1]). %% misc.
|
||||||
|
@ -38,7 +39,8 @@ groups() ->
|
||||||
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
[{http, [], [chunked_response, headers_dupe, headers_huge,
|
||||||
keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
|
keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
|
||||||
set_resp_header, set_resp_overwrite,
|
set_resp_header, set_resp_overwrite,
|
||||||
set_resp_body, response_as_req, stream_body_set_resp] ++ BaseTests},
|
set_resp_body, response_as_req, stream_body_set_resp,
|
||||||
|
static_mimetypes_function] ++ BaseTests},
|
||||||
{https, [], BaseTests},
|
{https, [], BaseTests},
|
||||||
{misc, [], [http_10_hostless]},
|
{misc, [], [http_10_hostless]},
|
||||||
{rest, [], [rest_simple, rest_keepalive]}].
|
{rest, [], [rest_simple, rest_keepalive]}].
|
||||||
|
@ -126,6 +128,10 @@ init_http_dispatch(Config) ->
|
||||||
{[<<"static">>, '...'], cowboy_http_static,
|
{[<<"static">>, '...'], cowboy_http_static,
|
||||||
[{directory, ?config(static_dir, Config)},
|
[{directory, ?config(static_dir, Config)},
|
||||||
{mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
|
{mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
|
||||||
|
{[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
|
||||||
|
[{directory, ?config(static_dir, Config)},
|
||||||
|
{mimetypes, {fun(Path, data) when is_binary(Path) ->
|
||||||
|
[<<"text/html">>] end, data}}]},
|
||||||
{[], http_handler, []}
|
{[], http_handler, []}
|
||||||
]}
|
]}
|
||||||
].
|
].
|
||||||
|
@ -142,6 +148,7 @@ init_static_dir(Config) ->
|
||||||
ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
|
ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
|
||||||
ok = file:write_file(Level1("test_noread"), "test_noread\n"),
|
ok = file:write_file(Level1("test_noread"), "test_noread\n"),
|
||||||
ok = file:change_mode(Level1("test_noread"), 8#0333),
|
ok = file:change_mode(Level1("test_noread"), 8#0333),
|
||||||
|
ok = file:write_file(Level1("test.html"), "test.html\n"),
|
||||||
ok = file:make_dir(Level1("test_dir")),
|
ok = file:make_dir(Level1("test_dir")),
|
||||||
[{static_dir, Dir}|Config].
|
[{static_dir, Dir}|Config].
|
||||||
|
|
||||||
|
@ -151,6 +158,7 @@ end_static_dir(Config) ->
|
||||||
ok = file:delete(Level1("test_file")),
|
ok = file:delete(Level1("test_file")),
|
||||||
ok = file:delete(Level1("test_file.css")),
|
ok = file:delete(Level1("test_file.css")),
|
||||||
ok = file:delete(Level1("test_noread")),
|
ok = file:delete(Level1("test_noread")),
|
||||||
|
ok = file:delete(Level1("test.html")),
|
||||||
ok = file:del_dir(Level1("test_dir")),
|
ok = file:del_dir(Level1("test_dir")),
|
||||||
ok = file:del_dir(Dir),
|
ok = file:del_dir(Dir),
|
||||||
Config.
|
Config.
|
||||||
|
@ -370,6 +378,12 @@ stream_body_set_resp(Config) ->
|
||||||
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
|
||||||
{_Start, _Length} = binary:match(Data, <<"stream_body_set_resp">>).
|
{_Start, _Length} = binary:match(Data, <<"stream_body_set_resp">>).
|
||||||
|
|
||||||
|
static_mimetypes_function(Config) ->
|
||||||
|
TestURL = build_url("/static_mimetypes_function/test.html", Config),
|
||||||
|
{ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
|
||||||
|
httpc:request(TestURL),
|
||||||
|
"text/html" = ?config("content-type", Headers1).
|
||||||
|
|
||||||
|
|
||||||
%% http and https.
|
%% http and https.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue