From b403a4c083ebe3423de6353f46824f224449299d Mon Sep 17 00:00:00 2001 From: Vladimir Kostyukov Date: Thu, 26 Feb 2015 11:32:48 -0800 Subject: [PATCH] Clear priority of routers --- core/src/main/scala/io/finch/route/Router.scala | 4 ++-- core/src/test/scala/io/finch/route/RouterSpec.scala | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/io/finch/route/Router.scala b/core/src/main/scala/io/finch/route/Router.scala index 9624c6885..ddc32532e 100644 --- a/core/src/main/scala/io/finch/route/Router.scala +++ b/core/src/main/scala/io/finch/route/Router.scala @@ -98,7 +98,7 @@ trait RouterN[+A] { self => def orElse[B >: A](that: RouterN[B]): RouterN[B] = new RouterN[B] { def apply(route: Route): Option[(Route, B)] = (self(route), that(route)) match { case (aa @ Some((a, _)), bb @ Some((b, _))) => - if (a.length < b.length) aa else bb + if (a.length <= b.length) aa else bb case (a, b) => a orElse b } @@ -185,7 +185,7 @@ trait Router0 { self => def orElse(that: Router0): Router0 = new Router0 { def apply(route: Route): Option[Route] = (self(route), that(route)) match { case (aa @ Some(a), bb @ Some(b)) => - if (a.length < b.length) aa else bb + if (a.length <= b.length) aa else bb case (a, b) => a orElse b } diff --git a/core/src/test/scala/io/finch/route/RouterSpec.scala b/core/src/test/scala/io/finch/route/RouterSpec.scala index 015120ce3..e6ae1b6db 100644 --- a/core/src/test/scala/io/finch/route/RouterSpec.scala +++ b/core/src/test/scala/io/finch/route/RouterSpec.scala @@ -240,4 +240,16 @@ class RouterSpec extends FlatSpec with Matchers { r3(emptyRoute) shouldBe None r4(emptyRoute) shouldBe None } + + it should "use the first router if both eats the same number of tokens" in { + val r = + Get /> "root" | + Get / "foo" /> "foo" + + val route1 = List(MethodToken(Method.Get)) + val route2 = List(MethodToken(Method.Get), PathToken("foo")) + + r(route1) shouldBe Some((Nil, "root")) + r(route2) shouldBe Some((Nil, "foo")) + } }