aboutsummaryrefslogtreecommitdiffstats
path: root/test/cowboy_test.erl
blob: f4a5706a59ea759531d8c5595565795a3c646387 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
%% Copyright (c) 2014, 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(cowboy_test).
-compile(export_all).

%% Start and stop applications and their dependencies.

start(Apps) ->
	_ = [do_start(App) || App <- Apps],
	ok.

do_start(App) ->
	case application:start(App) of
		ok ->
			ok;
		{error, {not_started, Dep}} ->
			do_start(Dep),
			do_start(App)
	end.

%% SSL certificate creation and safekeeping.

make_certs() ->
	{_, Cert, Key} = ct_helper:make_certs(),
	CertOpts = [{cert, Cert}, {key, Key}],
	Pid = spawn(fun() -> receive after infinity -> ok end end),
	?MODULE = ets:new(?MODULE, [ordered_set, public, named_table,
		{heir, Pid, undefined}]),
	ets:insert(?MODULE, {cert_opts, CertOpts}),
	ok.

get_certs() ->
	ets:lookup_element(?MODULE, cert_opts, 2).

%% Quick configuration value retrieval.

config(Key, Config) ->
	{_, Value} = lists:keyfind(Key, 1, Config),
	Value.

%% Test case description.

doc(String) ->
	ct:comment(String),
	ct:log(String).

%% List of all test cases in the suite.

all(Suite) ->
	lists:usort([F || {F, 1} <- Suite:module_info(exports),
		F =/= module_info,
		F =/= test, %% This is leftover from the eunit parse_transform...
		F =/= all,
		F =/= groups,
		string:substr(atom_to_list(F), 1, 5) =/= "init_",
		string:substr(atom_to_list(F), 1, 4) =/= "end_",
		string:substr(atom_to_list(F), 1, 3) =/= "do_"
	]).

%% Listeners initialization.

init_http(Ref, ProtoOpts, Config) ->
	{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
		{max_keepalive, 50},
		{timeout, 500}
		|ProtoOpts]),
	Port = ranch:get_port(Ref),
	[{type, tcp}, {port, Port}, {opts, []}|Config].

init_https(Ref, ProtoOpts, Config) ->
	Opts = get_certs(),
	{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
		{max_keepalive, 50},
		{timeout, 500}
		|ProtoOpts]),
	Port = ranch:get_port(Ref),
	[{type, ssl}, {port, Port}, {opts, Opts}|Config].

init_spdy(Ref, ProtoOpts, Config) ->
	Opts = get_certs(),
	{ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
		ProtoOpts),
	Port = ranch:get_port(Ref),
	[{type, ssl}, {port, Port}, {opts, Opts}|Config].

%% Common group of listeners used by most suites.

common_all() ->
	[
		{group, http},
		{group, https},
		{group, spdy},
		{group, http_compress},
		{group, https_compress},
		{group, spdy_compress}
	].

common_groups(Tests) ->
	[
		{http, [parallel], Tests},
		{https, [parallel], Tests},
		{spdy, [parallel], Tests},
		{http_compress, [parallel], Tests},
		{https_compress, [parallel], Tests},
		{spdy_compress, [parallel], Tests}
	].

init_common_groups(Name = http, Config, Mod) ->
	init_http(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]}
	], Config);
init_common_groups(Name = https, Config, Mod) ->
	init_https(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]}
	], Config);
init_common_groups(Name = spdy, Config, Mod) ->
	init_spdy(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]}
	], Config);
init_common_groups(Name = http_compress, Config, Mod) ->
	init_http(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]},
		{compress, true}
	], Config);
init_common_groups(Name = https_compress, Config, Mod) ->
	init_https(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]},
		{compress, true}
	], Config);
init_common_groups(Name = spdy_compress, Config, Mod) ->
	init_spdy(Name, [
		{env, [{dispatch, Mod:init_dispatch(Config)}]},
		{compress, true}
	], Config).

%% Support functions for testing using Gun.

gun_open(Config) ->
	gun_open(Config, []).

gun_open(Config, Opts) ->
	{ok, ConnPid} = gun:open("localhost", config(port, Config),
		[{retry, 0}, {type, config(type, Config)}|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, MRef) ->
	receive {'DOWN', MRef, process, ConnPid, gone} -> ok
	after 500 -> error(timeout) end.

%% Support functions for testing using a raw socket.

raw_open(Config) ->
	Transport = case config(type, Config) of
		tcp -> gen_tcp;
		ssl -> ssl
	end,
	{_, Opts} = lists:keyfind(opts, 1, Config),
	{ok, Socket} = Transport:connect("localhost", config(port, Config),
		[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.