diff options
author | Loïc Hoguin <[email protected]> | 2011-04-08 16:30:37 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2011-04-08 16:30:37 +0200 |
commit | 3a776b146e9a890eb2e1ce27ec59305c150dc156 (patch) | |
tree | 866f00f3863fd63432240087ebe7f57af7cb5fd9 /test/http_SUITE.erl | |
parent | 4cbba84a00e40d25211617194a643763963ee81b (diff) | |
download | cowboy-3a776b146e9a890eb2e1ce27ec59305c150dc156.tar.gz cowboy-3a776b146e9a890eb2e1ce27ec59305c150dc156.tar.bz2 cowboy-3a776b146e9a890eb2e1ce27ec59305c150dc156.zip |
Initial work on a ct test suite for the HTTP protocol.
Handles two basic tests for both HTTP and HTTPS.
Also renames 'make test' into 'make tests'.
Diffstat (limited to 'test/http_SUITE.erl')
-rw-r--r-- | test/http_SUITE.erl | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl new file mode 100644 index 0000000..e06a04c --- /dev/null +++ b/test/http_SUITE.erl @@ -0,0 +1,97 @@ +%% Copyright (c) 2011, Loïc Hoguin <[email protected]> +%% +%% 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"). + +-export([all/0, groups/0, init_per_suite/1, end_per_suite/1, + init_per_group/2, end_per_group/2]). %% ct. +-export([http_200/1, http_404/1]). %% Common tests for http and https. + +%% ct. + +all() -> + [{group, http}, {group, https}]. + +groups() -> + BaseTests = [http_200, http_404], + [{http, [], BaseTests}, + {https, [], BaseTests}]. + +init_per_suite(Config) -> + application:start(inets), + application:start(cowboy), + Config. + +end_per_suite(_Config) -> + application:stop(cowboy), + application:stop(inets), + ok. + +init_per_group(http, Config) -> + Port = 33080, + cowboy:start_listener(http, 100, + cowboy_tcp_transport, [{port, Port}], + cowboy_http_protocol, [{dispatch, init_http_dispatch()}] + ), + [{scheme, "http"}, {port, Port}|Config]; +init_per_group(https, Config) -> + Port = 33081, + application:start(crypto), + application:start(public_key), + application:start(ssl), + DataDir = ?config(data_dir, Config), + cowboy:start_listener(https, 100, + cowboy_ssl_transport, [ + {port, Port}, {certfile, DataDir ++ "cert.pem"}, + {keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}], + cowboy_http_protocol, [{dispatch, init_https_dispatch()}] + ), + [{scheme, "https"}, {port, Port}|Config]. + +end_per_group(http, _Config) -> + cowboy:stop_listener(http), + ok; +end_per_group(https, _Config) -> + cowboy:stop_listener(https), + application:stop(ssl), + application:stop(public_key), + application:stop(crypto), + ok. + +%% Dispatch configuration. + +init_http_dispatch() -> + [ + {["localhost"], [{[], http_handler, []}]} + ]. + +init_https_dispatch() -> + init_http_dispatch(). + +%% Common tests for http and https. + +build_url(Path, Config) -> + {scheme, Scheme} = lists:keyfind(scheme, 1, Config), + {port, Port} = lists:keyfind(port, 1, Config), + Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path. + +http_200(Config) -> + {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} = + httpc:request(build_url("/", Config)). + +http_404(Config) -> + {ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} = + httpc:request(build_url("/not/found", Config)). |