1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
%% 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([raw/1]). %% http.
-export([http_200/1, http_404/1]). %% http and https.
%% ct.
all() ->
[{group, http}, {group, https}].
groups() ->
BaseTests = [http_200, http_404],
[{http, [], [raw] ++ 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().
%% http.
raw_req(Packet, Config) ->
{port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port,
[binary, {active, false}, {packet, raw}]),
ok = gen_tcp:send(Socket, Packet),
{ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>}
= gen_tcp:recv(Socket, 0, 5000),
gen_tcp:close(Socket),
{Packet, list_to_integer(binary_to_list(Str))}.
raw(Config) ->
Tests = [
{"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
{"Garbage\r\n\r\n", 400},
{"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
{"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
{"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505}
],
[{Packet, StatusCode} = raw_req(Packet, Config)
|| {Packet, StatusCode} <- Tests].
%% 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)).
|