aboutsummaryrefslogtreecommitdiffstats
path: root/test/http_SUITE.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-05-01 01:59:36 +0200
committerLoïc Hoguin <[email protected]>2012-05-04 06:24:10 +0200
commit57fda142175a7fd0340d9030e0477c16e13dc5f5 (patch)
treea83701cdedf0a21b814fd3e78538d2473926e446 /test/http_SUITE.erl
parent7ed93fcc8f3fe42cbe22e96e3352b444c2480f74 (diff)
downloadcowboy-57fda142175a7fd0340d9030e0477c16e13dc5f5.tar.gz
cowboy-57fda142175a7fd0340d9030e0477c16e13dc5f5.tar.bz2
cowboy-57fda142175a7fd0340d9030e0477c16e13dc5f5.zip
Add an 'onresponse' hook
This new protocol option is a fun. It expects 3 args: the Status code used in the reply (this is the cowboy_http:status() type, it can be an integer or a binary), the headers that will be sent in the reply, and the Req. It should only return a possibly modified Req. This can be used for many things like error logging or custom error pages. If a reply is sent inside the hook, then Cowboy will discard the reply initially sent. Extra caution must be used in the handlers making use of inline chunked replies as they will throw an error. This fun cannot be used as a filter, you can either observe the reply sent or discard it to send a different one instead. The hook will not be called for replies sent from inside the hook.
Diffstat (limited to 'test/http_SUITE.erl')
-rw-r--r--test/http_SUITE.erl45
1 files changed, 39 insertions, 6 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index a906037..a94a410 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -44,6 +44,7 @@
-export([nc_zero/1]).
-export([onrequest/1]).
-export([onrequest_reply/1]).
+-export([onresponse_reply/1]).
-export([pipeline/1]).
-export([rest_keepalive/1]).
-export([rest_keepalive_post/1]).
@@ -66,7 +67,7 @@
%% ct.
all() ->
- [{group, http}, {group, https}, {group, hooks}].
+ [{group, http}, {group, https}, {group, onrequest}, {group, onresponse}].
groups() ->
Tests = [
@@ -108,9 +109,12 @@ groups() ->
[
{http, [], Tests},
{https, [], Tests},
- {hooks, [], [
+ {onrequest, [], [
onrequest,
onrequest_reply
+ ]},
+ {onresponse, [], [
+ onresponse_reply
]}
].
@@ -160,10 +164,10 @@ init_per_group(https, Config) ->
{ok, Client} = cowboy_client:init(Opts),
[{scheme, <<"https">>}, {port, Port}, {opts, Opts},
{transport, Transport}, {client, Client}|Config1];
-init_per_group(hooks, Config) ->
+init_per_group(onrequest, Config) ->
Port = 33082,
Transport = cowboy_tcp_transport,
- {ok, _} = cowboy:start_listener(hooks, 100,
+ {ok, _} = cowboy:start_listener(onrequest, 100,
Transport, [{port, Port}],
cowboy_http_protocol, [
{dispatch, init_dispatch(Config)},
@@ -173,6 +177,20 @@ init_per_group(hooks, Config) ->
]),
{ok, Client} = cowboy_client:init([]),
[{scheme, <<"http">>}, {port, Port}, {opts, []},
+ {transport, Transport}, {client, Client}|Config];
+init_per_group(onresponse, Config) ->
+ Port = 33083,
+ Transport = cowboy_tcp_transport,
+ {ok, _} = cowboy:start_listener(onresponse, 100,
+ Transport, [{port, Port}],
+ cowboy_http_protocol, [
+ {dispatch, init_dispatch(Config)},
+ {max_keepalive, 50},
+ {onresponse, fun onresponse_hook/3},
+ {timeout, 500}
+ ]),
+ {ok, Client} = cowboy_client:init([]),
+ [{scheme, <<"http">>}, {port, Port}, {opts, []},
{transport, Transport}, {client, Client}|Config].
end_per_group(https, Config) ->
@@ -185,8 +203,8 @@ end_per_group(https, Config) ->
end_per_group(http, Config) ->
cowboy:stop_listener(http),
end_static_dir(Config);
-end_per_group(hooks, _) ->
- cowboy:stop_listener(hooks),
+end_per_group(Name, _) ->
+ cowboy:stop_listener(Name),
ok.
%% Dispatch configuration.
@@ -568,6 +586,21 @@ onrequest_hook(Req) ->
Req3
end.
+onresponse_reply(Config) ->
+ Client = ?config(client, Config),
+ {ok, Client2} = cowboy_client:request(<<"GET">>,
+ build_url("/", Config), Client),
+ {ok, 777, Headers, Client3} = cowboy_client:response(Client2),
+ {<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
+ %% Make sure we don't get the body initially sent.
+ {error, closed} = cowboy_client:response_body(Client3).
+
+%% Hook for the above onresponse tests.
+onresponse_hook(_, Headers, Req) ->
+ {ok, Req2} = cowboy_http_req:reply(
+ <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
+ Req2.
+
pipeline(Config) ->
Client = ?config(client, Config),
{ok, Client2} = cowboy_client:request(<<"GET">>,