我正在关注here的喷雾手册。所以我开始收集非常简单的测试
class AtoImportServiceApiTest extends WordSpecLike with MustMatchers with ScalatestRouteTest {
"AtoImportService" must {
"return HTTP status 401 Unauhorized when accessing withou basic auth" in {
Post("/ato/v1/orders/updateStatus") ~>new AtoImportServiceApi().route ~> check {
handled must be(false)
rejections must have size 1
status === StatusCodes.Unauthorized
}
}
}
}
我正在调用包含授权指令的路由。所以我希望拒绝将转换为HTTP状态代码。但这并没有发生在这里,测试失败了。
Request was rejected with List(AuthenticationFailedRejection(CredentialsMissing,List(WWW-Authenticate: Basic realm="bd ato import api")))
ScalaTestFailureLocation: spray.testkit.RouteTest$class at (RouteTest.scala:74)
org.scalatest.exceptions.TestFailedException: Request was rejected with List(AuthenticationFailedRejection(CredentialsMissing,List(WWW-Authenticate: Basic realm="bd ato import api")))
at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
我在这里错过了一些重要的概念吗?
答案 0 :(得分:5)
你需要密封路线"查看实际状态代码。密封路由意味着默认拒绝和异常处理程序用于处理任何迄今未处理的拒绝和异常。这在您的服务中使用runRoute
时会自动完成,但不会在测试工具包中自动完成,以便您直接检查拒绝。
使用spray-testkit,您需要使用sealRoute(...)
包裹您的路线,如下所示:http://spray.io/documentation/1.2.2/spray-testkit/#sealing-routes
试试这个:
Post("/ato/v1/orders/updateStatus") ~> sealRoute(new AtoImportServiceApi().route) ~> check { // ...