Search code examples
scalaasynchronousscalatestakka-httpakka-testkit

Route TestKit in asynchronous ScalaTest


I am switching synchronous ScalaTest tests of some Akka HTTP code to AsyncFunSpec. Is there a simple way to make Akka TestKit tests asynchronous, as well? I am talking about code like:

Get("/test") ~> testRoute ~> check {
    responseAs[String] shouldEqual "Fragments of imagination"
}

What I would basically need is a version of check which returns a Future instead of calling await. Or a helper function which converts a HttpRequest like Get("/test") into a RequestContext so that I can apply the route to it.


Solution

  • I ended up using something like this:

    import akka.http.scaladsl.client.RequestBuilding.Get
    import akka.http.scaladsl.server.Route
    
    val handler = Route.asyncHandler(testRoute)
    
    for {
      response <- handler(Get("/test"))
      strict <- response.entity.toStrict
      res <- strict.toString shouldEqual "Fragments of imagination"
    } yield res