Spray variance not converting to status code?

I am following the spray instructions from here . So I put together a pretty simple test

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

      

I find a route that contains an authorized directive. So I expect the failure to be converted to an HTTP status code. But that doesn't happen here and the test fails.

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)

      

Am I missing an important concept here?

+3


source to share


1 answer


You need to "seal the route" to see the actual status codes. Route compacting means that default rejection and exception handlers are used to handle any unhandled failures and exceptions. This is automatically done when used runRoute

in your service, but it is not done automatically in testkit so you can check for deviations directly.

Using spray testkit you have to wrap your route sealRoute(...)

as described here: http://spray.io/documentation/1.2.2/spray-testkit/#sealing-routes



Try the following:

Post("/ato/v1/orders/updateStatus") ~> sealRoute(new AtoImportServiceApi().route) ~> check { // ...

      

+5


source







All Articles