sprayspray-test

Access request in Spray testkit test


I have an external validator library which needs access to both the request and the response in a test. The response is easy - I just need to write an implicit conversion to the Java interface the library wants - but I would like to write code that lets me call the external validator on a request/response pair as well as asserting other things, without having to mention the request twice (which would be a potential source of bugs - I could accidentally make the requests different). How can I do that?

I am thinking of maybe something like

Post(url) ~> validateRAML(sealRoute(myRoute)) ~> check { ... }

but what would validateRAML look like?


Solution

  • I couldn't figure out a way of using the Spray Testkit DSL for this, so I just ended up doing the obvious thing of creating a method:

    def validateRAML(req: HttpRequest, response: HttpResponse) = {
      // This invokes implicit methods that I have defined to convert req and response
      apiDef.testAgainst(req, response) should be('empty)
    }
    
    def validateRAML[T](req: HttpRequest, r: Route)(body: => T): Unit = {
      req ~> r ~> check {
        body
        validateRAML(req, response)
      }
    }