0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Add tests for direct Req access

This commit is contained in:
Loïc Hoguin 2017-09-05 15:28:11 +02:00
parent 2376983295
commit 9f5a1803da
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
3 changed files with 44 additions and 14 deletions

View file

@ -22,8 +22,6 @@ The peer can also be obtained using pattern matching:
#{peer := {IP, Port}} = Req.
----
// @todo So we need tests for accessing the Req directly.
== Arguments
Req::

View file

@ -55,8 +55,11 @@ echo(<<"match">>, Req, Opts) ->
end,
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
echo(What, Req, Opts) ->
F = binary_to_atom(What, latin1),
Value = cowboy_req:F(Req),
Key = binary_to_atom(What, latin1),
Value = case cowboy_req:path(Req) of
<<"/direct/",_/bits>> -> maps:get(Key, Req);
_ -> cowboy_req:Key(Req)
end,
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
echo_arg(Arg0, Req, Opts) ->

View file

@ -56,6 +56,7 @@ init_dispatch(Config) ->
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
{"/full/:key", echo_h, []},
{"/no/:key", echo_h, []},
{"/direct/:key/[...]", echo_h, []},
{"/:key/[...]", echo_h, []}
]}]).
@ -128,14 +129,19 @@ header(Config) ->
headers(Config) ->
doc("Request headers."),
do_headers("/headers", Config),
do_headers("/direct/headers", Config).
do_headers(Path, Config) ->
%% We always send accept-encoding with this test suite's requests.
<<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
= do_get_body("/headers", [{<<"header">>, "value"}], Config),
= do_get_body(Path, [{<<"header">>, "value"}], Config),
ok.
host(Config) ->
doc("Request URI host."),
<<"localhost">> = do_get_body("/host", Config),
<<"localhost">> = do_get_body("/direct/host", Config),
ok.
host_info(Config) ->
@ -168,6 +174,10 @@ match_qs(Config) ->
method(Config) ->
doc("Request method."),
do_method("/method", Config),
do_method("/direct/method", Config).
do_method(Path, Config) ->
<<"GET">> = do_body("GET", "/method", Config),
<<>> = do_body("HEAD", "/method", Config),
<<"OPTIONS">> = do_body("OPTIONS", "/method", Config),
@ -212,10 +222,15 @@ parse_qs(Config) ->
path(Config) ->
doc("Request URI path."),
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource", Config),
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource?query", Config),
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource?query#fragment", Config),
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource#fragment", Config),
do_path("/path", Config),
do_path("/direct/path", Config).
do_path(Path0, Config) ->
Path = list_to_binary(Path0 ++ "/to/the/resource"),
Path = do_get_body(Path, Config),
Path = do_get_body([Path, "?query"], Config),
Path = do_get_body([Path, "?query#fragment"], Config),
Path = do_get_body([Path, "#fragment"], Config),
ok.
path_info(Config) ->
@ -232,25 +247,35 @@ path_info(Config) ->
peer(Config) ->
doc("Request peer."),
<<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
ok.
port(Config) ->
doc("Request URI port."),
Port = integer_to_binary(config(port, Config)),
Port = do_get_body("/port", Config),
Port = do_get_body("/direct/port", Config),
ok.
qs(Config) ->
doc("Request URI query string."),
<<>> = do_get_body("/qs", Config),
<<"abc">> = do_get_body("/qs?abc", Config),
<<"a=b&c=d+e">> = do_get_body("/qs?a=b&c=d+e", Config),
do_qs("/qs", Config),
do_qs("/direct/qs", Config).
do_qs(Path, Config) ->
<<>> = do_get_body(Path, Config),
<<"abc">> = do_get_body(Path ++ "?abc", Config),
<<"a=b&c=d+e">> = do_get_body(Path ++ "?a=b&c=d+e", Config),
ok.
scheme(Config) ->
doc("Request URI scheme."),
do_scheme("/scheme", Config),
do_scheme("/direct/scheme", Config).
do_scheme(Path, Config) ->
Transport = config(type, Config),
case do_get_body("/scheme", Config) of
case do_get_body(Path, Config) of
<<"http">> when Transport =:= tcp -> ok;
<<"https">> when Transport =:= ssl -> ok
end.
@ -286,8 +311,12 @@ uri(Config) ->
version(Config) ->
doc("Request HTTP version."),
do_version("/version", Config),
do_version("/direct/version", Config).
do_version(Path, Config) ->
Protocol = config(protocol, Config),
case do_get_body("/version", Config) of
case do_get_body(Path, Config) of
<<"HTTP/1.1">> when Protocol =:= http -> ok;
<<"HTTP/2">> when Protocol =:= http2 -> ok
end.