2016-10-10 95 views
0

这里有三条路线,我尝试匹配阿卡路由DSL不符合预期的路线

/games/2016/1 
/games/2016 
/games 

,我想这个路由结构可以这样做:

pathPrefix("games") { 
    logger.info("INSIDE GAMES PATH KEY ") 
    path(PathEnd) { 
    complete("no numbers") 
    } ~ path(IntNumber/IntNumber) { case (year: Int, week: Int) => 
    logger.info("Matched week/year") 
    //gets games for this year and week 
    complete("two numbers") 
    } ~ path(IntNumber) { year: Int => 
    logger.info("Matched year") 
    complete("one number") 
    } 
} 

该方法适用于前两个例子我有,但不是最后的工作。我在做什么错了,我怎么能解决这个问题,所以我的路由匹配/games

我试图切换到PathEndPathNeturalRemainingPath等无济于事。

编辑:我使用的测试案例:

it must "return all games for this week" in { 
    Get("/games") ~> 
     NflRoutes.gamesRoutes ~> check { 
     val games: Seq[NflGame] = responseAs[Seq[NflGame]] 
     games.size must be (14) 
    } 
    } 

与日志错误:

> test-only *NflRoutesTest* 
[info] Compiling 1 Scala source to /home/chris/dev/suredbits-api/target/scala-2.11/classes... 
[info] Compiling 1 Scala source to /home/chris/dev/suredbits-api/target/scala-2.11/test-classes... 
SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/home/chris/dev/suredbits-api/lib/nfldb-api-assembly-0.0.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: Found binding in [jar:file:/home/chris/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 
13:09:39.721 TKD [com-suredbits-api-routes-NflRoutesTest-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started 
13:09:39.907 TKD [suredbits-api-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started 
13:09:40.012 TKD [pool-9-thread-6-ScalaTest-running-NflRoutesTest] INFO c.suredbits.api.routes.NflRoutes$ - INSIDE GAMES PATH KEY 
[info] NflRoutesTest: 
[info] - must return all games for this week *** FAILED *** 
[info] Request was rejected (DynamicVariable.scala:58) 
[info] ScalaTest 
[info] Run completed in 934 milliseconds. 
[info] Total number of tests run: 1 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0 
[info] *** 1 TEST FAILED *** 
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0 
[error] Failed tests: 
[error]  com.suredbits.api.routes.NflRoutesTest 
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful 
[error] Total time: 4 s, completed Oct 10, 2016 1:09:40 PM 
> 

回答

2

pathEnd第一次尝试匹配。基于该documentation我加了一些基本的逻辑来代替你的???的:

val fooRoute = pathPrefix("games") { 
    pathEnd { complete ("empty path") } ~ 
    path(IntNumber/IntNumber) {case (a : Int, b : Int) => complete(s"a = $a b = $b")} ~ 
    path(IntNumber) { case a : Int => complete(s"a = $a")} 
} 

这条路线可以用下面的代码通过测试:

Get("/games") ~> fooRoute ~> check { 
    responseAs[String] equalsIgnoreCase "empty path" 
} 

Get("/games/2016") ~> fooRoute ~> check { 
    responseAs[String] equalsIgnoreCase "a = 2016" 
} 

Get("/games/2016/1") ~> fooRoute ~> check { 
    responseAs[String] equalsIgnoreCase "a = 2016 b = 1" 
} 
+0

这并没有:-( –

+0

@ChrisStewart工作我已经添加了一些功能和单元测试,你可以详细说明“没有工作”吗? –

+0

我已经编辑了OP更加真实,注意日志消息“INSIDE GAMES PATH KEY”我测试我的路径 –