2012-05-23 14:53:48 +02:00
|
|
|
%% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
|
2011-11-08 00:51:49 +01:00
|
|
|
%% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
|
2011-04-08 16:30:37 +02:00
|
|
|
%%
|
|
|
|
%% Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
%% purpose with or without fee is hereby granted, provided that the above
|
|
|
|
%% copyright notice and this permission notice appear in all copies.
|
|
|
|
%%
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
-module(http_SUITE).
|
|
|
|
|
|
|
|
-include_lib("common_test/include/ct.hrl").
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
%% ct.
|
|
|
|
-export([all/0]).
|
|
|
|
-export([groups/0]).
|
|
|
|
-export([init_per_suite/1]).
|
|
|
|
-export([end_per_suite/1]).
|
|
|
|
-export([init_per_group/2]).
|
|
|
|
-export([end_per_group/2]).
|
|
|
|
|
|
|
|
%% Tests.
|
|
|
|
-export([check_raw_status/1]).
|
|
|
|
-export([check_status/1]).
|
|
|
|
-export([chunked_response/1]).
|
2012-05-05 07:53:17 +02:00
|
|
|
-export([echo_body/1]).
|
2012-04-29 01:20:24 +02:00
|
|
|
-export([error_chain_handle_after_reply/1]).
|
|
|
|
-export([error_chain_handle_before_reply/1]).
|
|
|
|
-export([error_handle_after_reply/1]).
|
|
|
|
-export([error_init_after_reply/1]).
|
|
|
|
-export([error_init_reply_handle_error/1]).
|
|
|
|
-export([headers_dupe/1]).
|
|
|
|
-export([http10_chunkless/1]).
|
|
|
|
-export([http10_hostless/1]).
|
|
|
|
-export([keepalive_max/1]).
|
|
|
|
-export([keepalive_nl/1]).
|
|
|
|
-export([multipart/1]).
|
|
|
|
-export([nc_rand/1]).
|
|
|
|
-export([nc_zero/1]).
|
|
|
|
-export([onrequest/1]).
|
|
|
|
-export([onrequest_reply/1]).
|
2012-05-21 15:17:24 +02:00
|
|
|
-export([onresponse_crash/1]).
|
2012-05-01 01:59:36 +02:00
|
|
|
-export([onresponse_reply/1]).
|
2012-04-29 01:20:24 +02:00
|
|
|
-export([pipeline/1]).
|
|
|
|
-export([rest_keepalive/1]).
|
|
|
|
-export([rest_keepalive_post/1]).
|
|
|
|
-export([rest_nodelete/1]).
|
|
|
|
-export([rest_resource_etags/1]).
|
|
|
|
-export([rest_resource_etags_if_none_match/1]).
|
|
|
|
-export([set_resp_body/1]).
|
|
|
|
-export([set_resp_header/1]).
|
|
|
|
-export([set_resp_overwrite/1]).
|
|
|
|
-export([static_attribute_etag/1]).
|
|
|
|
-export([static_function_etag/1]).
|
|
|
|
-export([static_mimetypes_function/1]).
|
2012-04-30 23:50:23 +02:00
|
|
|
-export([static_specify_file/1]).
|
|
|
|
-export([static_specify_file_catchall/1]).
|
2012-04-29 01:20:24 +02:00
|
|
|
-export([static_test_file/1]).
|
|
|
|
-export([static_test_file_css/1]).
|
|
|
|
-export([stream_body_set_resp/1]).
|
|
|
|
-export([te_chunked/1]).
|
|
|
|
-export([te_chunked_delayed/1]).
|
|
|
|
-export([te_identity/1]).
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
2012-05-01 01:59:36 +02:00
|
|
|
[{group, http}, {group, https}, {group, onrequest}, {group, onresponse}].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
groups() ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Tests = [
|
|
|
|
check_raw_status,
|
|
|
|
check_status,
|
|
|
|
chunked_response,
|
2012-05-05 07:53:17 +02:00
|
|
|
echo_body,
|
2012-04-29 01:20:24 +02:00
|
|
|
error_chain_handle_after_reply,
|
|
|
|
error_chain_handle_before_reply,
|
|
|
|
error_handle_after_reply,
|
|
|
|
error_init_after_reply,
|
|
|
|
error_init_reply_handle_error,
|
|
|
|
headers_dupe,
|
|
|
|
http10_chunkless,
|
|
|
|
http10_hostless,
|
|
|
|
keepalive_max,
|
|
|
|
keepalive_nl,
|
|
|
|
multipart,
|
|
|
|
nc_rand,
|
|
|
|
nc_zero,
|
|
|
|
pipeline,
|
|
|
|
rest_keepalive,
|
|
|
|
rest_keepalive_post,
|
|
|
|
rest_nodelete,
|
|
|
|
rest_resource_etags,
|
|
|
|
rest_resource_etags_if_none_match,
|
|
|
|
set_resp_body,
|
|
|
|
set_resp_header,
|
|
|
|
set_resp_overwrite,
|
|
|
|
static_attribute_etag,
|
|
|
|
static_function_etag,
|
|
|
|
static_mimetypes_function,
|
2012-04-30 23:50:23 +02:00
|
|
|
static_specify_file,
|
|
|
|
static_specify_file_catchall,
|
2012-04-29 01:20:24 +02:00
|
|
|
static_test_file,
|
|
|
|
static_test_file_css,
|
|
|
|
stream_body_set_resp,
|
|
|
|
te_chunked,
|
|
|
|
te_chunked_delayed,
|
|
|
|
te_identity
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{http, [], Tests},
|
|
|
|
{https, [], Tests},
|
2012-05-01 01:59:36 +02:00
|
|
|
{onrequest, [], [
|
2012-04-29 01:20:24 +02:00
|
|
|
onrequest,
|
|
|
|
onrequest_reply
|
2012-05-01 01:59:36 +02:00
|
|
|
]},
|
|
|
|
{onresponse, [], [
|
2012-05-21 15:17:24 +02:00
|
|
|
onresponse_crash,
|
2012-05-01 01:59:36 +02:00
|
|
|
onresponse_reply
|
2012-04-29 01:20:24 +02:00
|
|
|
]}
|
|
|
|
].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
|
|
|
application:start(inets),
|
2012-08-27 11:50:35 +02:00
|
|
|
application:start(ranch),
|
2011-04-08 16:30:37 +02:00
|
|
|
application:start(cowboy),
|
|
|
|
Config.
|
|
|
|
|
|
|
|
end_per_suite(_Config) ->
|
|
|
|
application:stop(cowboy),
|
2012-08-27 11:50:35 +02:00
|
|
|
application:stop(ranch),
|
2011-04-08 16:30:37 +02:00
|
|
|
application:stop(inets),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
init_per_group(http, Config) ->
|
|
|
|
Port = 33080,
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_tcp,
|
2011-12-28 18:02:32 +01:00
|
|
|
Config1 = init_static_dir(Config),
|
2012-08-27 11:50:35 +02:00
|
|
|
{ok, _} = cowboy:start_http(http, 100, [{port, Port}], [
|
|
|
|
{dispatch, init_dispatch(Config1)},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client} = cowboy_client:init([]),
|
|
|
|
[{scheme, <<"http">>}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}, {client, Client}|Config1];
|
2011-04-08 16:30:37 +02:00
|
|
|
init_per_group(https, Config) ->
|
|
|
|
Port = 33081,
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_ssl,
|
2012-04-29 01:20:24 +02:00
|
|
|
Opts = [
|
|
|
|
{certfile, ?config(data_dir, Config) ++ "cert.pem"},
|
|
|
|
{keyfile, ?config(data_dir, Config) ++ "key.pem"},
|
|
|
|
{password, "cowboy"}
|
|
|
|
],
|
2011-12-28 18:02:32 +01:00
|
|
|
Config1 = init_static_dir(Config),
|
2011-04-08 16:30:37 +02:00
|
|
|
application:start(crypto),
|
|
|
|
application:start(public_key),
|
|
|
|
application:start(ssl),
|
2012-08-27 11:50:35 +02:00
|
|
|
{ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, Port}], [
|
|
|
|
{dispatch, init_dispatch(Config1)},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client} = cowboy_client:init(Opts),
|
|
|
|
[{scheme, <<"https">>}, {port, Port}, {opts, Opts},
|
|
|
|
{transport, Transport}, {client, Client}|Config1];
|
2012-05-01 01:59:36 +02:00
|
|
|
init_per_group(onrequest, Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Port = 33082,
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_tcp,
|
|
|
|
{ok, _} = cowboy:start_http(onrequest, 100, [{port, Port}], [
|
|
|
|
{dispatch, init_dispatch(Config)},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{onrequest, fun onrequest_hook/1},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client} = cowboy_client:init([]),
|
2012-05-01 01:59:36 +02:00
|
|
|
[{scheme, <<"http">>}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}, {client, Client}|Config];
|
|
|
|
init_per_group(onresponse, Config) ->
|
|
|
|
Port = 33083,
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_tcp,
|
|
|
|
{ok, _} = cowboy:start_http(onresponse, 100, [{port, Port}], [
|
|
|
|
{dispatch, init_dispatch(Config)},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{onresponse, fun onresponse_hook/3},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2012-05-01 01:59:36 +02:00
|
|
|
{ok, Client} = cowboy_client:init([]),
|
2012-04-29 01:20:24 +02:00
|
|
|
[{scheme, <<"http">>}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}, {client, Client}|Config].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
2011-12-28 18:02:32 +01:00
|
|
|
end_per_group(https, Config) ->
|
2011-04-08 16:30:37 +02:00
|
|
|
cowboy:stop_listener(https),
|
|
|
|
application:stop(ssl),
|
|
|
|
application:stop(public_key),
|
|
|
|
application:stop(crypto),
|
2011-12-28 18:02:32 +01:00
|
|
|
end_static_dir(Config),
|
2011-09-14 01:41:03 +02:00
|
|
|
ok;
|
2012-01-05 00:57:02 +01:00
|
|
|
end_per_group(http, Config) ->
|
|
|
|
cowboy:stop_listener(http),
|
|
|
|
end_static_dir(Config);
|
2012-05-01 01:59:36 +02:00
|
|
|
end_per_group(Name, _) ->
|
|
|
|
cowboy:stop_listener(Name),
|
2011-04-08 16:30:37 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%% Dispatch configuration.
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
init_dispatch(Config) ->
|
2011-04-08 16:30:37 +02:00
|
|
|
[
|
2011-05-05 14:03:39 +02:00
|
|
|
{[<<"localhost">>], [
|
2011-05-08 17:26:21 +02:00
|
|
|
{[<<"chunked_response">>], chunked_handler, []},
|
2011-10-06 15:54:37 +02:00
|
|
|
{[<<"init_shutdown">>], http_handler_init_shutdown, []},
|
Add support for loops in standard HTTP handlers
Now init/3 can return one of the following values to enable loops:
- {loop, Req, State}
- {loop, Req, State, hibernate}
- {loop, Req, State, Timeout}
- {loop, Req, State, Timeout, hibernate}
Returning one of these tuples will activate looping in the HTTP handler.
When looping, handle/2 is never called. Instead, Cowboy will listen
for Erlang messages and forward them to the info/3 function of the
handler. If a timeout is defined, Cowboy will also close the connection
when no message has been received for Timeout milliseconds.
The info/3 function is defined as info(Msg, Req, State). It can return
either of the following tuples:
- {ok, Req, State}
- {loop, Req, State}
- {loop, Req, State, hibernate}
The first one ends the connection, calling terminate/2 before closing.
The others continue the loop.
Loops are useful when writing long-polling handlers that need to wait
and don't expect to receive anything. Therefore it is recommended to
set a timeout to close the connection if nothing arrives after a while
and to enable hibernate everywhere.
Normal HTTP handlers shouldn't need to use this and as such info/3
was made optional.
2011-10-10 17:27:52 +02:00
|
|
|
{[<<"long_polling">>], http_handler_long_polling, []},
|
2011-05-05 14:03:39 +02:00
|
|
|
{[<<"headers">>, <<"dupe">>], http_handler,
|
|
|
|
[{headers, [{<<"Connection">>, <<"close">>}]}]},
|
2011-11-28 09:09:41 +01:00
|
|
|
{[<<"set_resp">>, <<"header">>], http_handler_set_resp,
|
|
|
|
[{headers, [{<<"Vary">>, <<"Accept">>}]}]},
|
|
|
|
{[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
|
|
|
|
[{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
|
|
|
|
{[<<"set_resp">>, <<"body">>], http_handler_set_resp,
|
|
|
|
[{body, <<"A flameless dance does not equal a cycle">>}]},
|
2011-12-28 18:00:27 +01:00
|
|
|
{[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
|
|
|
|
[{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
|
2011-12-28 18:02:32 +01:00
|
|
|
{[<<"static">>, '...'], cowboy_http_static,
|
|
|
|
[{directory, ?config(static_dir, Config)},
|
|
|
|
{mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
|
2012-01-04 02:22:21 +01:00
|
|
|
{[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
|
|
|
|
[{directory, ?config(static_dir, Config)},
|
|
|
|
{mimetypes, {fun(Path, data) when is_binary(Path) ->
|
|
|
|
[<<"text/html">>] end, data}}]},
|
2011-12-29 00:06:22 +01:00
|
|
|
{[<<"handler_errors">>], http_handler_errors, []},
|
2012-01-07 23:07:45 +01:00
|
|
|
{[<<"static_attribute_etag">>, '...'], cowboy_http_static,
|
|
|
|
[{directory, ?config(static_dir, Config)},
|
|
|
|
{etag, {attributes, [filepath, filesize, inode, mtime]}}]},
|
|
|
|
{[<<"static_function_etag">>, '...'], cowboy_http_static,
|
|
|
|
[{directory, ?config(static_dir, Config)},
|
|
|
|
{etag, {fun static_function_etag/2, etag_data}}]},
|
2012-04-30 23:50:23 +02:00
|
|
|
{[<<"static_specify_file">>, '...'], cowboy_http_static,
|
|
|
|
[{directory, ?config(static_dir, Config)},
|
|
|
|
{mimetypes, [{<<".css">>, [<<"text/css">>]}]},
|
|
|
|
{file, <<"test_file.css">>}]},
|
2011-11-08 00:51:49 +01:00
|
|
|
{[<<"multipart">>], http_handler_multipart, []},
|
2012-03-29 01:14:44 +02:00
|
|
|
{[<<"echo">>, <<"body">>], http_handler_echo_body, []},
|
2012-04-29 01:20:24 +02:00
|
|
|
{[<<"simple">>], rest_simple_resource, []},
|
|
|
|
{[<<"forbidden_post">>], rest_forbidden_resource, [true]},
|
|
|
|
{[<<"simple_post">>], rest_forbidden_resource, [false]},
|
|
|
|
{[<<"nodelete">>], rest_nodelete_resource, []},
|
|
|
|
{[<<"resetags">>], rest_resource_etags, []},
|
2012-05-02 20:27:44 +02:00
|
|
|
{[<<"loop_timeout">>], http_handler_loop_timeout, []},
|
2011-04-14 01:26:02 +02:00
|
|
|
{[], http_handler, []}
|
|
|
|
]}
|
2011-04-08 16:30:37 +02:00
|
|
|
].
|
|
|
|
|
2011-12-28 18:02:32 +01:00
|
|
|
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),
|
2012-01-04 02:22:21 +01:00
|
|
|
ok = file:write_file(Level1("test.html"), "test.html\n"),
|
2011-12-28 18:02:32 +01:00
|
|
|
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")),
|
2012-01-04 02:22:21 +01:00
|
|
|
ok = file:delete(Level1("test.html")),
|
2011-12-28 18:02:32 +01:00
|
|
|
ok = file:del_dir(Level1("test_dir")),
|
|
|
|
ok = file:del_dir(Dir),
|
|
|
|
Config.
|
2011-04-08 16:30:37 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
%% Convenience functions.
|
|
|
|
|
|
|
|
quick_raw(Data, Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
Transport = ?config(transport, Config),
|
|
|
|
{ok, Client2} = cowboy_client:connect(
|
|
|
|
Transport, "localhost", ?config(port, Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:raw_request(Data, Client2),
|
|
|
|
case cowboy_client:response(Client3) of
|
|
|
|
{ok, Status, _, _} -> Status;
|
|
|
|
{error, _} -> closed
|
|
|
|
end.
|
|
|
|
|
|
|
|
build_url(Path, Config) ->
|
|
|
|
{scheme, Scheme} = lists:keyfind(scheme, 1, Config),
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
PortBin = list_to_binary(integer_to_list(Port)),
|
|
|
|
PathBin = list_to_binary(Path),
|
|
|
|
<< Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
|
|
|
|
|
|
|
|
quick_get(URL, Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url(URL, Config), Client),
|
|
|
|
{ok, Status, _, _} = cowboy_client:response(Client2),
|
|
|
|
Status.
|
|
|
|
|
|
|
|
body_to_chunks(_, <<>>, Acc) ->
|
|
|
|
lists:reverse([<<"0\r\n\r\n">>|Acc]);
|
|
|
|
body_to_chunks(ChunkSize, Body, Acc) ->
|
|
|
|
BodySize = byte_size(Body),
|
|
|
|
ChunkSize2 = case BodySize < ChunkSize of
|
|
|
|
true -> BodySize;
|
|
|
|
false -> ChunkSize
|
|
|
|
end,
|
|
|
|
<< Chunk:ChunkSize2/binary, Rest/binary >> = Body,
|
|
|
|
ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
|
|
|
|
body_to_chunks(ChunkSize, Rest,
|
|
|
|
[<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
|
|
|
|
|
|
|
|
%% Tests.
|
|
|
|
|
|
|
|
check_raw_status(Config) ->
|
|
|
|
Huge = [$0 || _ <- lists:seq(1, 5000)],
|
|
|
|
HugeCookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
|
|
|
|
"Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _ <- lists:seq(1, 40)]),
|
|
|
|
ResponsePacket =
|
|
|
|
"HTTP/1.0 302 Found
|
|
|
|
Location: http://www.google.co.il/
|
|
|
|
Cache-Control: private
|
|
|
|
Content-Type: text/html; charset=UTF-8
|
|
|
|
Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com
|
|
|
|
Date: Sun, 04 Dec 2011 15:55:01 GMT
|
|
|
|
Server: gws
|
|
|
|
Content-Length: 221
|
|
|
|
X-XSS-Protection: 1; mode=block
|
|
|
|
X-Frame-Options: SAMEORIGIN
|
|
|
|
|
|
|
|
<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
|
|
|
|
<TITLE>302 Moved</TITLE></HEAD><BODY>
|
|
|
|
<H1>302 Moved</H1>
|
|
|
|
The document has moved
|
|
|
|
<A HREF=\"http://www.google.co.il/\">here</A>.
|
|
|
|
</BODY></HTML>",
|
|
|
|
Tests = [
|
|
|
|
{200, ["GET / HTTP/1.0\r\nHost: localhost\r\n"
|
|
|
|
"Set-Cookie: ", HugeCookie, "\r\n\r\n"]},
|
|
|
|
{200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
|
|
|
|
{200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"},
|
|
|
|
{400, "\n"},
|
|
|
|
{400, "Garbage\r\n\r\n"},
|
|
|
|
{400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
|
2012-05-23 14:53:48 +02:00
|
|
|
{400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
|
2012-04-29 01:20:24 +02:00
|
|
|
{400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
|
|
|
|
{400, ResponsePacket},
|
|
|
|
{408, "GET / HTTP/1.1\r\n"},
|
|
|
|
{408, "GET / HTTP/1.1\r\nHost: localhost"},
|
|
|
|
{408, "GET / HTTP/1.1\r\nHost: localhost\r\n"},
|
|
|
|
{408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"},
|
|
|
|
{413, Huge},
|
|
|
|
{413, "GET / HTTP/1.1\r\n" ++ Huge},
|
|
|
|
{505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"},
|
|
|
|
{closed, ""},
|
|
|
|
{closed, "\r\n"},
|
|
|
|
{closed, "\r\n\r\n"},
|
|
|
|
{closed, "GET / HTTP/1.1"}
|
|
|
|
],
|
|
|
|
_ = [{Status, Packet} = begin
|
|
|
|
Ret = quick_raw(Packet, Config),
|
|
|
|
{Ret, Packet}
|
|
|
|
end || {Status, Packet} <- Tests].
|
|
|
|
|
|
|
|
check_status(Config) ->
|
|
|
|
Tests = [
|
|
|
|
{102, "/long_polling"},
|
|
|
|
{200, "/"},
|
|
|
|
{200, "/simple"},
|
2012-05-02 20:27:44 +02:00
|
|
|
{204, "/loop_timeout"},
|
2012-04-29 01:20:24 +02:00
|
|
|
{400, "/static/%2f"},
|
|
|
|
{400, "/static/%2e"},
|
|
|
|
{400, "/static/%2e%2e"},
|
|
|
|
{403, "/static/test_dir"},
|
|
|
|
{403, "/static/test_dir/"},
|
|
|
|
{403, "/static/test_noread"},
|
|
|
|
{404, "/not/found"},
|
|
|
|
{404, "/static/not_found"},
|
|
|
|
{500, "/handler_errors?case=handler_before_reply"},
|
|
|
|
{500, "/handler_errors?case=init_before_reply"},
|
|
|
|
{666, "/init_shutdown"}
|
|
|
|
],
|
|
|
|
_ = [{Status, URL} = begin
|
|
|
|
Ret = quick_get(URL, Config),
|
|
|
|
{Ret, URL}
|
|
|
|
end || {Status, URL} <- Tests].
|
2011-04-09 12:59:53 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
%% @todo Convert to cowboy_client.
|
2011-05-08 17:26:21 +02:00
|
|
|
chunked_response(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, {{"HTTP/1.1", 200, "OK"}, _, "chunked_handler\r\nworks fine!"}}
|
|
|
|
= httpc:request(binary_to_list(build_url("/chunked_response", Config))).
|
|
|
|
|
2012-05-05 07:53:17 +02:00
|
|
|
%% Check if sending requests whose size is around the MTU breaks something.
|
|
|
|
echo_body(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, [{mtu, MTU}]} = inet:ifget("lo", [mtu]),
|
|
|
|
[begin
|
|
|
|
Body = list_to_binary(lists:duplicate(Size, $a)),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"POST">>,
|
|
|
|
build_url("/echo/body", Config),
|
|
|
|
[{<<"connection">>, <<"close">>}],
|
|
|
|
Body, Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, Body, _} = cowboy_client:response_body(Client3)
|
|
|
|
end || Size <- lists:seq(MTU - 500, MTU)].
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
error_chain_handle_after_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=handle_after_reply", Config), Client2),
|
|
|
|
{ok, 200, _, Client4} = cowboy_client:response(Client3),
|
|
|
|
{ok, 200, _, Client5} = cowboy_client:response(Client4),
|
|
|
|
{error, closed} = cowboy_client:response(Client5).
|
|
|
|
|
|
|
|
error_chain_handle_before_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=handle_before_reply", Config), Client2),
|
|
|
|
{ok, 200, _, Client4} = cowboy_client:response(Client3),
|
|
|
|
{ok, 500, _, Client5} = cowboy_client:response(Client4),
|
|
|
|
{error, closed} = cowboy_client:response(Client5).
|
|
|
|
|
|
|
|
error_handle_after_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=handle_after_reply", Config), Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{error, closed} = cowboy_client:response(Client3).
|
|
|
|
|
|
|
|
error_init_after_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=init_after_reply", Config), Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{error, closed} = cowboy_client:response(Client3).
|
|
|
|
|
|
|
|
error_init_reply_handle_error(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{error, closed} = cowboy_client:response(Client3).
|
2011-05-08 17:26:21 +02:00
|
|
|
|
2011-04-14 01:26:02 +02:00
|
|
|
headers_dupe(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/headers/dupe", Config), Client),
|
|
|
|
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"connection">>, <<"close">>}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, Headers),
|
|
|
|
Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
|
|
|
|
1 = length(Connections),
|
|
|
|
{error, closed} = cowboy_client:response(Client3).
|
|
|
|
|
|
|
|
http10_chunkless(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
Transport = ?config(transport, Config),
|
|
|
|
{ok, Client2} = cowboy_client:connect(
|
|
|
|
Transport, "localhost", ?config(port, Config), Client),
|
|
|
|
Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
|
|
|
|
{ok, Client3} = cowboy_client:raw_request(Data, Client2),
|
|
|
|
{ok, 200, Headers, Client4} = cowboy_client:response(Client3),
|
|
|
|
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
|
|
|
%% Hack: we just try to get 28 bytes and compare.
|
|
|
|
{ok, Transport, Socket} = cowboy_client:transport(Client4),
|
|
|
|
Buffer = element(7, Client4),
|
|
|
|
Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
|
|
|
|
{ok, Recv} -> << Buffer/binary, Recv/binary >>;
|
|
|
|
_ -> Buffer
|
|
|
|
end,
|
|
|
|
<<"chunked_handler\r\nworks fine!">> = Buffer2.
|
2011-05-05 17:11:27 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
http10_hostless(Config) ->
|
|
|
|
Port10 = ?config(port, Config) + 10,
|
|
|
|
Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
|
2012-08-27 11:50:35 +02:00
|
|
|
ranch:start_listener(Name, 5,
|
2012-04-29 01:20:24 +02:00
|
|
|
?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}],
|
2012-08-27 12:58:04 +02:00
|
|
|
cowboy_protocol, [
|
2012-04-29 01:20:24 +02:00
|
|
|
{dispatch, [{'_', [
|
|
|
|
{[<<"http1.0">>, <<"hostless">>], http_handler, []}]}]},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}]
|
|
|
|
),
|
|
|
|
200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n",
|
|
|
|
[{port, Port10}|Config]),
|
|
|
|
cowboy:stop_listener(http10).
|
2011-09-13 23:41:34 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
keepalive_max(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
URL = build_url("/", Config),
|
|
|
|
ok = keepalive_max_loop(Client, URL, 50).
|
2011-12-22 21:35:40 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
keepalive_max_loop(_, _, 0) ->
|
2011-12-22 21:35:40 +01:00
|
|
|
ok;
|
2012-04-29 01:20:24 +02:00
|
|
|
keepalive_max_loop(Client, URL, N) ->
|
|
|
|
Headers = [{<<"connection">>, <<"keep-alive">>}],
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
|
|
|
|
{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
|
|
|
|
Expected = case N of
|
|
|
|
1 -> <<"close">>;
|
|
|
|
N -> <<"keep-alive">>
|
2011-12-22 21:35:40 +01:00
|
|
|
end,
|
2012-04-29 01:20:24 +02:00
|
|
|
{<<"connection">>, Expected}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, RespHeaders),
|
|
|
|
keepalive_max_loop(Client3, URL, N - 1).
|
|
|
|
|
|
|
|
keepalive_nl(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
URL = build_url("/", Config),
|
|
|
|
ok = keepalive_nl_loop(Client, URL, 10).
|
|
|
|
|
|
|
|
keepalive_nl_loop(_, _, 0) ->
|
|
|
|
ok;
|
|
|
|
keepalive_nl_loop(Client, URL, N) ->
|
|
|
|
Headers = [{<<"connection">>, <<"keep-alive">>}],
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
|
|
|
|
{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"connection">>, <<"keep-alive">>}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, RespHeaders),
|
|
|
|
{ok, Transport, Socket} = cowboy_client:transport(Client2),
|
|
|
|
ok = Transport:send(Socket, <<"\r\n">>), %% empty line
|
|
|
|
keepalive_nl_loop(Client3, URL, N - 1).
|
2011-12-22 21:35:40 +01:00
|
|
|
|
2011-11-08 00:51:49 +01:00
|
|
|
multipart(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
2011-11-08 00:51:49 +01:00
|
|
|
Body = <<
|
|
|
|
"This is a preamble."
|
|
|
|
"\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
|
|
|
|
"\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
|
|
|
|
"\r\n--OHai--"
|
|
|
|
"This is an epiloque."
|
|
|
|
>>,
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client2} = cowboy_client:request(<<"POST">>,
|
|
|
|
build_url("/multipart", Config),
|
|
|
|
[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
|
|
|
|
Body, Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, RespBody, _} = cowboy_client:response_body(Client3),
|
|
|
|
Parts = binary_to_term(RespBody),
|
2011-11-08 00:51:49 +01:00
|
|
|
Parts = [
|
|
|
|
{[{<<"X-Name">>, <<"answer">>}], <<"42">>},
|
|
|
|
{[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>}
|
|
|
|
].
|
|
|
|
|
2011-10-19 20:35:55 +02:00
|
|
|
nc_reqs(Config, Input) ->
|
2011-04-17 13:36:51 +02:00
|
|
|
Cat = os:find_executable("cat"),
|
|
|
|
Nc = os:find_executable("nc"),
|
|
|
|
case {Cat, Nc} of
|
|
|
|
{false, _} ->
|
|
|
|
{skip, {notfound, cat}};
|
|
|
|
{_, false} ->
|
|
|
|
{skip, {notfound, nc}};
|
|
|
|
_Good ->
|
|
|
|
%% Throw garbage at the server then check if it's still up.
|
|
|
|
{port, Port} = lists:keyfind(port, 1, Config),
|
2012-04-29 01:20:24 +02:00
|
|
|
StrPort = integer_to_list(Port),
|
|
|
|
[os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
|
|
|
|
|| _ <- lists:seq(1, 100)],
|
|
|
|
200 = quick_get("/", Config)
|
2011-04-17 13:36:51 +02:00
|
|
|
end.
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
nc_rand(Config) ->
|
|
|
|
nc_reqs(Config, "/dev/urandom").
|
2011-11-28 09:09:41 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
nc_zero(Config) ->
|
|
|
|
nc_reqs(Config, "/dev/zero").
|
2011-11-28 09:09:41 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
onrequest(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client),
|
|
|
|
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
|
|
|
|
{ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
|
2011-12-06 12:22:36 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
onrequest_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/?reply=1", Config), Client),
|
|
|
|
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
|
|
|
|
{ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
|
|
|
|
|
|
|
|
%% Hook for the above onrequest tests.
|
|
|
|
onrequest_hook(Req) ->
|
2012-08-27 13:28:57 +02:00
|
|
|
case cowboy_req:qs_val(<<"reply">>, Req) of
|
2012-04-29 01:20:24 +02:00
|
|
|
{undefined, Req2} ->
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req3} = cowboy_req:set_resp_header(
|
2012-04-29 01:20:24 +02:00
|
|
|
'Server', <<"Serenity">>, Req2),
|
|
|
|
Req3;
|
|
|
|
{_, Req2} ->
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req3} = cowboy_req:reply(
|
2012-04-29 01:20:24 +02:00
|
|
|
200, [], <<"replied!">>, Req2),
|
|
|
|
Req3
|
|
|
|
end.
|
2011-12-06 12:22:36 +01:00
|
|
|
|
2012-05-21 15:17:24 +02:00
|
|
|
onresponse_crash(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/handler_errors?case=init_before_reply", Config), Client),
|
2012-05-21 16:46:18 +02:00
|
|
|
{ok, 777, Headers, _} = cowboy_client:response(Client2),
|
2012-05-21 15:17:24 +02:00
|
|
|
{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
|
|
|
|
|
2012-05-01 01:59:36 +02:00
|
|
|
onresponse_reply(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client),
|
|
|
|
{ok, 777, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
|
|
|
|
%% Make sure we don't get the body initially sent.
|
|
|
|
{error, closed} = cowboy_client:response_body(Client3).
|
|
|
|
|
|
|
|
%% Hook for the above onresponse tests.
|
|
|
|
onresponse_hook(_, Headers, Req) ->
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req2} = cowboy_req:reply(
|
2012-05-01 01:59:36 +02:00
|
|
|
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
|
|
|
|
Req2.
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
pipeline(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client2),
|
|
|
|
{ok, Client4} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client3),
|
|
|
|
{ok, Client5} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), Client4),
|
|
|
|
{ok, Client6} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
|
|
|
|
{ok, 200, _, Client7} = cowboy_client:response(Client6),
|
|
|
|
{ok, 200, _, Client8} = cowboy_client:response(Client7),
|
|
|
|
{ok, 200, _, Client9} = cowboy_client:response(Client8),
|
|
|
|
{ok, 200, _, Client10} = cowboy_client:response(Client9),
|
|
|
|
{ok, 200, _, Client11} = cowboy_client:response(Client10),
|
|
|
|
{error, closed} = cowboy_client:response(Client11).
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
URL = build_url("/simple", Config),
|
|
|
|
ok = rest_keepalive_loop(Client, URL, 10).
|
2012-01-04 02:22:21 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive_loop(_, _, 0) ->
|
|
|
|
ok;
|
|
|
|
rest_keepalive_loop(Client, URL, N) ->
|
|
|
|
Headers = [{<<"connection">>, <<"keep-alive">>}],
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
|
|
|
|
{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"connection">>, <<"keep-alive">>}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, RespHeaders),
|
|
|
|
rest_keepalive_loop(Client3, URL, N - 1).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive_post(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive_post_loop(_, _, _, 0) ->
|
|
|
|
ok;
|
|
|
|
rest_keepalive_post_loop(Config, Client, simple_post, N) ->
|
|
|
|
Headers = [
|
|
|
|
{<<"connection">>, <<"keep-alive">>},
|
|
|
|
{<<"content-type">>, <<"text/plain">>}
|
|
|
|
],
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"POST">>,
|
|
|
|
build_url("/simple_post", Config), Headers, "12345", Client),
|
|
|
|
{ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"connection">>, <<"keep-alive">>}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, RespHeaders),
|
|
|
|
rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
|
|
|
|
rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
|
|
|
|
Headers = [
|
|
|
|
{<<"connection">>, <<"keep-alive">>},
|
|
|
|
{<<"content-type">>, <<"text/plain">>}
|
|
|
|
],
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"POST">>,
|
|
|
|
build_url("/forbidden_post", Config), Headers, "12345", Client),
|
|
|
|
{ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"connection">>, <<"keep-alive">>}
|
|
|
|
= lists:keyfind(<<"connection">>, 1, RespHeaders),
|
|
|
|
rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_nodelete(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"DELETE">>,
|
|
|
|
build_url("/nodelete", Config), Client),
|
|
|
|
{ok, 500, _, _} = cowboy_client:response(Client2).
|
|
|
|
|
|
|
|
rest_resource_get_etag(Config, Type) ->
|
|
|
|
rest_resource_get_etag(Config, Type, []).
|
|
|
|
|
|
|
|
rest_resource_get_etag(Config, Type, Headers) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/resetags?type=" ++ Type, Config), Headers, Client),
|
|
|
|
{ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
|
|
|
|
case lists:keyfind(<<"etag">>, 1, RespHeaders) of
|
|
|
|
false -> {Status, false};
|
|
|
|
{<<"etag">>, ETag} -> {Status, ETag}
|
|
|
|
end.
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_resource_etags(Config) ->
|
|
|
|
Tests = [
|
|
|
|
{200, <<"W/\"etag-header-value\"">>, "tuple-weak"},
|
|
|
|
{200, <<"\"etag-header-value\"">>, "tuple-strong"},
|
|
|
|
{200, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
|
|
|
|
{200, <<"\"etag-header-value\"">>, "binary-strong-quoted"},
|
|
|
|
{500, false, "binary-strong-unquoted"},
|
|
|
|
{500, false, "binary-weak-unquoted"}
|
|
|
|
],
|
|
|
|
_ = [{Status, ETag, Type} = begin
|
|
|
|
{Ret, RespETag} = rest_resource_get_etag(Config, Type),
|
|
|
|
{Ret, RespETag, Type}
|
|
|
|
end || {Status, ETag, Type} <- Tests].
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_resource_etags_if_none_match(Config) ->
|
|
|
|
Tests = [
|
|
|
|
{304, <<"W/\"etag-header-value\"">>, "tuple-weak"},
|
|
|
|
{304, <<"\"etag-header-value\"">>, "tuple-strong"},
|
|
|
|
{304, <<"W/\"etag-header-value\"">>, "binary-weak-quoted"},
|
|
|
|
{304, <<"\"etag-header-value\"">>, "binary-strong-quoted"}
|
|
|
|
],
|
|
|
|
_ = [{Status, Type} = begin
|
|
|
|
{Ret, _} = rest_resource_get_etag(Config, Type,
|
|
|
|
[{<<"if-none-match">>, ETag}]),
|
|
|
|
{Ret, Type}
|
|
|
|
end || {Status, ETag, Type} <- Tests].
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_body(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/set_resp/body", Config), Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, <<"A flameless dance does not equal a cycle">>, _}
|
|
|
|
= cowboy_client:response_body(Client3).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_header(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/set_resp/header", Config), Client),
|
|
|
|
{ok, 200, Headers, _} = cowboy_client:response(Client2),
|
|
|
|
{<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
|
|
|
|
{<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_overwrite(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/set_resp/overwrite", Config), Client),
|
|
|
|
{ok, 200, Headers, _} = cowboy_client:response(Client2),
|
|
|
|
{<<"server">>, <<"DesireDrive/1.0">>}
|
|
|
|
= lists:keyfind(<<"server">>, 1, Headers).
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2012-01-07 23:07:45 +01:00
|
|
|
static_attribute_etag(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_attribute_etag/test.html", Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_attribute_etag/test.html", Config), Client2),
|
|
|
|
{ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
|
|
|
|
{ok, 200, Headers2, _} = cowboy_client:response(Client4),
|
|
|
|
{<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
|
|
|
|
{<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
|
|
|
|
false = ETag1 =:= undefined,
|
|
|
|
ETag1 = ETag2.
|
2012-01-07 23:07:45 +01:00
|
|
|
|
|
|
|
static_function_etag(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_function_etag/test.html", Config), Client),
|
|
|
|
{ok, Client3} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_function_etag/test.html", Config), Client2),
|
|
|
|
{ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
|
|
|
|
{ok, 200, Headers2, _} = cowboy_client:response(Client4),
|
|
|
|
{<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
|
|
|
|
{<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
|
|
|
|
false = ETag1 =:= undefined,
|
|
|
|
ETag1 = ETag2.
|
|
|
|
|
|
|
|
%% Callback function for generating the ETag for the above test.
|
2012-01-07 23:07:45 +01:00
|
|
|
static_function_etag(Arguments, etag_data) ->
|
|
|
|
{_, Filepath} = lists:keyfind(filepath, 1, Arguments),
|
|
|
|
{_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
|
|
|
|
{_, _INode} = lists:keyfind(inode, 1, Arguments),
|
|
|
|
{_, _Modified} = lists:keyfind(mtime, 1, Arguments),
|
|
|
|
ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
|
|
|
|
[Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
|
2012-02-28 18:35:26 +01:00
|
|
|
{strong, iolist_to_binary(Checksum)}.
|
2012-01-07 23:07:45 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
static_mimetypes_function(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_mimetypes_function/test.html", Config), Client),
|
|
|
|
{ok, 200, Headers, _} = cowboy_client:response(Client2),
|
|
|
|
{<<"content-type">>, <<"text/html">>}
|
|
|
|
= lists:keyfind(<<"content-type">>, 1, Headers).
|
|
|
|
|
2012-05-21 15:53:31 +02:00
|
|
|
static_specify_file(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_specify_file", Config), Client),
|
|
|
|
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"content-type">>, <<"text/css">>}
|
|
|
|
= lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
{ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
|
|
|
|
|
|
|
|
static_specify_file_catchall(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static_specify_file/none", Config), Client),
|
|
|
|
{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
|
|
|
|
{<<"content-type">>, <<"text/css">>}
|
|
|
|
= lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
{ok, <<"test_file.css\n">>, _} = cowboy_client:response_body(Client3).
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
static_test_file(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static/test_file", Config), Client),
|
|
|
|
{ok, 200, Headers, _} = cowboy_client:response(Client2),
|
|
|
|
{<<"content-type">>, <<"application/octet-stream">>}
|
|
|
|
= lists:keyfind(<<"content-type">>, 1, Headers).
|
|
|
|
|
|
|
|
static_test_file_css(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/static/test_file.css", Config), Client),
|
|
|
|
{ok, 200, Headers, _} = cowboy_client:response(Client2),
|
|
|
|
{<<"content-type">>, <<"text/css">>}
|
|
|
|
= lists:keyfind(<<"content-type">>, 1, Headers).
|
|
|
|
|
|
|
|
stream_body_set_resp(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/stream_body/set_resp", Config), Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, <<"stream_body_set_resp">>, _}
|
|
|
|
= cowboy_client:response_body(Client3).
|
2012-03-29 01:14:44 +02:00
|
|
|
|
|
|
|
te_chunked(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
2012-03-29 01:14:44 +02:00
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Chunks = body_to_chunks(50, Body, []),
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/echo/body", Config),
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}],
|
|
|
|
Chunks, Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, Body, _} = cowboy_client:response_body(Client3).
|
2012-03-29 01:14:44 +02:00
|
|
|
|
|
|
|
te_chunked_delayed(Config) ->
|
2012-04-29 01:20:24 +02:00
|
|
|
Client = ?config(client, Config),
|
2012-03-29 01:14:44 +02:00
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Chunks = body_to_chunks(50, Body, []),
|
2012-04-29 01:20:24 +02:00
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/echo/body", Config),
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}], Client),
|
|
|
|
{ok, Transport, Socket} = cowboy_client:transport(Client2),
|
|
|
|
_ = [begin
|
|
|
|
ok = Transport:send(Socket, Chunk),
|
|
|
|
ok = timer:sleep(10)
|
|
|
|
end || Chunk <- Chunks],
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, Body, _} = cowboy_client:response_body(Client3).
|
2012-03-29 01:14:44 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
te_identity(Config) ->
|
|
|
|
Client = ?config(client, Config),
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
|
|
|
build_url("/echo/body", Config), [], Body, Client),
|
|
|
|
{ok, 200, _, Client3} = cowboy_client:response(Client2),
|
|
|
|
{ok, Body, _} = cowboy_client:response_body(Client3).
|