aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2016-06-21 19:04:52 +0200
committerLoïc Hoguin <[email protected]>2016-06-21 19:04:52 +0200
commit19468d0503f80a4a2ef4d40abe1b91bdb3516988 (patch)
tree4db24f9ac7768a647c3d42fe83eaa9083be3b3bb /test/handlers
parent3a7643782edd56dad7cf641d150bcef35399f33f (diff)
downloadcowboy-19468d0503f80a4a2ef4d40abe1b91bdb3516988.tar.gz
cowboy-19468d0503f80a4a2ef4d40abe1b91bdb3516988.tar.bz2
cowboy-19468d0503f80a4a2ef4d40abe1b91bdb3516988.zip
Add cowboy_req:uri/1,2
Along with more cowboy_req tests. This commit also removes cowboy_req:url/1 and cowboy_req:host_url/1 in favor of the much more powerful new set of functions.
Diffstat (limited to 'test/handlers')
-rw-r--r--test/handlers/echo_h.erl47
1 files changed, 40 insertions, 7 deletions
diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl
index cdb9be4..802d537 100644
--- a/test/handlers/echo_h.erl
+++ b/test/handlers/echo_h.erl
@@ -5,15 +5,48 @@
-export([init/2]).
init(Req, Opts) ->
- echo(cowboy_req:binding(key, Req), Req, Opts).
+ case cowboy_req:binding(arg, Req) of
+ undefined ->
+ echo(cowboy_req:binding(key, Req), Req, Opts);
+ Arg ->
+ echo_arg(Arg, Req, Opts)
+ end.
+echo(<<"body">>, Req0, Opts) ->
+ {ok, Body, Req} = cowboy_req:read_body(Req0),
+ cowboy_req:reply(200, #{}, Body, Req),
+ {ok, Req, Opts};
+echo(<<"uri">>, Req, Opts) ->
+ Value = case cowboy_req:path_info(Req) of
+ [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined});
+ [<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined});
+ [<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined});
+ [<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined});
+ [<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123});
+ [] -> cowboy_req:uri(Req)
+ end,
+ cowboy_req:reply(200, #{}, Value, Req),
+ {ok, Req, Opts};
echo(What, Req, Opts) ->
F = binary_to_atom(What, latin1),
- Value = case cowboy_req:F(Req) of
- V when is_integer(V) -> integer_to_binary(V);
- V when is_atom(V) -> atom_to_binary(V, latin1);
- V when is_list(V); is_tuple(V) -> io_lib:format("~p", [V]);
- V -> V
+ Value = cowboy_req:F(Req),
+ cowboy_req:reply(200, #{}, value_to_iodata(Value), Req),
+ {ok, Req, Opts}.
+
+echo_arg(Arg0, Req, Opts) ->
+ F = binary_to_atom(cowboy_req:binding(key, Req), latin1),
+ Arg = case F of
+ binding -> binary_to_atom(Arg0, latin1);
+ _ -> Arg0
end,
- cowboy_req:reply(200, #{}, Value, Req),
+ Value = case cowboy_req:binding(default, Req) of
+ undefined -> cowboy_req:F(Arg, Req);
+ Default -> cowboy_req:F(Arg, Req, Default)
+ end,
+ cowboy_req:reply(200, #{}, value_to_iodata(Value), Req),
{ok, Req, Opts}.
+
+value_to_iodata(V) when is_integer(V) -> integer_to_binary(V);
+value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1);
+value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~p", [V]);
+value_to_iodata(V) -> V.