Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
! testkit: move result.awaitResult call from injectIntoRoute into `…
Browse files Browse the repository at this point in the history
…check`, fixes #205
  • Loading branch information
jrudolph committed Oct 15, 2013
1 parent 50128fb commit 6a99cb7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 10 additions & 4 deletions spray-testkit/src/main/scala/spray/testkit/RouteTest.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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!") 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 } private def result = { assertInCheck(); dynRR.value }
def handled: Boolean = result.handled def handled: Boolean = result.handled
Expand Down Expand Up @@ -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)) 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) // there is already an implicit class WithTransformation in scope (inherited from spray.httpx.TransformerPipelineSupport)
// however, this one takes precedence // however, this one takes precedence
implicit class WithTransformation2(request: HttpRequest) { implicit class WithTransformation2(request: HttpRequest) {
Expand Down Expand Up @@ -115,13 +122,12 @@ trait RouteTest extends RequestBuilding with RouteResultComponent {
responder = routeResult.handler, responder = routeResult.handler,
unmatchedPath = effectiveRequest.uri.path) unmatchedPath = effectiveRequest.uri.path)
} }
// since the route might detach we block until the route actually completes or times out routeResult
routeResult.awaitResult
} }
} }
} }
} }


trait ScalatestRouteTest extends RouteTest with ScalatestInterface { this: Suite } trait ScalatestRouteTest extends RouteTest with ScalatestInterface { this: Suite }


trait Specs2RouteTest extends RouteTest with Specs2Interface trait Specs2RouteTest extends RouteTest with Specs2Interface
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import MediaTypes._
import HttpCharsets._ import HttpCharsets._
import StatusCodes._ import StatusCodes._
import HttpHeaders._ import HttpHeaders._
import akka.testkit.TestProbe


class ScalatestRouteTestSpec extends FreeSpec with MustMatchers with Directives with ScalatestRouteTest { class ScalatestRouteTestSpec extends FreeSpec with MustMatchers with Directives with ScalatestRouteTest {


Expand Down Expand Up @@ -56,6 +57,28 @@ class ScalatestRouteTestSpec extends FreeSpec with MustMatchers with Directives
rejections must be === List(MethodRejection(GET), MethodRejection(PUT)) 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.