Permalink
Browse files

! testkit: move result.awaitResult call from `injectIntoRoute` into `…

…check`, fixes #205
  • Loading branch information...
jrudolph committed Oct 15, 2013
1 parent 50128fb commit 6a99cb74ad3eee05d717ecc76c4fcfb9dda8210c
@@ -49,7 +49,7 @@ trait RouteTest extends RequestBuilding with RouteResultComponent {
if (dynRR.value == null) sys.error("This value is only available inside of a `check` construct!")
}
- def check[T](body: T): RouteResult T = dynRR.withValue(_)(body)
+ def check[T](body: T): RouteResult T = result dynRR.withValue(result.awaitResult)(body)
private def result = { assertInCheck(); dynRR.value }
def handled: Boolean = result.handled
@@ -78,6 +78,13 @@ trait RouteTest extends RequestBuilding with RouteResultComponent {
if (r.size == 1) r.head else failTest("Expected a single rejection but got %s (%s)".format(r.size, r))
}
+ /**
+ * A dummy that can be used as `~> runRoute` to run the route but without blocking for the result.
+ * The result of the pipeline is the result that can later be checked with `check`. See the
+ * "separate running route from checking" example from ScalatestRouteTestSpec.scala.
+ */
+ def runRoute: RouteResult RouteResult = identity
+
// there is already an implicit class WithTransformation in scope (inherited from spray.httpx.TransformerPipelineSupport)
// however, this one takes precedence
implicit class WithTransformation2(request: HttpRequest) {
@@ -115,13 +122,12 @@ trait RouteTest extends RequestBuilding with RouteResultComponent {
responder = routeResult.handler,
unmatchedPath = effectiveRequest.uri.path)
}
- // since the route might detach we block until the route actually completes or times out
- routeResult.awaitResult
+ routeResult
}
}
}
}
trait ScalatestRouteTest extends RouteTest with ScalatestInterface { this: Suite }
-trait Specs2RouteTest extends RouteTest with Specs2Interface
+trait Specs2RouteTest extends RouteTest with Specs2Interface
@@ -25,6 +25,7 @@ import MediaTypes._
import HttpCharsets._
import StatusCodes._
import HttpHeaders._
+import akka.testkit.TestProbe
class ScalatestRouteTestSpec extends FreeSpec with MustMatchers with Directives with ScalatestRouteTest {
@@ -56,6 +57,28 @@ class ScalatestRouteTestSpec extends FreeSpec with MustMatchers with Directives
rejections must be === List(MethodRejection(GET), MethodRejection(PUT))
}
}
+
+ "separate running route from checking" in {
+ val pinkHeader = RawHeader("Fancy", "pink")
+
+ case class HandleRequest(ctx: RequestContext)
+ val service = TestProbe()
+ val handler = TestProbe()
+
+ val result =
+ Get() ~> addHeader(pinkHeader) ~> {
+ respondWithHeader(pinkHeader) { ctx service.send(handler.ref, HandleRequest(ctx)) }
+ } ~> runRoute
+
+ val ctx = handler.expectMsgType[HandleRequest].ctx
+ ctx.complete("abc")
+
+ check {
+ status must be === OK
+ body must be === HttpEntity(ContentType(`text/plain`, `UTF-8`), "abc")
+ header("Fancy") must be === Some(pinkHeader)
+ }(result)
+ }
}
-}
+}

0 comments on commit 6a99cb7

Please sign in to comment.