diff options
author | Loïc Hoguin <[email protected]> | 2017-10-22 14:54:13 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2017-10-22 14:54:13 +0100 |
commit | 133d359674414587d34c5f58361513eb13bb55aa (patch) | |
tree | 68e71daa2b483946151c2979024d5e57236d7492 /test/metrics_SUITE.erl | |
parent | 4bebe39975aab28962ac3850aa25a7d768c349bb (diff) | |
download | cowboy-133d359674414587d34c5f58361513eb13bb55aa.tar.gz cowboy-133d359674414587d34c5f58361513eb13bb55aa.tar.bz2 cowboy-133d359674414587d34c5f58361513eb13bb55aa.zip |
Add a metrics test for switching to Websocket
Diffstat (limited to 'test/metrics_SUITE.erl')
-rw-r--r-- | test/metrics_SUITE.erl | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/test/metrics_SUITE.erl b/test/metrics_SUITE.erl index 9ccb1d1..d41ae1d 100644 --- a/test/metrics_SUITE.erl +++ b/test/metrics_SUITE.erl @@ -69,7 +69,8 @@ init_routes(_) -> [ {"/", hello_h, []}, {"/default", default_h, []}, {"/full/:key", echo_h, []}, - {"/resp/:key[/:arg]", resp_h, []} + {"/resp/:key[/:arg]", resp_h, []}, + {"/ws_echo", ws_echo, []} ]} ]. @@ -311,3 +312,67 @@ do_early_error(Config) -> stream_reply(Config) -> doc("Confirm metrics are correct for long polling."), do_get("/resp/stream_reply2/200", Config). + +ws(Config) -> + case config(protocol, Config) of + http -> do_ws(Config); + http2 -> doc("It is not currently possible to switch to Websocket over HTTP/2.") + end. + +do_ws(Config) -> + doc("Confirm metrics are correct when switching to Websocket."), + ConnPid = gun_open(Config), + {ok, http} = gun:await_up(ConnPid), + gun:ws_upgrade(ConnPid, "/ws_echo", [ + {<<"accept-encoding">>, <<"gzip">>}, + {<<"x-test-pid">>, pid_to_list(self())} + ]), + receive + {metrics, From, Metrics} -> + %% Ensure the timestamps are in the expected order. + #{ + req_start := ReqStart, + req_end := ReqEnd + } = Metrics, + true = ReqStart =< ReqEnd, + %% We didn't send a body. + #{ + req_body_start := undefined, + req_body_end := undefined, + req_body_length := 0 + } = Metrics, + %% We didn't send a response. + #{ + resp_start := undefined, + resp_end := undefined, + resp_status := undefined, + resp_headers := undefined, + resp_body_length := 0 + } = Metrics, + %% The request process may not have terminated before terminate + %% is called. We therefore only check when it spawned. + #{procs := Procs} = Metrics, + [{_, #{ + spawn := ProcSpawn + }}] = maps:to_list(Procs), + %% Confirm other metadata are as expected. + #{ + ref := _, + pid := From, + streamid := 1, + reason := switch_protocol, + req := #{} + } = Metrics, + %% All good! + ok + after 1000 -> + error(timeout) + end, + %% And of course the upgrade completed successfully after that. + receive + {gun_ws_upgrade, ConnPid, ok, _} -> + ok + after 1000 -> + error(timeout) + end, + gun:close(ConnPid). |