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:
parent
2376983295
commit
9f5a1803da
3 changed files with 44 additions and 14 deletions
|
@ -22,8 +22,6 @@ The peer can also be obtained using pattern matching:
|
||||||
#{peer := {IP, Port}} = Req.
|
#{peer := {IP, Port}} = Req.
|
||||||
----
|
----
|
||||||
|
|
||||||
// @todo So we need tests for accessing the Req directly.
|
|
||||||
|
|
||||||
== Arguments
|
== Arguments
|
||||||
|
|
||||||
Req::
|
Req::
|
||||||
|
|
|
@ -55,8 +55,11 @@ echo(<<"match">>, Req, Opts) ->
|
||||||
end,
|
end,
|
||||||
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
|
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts};
|
||||||
echo(What, Req, Opts) ->
|
echo(What, Req, Opts) ->
|
||||||
F = binary_to_atom(What, latin1),
|
Key = binary_to_atom(What, latin1),
|
||||||
Value = cowboy_req:F(Req),
|
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}.
|
{ok, cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), Opts}.
|
||||||
|
|
||||||
echo_arg(Arg0, Req, Opts) ->
|
echo_arg(Arg0, Req, Opts) ->
|
||||||
|
|
|
@ -56,6 +56,7 @@ init_dispatch(Config) ->
|
||||||
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
|
{"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}},
|
||||||
{"/full/:key", echo_h, []},
|
{"/full/:key", echo_h, []},
|
||||||
{"/no/:key", echo_h, []},
|
{"/no/:key", echo_h, []},
|
||||||
|
{"/direct/:key/[...]", echo_h, []},
|
||||||
{"/:key/[...]", echo_h, []}
|
{"/:key/[...]", echo_h, []}
|
||||||
]}]).
|
]}]).
|
||||||
|
|
||||||
|
@ -128,14 +129,19 @@ header(Config) ->
|
||||||
|
|
||||||
headers(Config) ->
|
headers(Config) ->
|
||||||
doc("Request headers."),
|
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.
|
%% We always send accept-encoding with this test suite's requests.
|
||||||
<<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
|
<<"#{<<\"accept-encoding\">> => <<\"gzip\">>,<<\"header\">> => <<\"value\">>", _/bits>>
|
||||||
= do_get_body("/headers", [{<<"header">>, "value"}], Config),
|
= do_get_body(Path, [{<<"header">>, "value"}], Config),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
host(Config) ->
|
host(Config) ->
|
||||||
doc("Request URI host."),
|
doc("Request URI host."),
|
||||||
<<"localhost">> = do_get_body("/host", Config),
|
<<"localhost">> = do_get_body("/host", Config),
|
||||||
|
<<"localhost">> = do_get_body("/direct/host", Config),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
host_info(Config) ->
|
host_info(Config) ->
|
||||||
|
@ -168,6 +174,10 @@ match_qs(Config) ->
|
||||||
|
|
||||||
method(Config) ->
|
method(Config) ->
|
||||||
doc("Request method."),
|
doc("Request method."),
|
||||||
|
do_method("/method", Config),
|
||||||
|
do_method("/direct/method", Config).
|
||||||
|
|
||||||
|
do_method(Path, Config) ->
|
||||||
<<"GET">> = do_body("GET", "/method", Config),
|
<<"GET">> = do_body("GET", "/method", Config),
|
||||||
<<>> = do_body("HEAD", "/method", Config),
|
<<>> = do_body("HEAD", "/method", Config),
|
||||||
<<"OPTIONS">> = do_body("OPTIONS", "/method", Config),
|
<<"OPTIONS">> = do_body("OPTIONS", "/method", Config),
|
||||||
|
@ -212,10 +222,15 @@ parse_qs(Config) ->
|
||||||
|
|
||||||
path(Config) ->
|
path(Config) ->
|
||||||
doc("Request URI path."),
|
doc("Request URI path."),
|
||||||
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource", Config),
|
do_path("/path", Config),
|
||||||
<<"/path/to/the/resource">> = do_get_body("/path/to/the/resource?query", Config),
|
do_path("/direct/path", 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(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.
|
ok.
|
||||||
|
|
||||||
path_info(Config) ->
|
path_info(Config) ->
|
||||||
|
@ -232,25 +247,35 @@ path_info(Config) ->
|
||||||
peer(Config) ->
|
peer(Config) ->
|
||||||
doc("Request peer."),
|
doc("Request peer."),
|
||||||
<<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/peer", Config),
|
||||||
|
<<"{{127,0,0,1},", _/bits >> = do_get_body("/direct/peer", Config),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
port(Config) ->
|
port(Config) ->
|
||||||
doc("Request URI port."),
|
doc("Request URI port."),
|
||||||
Port = integer_to_binary(config(port, Config)),
|
Port = integer_to_binary(config(port, Config)),
|
||||||
Port = do_get_body("/port", Config),
|
Port = do_get_body("/port", Config),
|
||||||
|
Port = do_get_body("/direct/port", Config),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
qs(Config) ->
|
qs(Config) ->
|
||||||
doc("Request URI query string."),
|
doc("Request URI query string."),
|
||||||
<<>> = do_get_body("/qs", Config),
|
do_qs("/qs", Config),
|
||||||
<<"abc">> = do_get_body("/qs?abc", Config),
|
do_qs("/direct/qs", Config).
|
||||||
<<"a=b&c=d+e">> = do_get_body("/qs?a=b&c=d+e", 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.
|
ok.
|
||||||
|
|
||||||
scheme(Config) ->
|
scheme(Config) ->
|
||||||
doc("Request URI scheme."),
|
doc("Request URI scheme."),
|
||||||
|
do_scheme("/scheme", Config),
|
||||||
|
do_scheme("/direct/scheme", Config).
|
||||||
|
|
||||||
|
do_scheme(Path, Config) ->
|
||||||
Transport = config(type, Config),
|
Transport = config(type, Config),
|
||||||
case do_get_body("/scheme", Config) of
|
case do_get_body(Path, Config) of
|
||||||
<<"http">> when Transport =:= tcp -> ok;
|
<<"http">> when Transport =:= tcp -> ok;
|
||||||
<<"https">> when Transport =:= ssl -> ok
|
<<"https">> when Transport =:= ssl -> ok
|
||||||
end.
|
end.
|
||||||
|
@ -286,8 +311,12 @@ uri(Config) ->
|
||||||
|
|
||||||
version(Config) ->
|
version(Config) ->
|
||||||
doc("Request HTTP version."),
|
doc("Request HTTP version."),
|
||||||
|
do_version("/version", Config),
|
||||||
|
do_version("/direct/version", Config).
|
||||||
|
|
||||||
|
do_version(Path, Config) ->
|
||||||
Protocol = config(protocol, 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/1.1">> when Protocol =:= http -> ok;
|
||||||
<<"HTTP/2">> when Protocol =:= http2 -> ok
|
<<"HTTP/2">> when Protocol =:= http2 -> ok
|
||||||
end.
|
end.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue