aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-10-20 18:00:40 +0100
committerLoïc Hoguin <[email protected]>2017-10-20 18:00:40 +0100
commit1ef5a1c45bd56fb9080ff0195c5ea74f88bcd71a (patch)
treefafe3bfa277c2ae5735aeea064b47a3634a5762e
parent4211ea41bd49be3cf152acb62ac8330f7f6a927d (diff)
downloadcowboy-1ef5a1c45bd56fb9080ff0195c5ea74f88bcd71a.tar.gz
cowboy-1ef5a1c45bd56fb9080ff0195c5ea74f88bcd71a.tar.bz2
cowboy-1ef5a1c45bd56fb9080ff0195c5ea74f88bcd71a.zip
Add a test for metrics with a request body
-rw-r--r--src/cowboy_metrics_h.erl6
-rw-r--r--test/metrics_SUITE.erl76
2 files changed, 76 insertions, 6 deletions
diff --git a/src/cowboy_metrics_h.erl b/src/cowboy_metrics_h.erl
index 91709ad..1203518 100644
--- a/src/cowboy_metrics_h.erl
+++ b/src/cowboy_metrics_h.erl
@@ -152,8 +152,10 @@ data(StreamID, IsFin, Data, State=#state{req_body_start=undefined}) ->
req_body_start=ReqBodyStart,
req_body_length=byte_size(Data)
});
-data(StreamID, IsFin, Data, State) ->
- do_data(StreamID, IsFin, Data, State).
+data(StreamID, IsFin, Data, State=#state{req_body_length=ReqBodyLen}) ->
+ do_data(StreamID, IsFin, Data, State#state{
+ req_body_length=ReqBodyLen + byte_size(Data)
+ }).
do_data(StreamID, IsFin, Data, State0=#state{next=Next0}) ->
{Commands, Next} = cowboy_stream:data(StreamID, IsFin, Data, Next0),
diff --git a/test/metrics_SUITE.erl b/test/metrics_SUITE.erl
index 6b10fb3..90ffc9b 100644
--- a/test/metrics_SUITE.erl
+++ b/test/metrics_SUITE.erl
@@ -66,7 +66,8 @@ init_compress_opts(Config) ->
init_routes(_) -> [
{"localhost", [
- {"/", hello_h, []}
+ {"/", hello_h, []},
+ {"/full/:key", echo_h, []}
]}
].
@@ -80,13 +81,17 @@ do_metrics_callback() ->
%% Tests.
hello_world(Config) ->
- %% Perform a request.
+ doc("Confirm metrics are correct for a normal GET request."),
+ %% Perform a GET request.
ConnPid = gun_open(Config),
- Ref = gun:get(ConnPid, "/", [{<<"x-test-pid">>, pid_to_list(self())}]),
+ Ref = gun:get(ConnPid, "/", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"x-test-pid">>, pid_to_list(self())}
+ ]),
{response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
{ok, RespBody} = gun:await_body(ConnPid, Ref),
gun:close(ConnPid),
- %% Receive the metrics and print them.
+ %% Receive the metrics and validate them.
receive
{metrics, From, Metrics} ->
%% Ensure the timestamps are in the expected order.
@@ -110,6 +115,69 @@ hello_world(Config) ->
resp_body_length := RespBodyLen
} = Metrics,
ExpectedRespHeaders = maps:from_list(RespHeaders),
+ true = byte_size(RespBody) > 0,
+ true = RespBodyLen > 0,
+ %% The request process executed normally.
+ #{procs := Procs} = Metrics,
+ [{_, #{
+ spawn := ProcSpawn,
+ exit := ProcExit,
+ reason := normal
+ }}] = maps:to_list(Procs),
+ true = ProcSpawn =< ProcExit,
+ %% Confirm other metadata are as expected.
+ #{
+ ref := _,
+ pid := From,
+ streamid := 1,
+ reason := normal,
+ req := #{}
+ } = Metrics,
+ %% All good!
+ ok
+ after 1000 ->
+ error(timeout)
+ end.
+
+post_body(Config) ->
+ doc("Confirm metrics are correct for a normal POST request."),
+ %% Perform a POST request.
+ ConnPid = gun_open(Config),
+ Body = <<0:8000000>>,
+ Ref = gun:post(ConnPid, "/full/read_body", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"x-test-pid">>, pid_to_list(self())}
+ ], Body),
+ {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
+ {ok, RespBody} = gun:await_body(ConnPid, Ref),
+ gun:close(ConnPid),
+ %% Receive the metrics and validate them.
+ receive
+ {metrics, From, Metrics} ->
+ %% Ensure the timestamps are in the expected order.
+ #{
+ req_start := ReqStart, req_end := ReqEnd,
+ resp_start := RespStart, resp_end := RespEnd
+ } = Metrics,
+ true = (ReqStart =< RespStart)
+ and (RespStart =< RespEnd)
+ and (RespEnd =< ReqEnd),
+ %% We didn't send a body.
+ #{
+ req_body_start := ReqBodyStart,
+ req_body_end := ReqBodyEnd,
+ req_body_length := ReqBodyLen
+ } = Metrics,
+ true = ReqBodyStart =< ReqBodyEnd,
+ ReqBodyLen = byte_size(Body),
+ %% We got a 200 response with a body.
+ #{
+ resp_status := 200,
+ resp_headers := ExpectedRespHeaders,
+ resp_body_length := RespBodyLen
+ } = Metrics,
+ ExpectedRespHeaders = maps:from_list(RespHeaders),
+ true = byte_size(RespBody) > 0,
true = RespBodyLen > 0,
%% The request process executed normally.
#{procs := Procs} = Metrics,