aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMagnus Klaar <[email protected]>2011-12-29 00:06:22 +0100
committerMagnus Klaar <[email protected]>2011-12-29 00:06:22 +0100
commit50578254d5d553a7ccbb82ea1c139bb8c3d37197 (patch)
tree29f58bbc9f8ae3e721e5d1be82f42c23916086f0
parent156c84ff29a13f3d21c1b7ff271bda50797ca0ef (diff)
downloadcowboy-50578254d5d553a7ccbb82ea1c139bb8c3d37197.tar.gz
cowboy-50578254d5d553a7ccbb82ea1c139bb8c3d37197.tar.bz2
cowboy-50578254d5d553a7ccbb82ea1c139bb8c3d37197.zip
Add tests for #114
-rw-r--r--test/http_SUITE.erl76
-rw-r--r--test/http_handler_errors.erl40
2 files changed, 114 insertions, 2 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 3c4af28..7388618 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -22,7 +22,7 @@
keepalive_nl/1, max_keepalive/1, nc_rand/1, nc_zero/1,
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
set_resp_body/1, response_as_req/1]). %% http.
--export([http_200/1, http_404/1]). %% http and https.
+-export([http_200/1, http_404/1, handler_errors/1]). %% http and https.
-export([http_10_hostless/1]). %% misc.
-export([rest_simple/1, rest_keepalive/1]). %% rest.
@@ -32,7 +32,7 @@ all() ->
[{group, http}, {group, https}, {group, misc}, {group, rest}].
groups() ->
- BaseTests = [http_200, http_404],
+ BaseTests = [http_200, http_404, handler_errors],
[{http, [], [chunked_response, headers_dupe, headers_huge,
keepalive_nl, max_keepalive, nc_rand, nc_zero, pipeline, raw,
set_resp_header, set_resp_overwrite,
@@ -115,6 +115,7 @@ init_http_dispatch() ->
[{headers, [{<<"Server">>, <<"DesireDrive/1.0">>}]}]},
{[<<"set_resp">>, <<"body">>], http_handler_set_resp,
[{body, <<"A flameless dance does not equal a cycle">>}]},
+ {[<<"handler_errors">>], http_handler_errors, []},
{[], http_handler, []}
]}
].
@@ -252,6 +253,40 @@ raw_req(Packet, Config) ->
gen_tcp:close(Socket),
{Packet, Res}.
+%% Send a raw request. Return the response code and the full response.
+raw_resp(Request, Config) ->
+ {port, Port} = lists:keyfind(port, 1, Config),
+ Transport = case ?config(scheme, Config) of
+ "http" -> gen_tcp;
+ "https" -> ssl
+ end,
+ {ok, Socket} = Transport:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = Transport:send(Socket, Request),
+ {StatusCode, Response} = case recv_loop(Transport, Socket, <<>>) of
+ {ok, << "HTTP/1.1 ", Str:24/bits, _Rest/bits >> = Bin} ->
+ {list_to_integer(binary_to_list(Str)), Bin};
+ {ok, Bin} ->
+ {badresp, Bin};
+ {error, Reason} ->
+ {Reason, <<>>}
+ end,
+ Transport:close(Socket),
+ {Response, StatusCode}.
+
+recv_loop(Transport, Socket, Acc) ->
+ case Transport:recv(Socket, 0, 6000) of
+ {ok, Data} ->
+ recv_loop(Transport, Socket, <<Acc/binary, Data/binary>>);
+ {error, closed} ->
+ ok = Transport:close(Socket),
+ {ok, Acc};
+ {error, Reason} ->
+ {error, Reason}
+ end.
+
+
+
raw(Config) ->
Huge = [$0 || _N <- lists:seq(1, 5000)],
Tests = [
@@ -328,6 +363,43 @@ The document has moved
</BODY></HTML>",
{Packet, 400} = raw_req(Packet, Config).
+handler_errors(Config) ->
+ Request = fun(Case) ->
+ raw_resp(["GET /handler_errors?case=", Case, " HTTP/1.1\r\n",
+ "Host: localhost\r\n\r\n"], Config) end,
+
+ {_Packet1, 500} = Request("init_before_reply"),
+
+ {Packet2, 200} = Request("init_after_reply"),
+ nomatch = binary:match(Packet2, <<"HTTP/1.1 500">>),
+
+ {Packet3, 200} = Request("init_reply_handle_error"),
+ nomatch = binary:match(Packet3, <<"HTTP/1.1 500">>),
+
+ {_Packet4, 500} = Request("handle_before_reply"),
+
+ {Packet5, 200} = Request("handle_after_reply"),
+ nomatch = binary:match(Packet5, <<"HTTP/1.1 500">>),
+
+ {Packet6, 200} = raw_resp([
+ "GET / HTTP/1.1\r\n",
+ "Host: localhost\r\n",
+ "Connection: keep-alive\r\n\r\n",
+ "GET /handler_errors?case=handle_after_reply\r\n",
+ "Host: localhost\r\n\r\n"], Config),
+ nomatch = binary:match(Packet6, <<"HTTP/1.1 500">>),
+
+ {Packet7, 200} = raw_resp([
+ "GET / HTTP/1.1\r\n",
+ "Host: localhost\r\n",
+ "Connection: keep-alive\r\n\r\n",
+ "GET /handler_errors?case=handle_before_reply HTTP/1.1\r\n",
+ "Host: localhost\r\n\r\n"], Config),
+ {{_, _}, _} = {binary:match(Packet7, <<"HTTP/1.1 500">>), Packet7},
+
+ done.
+
+
%% http and https.
build_url(Path, Config) ->
diff --git a/test/http_handler_errors.erl b/test/http_handler_errors.erl
new file mode 100644
index 0000000..1c23207
--- /dev/null
+++ b/test/http_handler_errors.erl
@@ -0,0 +1,40 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(http_handler_errors).
+-behaviour(cowboy_http_handler).
+-export([init/3, handle/2, terminate/2]).
+
+init({_Transport, http}, Req, _Opts) ->
+ {Case, Req1} = cowboy_http_req:qs_val(<<"case">>, Req),
+ case_init(Case, Req1).
+
+case_init(<<"init_before_reply">> = Case, _Req) ->
+ erlang:error(Case);
+
+case_init(<<"init_after_reply">> = Case, Req) ->
+ {ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
+ erlang:error(Case);
+
+case_init(<<"init_reply_handle_error">> = Case, Req) ->
+ {ok, Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
+ {ok, Req1, Case};
+
+case_init(<<"handle_before_reply">> = Case, Req) ->
+ {ok, Req, Case};
+
+case_init(<<"handle_after_reply">> = Case, Req) ->
+ {ok, Req, Case}.
+
+
+handle(_Req, <<"init_reply_handle_error">> = Case) ->
+ erlang:error(Case);
+
+handle(_Req, <<"handle_before_reply">> = Case) ->
+ erlang:error(Case);
+
+handle(Req, <<"handle_after_reply">> = Case) ->
+ {ok, _Req1} = cowboy_http_req:reply(200, [], "http_handler_crashes", Req),
+ erlang:error(Case).
+
+terminate(_Req, _State) ->
+ ok.