2014-02-06 19:36:25 +01:00
|
|
|
%% Copyright (c) 2011-2014, 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).
|
2014-03-24 14:20:52 +01:00
|
|
|
-compile(export_all).
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
-include_lib("common_test/include/ct.hrl").
|
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
2013-01-07 22:42:16 +01:00
|
|
|
[
|
|
|
|
{group, http},
|
|
|
|
{group, https},
|
|
|
|
{group, http_compress},
|
|
|
|
{group, https_compress},
|
|
|
|
{group, onrequest},
|
2013-01-20 18:57:42 +01:00
|
|
|
{group, onresponse},
|
2013-02-20 12:14:21 +01:00
|
|
|
{group, onresponse_capitalize},
|
2013-08-25 02:13:53 +09:00
|
|
|
{group, parse_host},
|
2013-02-20 12:14:21 +01:00
|
|
|
{group, set_env}
|
2013-01-07 22:42:16 +01:00
|
|
|
].
|
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,
|
2013-03-06 08:50:45 -05:00
|
|
|
echo_body_max_length,
|
|
|
|
echo_body_qs,
|
|
|
|
echo_body_qs_max_length,
|
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,
|
2013-11-18 20:32:47 +00:00
|
|
|
keepalive_stream_loop,
|
2012-04-29 01:20:24 +02:00
|
|
|
multipart,
|
2014-02-06 19:36:25 +01:00
|
|
|
multipart_large,
|
2012-04-29 01:20:24 +02:00
|
|
|
nc_rand,
|
|
|
|
nc_zero,
|
|
|
|
pipeline,
|
2013-02-11 09:03:13 +01:00
|
|
|
pipeline_long_polling,
|
2012-11-30 16:44:57 +01:00
|
|
|
rest_bad_accept,
|
2013-04-12 09:51:47 +04:00
|
|
|
rest_bad_content_type,
|
2012-12-03 15:57:27 +01:00
|
|
|
rest_expires,
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive,
|
|
|
|
rest_keepalive_post,
|
2012-11-29 14:33:45 +01:00
|
|
|
rest_missing_get_callbacks,
|
|
|
|
rest_missing_put_callbacks,
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_nodelete,
|
2013-04-12 18:20:41 +02:00
|
|
|
rest_options_default,
|
2013-02-15 22:41:55 +07:00
|
|
|
rest_param_all,
|
2012-10-15 17:56:55 -05:00
|
|
|
rest_patch,
|
2013-05-31 18:31:28 +02:00
|
|
|
rest_post_charset,
|
2013-04-26 18:34:01 +07:00
|
|
|
rest_postonly,
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_resource_etags,
|
|
|
|
rest_resource_etags_if_none_match,
|
|
|
|
set_resp_body,
|
|
|
|
set_resp_header,
|
|
|
|
set_resp_overwrite,
|
2013-01-06 19:49:01 +01:00
|
|
|
slowloris,
|
|
|
|
slowloris2,
|
2012-04-29 01:20:24 +02:00
|
|
|
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,
|
2013-01-05 23:35:30 +01:00
|
|
|
stream_body_set_resp_close,
|
2013-02-18 21:20:36 +00:00
|
|
|
stream_body_set_resp_chunked,
|
|
|
|
stream_body_set_resp_chunked10,
|
2013-06-26 18:04:20 +01:00
|
|
|
streamed_response,
|
2012-04-29 01:20:24 +02:00
|
|
|
te_chunked,
|
2013-01-29 19:12:34 +01:00
|
|
|
te_chunked_chopped,
|
2012-04-29 01:20:24 +02:00
|
|
|
te_chunked_delayed,
|
2013-06-11 21:47:26 +01:00
|
|
|
te_chunked_split_body,
|
|
|
|
te_chunked_split_crlf,
|
2012-04-29 01:20:24 +02:00
|
|
|
te_identity
|
|
|
|
],
|
|
|
|
[
|
2013-03-02 16:15:48 +01:00
|
|
|
{http, [parallel], Tests},
|
|
|
|
{https, [parallel], Tests},
|
|
|
|
{http_compress, [parallel], Tests},
|
|
|
|
{https_compress, [parallel], Tests},
|
|
|
|
{onrequest, [parallel], [
|
2012-04-29 01:20:24 +02:00
|
|
|
onrequest,
|
|
|
|
onrequest_reply
|
2012-05-01 01:59:36 +02:00
|
|
|
]},
|
2013-03-02 16:15:48 +01:00
|
|
|
{onresponse, [parallel], [
|
2012-05-21 15:17:24 +02:00
|
|
|
onresponse_crash,
|
2012-05-01 01:59:36 +02:00
|
|
|
onresponse_reply
|
2013-01-20 18:57:42 +01:00
|
|
|
]},
|
2013-03-02 16:15:48 +01:00
|
|
|
{onresponse_capitalize, [parallel], [
|
2013-01-20 18:57:42 +01:00
|
|
|
onresponse_capitalize
|
2013-02-20 12:14:21 +01:00
|
|
|
]},
|
2013-08-25 02:13:53 +09:00
|
|
|
{parse_host, [], [
|
|
|
|
parse_host
|
|
|
|
]},
|
2013-02-20 12:14:21 +01:00
|
|
|
{set_env, [], [
|
|
|
|
set_env_dispatch
|
2012-04-29 01:20:24 +02:00
|
|
|
]}
|
|
|
|
].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
2012-10-11 20:07:49 +02:00
|
|
|
application:start(crypto),
|
2014-03-24 14:20:52 +01:00
|
|
|
application:start(asn1),
|
|
|
|
application:start(public_key),
|
|
|
|
application:start(ssl),
|
2012-08-27 11:50:35 +02:00
|
|
|
application:start(ranch),
|
2014-03-24 14:20:52 +01:00
|
|
|
application:start(cowlib),
|
2014-04-11 14:34:35 +02:00
|
|
|
application:start(gun),
|
2011-04-08 16:30:37 +02:00
|
|
|
application:start(cowboy),
|
2013-06-03 17:20:54 +02:00
|
|
|
Dir = ?config(priv_dir, Config) ++ "/static",
|
|
|
|
ct_helper:create_static_dir(Dir),
|
|
|
|
[{static_dir, Dir}|Config].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
2013-06-03 17:20:54 +02:00
|
|
|
end_per_suite(Config) ->
|
|
|
|
Dir = ?config(static_dir, Config),
|
|
|
|
ct_helper:delete_static_dir(Dir),
|
2011-04-08 16:30:37 +02:00
|
|
|
application:stop(cowboy),
|
2014-03-24 14:20:52 +01:00
|
|
|
application:stop(gun),
|
2014-04-11 14:34:35 +02:00
|
|
|
application:stop(cowlib),
|
2014-03-24 14:20:52 +01:00
|
|
|
application:stop(ranch),
|
|
|
|
application:stop(ssl),
|
|
|
|
application:stop(public_key),
|
|
|
|
application:stop(asn1),
|
2012-10-11 20:07:49 +02:00
|
|
|
application:stop(crypto),
|
2011-04-08 16:30:37 +02:00
|
|
|
ok.
|
|
|
|
|
2014-03-24 14:20:52 +01:00
|
|
|
init_tcp_group(Ref, ProtoOpts, Config) ->
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_tcp,
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
|
2013-06-03 17:20:54 +02:00
|
|
|
{env, [{dispatch, init_dispatch(Config)}]},
|
2012-08-27 11:50:35 +02:00
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
2014-03-24 14:20:52 +01:00
|
|
|
|ProtoOpts]),
|
|
|
|
Port = ranch:get_port(Ref),
|
|
|
|
[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
|
|
|
|
|
|
|
|
init_ssl_group(Ref, ProtoOpts, Config) ->
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_ssl,
|
2013-05-01 18:20:56 +02:00
|
|
|
{_, Cert, Key} = ct_helper:make_certs(),
|
|
|
|
Opts = [{cert, Cert}, {key, Key}],
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
|
2013-06-03 17:20:54 +02:00
|
|
|
{env, [{dispatch, init_dispatch(Config)}]},
|
2012-08-27 11:50:35 +02:00
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
2014-03-24 14:20:52 +01:00
|
|
|
|ProtoOpts]),
|
|
|
|
Port = ranch:get_port(Ref),
|
|
|
|
[{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
|
|
|
|
|
|
|
|
init_per_group(http, Config) ->
|
|
|
|
init_tcp_group(http, [], Config);
|
|
|
|
init_per_group(https, Config) ->
|
|
|
|
init_ssl_group(https, [], Config);
|
2013-01-07 22:42:16 +01:00
|
|
|
init_per_group(http_compress, Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
init_tcp_group(http_compress, [{compress, true}], Config);
|
2013-01-07 22:42:16 +01:00
|
|
|
init_per_group(https_compress, Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
init_ssl_group(https_compress, [{compress, true}], Config);
|
|
|
|
%% Most, if not all of these, should be in separate test suites.
|
2013-01-07 22:42:16 +01:00
|
|
|
init_per_group(onrequest, Config) ->
|
|
|
|
Transport = ranch_tcp,
|
2013-03-02 14:38:10 +01:00
|
|
|
{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
|
2013-01-03 22:47:51 +01:00
|
|
|
{env, [{dispatch, init_dispatch(Config)}]},
|
2012-08-27 11:50:35 +02:00
|
|
|
{max_keepalive, 50},
|
|
|
|
{onrequest, fun onrequest_hook/1},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2013-03-02 14:38:10 +01:00
|
|
|
Port = ranch:get_port(onrequest),
|
2014-03-24 14:20:52 +01:00
|
|
|
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}|Config];
|
2012-05-01 01:59:36 +02:00
|
|
|
init_per_group(onresponse, Config) ->
|
2012-08-27 11:50:35 +02:00
|
|
|
Transport = ranch_tcp,
|
2013-03-02 14:38:10 +01:00
|
|
|
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
|
2013-01-03 22:47:51 +01:00
|
|
|
{env, [{dispatch, init_dispatch(Config)}]},
|
2012-08-27 11:50:35 +02:00
|
|
|
{max_keepalive, 50},
|
2012-10-04 22:59:33 +02:00
|
|
|
{onresponse, fun onresponse_hook/4},
|
2012-08-27 11:50:35 +02:00
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2013-03-02 14:38:10 +01:00
|
|
|
Port = ranch:get_port(onresponse),
|
2014-03-24 14:20:52 +01:00
|
|
|
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}|Config];
|
2013-01-20 18:57:42 +01:00
|
|
|
init_per_group(onresponse_capitalize, Config) ->
|
|
|
|
Transport = ranch_tcp,
|
2013-03-02 14:38:10 +01:00
|
|
|
{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
|
2013-01-20 18:57:42 +01:00
|
|
|
{env, [{dispatch, init_dispatch(Config)}]},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{onresponse, fun onresponse_capitalize_hook/4},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2013-03-02 14:38:10 +01:00
|
|
|
Port = ranch:get_port(onresponse_capitalize),
|
2014-03-24 14:20:52 +01:00
|
|
|
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}|Config];
|
2013-08-25 02:13:53 +09:00
|
|
|
init_per_group(parse_host, Config) ->
|
|
|
|
Transport = ranch_tcp,
|
|
|
|
Dispatch = cowboy_router:compile([
|
|
|
|
{'_', [
|
|
|
|
{"/req_attr", http_req_attr, []}
|
|
|
|
]}
|
|
|
|
]),
|
|
|
|
{ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
|
|
|
|
{env, [{dispatch, Dispatch}]},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
|
|
|
Port = ranch:get_port(http),
|
2014-03-24 14:20:52 +01:00
|
|
|
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}|Config];
|
2013-02-20 12:14:21 +01:00
|
|
|
init_per_group(set_env, Config) ->
|
|
|
|
Transport = ranch_tcp,
|
2013-03-02 14:38:10 +01:00
|
|
|
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
|
2013-02-20 12:14:21 +01:00
|
|
|
{env, [{dispatch, []}]},
|
|
|
|
{max_keepalive, 50},
|
|
|
|
{timeout, 500}
|
|
|
|
]),
|
2013-03-02 14:38:10 +01:00
|
|
|
Port = ranch:get_port(set_env),
|
2014-03-24 14:20:52 +01:00
|
|
|
[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
|
|
|
|
{transport, Transport}|Config].
|
2011-04-08 16:30:37 +02:00
|
|
|
|
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) ->
|
2013-01-28 16:53:09 +01:00
|
|
|
cowboy_router:compile([
|
|
|
|
{"localhost", [
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/chunked_response", http_chunked, []},
|
2013-06-26 18:04:20 +01:00
|
|
|
{"/streamed_response", http_streamed, []},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/init_shutdown", http_init_shutdown, []},
|
|
|
|
{"/long_polling", http_long_polling, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/headers/dupe", http_handler,
|
2012-09-21 09:18:56 +02:00
|
|
|
[{headers, [{<<"connection">>, <<"close">>}]}]},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/set_resp/header", http_set_resp,
|
2012-09-21 09:18:56 +02:00
|
|
|
[{headers, [{<<"vary">>, <<"Accept">>}]}]},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/set_resp/overwrite", http_set_resp,
|
2012-09-21 09:18:56 +02:00
|
|
|
[{headers, [{<<"server">>, <<"DesireDrive/1.0">>}]}]},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/set_resp/body", http_set_resp,
|
2011-11-28 09:09:41 +01:00
|
|
|
[{body, <<"A flameless dance does not equal a cycle">>}]},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/stream_body/set_resp", http_stream_body,
|
2011-12-28 18:00:27 +01:00
|
|
|
[{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/stream_body/set_resp_close",
|
2013-04-24 20:28:44 +02:00
|
|
|
http_stream_body, [
|
2013-01-05 23:35:30 +01:00
|
|
|
{reply, set_resp_close},
|
|
|
|
{body, <<"stream_body_set_resp_close">>}]},
|
2013-02-18 21:20:36 +00:00
|
|
|
{"/stream_body/set_resp_chunked",
|
|
|
|
http_stream_body, [
|
|
|
|
{reply, set_resp_chunked},
|
|
|
|
{body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/static/[...]", cowboy_static,
|
2013-11-02 14:41:46 +01:00
|
|
|
{dir, ?config(static_dir, Config)}},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/static_mimetypes_function/[...]", cowboy_static,
|
2013-11-02 14:41:46 +01:00
|
|
|
{dir, ?config(static_dir, Config),
|
|
|
|
[{mimetypes, ?MODULE, mimetypes_text_html}]}},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/handler_errors", http_errors, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/static_attribute_etag/[...]", cowboy_static,
|
2013-11-02 14:41:46 +01:00
|
|
|
{dir, ?config(static_dir, Config)}},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/static_function_etag/[...]", cowboy_static,
|
2013-11-02 14:41:46 +01:00
|
|
|
{dir, ?config(static_dir, Config),
|
|
|
|
[{etag, ?MODULE, etag_gen}]}},
|
|
|
|
{"/static_specify_file/[...]", cowboy_static,
|
|
|
|
{file, ?config(static_dir, Config) ++ "/style.css"}},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/multipart", http_multipart, []},
|
2014-02-06 19:36:25 +01:00
|
|
|
{"/multipart/large", http_multipart_stream, []},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/echo/body", http_echo_body, []},
|
|
|
|
{"/echo/body_qs", http_body_qs, []},
|
2013-02-15 22:41:55 +07:00
|
|
|
{"/param_all", rest_param_all, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/bad_accept", rest_simple_resource, []},
|
2013-04-12 09:51:47 +04:00
|
|
|
{"/bad_content_type", rest_patch_resource, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/simple", rest_simple_resource, []},
|
|
|
|
{"/forbidden_post", rest_forbidden_resource, [true]},
|
|
|
|
{"/simple_post", rest_forbidden_resource, [false]},
|
|
|
|
{"/missing_get_callbacks", rest_missing_callbacks, []},
|
|
|
|
{"/missing_put_callbacks", rest_missing_callbacks, []},
|
|
|
|
{"/nodelete", rest_nodelete_resource, []},
|
2013-05-31 18:31:28 +02:00
|
|
|
{"/post_charset", rest_post_charset_resource, []},
|
2013-04-26 18:34:01 +07:00
|
|
|
{"/postonly", rest_postonly_resource, []},
|
2012-10-15 17:56:55 -05:00
|
|
|
{"/patch", rest_patch_resource, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/resetags", rest_resource_etags, []},
|
|
|
|
{"/rest_expires", rest_expires, []},
|
2013-04-12 18:20:41 +02:00
|
|
|
{"/rest_empty_resource", rest_empty_resource, []},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/loop_recv", http_loop_recv, []},
|
2013-11-18 20:32:47 +00:00
|
|
|
{"/loop_stream_recv", http_loop_stream_recv, []},
|
2013-04-24 20:28:44 +02:00
|
|
|
{"/loop_timeout", http_loop_timeout, []},
|
2013-01-28 16:53:09 +01:00
|
|
|
{"/", http_handler, []}
|
2011-04-14 01:26:02 +02:00
|
|
|
]}
|
2013-01-28 16:53:09 +01:00
|
|
|
]).
|
2011-04-08 16:30:37 +02:00
|
|
|
|
2013-11-02 14:41:46 +01:00
|
|
|
etag_gen(_, _, _) ->
|
|
|
|
{strong, <<"etag">>}.
|
|
|
|
|
|
|
|
mimetypes_text_html(_) ->
|
|
|
|
<<"text/html">>.
|
|
|
|
|
2014-03-24 14:20:52 +01:00
|
|
|
%% Support functions for testing using Gun.
|
|
|
|
|
|
|
|
gun_open(Config) ->
|
|
|
|
gun_open(Config, []).
|
|
|
|
|
|
|
|
gun_open(Config, Opts) ->
|
|
|
|
{_, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{_, Type} = lists:keyfind(type, 1, Config),
|
|
|
|
{ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}, {type, Type}|Opts]),
|
|
|
|
ConnPid.
|
|
|
|
|
|
|
|
gun_monitor_open(Config) ->
|
|
|
|
gun_monitor_open(Config, []).
|
|
|
|
|
|
|
|
gun_monitor_open(Config, Opts) ->
|
|
|
|
ConnPid = gun_open(Config, Opts),
|
|
|
|
{ConnPid, monitor(process, ConnPid)}.
|
|
|
|
|
|
|
|
gun_is_gone(ConnPid) ->
|
|
|
|
gun_is_gone(ConnPid, monitor(process, ConnPid)).
|
|
|
|
|
|
|
|
gun_is_gone(ConnPid, MRef) ->
|
|
|
|
receive {'DOWN', MRef, process, ConnPid, gone} -> ok
|
|
|
|
after 500 -> error(timeout) end.
|
|
|
|
|
|
|
|
%% Support functions for testing using a raw socket.
|
|
|
|
|
|
|
|
raw_open(Config) ->
|
|
|
|
{_, Port} = lists:keyfind(port, 1, Config),
|
|
|
|
{_, Type} = lists:keyfind(type, 1, Config),
|
|
|
|
Transport = case Type of
|
|
|
|
tcp -> gen_tcp;
|
|
|
|
ssl -> ssl
|
|
|
|
end,
|
|
|
|
{_, Opts} = lists:keyfind(opts, 1, Config),
|
|
|
|
{ok, Socket} = Transport:connect("localhost", Port,
|
|
|
|
[binary, {active, false}, {packet, raw},
|
|
|
|
{reuseaddr, true}, {nodelay, true}|Opts]),
|
|
|
|
{raw_client, Socket, Transport}.
|
|
|
|
|
|
|
|
raw_send({raw_client, Socket, Transport}, Data) ->
|
|
|
|
Transport:send(Socket, Data).
|
|
|
|
|
|
|
|
raw_recv_head({raw_client, Socket, Transport}) ->
|
|
|
|
{ok, Data} = Transport:recv(Socket, 0, 5000),
|
|
|
|
raw_recv_head(Socket, Transport, Data).
|
|
|
|
|
|
|
|
raw_recv_head(Socket, Transport, Buffer) ->
|
|
|
|
case binary:match(Buffer, <<"\r\n\r\n">>) of
|
|
|
|
nomatch ->
|
|
|
|
{ok, Data} = Transport:recv(Socket, 0, 5000),
|
|
|
|
raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
|
|
|
|
{_, _} ->
|
|
|
|
Buffer
|
|
|
|
end.
|
|
|
|
|
|
|
|
raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
|
|
|
|
{ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
|
|
|
|
ok.
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
%% Convenience functions.
|
|
|
|
|
|
|
|
quick_raw(Data, Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
Client = raw_open(Config),
|
|
|
|
ok = raw_send(Client, Data),
|
|
|
|
case catch raw_recv_head(Client) of
|
|
|
|
{'EXIT', _} -> closed;
|
|
|
|
Resp -> element(2, cow_http:parse_status_line(Resp))
|
2012-04-29 01:20:24 +02:00
|
|
|
end.
|
|
|
|
|
2014-03-24 14:20:52 +01:00
|
|
|
quick_get(Path, Config) ->
|
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, Path),
|
|
|
|
{response, _, Status, _} = gun:await(ConnPid, Ref),
|
|
|
|
gun:close(ConnPid),
|
2012-04-29 01:20:24 +02:00
|
|
|
Status.
|
|
|
|
|
|
|
|
%% 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 =
|
2012-09-20 06:22:51 +02:00
|
|
|
"HTTP/1.0 302 Found\r
|
|
|
|
Location: http://www.google.co.il/\r
|
|
|
|
Cache-Control: private\r
|
|
|
|
Content-Type: text/html; charset=UTF-8\r
|
|
|
|
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\r
|
|
|
|
Date: Sun, 04 Dec 2011 15:55:01 GMT\r
|
|
|
|
Server: gws\r
|
|
|
|
Content-Length: 221\r
|
|
|
|
X-XSS-Protection: 1; mode=block\r
|
|
|
|
X-Frame-Options: SAMEORIGIN\r
|
|
|
|
\r
|
2012-04-29 01:20:24 +02:00
|
|
|
<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 = [
|
2013-02-11 09:03:13 +01:00
|
|
|
{102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
|
|
|
|
"Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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"},
|
2013-04-12 14:16:59 +02:00
|
|
|
{200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n"
|
|
|
|
"Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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"},
|
2012-12-22 16:45:04 +01:00
|
|
|
{400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
|
2012-09-26 14:11:53 +02:00
|
|
|
{505, ResponsePacket},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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"},
|
2012-09-29 13:57:30 +02:00
|
|
|
{414, Huge},
|
|
|
|
{400, "GET / HTTP/1.1\r\n" ++ Huge},
|
2013-02-11 09:03:13 +01:00
|
|
|
{500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n"
|
|
|
|
"Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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}
|
2013-04-26 00:46:15 +01:00
|
|
|
end || {Status, Packet} <- Tests],
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
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"},
|
2013-06-03 17:20:54 +02:00
|
|
|
{403, "/static/directory"},
|
|
|
|
{403, "/static/directory/"},
|
|
|
|
{403, "/static/unreadable"},
|
2012-04-29 01:20:24 +02:00
|
|
|
{404, "/not/found"},
|
|
|
|
{404, "/static/not_found"},
|
2013-12-28 17:34:59 +01:00
|
|
|
{500, "/handler_errors?case=handle_before_reply"},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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
|
|
|
|
2011-05-08 17:26:21 +02:00
|
|
|
chunked_response(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/chunked_response"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
2013-01-22 14:22:28 +01:00
|
|
|
true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
2012-05-05 07:53:17 +02:00
|
|
|
%% Check if sending requests whose size is around the MTU breaks something.
|
|
|
|
echo_body(Config) ->
|
2013-06-13 11:47:24 +02:00
|
|
|
MTU = ct_helper:get_loopback_mtu(),
|
2013-04-26 00:46:15 +01:00
|
|
|
_ = [begin
|
2012-05-05 07:53:17 +02:00
|
|
|
Body = list_to_binary(lists:duplicate(Size, $a)),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body", [], Body),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref)
|
2013-04-26 00:46:15 +01:00
|
|
|
end || Size <- lists:seq(MTU - 500, MTU)],
|
|
|
|
ok.
|
2012-05-05 07:53:17 +02:00
|
|
|
|
2013-03-06 08:50:45 -05:00
|
|
|
%% Check if sending request whose size is bigger than 1000000 bytes causes 413
|
|
|
|
echo_body_max_length(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body", [], << 0:8000008 >>),
|
|
|
|
{response, nofin, 413, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2013-03-06 08:50:45 -05:00
|
|
|
|
|
|
|
% check if body_qs echo's back results
|
|
|
|
echo_body_qs(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
|
|
|
|
2013-03-06 08:50:45 -05:00
|
|
|
echo_body_qs_max_length(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:15996/unit:8 >>),
|
|
|
|
{response, nofin, 413, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2013-03-06 08:50:45 -05:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
error_chain_handle_after_reply(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid, "/"),
|
|
|
|
Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
error_chain_handle_before_reply(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid, "/"),
|
|
|
|
Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
|
|
|
|
{response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
error_handle_after_reply(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
error_init_after_reply(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
error_init_reply_handle_error(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2011-05-08 17:26:21 +02:00
|
|
|
|
2011-04-14 01:26:02 +02:00
|
|
|
headers_dupe(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/headers/dupe"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
%% Ensure that only one connection header was received.
|
|
|
|
[<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
http10_chunkless(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
|
|
|
|
Ref = gun:get(ConnPid, "/chunked_response"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
2012-04-29 01:20:24 +02:00
|
|
|
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
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, [
|
2013-01-28 16:53:09 +01:00
|
|
|
{env, [{dispatch, cowboy_router:compile([
|
|
|
|
{'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
|
2012-04-29 01:20:24 +02:00
|
|
|
{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) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
|
|
|
|
|| _ <- lists:seq(1, 49)],
|
|
|
|
CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
|
|
|
|
_ = [begin
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
|
|
|
|
end || Ref <- Refs],
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
|
|
|
|
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
keepalive_nl(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [begin
|
|
|
|
Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
|
|
|
|
gun:dbg_send_raw(ConnPid, <<"\r\n">>),
|
|
|
|
Ref
|
|
|
|
end || _ <- lists:seq(1, 10)],
|
|
|
|
_ = [begin
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
|
|
|
|
end || Ref <- Refs],
|
|
|
|
ok.
|
2011-12-22 21:35:40 +01:00
|
|
|
|
2013-11-18 20:32:47 +00:00
|
|
|
keepalive_stream_loop(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [begin
|
|
|
|
Ref = gun:post(ConnPid, "/loop_stream_recv",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
|
|
|
_ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
|
|
|
|
|| ID <- lists:seq(1, 250)],
|
|
|
|
gun:data(ConnPid, Ref, fin, <<>>),
|
|
|
|
Ref
|
|
|
|
end || _ <- lists:seq(1, 10)],
|
|
|
|
_ = [begin
|
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref)
|
|
|
|
end || Ref <- Refs],
|
|
|
|
ok.
|
2013-11-18 20:32:47 +00:00
|
|
|
|
2011-11-08 00:51:49 +01:00
|
|
|
multipart(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(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"
|
2014-02-06 19:36:25 +01:00
|
|
|
"\r\n--OHai--\r\n"
|
|
|
|
"This is an epilogue."
|
2011-11-08 00:51:49 +01:00
|
|
|
>>,
|
2014-03-24 14:20:52 +01:00
|
|
|
Ref = gun:post(ConnPid, "/multipart",
|
2012-04-29 01:20:24 +02:00
|
|
|
[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
|
2014-03-24 14:20:52 +01:00
|
|
|
Body),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, RespBody} = gun:await_body(ConnPid, Ref),
|
2012-04-29 01:20:24 +02:00
|
|
|
Parts = binary_to_term(RespBody),
|
2011-11-08 00:51:49 +01:00
|
|
|
Parts = [
|
2012-09-21 13:07:51 +02:00
|
|
|
{[{<<"x-name">>, <<"answer">>}], <<"42">>},
|
|
|
|
{[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
|
2014-03-24 14:20:52 +01:00
|
|
|
],
|
|
|
|
ok.
|
2011-11-08 00:51:49 +01:00
|
|
|
|
2014-02-06 19:36:25 +01:00
|
|
|
multipart_large(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
2014-02-06 19:36:25 +01:00
|
|
|
Boundary = "----------",
|
|
|
|
Big = << 0:9000000/unit:8 >>,
|
|
|
|
Bigger = << 0:9999999/unit:8 >>,
|
|
|
|
Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
|
|
|
|
"--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
|
|
|
|
"--", Boundary, "--\r\n"],
|
2014-03-24 14:20:52 +01:00
|
|
|
Ref = gun:post(ConnPid, "/multipart/large",
|
2014-02-06 19:36:25 +01:00
|
|
|
[{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
|
2014-03-24 14:20:52 +01:00
|
|
|
Body),
|
|
|
|
{response, fin, 200, _} = gun:await(ConnPid, Ref),
|
2014-02-06 19:36:25 +01:00
|
|
|
ok.
|
|
|
|
|
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) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
2012-04-29 01:20:24 +02:00
|
|
|
{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2011-12-06 12:22:36 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
onrequest_reply(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/?reply=1"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
2012-04-29 01:20:24 +02:00
|
|
|
{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
%% 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-09-21 09:18:56 +02:00
|
|
|
cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2);
|
2012-04-29 01:20:24 +02:00
|
|
|
{_, 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
|
|
|
|
2013-01-20 18:57:42 +01:00
|
|
|
onresponse_capitalize(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
Client = raw_open(Config),
|
|
|
|
ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
|
|
|
|
Data = raw_recv_head(Client),
|
|
|
|
false = nomatch =:= binary:match(Data, <<"Content-Length">>),
|
|
|
|
ok.
|
2013-01-20 18:57:42 +01:00
|
|
|
|
|
|
|
%% Hook for the above onresponse_capitalize test.
|
|
|
|
onresponse_capitalize_hook(Status, Headers, Body, Req) ->
|
|
|
|
Headers2 = [{cowboy_bstr:capitalize_token(N), V}
|
|
|
|
|| {N, V} <- Headers],
|
|
|
|
{ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
|
|
|
|
Req2.
|
|
|
|
|
2012-05-21 15:17:24 +02:00
|
|
|
onresponse_crash(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
|
|
|
|
{response, fin, 777, Headers} = gun:await(ConnPid, Ref),
|
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) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/"),
|
|
|
|
{response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
|
2012-05-01 01:59:36 +02:00
|
|
|
{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
ok.
|
2012-05-01 01:59:36 +02:00
|
|
|
|
|
|
|
%% Hook for the above onresponse tests.
|
2012-10-04 22:59:33 +02:00
|
|
|
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.
|
|
|
|
|
2013-08-25 02:13:53 +09:00
|
|
|
parse_host(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
2013-08-25 02:13:53 +09:00
|
|
|
Tests = [
|
2014-03-24 14:20:52 +01:00
|
|
|
{<<"example.org:8080">>, <<"example.org\n8080">>},
|
|
|
|
{<<"example.org">>, <<"example.org\n80">>},
|
|
|
|
{<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
|
|
|
|
{<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
|
|
|
|
{<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
|
|
|
|
{<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
|
|
|
|
{<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
|
|
|
|
{<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
|
2013-08-25 02:13:53 +09:00
|
|
|
],
|
|
|
|
[begin
|
2014-03-24 14:20:52 +01:00
|
|
|
Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
|
|
|
|
[{<<"host">>, Host}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref)
|
|
|
|
end || {Host, Body} <- Tests],
|
|
|
|
ok.
|
2013-08-25 02:13:53 +09:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
pipeline(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
|
|
|
|
_ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
|
|
|
|
ok.
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2013-02-11 09:03:13 +01:00
|
|
|
pipeline_long_polling(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)],
|
|
|
|
_ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
|
|
|
|
ok.
|
2013-02-11 09:03:13 +01:00
|
|
|
|
2013-02-15 22:41:55 +07:00
|
|
|
rest_param_all(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
%% Accept without param.
|
|
|
|
Ref1 = gun:get(ConnPid, "/param_all",
|
|
|
|
[{<<"accept">>, <<"text/plain">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref1),
|
|
|
|
{ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
|
|
|
|
%% Accept with param.
|
|
|
|
Ref2 = gun:get(ConnPid, "/param_all",
|
|
|
|
[{<<"accept">>, <<"text/plain;level=1">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref2),
|
|
|
|
{ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
|
|
|
|
%% Accept with param and quality.
|
|
|
|
Ref3 = gun:get(ConnPid, "/param_all",
|
|
|
|
[{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref3),
|
|
|
|
{ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
|
|
|
|
Ref4 = gun:get(ConnPid, "/param_all",
|
|
|
|
[{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref4),
|
|
|
|
{ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
|
|
|
|
%% Without Accept.
|
|
|
|
Ref5 = gun:get(ConnPid, "/param_all"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref5),
|
|
|
|
{ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
|
|
|
|
%% Content-Type without param.
|
|
|
|
Ref6 = gun:put(ConnPid, "/param_all",
|
|
|
|
[{<<"content-type">>, <<"text/plain">>}]),
|
|
|
|
{response, fin, 204, _} = gun:await(ConnPid, Ref6),
|
|
|
|
%% Content-Type with param.
|
|
|
|
Ref7 = gun:put(ConnPid, "/param_all",
|
|
|
|
[{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
|
|
|
|
{response, fin, 204, _} = gun:await(ConnPid, Ref7),
|
|
|
|
ok.
|
2013-02-15 22:41:55 +07:00
|
|
|
|
2012-11-30 16:44:57 +01:00
|
|
|
rest_bad_accept(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/bad_accept",
|
|
|
|
[{<<"accept">>, <<"1">>}]),
|
|
|
|
{response, fin, 400, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2012-11-30 16:44:57 +01:00
|
|
|
|
2013-04-12 09:51:47 +04:00
|
|
|
rest_bad_content_type(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:patch(ConnPid, "/bad_content_type",
|
|
|
|
[{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
|
|
|
|
{response, fin, 415, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2013-04-12 09:51:47 +04:00
|
|
|
|
2012-12-03 15:57:27 +01:00
|
|
|
rest_expires(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/rest_expires"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
|
|
|
|
{_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
|
2012-12-03 15:57:27 +01:00
|
|
|
Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
|
|
|
|
ok.
|
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
|
|
|
|
_ = [begin
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
|
|
|
|
end || Ref <- Refs],
|
|
|
|
ok.
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_keepalive_post(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Refs = [{
|
|
|
|
gun:post(ConnPid, "/forbidden_post",
|
|
|
|
[{<<"content-type">>, <<"text/plain">>}]),
|
|
|
|
gun:post(ConnPid, "/simple_post",
|
|
|
|
[{<<"content-type">>, <<"text/plain">>}])
|
|
|
|
} || _ <- lists:seq(1, 5)],
|
|
|
|
_ = [begin
|
|
|
|
{response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
|
|
|
|
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
|
|
|
|
{response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
|
|
|
|
{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
|
|
|
|
end || {Ref1, Ref2} <- Refs],
|
|
|
|
ok.
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-11-29 14:33:45 +01:00
|
|
|
rest_missing_get_callbacks(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/missing_get_callbacks"),
|
|
|
|
{response, fin, 500, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2012-11-29 14:33:45 +01:00
|
|
|
|
|
|
|
rest_missing_put_callbacks(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:put(ConnPid, "/missing_put_callbacks",
|
|
|
|
[{<<"content-type">>, <<"application/json">>}], <<"{}">>),
|
|
|
|
{response, fin, 500, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2012-11-29 14:33:45 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_nodelete(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:delete(ConnPid, "/nodelete"),
|
|
|
|
{response, fin, 500, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
2013-04-12 18:20:41 +02:00
|
|
|
rest_options_default(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:options(ConnPid, "/rest_empty_resource"),
|
|
|
|
{response, fin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
|
|
|
|
ok.
|
2013-04-12 18:20:41 +02:00
|
|
|
|
2012-10-15 17:56:55 -05:00
|
|
|
rest_patch(Config) ->
|
|
|
|
Tests = [
|
|
|
|
{204, [{<<"content-type">>, <<"text/plain">>}], <<"whatever">>},
|
2013-01-29 22:27:50 +01:00
|
|
|
{422, [{<<"content-type">>, <<"text/plain">>}], <<"false">>},
|
2012-10-15 17:56:55 -05:00
|
|
|
{400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
|
|
|
|
{415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
|
|
|
|
],
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
2012-10-15 17:56:55 -05:00
|
|
|
_ = [begin
|
2014-03-24 14:20:52 +01:00
|
|
|
Ref = gun:patch(ConnPid, "/patch", Headers, Body),
|
|
|
|
{response, fin, Status, _} = gun:await(ConnPid, Ref)
|
|
|
|
end || {Status, Headers, Body} <- Tests],
|
|
|
|
ok.
|
2012-10-15 17:56:55 -05:00
|
|
|
|
2013-05-31 18:31:28 +02:00
|
|
|
rest_post_charset(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/post_charset",
|
|
|
|
[{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
|
|
|
|
{response, fin, 204, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2013-05-31 18:31:28 +02:00
|
|
|
|
2013-04-26 18:34:01 +07:00
|
|
|
rest_postonly(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/postonly",
|
|
|
|
[{<<"content-type">>, <<"text/plain">>}], "12345"),
|
|
|
|
{response, fin, 204, _} = gun:await(ConnPid, Ref),
|
|
|
|
ok.
|
2013-04-26 18:34:01 +07:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
rest_resource_get_etag(Config, Type) ->
|
|
|
|
rest_resource_get_etag(Config, Type, []).
|
|
|
|
|
|
|
|
rest_resource_get_etag(Config, Type, Headers) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
|
|
|
|
{response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
|
2012-04-29 01:20:24 +02:00
|
|
|
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
|
|
|
|
2013-02-20 12:14:21 +01:00
|
|
|
set_env_dispatch(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid1 = gun_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid1, "/"),
|
|
|
|
{response, fin, 400, _} = gun:await(ConnPid1, Ref1),
|
2013-02-20 12:14:21 +01:00
|
|
|
ok = cowboy:set_env(set_env, dispatch,
|
|
|
|
cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid2 = gun_open(Config),
|
|
|
|
Ref2 = gun:get(ConnPid2, "/"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
|
|
|
|
ok.
|
2013-02-20 12:14:21 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_body(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/set_resp/body"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, <<"A flameless dance does not equal a cycle">>}
|
|
|
|
= gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_header(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/set_resp/header"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
|
|
|
|
{_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
|
|
|
|
ok.
|
2011-12-29 00:06:22 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
set_resp_overwrite(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/set_resp/overwrite"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
|
|
|
|
ok.
|
2011-12-28 18:00:27 +01:00
|
|
|
|
2013-01-06 19:49:01 +01:00
|
|
|
slowloris(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
Client = raw_open(Config),
|
2013-01-06 19:49:01 +01:00
|
|
|
try
|
|
|
|
[begin
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = raw_send(Client, [C]),
|
2013-01-06 19:49:01 +01:00
|
|
|
receive after 25 -> ok end
|
|
|
|
end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
|
|
|
|
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
|
|
|
|
"Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
|
|
|
|
error(failure)
|
|
|
|
catch error:{badmatch, _} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
slowloris2(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
Client = raw_open(Config),
|
|
|
|
ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
|
2013-01-06 19:49:01 +01:00
|
|
|
receive after 300 -> ok end,
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = raw_send(Client, "Host: localhost\r\n"),
|
2013-01-06 19:49:01 +01:00
|
|
|
receive after 300 -> ok end,
|
2014-03-24 14:20:52 +01:00
|
|
|
Data = raw_recv_head(Client),
|
|
|
|
{_, 408, _, _} = cow_http:parse_status_line(Data),
|
|
|
|
ok.
|
2013-01-06 19:49:01 +01:00
|
|
|
|
2012-01-07 23:07:45 +01:00
|
|
|
static_attribute_etag(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
|
|
|
|
Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
|
|
|
|
{response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
|
|
|
|
{response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
|
|
|
|
{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
|
|
|
|
{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
|
|
|
|
true = ETag =/= undefined,
|
|
|
|
ok.
|
2012-01-07 23:07:45 +01:00
|
|
|
|
|
|
|
static_function_etag(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
|
|
|
|
Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
|
|
|
|
{response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
|
|
|
|
{response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
|
|
|
|
{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
|
|
|
|
{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
|
|
|
|
true = ETag =/= undefined,
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
static_mimetypes_function(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
2012-05-21 15:53:31 +02:00
|
|
|
static_specify_file(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/static_specify_file"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-05-21 15:53:31 +02:00
|
|
|
|
|
|
|
static_specify_file_catchall(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/static_specify_file/none"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-05-21 15:53:31 +02:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
static_test_file(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/static/unknown"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
static_test_file_css(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/static/style.css"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
|
|
|
{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
|
|
|
ok.
|
2012-04-29 01:20:24 +02:00
|
|
|
|
|
|
|
stream_body_set_resp(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/stream_body/set_resp"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-03-29 01:14:44 +02:00
|
|
|
|
2013-01-05 23:35:30 +01:00
|
|
|
stream_body_set_resp_close(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
{ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2013-01-05 23:35:30 +01:00
|
|
|
|
2013-02-18 21:20:36 +00:00
|
|
|
stream_body_set_resp_chunked(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
2013-02-18 21:20:36 +00:00
|
|
|
{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2013-02-18 21:20:36 +00:00
|
|
|
|
|
|
|
stream_body_set_resp_chunked10(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
|
|
|
|
Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
|
|
|
|
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
|
|
|
|
false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
|
|
|
|
{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
|
|
|
|
gun_is_gone(ConnPid, MRef).
|
2013-02-18 21:20:36 +00:00
|
|
|
|
2014-03-24 14:20:52 +01:00
|
|
|
%% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
|
2013-06-26 18:04:20 +01:00
|
|
|
streamed_response(Config) ->
|
2014-03-24 14:20:52 +01:00
|
|
|
Client = raw_open(Config),
|
|
|
|
ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
|
|
|
|
Data = raw_recv_head(Client),
|
|
|
|
{'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
|
|
|
|
{Headers, Rest2} = cow_http:parse_headers(Rest),
|
2013-06-26 18:04:20 +01:00
|
|
|
false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
|
2014-03-24 14:20:52 +01:00
|
|
|
Rest2Size = byte_size(Rest2),
|
|
|
|
ok = case <<"streamed_handler\r\nworks fine!">> of
|
|
|
|
Rest2 -> ok;
|
|
|
|
<< Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
|
|
|
|
end.
|
2013-06-26 18:04:20 +01:00
|
|
|
|
2012-03-29 01:14:44 +02:00
|
|
|
te_chunked(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}], Body),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
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]).
|
2013-01-29 19:12:34 +01:00
|
|
|
|
|
|
|
te_chunked_chopped(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
2013-01-29 19:12:34 +01:00
|
|
|
_ = [begin
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = gun:dbg_send_raw(ConnPid, << C >>),
|
|
|
|
receive after 10 -> ok end
|
2013-01-29 19:12:34 +01:00
|
|
|
end || << C >> <= Body2],
|
2014-03-24 14:20:52 +01:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-03-29 01:14:44 +02:00
|
|
|
|
|
|
|
te_chunked_delayed(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Chunks = body_to_chunks(50, Body, []),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
2012-04-29 01:20:24 +02:00
|
|
|
_ = [begin
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = gun:dbg_send_raw(ConnPid, Chunk),
|
|
|
|
receive after 10 -> ok end
|
2012-04-29 01:20:24 +02:00
|
|
|
end || Chunk <- Chunks],
|
2014-03-24 14:20:52 +01:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2012-03-29 01:14:44 +02:00
|
|
|
|
2013-06-11 21:47:26 +01:00
|
|
|
te_chunked_split_body(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Chunks = body_to_chunks(50, Body, []),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
2013-06-11 21:47:26 +01:00
|
|
|
_ = [begin
|
|
|
|
case Chunk of
|
|
|
|
<<"0\r\n\r\n">> ->
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = gun:dbg_send_raw(ConnPid, Chunk);
|
2013-06-11 21:47:26 +01:00
|
|
|
_ ->
|
|
|
|
[Size, ChunkBody, <<>>] =
|
|
|
|
binary:split(Chunk, [<<"\r\n">>], [global]),
|
|
|
|
PartASize = random:uniform(byte_size(ChunkBody)),
|
|
|
|
<<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
|
2014-03-24 14:20:52 +01:00
|
|
|
ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
|
|
|
|
receive after 10 -> ok end,
|
|
|
|
ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
|
2013-06-11 21:47:26 +01:00
|
|
|
end
|
|
|
|
end || Chunk <- Chunks],
|
2014-03-24 14:20:52 +01:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2013-06-11 21:47:26 +01:00
|
|
|
|
|
|
|
te_chunked_split_crlf(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
|
|
|
Chunks = body_to_chunks(50, Body, []),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body",
|
|
|
|
[{<<"transfer-encoding">>, <<"chunked">>}]),
|
2013-06-11 21:47:26 +01:00
|
|
|
_ = [begin
|
2014-03-24 14:20:52 +01:00
|
|
|
%% Split in the newline just before the end of the chunk.
|
2013-06-11 21:47:26 +01:00
|
|
|
Len = byte_size(Chunk) - (random:uniform(2) - 1),
|
2014-03-24 14:20:52 +01:00
|
|
|
<< Chunk2:Len/binary, End/binary >> = Chunk,
|
|
|
|
ok = gun:dbg_send_raw(ConnPid, Chunk2),
|
|
|
|
receive after 10 -> ok end,
|
|
|
|
ok = gun:dbg_send_raw(ConnPid, End)
|
2013-06-11 21:47:26 +01:00
|
|
|
end || Chunk <- Chunks],
|
2014-03-24 14:20:52 +01:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|
2013-06-11 21:47:26 +01:00
|
|
|
|
2012-04-29 01:20:24 +02:00
|
|
|
te_identity(Config) ->
|
|
|
|
Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
|
2014-03-24 14:20:52 +01:00
|
|
|
ConnPid = gun_open(Config),
|
|
|
|
Ref = gun:post(ConnPid, "/echo/body", [], Body),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, Ref),
|
|
|
|
ok.
|