1
%% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
2
%% Copyright (c) 2011, Anthony Ramine <nox@dev-extend.eu>
4
%% Permission to use, copy, modify, and/or distribute this software for any
5
%% purpose with or without fee is hereby granted, provided that the above
6
%% copyright notice and this permission notice appear in all copies.
8
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
-include_lib("common_test/include/ct.hrl").
20
-export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
21
init_per_group/2, end_per_group/2]). %% ct.
22
-export([chunked_response/1, headers_dupe/1, headers_huge/1,
23
keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
24
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
25
set_resp_body/1, stream_body_set_resp/1, response_as_req/1,
26
static_mimetypes_function/1, static_attribute_etag/1,
27
static_function_etag/1, multipart/1]). %% http.
28
-export([http_200/1, http_404/1, handler_errors/1,
29
file_200/1, file_403/1, dir_403/1, file_404/1,
30
file_400/1]). %% http and https.
31
-export([http_10_hostless/1]). %% misc.
32
-export([rest_simple/1, rest_keepalive/1, rest_keepalive_post/1]). %% rest.
37
[{group, http}, {group, https}, {group, misc}, {group, rest}].
40
BaseTests = [http_200, http_404, handler_errors,
41
file_200, file_403, dir_403, file_404, file_400],
42
[{http, [], [chunked_response, headers_dupe, headers_huge,
43
keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
44
set_resp_header, set_resp_overwrite,
45
set_resp_body, response_as_req, stream_body_set_resp,
46
static_mimetypes_function, static_attribute_etag,
47
static_function_etag, multipart] ++ BaseTests},
48
{https, [], BaseTests},
49
{misc, [], [http_10_hostless]},
50
{rest, [], [rest_simple, rest_keepalive, rest_keepalive_post]}].
52
init_per_suite(Config) ->
53
application:start(inets),
54
application:start(cowboy),
57
end_per_suite(_Config) ->
58
application:stop(cowboy),
59
application:stop(inets),
62
init_per_group(http, Config) ->
64
Config1 = init_static_dir(Config),
65
cowboy:start_listener(http, 100,
66
cowboy_tcp_transport, [{port, Port}],
67
cowboy_http_protocol, [{max_keepalive, 50},
68
{dispatch, init_http_dispatch(Config1)}]
70
[{scheme, "http"}, {port, Port}|Config1];
71
init_per_group(https, Config) ->
73
Config1 = init_static_dir(Config),
74
application:start(crypto),
75
application:start(public_key),
76
application:start(ssl),
77
DataDir = ?config(data_dir, Config),
78
cowboy:start_listener(https, 100,
79
cowboy_ssl_transport, [
80
{port, Port}, {certfile, DataDir ++ "cert.pem"},
81
{keyfile, DataDir ++ "key.pem"}, {password, "cowboy"}],
82
cowboy_http_protocol, [{dispatch, init_https_dispatch(Config1)}]
84
[{scheme, "https"}, {port, Port}|Config1];
85
init_per_group(misc, Config) ->
87
cowboy:start_listener(misc, 100,
88
cowboy_tcp_transport, [{port, Port}],
89
cowboy_http_protocol, [{dispatch, [{'_', [
90
{[], http_handler, []}
92
[{port, Port}|Config];
93
init_per_group(rest, Config) ->
95
cowboy:start_listener(reset, 100,
96
cowboy_tcp_transport, [{port, Port}],
97
cowboy_http_protocol, [{dispatch, [{'_', [
98
{[<<"simple">>], rest_simple_resource, []},
99
{[<<"forbidden_post">>], rest_forbidden_resource, [true]},
100
{[<<"simple_post">>], rest_forbidden_resource, [false]}
102
[{port, Port}|Config].
104
end_per_group(https, Config) ->
105
cowboy:stop_listener(https),
106
application:stop(ssl),
107
application:stop(public_key),
108
application:stop(crypto),
109
end_static_dir(Config),
111
end_per_group(http, Config) ->
112
cowboy:stop_listener(http),
113
end_static_dir(Config);
114
end_per_group(Listener, _Config) ->
115
cowboy:stop_listener(Listener),
118
%% Dispatch configuration.
120
init_http_dispatch(Config) ->
122
{[<<"localhost">>], [
123
{[<<"chunked_response">>], chunked_handler, []},
124
{[<<"init_shutdown">>], http_handler_init_shutdown, []},
125
{[<<"long_polling">>], http_handler_long_polling, []},
126
{[<<"headers">>, <<"dupe">>], http_handler,
127
[{headers, [{<<"Connection">>, <<"close">>}]}]},
128
{[<<"set_resp">>, <<"header">>], http_handler_set_resp,
129
[{headers, [{<<"Vary">>, <<"Accept">>}]}]},
130
{[<<"set_resp">>, <<"overwrite">>], http_handler_set_resp,
131
[{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
132
{[<<"set_resp">>, <<"body">>], http_handler_set_resp,
133
[{body, <<"A flameless dance does not equal a cycle">>}]},
134
{[<<"stream_body">>, <<"set_resp">>], http_handler_stream_body,
135
[{reply, set_resp}, {body, <<"stream_body_set_resp">>}]},
136
{[<<"static">>, '...'], cowboy_http_static,
137
[{directory, ?config(static_dir, Config)},
138
{mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
139
{[<<"static_mimetypes_function">>, '...'], cowboy_http_static,
140
[{directory, ?config(static_dir, Config)},
141
{mimetypes, {fun(Path, data) when is_binary(Path) ->
142
[<<"text/html">>] end, data}}]},
143
{[<<"handler_errors">>], http_handler_errors, []},
144
{[<<"static_attribute_etag">>, '...'], cowboy_http_static,
145
[{directory, ?config(static_dir, Config)},
146
{etag, {attributes, [filepath, filesize, inode, mtime]}}]},
147
{[<<"static_function_etag">>, '...'], cowboy_http_static,
148
[{directory, ?config(static_dir, Config)},
149
{etag, {fun static_function_etag/2, etag_data}}]},
150
{[<<"multipart">>], http_handler_multipart, []},
151
{[], http_handler, []}
155
init_https_dispatch(Config) ->
156
init_http_dispatch(Config).
159
init_static_dir(Config) ->
160
Dir = filename:join(?config(priv_dir, Config), "static"),
161
Level1 = fun(Name) -> filename:join(Dir, Name) end,
162
ok = file:make_dir(Dir),
163
ok = file:write_file(Level1("test_file"), "test_file\n"),
164
ok = file:write_file(Level1("test_file.css"), "test_file.css\n"),
165
ok = file:write_file(Level1("test_noread"), "test_noread\n"),
166
ok = file:change_mode(Level1("test_noread"), 8#0333),
167
ok = file:write_file(Level1("test.html"), "test.html\n"),
168
ok = file:make_dir(Level1("test_dir")),
169
[{static_dir, Dir}|Config].
171
end_static_dir(Config) ->
172
Dir = ?config(static_dir, Config),
173
Level1 = fun(Name) -> filename:join(Dir, Name) end,
174
ok = file:delete(Level1("test_file")),
175
ok = file:delete(Level1("test_file.css")),
176
ok = file:delete(Level1("test_noread")),
177
ok = file:delete(Level1("test.html")),
178
ok = file:del_dir(Level1("test_dir")),
179
ok = file:del_dir(Dir),
184
chunked_response(Config) ->
185
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "chunked_handler\r\nworks fine!"}} =
186
httpc:request(build_url("/chunked_response", Config)).
188
headers_dupe(Config) ->
189
{port, Port} = lists:keyfind(port, 1, Config),
190
{ok, Socket} = gen_tcp:connect("localhost", Port,
191
[binary, {active, false}, {packet, raw}]),
192
ok = gen_tcp:send(Socket, "GET /headers/dupe HTTP/1.1\r\n"
193
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
194
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
195
{_Start, _Length} = binary:match(Data, <<"Connection: close">>),
196
nomatch = binary:match(Data, <<"Connection: keep-alive">>),
197
{error, closed} = gen_tcp:recv(Socket, 0, 1000).
199
headers_huge(Config) ->
200
Cookie = lists:flatten(["whatever_man_biiiiiiiiiiiig_cookie_me_want_77="
201
"Wed Apr 06 2011 10:38:52 GMT-0500 (CDT)" || _N <- lists:seq(1, 40)]),
202
{_Packet, 200} = raw_req(["GET / HTTP/1.0\r\nHost: localhost\r\n"
203
"Set-Cookie: ", Cookie, "\r\n\r\n"], Config).
205
keepalive_nl(Config) ->
206
{port, Port} = lists:keyfind(port, 1, Config),
207
{ok, Socket} = gen_tcp:connect("localhost", Port,
208
[binary, {active, false}, {packet, raw}]),
209
ok = keepalive_nl_loop(Socket, 10),
210
ok = gen_tcp:close(Socket).
212
keepalive_nl_loop(_Socket, 0) ->
214
keepalive_nl_loop(Socket, N) ->
215
ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
216
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
217
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
218
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
219
nomatch = binary:match(Data, <<"Connection: close">>),
220
ok = gen_tcp:send(Socket, "\r\n"), %% extra nl
221
keepalive_nl_loop(Socket, N - 1).
223
max_keepalive(Config) ->
224
{port, Port} = lists:keyfind(port, 1, Config),
225
{ok, Socket} = gen_tcp:connect("localhost", Port,
226
[binary, {active, false}, {packet, raw}]),
227
ok = max_keepalive_loop(Socket, 50),
228
{error, closed} = gen_tcp:recv(Socket, 0, 1000).
230
max_keepalive_loop(_Socket, 0) ->
232
max_keepalive_loop(Socket, N) ->
233
ok = gen_tcp:send(Socket, "GET / HTTP/1.1\r\n"
234
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
235
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
236
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
238
1 -> {_, _} = binary:match(Data, <<"Connection: close">>);
239
N -> nomatch = binary:match(Data, <<"Connection: close">>)
241
keepalive_nl_loop(Socket, N - 1).
244
Url = build_url("/multipart", Config),
246
"This is a preamble."
247
"\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
248
"\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
250
"This is an epiloque."
252
Request = {Url, [], "multipart/x-makes-no-sense; boundary=OHai", Body},
253
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Response}} =
254
httpc:request(post, Request, [], [{body_format, binary}]),
255
Parts = binary_to_term(Response),
257
{[{<<"X-Name">>, <<"answer">>}], <<"42">>},
258
{[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>}
262
nc_reqs(Config, "/dev/urandom").
265
nc_reqs(Config, "/dev/zero").
267
nc_reqs(Config, Input) ->
268
Cat = os:find_executable("cat"),
269
Nc = os:find_executable("nc"),
272
{skip, {notfound, cat}};
274
{skip, {notfound, nc}};
276
%% Throw garbage at the server then check if it's still up.
277
{port, Port} = lists:keyfind(port, 1, Config),
278
[nc_run_req(Port, Input) || _N <- lists:seq(1, 100)],
279
Packet = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n",
280
{Packet, 200} = raw_req(Packet, Config)
283
nc_run_req(Port, Input) ->
284
os:cmd("cat " ++ Input ++ " | nc localhost " ++ integer_to_list(Port)).
287
{port, Port} = lists:keyfind(port, 1, Config),
288
{ok, Socket} = gen_tcp:connect("localhost", Port,
289
[binary, {active, false}, {packet, raw}]),
290
ok = gen_tcp:send(Socket,
291
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
292
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
293
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
294
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: keep-alive\r\n\r\n"
295
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"),
296
Data = pipeline_recv(Socket, <<>>),
297
Reqs = binary:split(Data, << "\r\n\r\nhttp_handler" >>, [global, trim]),
299
pipeline_check(Reqs).
301
pipeline_check([]) ->
303
pipeline_check([Req|Tail]) ->
304
<< "HTTP/1.1 200", _Rest/bits >> = Req,
305
pipeline_check(Tail).
307
pipeline_recv(Socket, SoFar) ->
308
case gen_tcp:recv(Socket, 0, 6000) of
310
pipeline_recv(Socket, << SoFar/binary, Data/binary >>);
312
ok = gen_tcp:close(Socket),
316
raw_req(Packet, Config) ->
317
{port, Port} = lists:keyfind(port, 1, Config),
318
{ok, Socket} = gen_tcp:connect("localhost", Port,
319
[binary, {active, false}, {packet, raw}]),
320
ok = gen_tcp:send(Socket, Packet),
321
Res = case gen_tcp:recv(Socket, 0, 6000) of
322
{ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >>} ->
323
list_to_integer(binary_to_list(Str));
327
gen_tcp:close(Socket),
330
%% Send a raw request. Return the response code and the full response.
331
raw_resp(Request, Config) ->
332
{port, Port} = lists:keyfind(port, 1, Config),
333
Transport = case ?config(scheme, Config) of
337
{ok, Socket} = Transport:connect("localhost", Port,
338
[binary, {active, false}, {packet, raw}]),
339
ok = Transport:send(Socket, Request),
340
{StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of
341
{ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} ->
342
{list_to_integer(binary_to_list(Str)), Bin};
348
Transport:close(Socket),
349
{Response, StatusCode}.
351
recv_loop(Transport, Socket, Acc) ->
352
case Transport:recv(Socket, 0, 6000) of
354
recv_loop(Transport, Socket, <<Acc/binary, Data/binary>>);
356
ok = Transport:close(Socket),
365
Huge = [$0 || _N <- lists:seq(1, 5000)],
367
{"\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n", 200},
369
{"Garbage\r\n\r\n", 400},
370
{"\r\n\r\n\r\n\r\n\r\n\r\n", 400},
371
{"GET / HTTP/1.1\r\nHost: dev-extend.eu\r\n\r\n", 400},
374
{"\r\n\r\n", closed},
375
{"GET / HTTP/1.1", closed},
376
{"GET / HTTP/1.1\r\n", 408},
377
{"GET / HTTP/1.1\r\nHost: localhost", 408},
378
{"GET / HTTP/1.1\r\nHost: localhost\r\n", 408},
379
{"GET / HTTP/1.1\r\nHost: localhost\r\n\r", 408},
380
{"GET http://localhost/ HTTP/1.1\r\n\r\n", 501},
381
{"GET / HTTP/1.2\r\nHost: localhost\r\n\r\n", 505},
382
{"GET /init_shutdown HTTP/1.1\r\nHost: localhost\r\n\r\n", 666},
383
{"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n\r\n", 102},
385
{"GET / HTTP/1.1\r\n" ++ Huge, 413}
387
[{Packet, StatusCode} = raw_req(Packet, Config)
388
|| {Packet, StatusCode} <- Tests].
390
set_resp_header(Config) ->
391
{port, Port} = lists:keyfind(port, 1, Config),
392
{ok, Socket} = gen_tcp:connect("localhost", Port,
393
[binary, {active, false}, {packet, raw}]),
394
ok = gen_tcp:send(Socket, "GET /set_resp/header HTTP/1.1\r\n"
395
"Host: localhost\r\nConnection: close\r\n\r\n"),
396
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
397
{_, _} = binary:match(Data, <<"Vary: Accept">>),
398
{_, _} = binary:match(Data, <<"Set-Cookie: ">>).
400
set_resp_overwrite(Config) ->
401
{port, Port} = lists:keyfind(port, 1, Config),
402
{ok, Socket} = gen_tcp:connect("localhost", Port,
403
[binary, {active, false}, {packet, raw}]),
404
ok = gen_tcp:send(Socket, "GET /set_resp/overwrite HTTP/1.1\r\n"
405
"Host: localhost\r\nConnection: close\r\n\r\n"),
406
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
407
{_Start, _Length} = binary:match(Data, <<"Server: DesireDrive/1.0">>).
409
set_resp_body(Config) ->
410
{port, Port} = lists:keyfind(port, 1, Config),
411
{ok, Socket} = gen_tcp:connect("localhost", Port,
412
[binary, {active, false}, {packet, raw}]),
413
ok = gen_tcp:send(Socket, "GET /set_resp/body HTTP/1.1\r\n"
414
"Host: localhost\r\nConnection: close\r\n\r\n"),
415
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
416
{_Start, _Length} = binary:match(Data, <<"\r\n\r\n"
417
"A flameless dance does not equal a cycle">>).
419
response_as_req(Config) ->
422
Location: http://www.google.co.il/
423
Cache-Control: private
424
Content-Type: text/html; charset=UTF-8
425
Set-Cookie: PREF=ID=568f67013d4a7afa:FF=0:TM=1323014101:LM=1323014101:S=XqctDWC65MzKT0zC; expires=Tue, 03-Dec-2013 15:55:01 GMT; path=/; domain=.google.com
426
Date: Sun, 04 Dec 2011 15:55:01 GMT
429
X-XSS-Protection: 1; mode=block
430
X-Frame-Options: SAMEORIGIN
432
<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">
433
<TITLE>302 Moved</TITLE></HEAD><BODY>
435
The document has moved
436
<A HREF=\"http://www.google.co.il/\">here</A>.
438
{Packet, 400} = raw_req(Packet, Config).
440
stream_body_set_resp(Config) ->
441
{Packet, 200} = raw_resp(
442
"GET /stream_body/set_resp HTTP/1.1\r\n"
443
"Host: localhost\r\nConnection: close\r\n\r\n", Config),
444
{_Start, _Length} = binary:match(Packet, <<"stream_body_set_resp">>).
446
static_mimetypes_function(Config) ->
447
TestURL = build_url("/static_mimetypes_function/test.html", Config),
448
{ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
449
httpc:request(TestURL),
450
"text/html" = ?config("content-type", Headers1).
452
handler_errors(Config) ->
453
Request = fun(Case) ->
454
raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n",
455
"Host: localhost\r\n\r\n"], Config) end,
457
{_Packet1, 500} = Request("init_before_reply"),
459
{Packet2, 200} = Request("init_after_reply"),
460
nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>),
462
{Packet3, 200} = Request("init_reply_handle_error"),
463
nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>),
465
{_Packet4, 500} = Request("handle_before_reply"),
467
{Packet5, 200} = Request("handle_after_reply"),
468
nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>),
470
{Packet6, 200} = raw_resp([
471
"GET / HTTP/1.1\r\n",
472
"Host: localhost\r\n",
473
"Connection: keep-alive\r\n\r\n",
474
"GET /handler_errors?case=handle_after_reply\r\n",
475
"Host: localhost\r\n\r\n"], Config),
476
nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>),
478
{Packet7, 200} = raw_resp([
479
"GET / HTTP/1.1\r\n",
480
"Host: localhost\r\n",
481
"Connection: keep-alive\r\n\r\n",
482
"GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n",
483
"Host: localhost\r\n\r\n"], Config),
484
{{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7},
488
static_attribute_etag(Config) ->
489
TestURL = build_url("/static_attribute_etag/test.html", Config),
490
{ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
491
httpc:request(TestURL),
492
false = ?config("etag", Headers1) =:= undefined,
493
{ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
494
httpc:request(TestURL),
495
true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
497
static_function_etag(Config) ->
498
TestURL = build_url("/static_function_etag/test.html", Config),
499
{ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test.html\n"}} =
500
httpc:request(TestURL),
501
false = ?config("etag", Headers1) =:= undefined,
502
{ok, {{"HTTP/1.1", 200, "OK"}, Headers2, "test.html\n"}} =
503
httpc:request(TestURL),
504
true = ?config("etag", Headers1) =:= ?config("etag", Headers2).
506
static_function_etag(Arguments, etag_data) ->
507
{_, Filepath} = lists:keyfind(filepath, 1, Arguments),
508
{_, _Filesize} = lists:keyfind(filesize, 1, Arguments),
509
{_, _INode} = lists:keyfind(inode, 1, Arguments),
510
{_, _Modified} = lists:keyfind(mtime, 1, Arguments),
511
ChecksumCommand = lists:flatten(io_lib:format("sha1sum ~s", [Filepath])),
512
[Checksum|_] = string:tokens(os:cmd(ChecksumCommand), " "),
513
iolist_to_binary(Checksum).
517
build_url(Path, Config) ->
518
{scheme, Scheme} = lists:keyfind(scheme, 1, Config),
519
{port, Port} = lists:keyfind(port, 1, Config),
520
Scheme ++ "://localhost:" ++ integer_to_list(Port) ++ Path.
523
{ok, {{"HTTP/1.1", 200, "OK"}, _Headers, "http_handler"}} =
524
httpc:request(build_url("/", Config)).
527
{ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
528
httpc:request(build_url("/not/found", Config)).
531
{ok, {{"HTTP/1.1", 200, "OK"}, Headers, "test_file\n"}} =
532
httpc:request(build_url("/static/test_file", Config)),
533
"application/octet-stream" = ?config("content-type", Headers),
535
{ok, {{"HTTP/1.1", 200, "OK"}, Headers1, "test_file.css\n"}} =
536
httpc:request(build_url("/static/test_file.css", Config)),
537
"text/css" = ?config("content-type", Headers1).
540
{ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
541
httpc:request(build_url("/static/test_noread", Config)).
544
{ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
545
httpc:request(build_url("/static/test_dir", Config)),
546
{ok, {{"HTTP/1.1", 403, "Forbidden"}, _Headers, _Body}} =
547
httpc:request(build_url("/static/test_dir/", Config)).
550
{ok, {{"HTTP/1.1", 404, "Not Found"}, _Headers, _Body}} =
551
httpc:request(build_url("/static/not_found", Config)).
554
{ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers, _Body}} =
555
httpc:request(build_url("/static/%2f", Config)),
556
{ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers1, _Body1}} =
557
httpc:request(build_url("/static/%2e", Config)),
558
{ok, {{"HTTP/1.1", 400, "Bad Request"}, _Headers2, _Body2}} =
559
httpc:request(build_url("/static/%2e%2e", Config)).
562
http_10_hostless(Config) ->
563
Packet = "GET / HTTP/1.0\r\n\r\n",
564
{Packet, 200} = raw_req(Packet, Config).
568
rest_simple(Config) ->
569
Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
570
{Packet, 200} = raw_req(Packet, Config).
572
rest_keepalive(Config) ->
573
{port, Port} = lists:keyfind(port, 1, Config),
574
{ok, Socket} = gen_tcp:connect("localhost", Port,
575
[binary, {active, false}, {packet, raw}]),
576
ok = rest_keepalive_loop(Socket, 100),
577
ok = gen_tcp:close(Socket).
579
rest_keepalive_loop(_Socket, 0) ->
581
rest_keepalive_loop(Socket, N) ->
582
ok = gen_tcp:send(Socket, "GET /simple HTTP/1.1\r\n"
583
"Host: localhost\r\nConnection: keep-alive\r\n\r\n"),
584
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
585
{0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
586
nomatch = binary:match(Data, <<"Connection: close">>),
587
rest_keepalive_loop(Socket, N - 1).
589
rest_keepalive_post(Config) ->
590
{port, Port} = lists:keyfind(port, 1, Config),
591
{ok, Socket} = gen_tcp:connect("localhost", Port,
592
[binary, {active, false}, {packet, raw}]),
593
ok = rest_keepalive_post_loop(Socket, 10, forbidden_post),
594
ok = gen_tcp:close(Socket).
596
rest_keepalive_post_loop(_Socket, 0, _) ->
598
rest_keepalive_post_loop(Socket, N, simple_post) ->
599
ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n"
600
"Host: localhost\r\nConnection: keep-alive\r\n"
601
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
602
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
603
{0, 12} = binary:match(Data, <<"HTTP/1.1 303">>),
604
nomatch = binary:match(Data, <<"Connection: close">>),
605
rest_keepalive_post_loop(Socket, N - 1, forbidden_post);
606
rest_keepalive_post_loop(Socket, N, forbidden_post) ->
607
ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n"
608
"Host: localhost\r\nConnection: keep-alive\r\n"
609
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
610
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
611
{0, 12} = binary:match(Data, <<"HTTP/1.1 403">>),
612
nomatch = binary:match(Data, <<"Connection: close">>),
613
rest_keepalive_post_loop(Socket, N - 1, simple_post).