This repository was archived by the owner on Aug 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmochiweb_tests.erl
More file actions
203 lines (173 loc) · 6.98 KB
/
Copy pathmochiweb_tests.erl
File metadata and controls
203 lines (173 loc) · 6.98 KB
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
201
202
203
-module(mochiweb_tests).
-include_lib("eunit/include/eunit.hrl").
-include("mochiweb_test_util.hrl").
with_server(Transport, ServerFun, ClientFun) ->
mochiweb_test_util:with_server(Transport, ServerFun, ClientFun).
request_test() ->
R = mochiweb_request:new(z, z, "/foo/bar/baz%20wibble+quux?qs=2", z, []),
"/foo/bar/baz wibble quux" = R:get(path),
ok.
-define(LARGE_TIMEOUT, 60).
single_http_GET_test() ->
do_GET(plain, 1).
single_https_GET_test() ->
do_GET(ssl, 1).
multiple_http_GET_test() ->
do_GET(plain, 3).
multiple_https_GET_test() ->
do_GET(ssl, 3).
hundred_http_GET_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_GET(plain,100)) end}.
hundred_https_GET_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_GET(ssl,100)) end}.
single_128_http_POST_test() ->
do_POST(plain, 128, 1).
single_128_https_POST_test() ->
do_POST(ssl, 128, 1).
single_2k_http_POST_test() ->
do_POST(plain, 2048, 1).
single_2k_https_POST_test() ->
do_POST(ssl, 2048, 1).
single_100k_http_POST_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(plain, 102400, 1)) end}.
single_100k_https_POST_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(ssl, 102400, 1)) end}.
multiple_100k_http_POST_test() ->
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(plain, 102400, 3)) end}.
multiple_100K_https_POST_test() ->
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(ssl, 102400, 3)) end}.
hundred_128_http_POST_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(plain, 128, 100)) end}.
hundred_128_https_POST_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(ssl, 128, 100)) end}.
single_GET_scheme_test_() ->
[{"ssl", ?_assertEqual(ok, do_GET("derp", ssl, 1))},
{"plain", ?_assertEqual(ok, do_GET("derp", plain, 1))}].
single_GET_absoluteURI_test_() ->
Uri = "https://example.com:123/x/",
ServerFun = fun (Req) ->
Req:ok({"text/plain", Req:get(path)})
end,
%% Note that all the scheme/host/port information is discarded from path
ClientFun = new_client_fun('GET', [#treq{path = Uri, xreply = <<"/x/">>}]),
[{atom_to_list(Transport),
?_assertEqual(ok, with_server(Transport, ServerFun, ClientFun))}
|| Transport <- [ssl, plain]].
single_CONNECT_test_() ->
[{"ssl", ?_assertEqual(ok, do_CONNECT(ssl, 1))},
{"plain", ?_assertEqual(ok, do_CONNECT(plain, 1))}].
single_GET_any_test_() ->
ServerFun = fun (Req) ->
Req:ok({"text/plain", Req:get(path)})
end,
ClientFun = new_client_fun('GET', [#treq{path = "*", xreply = <<"*">>}]),
[{atom_to_list(Transport),
?_assertEqual(ok, with_server(Transport, ServerFun, ClientFun))}
|| Transport <- [ssl, plain]].
do_CONNECT(Transport, Times) ->
PathPrefix = "example.com:",
ReplyPrefix = "You requested: ",
ServerFun = fun (Req) ->
Reply = ReplyPrefix ++ Req:get(path),
Req:ok({"text/plain", Reply})
end,
TestReqs = [begin
Path = PathPrefix ++ integer_to_list(N),
ExpectedReply = list_to_binary(ReplyPrefix ++ Path),
#treq{path=Path, xreply=ExpectedReply}
end || N <- lists:seq(1, Times)],
ClientFun = new_client_fun('CONNECT', TestReqs),
ok = with_server(Transport, ServerFun, ClientFun),
ok.
do_GET(Transport, Times) ->
do_GET("/whatever/", Transport, Times).
do_GET(PathPrefix, Transport, Times) ->
ReplyPrefix = "You requested: ",
ServerFun = fun (Req) ->
Reply = ReplyPrefix ++ Req:get(path),
Req:ok({"text/plain", Reply})
end,
TestReqs = [begin
Path = PathPrefix ++ integer_to_list(N),
ExpectedReply = list_to_binary(ReplyPrefix ++ Path),
#treq{path=Path, xreply=ExpectedReply}
end || N <- lists:seq(1, Times)],
ClientFun = new_client_fun('GET', TestReqs),
ok = with_server(Transport, ServerFun, ClientFun),
ok.
do_POST(Transport, Size, Times) ->
ServerFun = fun (Req) ->
Body = Req:recv_body(),
Headers = [{"Content-Type", "application/octet-stream"}],
Req:respond({201, Headers, Body})
end,
TestReqs = [begin
Path = "/stuff/" ++ integer_to_list(N),
Body = crypto:strong_rand_bytes(Size),
#treq{path=Path, body=Body, xreply=Body}
end || N <- lists:seq(1, Times)],
ClientFun = new_client_fun('POST', TestReqs),
ok = with_server(Transport, ServerFun, ClientFun),
ok.
new_client_fun(Method, TestReqs) ->
fun (Transport, Port) ->
mochiweb_test_util:client_request(Transport, Port, Method, TestReqs)
end.
close_on_unread_data_test() ->
ok = with_server(
plain,
fun mochiweb_request:not_found/1,
fun close_on_unread_data_client/2).
close_on_unread_data_client(Transport, Port) ->
SockFun = mochiweb_test_util:sock_fun(Transport, Port),
%% A normal GET request should not trigger this behavior
Request0 = string:join(
["GET / HTTP/1.1",
"Host: localhost",
"",
""],
"\r\n"),
ok = SockFun({setopts, [{packet, http}]}),
ok = SockFun({send, Request0}),
?assertMatch(
{ok, {http_response, {1, 1}, 404, _}},
SockFun(recv)),
Headers0 = mochiweb_test_util:read_server_headers(SockFun),
?assertEqual(
undefined,
mochiweb_headers:get_value("Connection", Headers0)),
Len0 = list_to_integer(
mochiweb_headers:get_value("Content-Length", Headers0)),
_Body0 = mochiweb_test_util:drain_reply(SockFun, Len0, <<>>),
%% Re-use same socket
Request = string:join(
["POST / HTTP/1.1",
"Host: localhost",
"Content-Type: application/json",
"Content-Length: 2",
"",
"{}"],
"\r\n"),
ok = SockFun({setopts, [{packet, http}]}),
ok = SockFun({send, Request}),
?assertMatch(
{ok, {http_response, {1, 1}, 404, _}},
SockFun(recv)),
Headers = mochiweb_test_util:read_server_headers(SockFun),
%% Expect to see a Connection: close header when we know the
%% server will close the connection re #146
?assertEqual(
"close",
mochiweb_headers:get_value("Connection", Headers)),
Len = list_to_integer(mochiweb_headers:get_value("Content-Length", Headers)),
_Body = mochiweb_test_util:drain_reply(SockFun, Len, <<>>),
?assertEqual({error, closed}, SockFun(recv)),
ok.