2014-02-06 19:57:23 +01:00
|
|
|
%% Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
|
2013-05-30 20:21:01 +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(spdy_SUITE).
|
2014-04-20 22:20:54 +02:00
|
|
|
-compile(export_all).
|
2013-05-30 20:21:01 +02:00
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
-import(cowboy_test, [config/2]).
|
|
|
|
-import(cowboy_test, [gun_monitor_open/1]).
|
2013-05-30 20:21:01 +02:00
|
|
|
|
|
|
|
%% ct.
|
|
|
|
|
|
|
|
all() ->
|
|
|
|
[{group, spdy}].
|
|
|
|
|
|
|
|
groups() ->
|
2014-04-20 22:20:54 +02:00
|
|
|
[{spdy, [], cowboy_test:all(?MODULE)}].
|
2013-05-30 20:21:01 +02:00
|
|
|
|
|
|
|
init_per_suite(Config) ->
|
2013-12-28 17:49:37 +01:00
|
|
|
case proplists:get_value(ssl_app, ssl:versions()) of
|
|
|
|
Version when Version < "5.2.1" ->
|
|
|
|
{skip, "No NPN support in SSL application."};
|
|
|
|
_ ->
|
2014-04-20 22:20:54 +02:00
|
|
|
cowboy_test:start([cowboy, gun]),
|
|
|
|
Dir = config(priv_dir, Config) ++ "/static",
|
2013-12-28 17:49:37 +01:00
|
|
|
ct_helper:create_static_dir(Dir),
|
|
|
|
[{static_dir, Dir}|Config]
|
|
|
|
end.
|
2013-05-30 20:21:01 +02:00
|
|
|
|
2013-06-03 19:10:03 +02:00
|
|
|
end_per_suite(Config) ->
|
2014-04-20 22:20:54 +02:00
|
|
|
Dir = config(static_dir, Config),
|
2013-06-03 19:10:03 +02:00
|
|
|
ct_helper:delete_static_dir(Dir),
|
2014-04-20 22:20:54 +02:00
|
|
|
cowboy_test:stop([cowboy, gun]).
|
2013-05-30 20:21:01 +02:00
|
|
|
|
|
|
|
init_per_group(Name, Config) ->
|
|
|
|
{_, Cert, Key} = ct_helper:make_certs(),
|
|
|
|
Opts = [{cert, Cert}, {key, Key}],
|
|
|
|
{ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
|
|
|
|
{env, [{dispatch, init_dispatch(Config)}]}
|
|
|
|
]),
|
|
|
|
Port = ranch:get_port(Name),
|
2014-04-20 22:20:54 +02:00
|
|
|
[{port, Port}, {type, ssl}|Config].
|
2013-05-30 20:21:01 +02:00
|
|
|
|
|
|
|
end_per_group(Name, _) ->
|
|
|
|
cowboy:stop_listener(Name),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%% Dispatch configuration.
|
|
|
|
|
2013-06-03 19:10:03 +02:00
|
|
|
init_dispatch(Config) ->
|
2013-05-30 20:21:01 +02:00
|
|
|
cowboy_router:compile([
|
|
|
|
{"localhost", [
|
2013-06-03 19:10:03 +02:00
|
|
|
{"/static/[...]", cowboy_static,
|
2014-04-20 22:20:54 +02:00
|
|
|
{dir, config(static_dir, Config)}},
|
2013-09-02 19:14:28 +02:00
|
|
|
{"/echo/body", http_echo_body, []},
|
2013-05-30 20:21:01 +02:00
|
|
|
{"/chunked", http_chunked, []},
|
|
|
|
{"/", http_handler, []}
|
|
|
|
]}
|
|
|
|
]).
|
|
|
|
|
|
|
|
%% Convenience functions.
|
|
|
|
|
2014-04-20 22:20:54 +02:00
|
|
|
do_get(ConnPid, MRef, Host, Path) ->
|
2014-03-25 13:01:23 +01:00
|
|
|
StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
|
|
|
|
{response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
|
|
|
|
{IsFin, Status}.
|
2013-05-30 20:21:01 +02:00
|
|
|
|
|
|
|
%% Tests.
|
|
|
|
|
|
|
|
check_status(Config) ->
|
|
|
|
Tests = [
|
|
|
|
{200, nofin, "localhost", "/"},
|
|
|
|
{200, nofin, "localhost", "/chunked"},
|
2013-06-03 19:10:03 +02:00
|
|
|
{200, nofin, "localhost", "/static/style.css"},
|
2013-05-30 20:21:01 +02:00
|
|
|
{400, fin, "bad-host", "/"},
|
|
|
|
{400, fin, "localhost", "bad-path"},
|
|
|
|
{404, fin, "localhost", "/this/path/does/not/exist"}
|
|
|
|
],
|
2014-03-25 13:01:23 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
2013-05-30 20:21:01 +02:00
|
|
|
_ = [{Status, Fin, Host, Path} = begin
|
2014-04-20 22:20:54 +02:00
|
|
|
{IsFin, Ret} = do_get(ConnPid, MRef, Host, Path),
|
2013-08-31 18:39:49 +02:00
|
|
|
{Ret, IsFin, Host, Path}
|
|
|
|
end || {Status, Fin, Host, Path} <- Tests],
|
2014-03-25 13:01:23 +01:00
|
|
|
gun:close(ConnPid).
|
2013-09-02 19:14:28 +02:00
|
|
|
|
|
|
|
echo_body(Config) ->
|
2014-03-25 13:01:23 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
2013-09-02 19:14:28 +02:00
|
|
|
Body = << 0:800000 >>,
|
2014-03-25 13:01:23 +01:00
|
|
|
StreamRef = gun:post(ConnPid, "/echo/body", [
|
2013-09-02 19:14:28 +02:00
|
|
|
{<<"content-type">>, "application/octet-stream"}
|
|
|
|
], Body),
|
2014-03-25 13:01:23 +01:00
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
|
|
|
|
{ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
|
|
|
|
gun:close(ConnPid).
|
2013-09-02 19:14:28 +02:00
|
|
|
|
|
|
|
echo_body_multi(Config) ->
|
2014-03-25 13:01:23 +01:00
|
|
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
2013-09-02 19:14:28 +02:00
|
|
|
BodyChunk = << 0:80000 >>,
|
2014-03-25 13:01:23 +01:00
|
|
|
StreamRef = gun:post(ConnPid, "/echo/body", [
|
|
|
|
%% @todo I'm still unhappy with this. It shouldn't be required...
|
2013-09-02 19:14:28 +02:00
|
|
|
{<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
|
|
|
|
{<<"content-type">>, "application/octet-stream"}
|
|
|
|
]),
|
2014-03-25 13:01:23 +01:00
|
|
|
_ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
|
|
|
|
gun:data(ConnPid, StreamRef, fin, BodyChunk),
|
|
|
|
{response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
|
|
|
|
{ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
|
|
|
|
gun:close(ConnPid).
|