From ea7ae14df888ce6b37e3e219c6dae557c37d44f3 Mon Sep 17 00:00:00 2001 From: Magnus Klaar Date: Wed, 28 Dec 2011 18:02:32 +0100 Subject: Add built-in cowboy_http_static handler. --- test/http_SUITE.erl | 83 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index b7fd551..bc8d1f7 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -22,7 +22,8 @@ keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/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. --export([http_200/1, http_404/1]). %% http and https. +-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. -export([http_10_hostless/1]). %% misc. -export([rest_simple/1, rest_keepalive/1]). %% rest. @@ -32,7 +33,8 @@ all() -> [{group, http}, {group, https}, {group, misc}, {group, rest}]. groups() -> - BaseTests = [http_200, http_404], + BaseTests = [http_200, http_404, file_200, file_403, dir_403, file_404, + file_400], [{http, [], [chunked_response, headers_dupe, headers_huge, keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw, set_resp_header, set_resp_overwrite, @@ -53,14 +55,16 @@ end_per_suite(_Config) -> init_per_group(http, Config) -> Port = 33080, + Config1 = init_static_dir(Config), cowboy:start_listener(http, 100, cowboy_tcp_transport, [{port, Port}], cowboy_http_protocol, [{max_keepalive, 50}, - {dispatch, init_http_dispatch()}] + {dispatch, init_http_dispatch(Config1)}] ), - [{scheme, "http"}, {port, Port}|Config]; + [{scheme, "http"}, {port, Port}|Config1]; init_per_group(https, Config) -> Port = 33081, + Config1 = init_static_dir(Config), application:start(crypto), application:start(public_key), application:start(ssl), @@ -69,9 +73,9 @@ init_per_group(https, Config) -> cowboy_ssl_transport, [ {port, Port}, {certfile, DataDir ++ "cert.pem"}, {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}], - cowboy_http_protocol, [{dispatch, init_https_dispatch()}] + cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}] ), - [{scheme, "https"}, {port, Port}|Config]; + [{scheme, "https"}, {port, Port}|Config1]; init_per_group(misc, Config) -> Port = 33082, cowboy:start_listener(misc, 100, @@ -89,19 +93,21 @@ init_per_group(rest, Config) -> ]}]}]), [{port, Port}|Config]. -end_per_group(https, _Config) -> +end_per_group(https, Config) -> cowboy:stop_listener(https), application:stop(ssl), application:stop(public_key), application:stop(crypto), + end_static_dir(Config), ok; -end_per_group(Listener, _Config) -> +end_per_group(Listener, Config) -> cowboy:stop_listener(Listener), + end_static_dir(Config), ok. %% Dispatch configuration. -init_http_dispatch() -> +init_http_dispatch(Config) -> [ {[<<"localhost">>], [ {[<<"chunked_response">>], chunked_handler, []}, @@ -117,12 +123,37 @@ init_http_dispatch() -> [{body, <<"A flameless dance does not equal a cycle">>}]}, {[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body, [{reply, set_resp}, {body, <<"stream_body_set_resp">>}]}, + {[<<"static">>, '...'], cowboy_http_static, + [{directory, ?config(static_dir, Config)}, + {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]}, {[], http_handler, []} ]} ]. -init_https_dispatch() -> - init_http_dispatch(). +init_https_dispatch(Config) -> + init_http_dispatch(Config). + + +init_static_dir(Config) -> + Dir = filename:join(?config(priv_dir, Config), "static"), + Level1 = fun(Name) -> filename:join(Dir, Name) end, + ok = file:make_dir(Dir), + ok = file:write_file(Level1("test_file"), "test_file\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:change_mode(Level1("test_noread"), 8#0333), + ok = file:make_dir(Level1("test_dir")), + [{static_dir, Dir}|Config]. + +end_static_dir(Config) -> + Dir = ?config(static_dir, Config), + Level1 = fun(Name) -> filename:join(Dir, Name) end, + ok = file:delete(Level1("test_file")), + ok = file:delete(Level1("test_file.css")), + ok = file:delete(Level1("test_noread")), + ok = file:del_dir(Level1("test_dir")), + ok = file:del_dir(Dir), + Config. %% http. @@ -355,6 +386,36 @@ http_404(Config) -> {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = httpc:request(build_url("/not/found", Config)). +file_200(Config) -> + {ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} = + httpc:request(build_url("/static/test_file", Config)), + "application/octet-stream" = ?config("content-type", Headers), + + {ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} = + httpc:request(build_url("/static/test_file.css", Config)), + "text/css" = ?config("content-type", Headers1). + +file_403(Config) -> + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_noread", Config)). + +dir_403(Config) -> + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_dir", Config)), + {ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} = + httpc:request(build_url("/static/test_dir/", Config)). + +file_404(Config) -> + {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = + httpc:request(build_url("/static/not_found", Config)). + +file_400(Config) -> + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} = + httpc:request(build_url("/static/%2f", Config)), + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} = + httpc:request(build_url("/static/%2e", Config)), + {ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} = + httpc:request(build_url("/static/%2e%2e", Config)). %% misc. http_10_hostless(Config) -> -- cgit v1.2.3