From 3a776b146e9a890eb2e1ce27ec59305c150dc156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 8 Apr 2011 16:30:37 +0200 Subject: 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'. --- .gitignore | 3 ++ Makefile | 4 +- test/http_SUITE.erl | 97 +++++++++++++++++++++++++++++++++++++++++++ test/http_SUITE_data/cert.pem | 14 +++++++ test/http_SUITE_data/key.pem | 18 ++++++++ test/http_handler.erl | 15 +++++++ 6 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 test/http_SUITE.erl create mode 100644 test/http_SUITE_data/cert.pem create mode 100644 test/http_SUITE_data/key.pem create mode 100644 test/http_handler.erl diff --git a/.gitignore b/.gitignore index cfb2329..334e906 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ +.eunit ebin +logs +test/*.beam diff --git a/Makefile b/Makefile index a6fb96c..8ee24b1 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,12 @@ app: clean: @$(REBAR) clean + rm -f test/*.beam rm -f erl_crash.dump -test: +tests: app @$(REBAR) eunit + @$(REBAR) ct dialyze: @$(REBAR) dialyze 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 +%% +%% 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)). diff --git a/test/http_SUITE_data/cert.pem b/test/http_SUITE_data/cert.pem new file mode 100644 index 0000000..a772007 --- /dev/null +++ b/test/http_SUITE_data/cert.pem @@ -0,0 +1,14 @@ +-----BEGIN CERTIFICATE----- +MIICKTCCAZICCQCl9gdHk5NqUjANBgkqhkiG9w0BAQUFADBZMQswCQYDVQQGEwJB +VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 +cyBQdHkgTHRkMRIwEAYDVQQDDAlsb2NhbGhvc3QwHhcNMTEwNDA4MTMxNTE3WhcN +MTEwNTA4MTMxNTE3WjBZMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0 +ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRIwEAYDVQQDDAls +b2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOjgFPS0dP4d8F1e +bNJPB+kAjM2FyTZGmkFCLUYONTPrdGOUIHL/UOGtU22BQzlskE+a6/j2Kg72tm8x +4X7yf+6s7CdRe086idNx9+GymZ64ZTnly33rD3AJffbBeWHwT2e9fuBeFk9WGC8v +kqECFZyqf7+znS0o48oBNcx3ePB5AgMBAAEwDQYJKoZIhvcNAQEFBQADgYEASTkv +oHuZyO8DgT8bIE6W3yM2fvlNshkhh7Thgpf32qQoVOxRU9EF0KpuJCCAHQHQNQlI +nf9Zc4UzOrLhxZBGocNhkkn4WLw2ysto/7+/+9xHah0M0l4auHLQagVLCoOsHUn2 +JX+A2NrbvuX5wnUrZGOdgY70tvMBeU/xLtp3af8= +-----END CERTIFICATE----- diff --git a/test/http_SUITE_data/key.pem b/test/http_SUITE_data/key.pem new file mode 100644 index 0000000..0b699cc --- /dev/null +++ b/test/http_SUITE_data/key.pem @@ -0,0 +1,18 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,F11262DB77BB804C + +jOJ+ft/dihIxz7CTuuK47fCTGdX7xMLANmA7mRg8y9OYhNZQiCz5GjcWLqe0NNl5 +qXPW0uvT/9B5O9o21Y2i/CKU1BqRLuXHXDsjHg7RGaSH6wIavWt+lR+I1sjieFbX +VByK1KHXjEU704DEILKJIA9gVzoYAgMzo+FTw2e/2jusXntxk8HXyF5zKTzjHBtI +NQGweJqTmfZjX3SgPP4Co/ShrA6fUG0uTp1HwbByJnwtAeT3xWJrAD4QSn7+qrlv +3qmEIqVXsvLrfZRY1WZ4uIsbLK8wkvxboSIoIK55VV9R2zRbwQULon6QJwKYujAr +J2WUYkHHQOMpaAzUmalaT+8GUt8/A1oSK4BdiSZywsMMm46/hDadXBzFg+dPL5g2 +Td+7/L0S6tUVWq4+YBp5EalZH6VQ4cqPYDJZUZ9xt6+yY7V5748lSdA7cHCROnbG +bKbSW9WbF7MPDHCjvCAfq+s1dafHJgyIOlMg2bm7V8eHWAA0xKQ/o7i5EyEyaKYR +UXGeAf+KfXcclEZ77v2RCXZvd6ceWkifm59qWv/3TCYaHiS2Aa3lVToMKTwYzzXQ +p5X5os6wv3IAi2nGyAIOoSDisdHmFteZNXNQsw0n3XCAYfsNMk+r5/r5YqDffURH +c8SMOCP4BIPoZ/abi/gnEntGqsx1YALg0aosHwHGDJ/l+QJC6u6PZk310YzRw4GL +K9+wscFgEub2OO+R83Vkfesj4tYzgOjab7+92a/soHdW0zhGejlvehODOgNZ6NUG +MPQlT+qpF9Jh5IThYXupXXFzJzQe3O/qVXy89m69JGa+AWRvbu+M/A== +-----END RSA PRIVATE KEY----- diff --git a/test/http_handler.erl b/test/http_handler.erl new file mode 100644 index 0000000..3882e17 --- /dev/null +++ b/test/http_handler.erl @@ -0,0 +1,15 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_handler). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/2]). + +init({_Transport, http}, Req, _Opts) -> + {ok, Req, undefined}. + +handle(Req, State) -> + {ok, Req2} = cowboy_http_req:reply(200, [], "http_handler", Req), + {ok, Req2, State}. + +terminate(_Req, _State) -> + ok. -- cgit v1.2.3