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

Add more router tests

This commit is contained in:
Loïc Hoguin 2020-03-29 15:44:47 +02:00
parent 6ad842a742
commit 70e43ec112
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764

View file

@ -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