Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 1e34e4d6e8
Fetching contributors…

Cannot retrieve contributors at this time

83 lines (67 sloc) 2.944 kb
package org.cakesolutions.akkapatterns.api
import cc.spray.test.SprayTest
import java.util.concurrent.TimeUnit
import akka.actor.ActorRef
import cc.spray.RequestContext
import cc.spray.http._
import io.Source
import org.specs2.mutable.Specification
import org.cakesolutions.akkapatterns.test.{DefaultTestData, SpecConfiguration}
import org.cakesolutions.akkapatterns.core.Core
import concurrent.util.Duration
trait RootSprayTest extends SprayTest {
protected def testRoot(request: HttpRequest, timeout: Duration = Duration(10000, TimeUnit.MILLISECONDS))
(root: ActorRef): ServiceResultWrapper = {
val routeResult = new RouteResult
root !
RequestContext(
request = request,
responder = routeResult.requestResponder,
unmatchedPath = request.path
)
// since the route might detach we block until the route actually completes or times out
routeResult.awaitResult(timeout)
new ServiceResultWrapper(routeResult, timeout)
}
}
trait JsonSource {
def jsonFor(location: String) = Source.fromInputStream(classOf[JsonSource].getResourceAsStream(location)).mkString
def jsonContent(location: String) = Some(HttpContent(ContentType(MediaTypes.`application/json`), jsonFor(location)))
}
/**
* Convenience trait for API tests
*/
trait ApiSpecification extends Specification with SpecConfiguration with RootSprayTest with Core with Api with Unmarshallers with Marshallers with LiftJSON {
import cc.spray.typeconversion._
protected def respond(method: HttpMethod, url: String, content: Option[HttpContent] = None)
(implicit root: ActorRef) = {
val request = HttpRequest(method, url, content = content)
testRoot(request)(root).response
}
protected def perform[A](method: HttpMethod, url: String, content: Option[HttpContent] = None)
(implicit root: ActorRef, unmarshaller: Unmarshaller[A]): A = {
val request = HttpRequest(method, url, content = content)
val response = testRoot(request)(root).response.content
val obj = response.as[A] match {
case Left(e) => throw new Exception(e.toString)
case Right(r) => r
}
obj
}
protected def perform[In, Out](method: HttpMethod, url: String, in: In)
(implicit root: ActorRef, marshaller: Marshaller[In], unmarshaller: Unmarshaller[Out]): Out = {
marshaller(t => Some(t)) match {
case MarshalWith(f) =>
val sb = new StringBuilder()
val ctx = new StringBuilderMarshallingContent(sb)
f(ctx)(in)
perform[Out](method, url, Some(HttpContent(ContentType(MediaTypes.`application/json`), sb.toString())))
case CantMarshal(_) =>
throw new Exception("Cant marshal " + in)
}
}
}
/**
* Convenience trait for API tests; with default test data
*/
trait DefaultApiSpecification extends ApiSpecification with DefaultTestData with JsonSource
Jump to Line
Something went wrong with that request. Please try again.