2015-05-21 17:17:55 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.http.javadsl.testkit
|
|
|
|
|
|
|
|
|
|
import scala.annotation.varargs
|
|
|
|
|
import scala.concurrent.ExecutionContext
|
|
|
|
|
import scala.concurrent.duration._
|
2015-08-13 10:14:12 +02:00
|
|
|
import akka.stream.Materializer
|
2015-05-21 17:17:55 +02:00
|
|
|
import akka.http.scaladsl.server
|
|
|
|
|
import akka.http.javadsl.model.HttpRequest
|
2015-07-17 14:02:23 +02:00
|
|
|
import akka.http.javadsl.server.{ HttpApp, AllDirectives, Route, Directives }
|
2015-05-21 17:17:55 +02:00
|
|
|
import akka.http.impl.util.JavaMapping.Implicits._
|
|
|
|
|
import akka.http.impl.server.RouteImplementation
|
|
|
|
|
import akka.http.scaladsl.model.HttpResponse
|
|
|
|
|
import akka.http.scaladsl.server.{ RouteResult, RoutingSettings, Route ⇒ ScalaRoute }
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.event.NoLogging
|
|
|
|
|
import akka.http.impl.util._
|
|
|
|
|
|
2015-08-13 10:14:12 +02:00
|
|
|
/**
|
|
|
|
|
* A base class to create route tests for testing libraries. An implementation needs to provide
|
|
|
|
|
* code to provide and shutdown an [[ActorSystem]], [[Materializer]], and [[ExecutionContext]].
|
|
|
|
|
* Also an implementation should provide instances of [[TestResponse]] to define the assertion
|
|
|
|
|
* facilities of the testing library.
|
|
|
|
|
*
|
|
|
|
|
* See `JUnitRouteTest` for an example of a concrete implementation.
|
|
|
|
|
*/
|
2015-05-07 17:00:45 +02:00
|
|
|
abstract class RouteTest extends AllDirectives {
|
2015-05-21 17:17:55 +02:00
|
|
|
implicit def system: ActorSystem
|
2015-08-13 10:14:12 +02:00
|
|
|
implicit def materializer: Materializer
|
2015-05-21 17:17:55 +02:00
|
|
|
implicit def executionContext: ExecutionContext = system.dispatcher
|
|
|
|
|
|
|
|
|
|
protected def awaitDuration: FiniteDuration = 500.millis
|
|
|
|
|
|
2015-06-18 16:44:16 +02:00
|
|
|
def runRoute(route: Route, request: HttpRequest): TestResponse =
|
|
|
|
|
runScalaRoute(ScalaRoute.seal(RouteImplementation(route)), request)
|
|
|
|
|
def runRouteUnSealed(route: Route, request: HttpRequest): TestResponse =
|
|
|
|
|
runScalaRoute(RouteImplementation(route), request)
|
|
|
|
|
|
|
|
|
|
private def runScalaRoute(scalaRoute: ScalaRoute, request: HttpRequest): TestResponse = {
|
2015-05-21 17:17:55 +02:00
|
|
|
val result = scalaRoute(new server.RequestContextImpl(request.asScala, NoLogging, RoutingSettings(system)))
|
|
|
|
|
|
|
|
|
|
result.awaitResult(awaitDuration) match {
|
|
|
|
|
case RouteResult.Complete(response) ⇒ createTestResponse(response)
|
2015-09-25 12:51:55 +02:00
|
|
|
case RouteResult.Rejected(ex) ⇒ throw new AssertionError("got unexpected rejection: " + ex)
|
2015-05-21 17:17:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 14:01:29 +02:00
|
|
|
/**
|
|
|
|
|
* Wraps a list of route alternatives with testing support.
|
|
|
|
|
*/
|
2015-05-21 17:17:55 +02:00
|
|
|
@varargs
|
|
|
|
|
def testRoute(first: Route, others: Route*): TestRoute =
|
|
|
|
|
new TestRoute {
|
|
|
|
|
val underlying: Route = Directives.route(first, others: _*)
|
|
|
|
|
|
|
|
|
|
def run(request: HttpRequest): TestResponse = runRoute(underlying, request)
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 14:02:23 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a [[TestRoute]] for the main route of an [[HttpApp]].
|
|
|
|
|
*/
|
2015-08-13 10:14:12 +02:00
|
|
|
def testAppRoute(app: HttpApp): TestRoute = testRoute(app.createRoute())
|
2015-07-17 14:02:23 +02:00
|
|
|
|
2015-05-21 17:17:55 +02:00
|
|
|
protected def createTestResponse(response: HttpResponse): TestResponse
|
|
|
|
|
}
|