From 70e43ec11213e2e8f43f0ec4e407ea3dc7120fe1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= <essen@ninenines.eu>
Date: Sun, 29 Mar 2020 15:44:47 +0200
Subject: Add more router tests

---
 src/cowboy_router.erl | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/cowboy_router.erl b/src/cowboy_router.erl
index 0eb4bdd..0b7fe41 100644
--- a/src/cowboy_router.erl
+++ b/src/cowboy_router.erl
@@ -372,6 +372,8 @@ list_match([E|Tail], [E|TailMatch], Binds) ->
 %% unless V was already defined and E isn't identical to the previous value.
 list_match([E|Tail], [V|TailMatch], Binds) when is_atom(V) ->
 	case Binds of
+		%% @todo This isn't right, the constraint must be applied FIRST
+		%% otherwise we can't check for example ints in both host/path.
 		#{V := E} ->
 			list_match(Tail, TailMatch, Binds);
 		#{V := _} ->
@@ -514,7 +516,9 @@ match_test_() ->
 		{<<"erlang.fr">>, '_',
 			{ok, match_erlang_ext, [], #{ext => <<"fr">>}}},
 		{<<"any">>, <<"/users/444/friends">>,
-			{ok, match_users_friends, [], #{id => <<"444">>}}}
+			{ok, match_users_friends, [], #{id => <<"444">>}}},
+		{<<"any">>, <<"/users//friends">>,
+			{ok, match_users_friends, [], #{id => <<>>}}}
 	],
 	[{lists:flatten(io_lib:format("~p, ~p", [H, P])), fun() ->
 		{ok, Handler, Opts, Binds, undefined, undefined}
@@ -549,14 +553,20 @@ match_info_test_() ->
 	end} || {H, P, R} <- Tests].
 
 match_constraints_test() ->
-	Dispatch = [{'_', [],
+	Dispatch0 = [{'_', [],
 		[{[<<"path">>, value], [{value, int}], match, []}]}],
-	{ok, _, [], #{value := 123}, _, _} = match(Dispatch,
+	{ok, _, [], #{value := 123}, _, _} = match(Dispatch0,
 		<<"ninenines.eu">>, <<"/path/123">>),
-	{ok, _, [], #{value := 123}, _, _} = match(Dispatch,
+	{ok, _, [], #{value := 123}, _, _} = match(Dispatch0,
 		<<"ninenines.eu">>, <<"/path/123/">>),
-	{error, notfound, path} = match(Dispatch,
+	{error, notfound, path} = match(Dispatch0,
 		<<"ninenines.eu">>, <<"/path/NaN/">>),
+	Dispatch1 = [{'_', [],
+		[{[<<"path">>, value, <<"more">>], [{value, nonempty}], match, []}]}],
+	{ok, _, [], #{value := <<"something">>}, _, _} = match(Dispatch1,
+		<<"ninenines.eu">>, <<"/path/something/more">>),
+	{error, notfound, path} = match(Dispatch1,
+		<<"ninenines.eu">>, <<"/path//more">>),
 	Dispatch2 = [{'_', [], [{[<<"path">>, username],
 		[{username, fun(_, Value) ->
 			case cowboy_bstr:to_lower(Value) of
-- 
cgit v1.2.3