Skip to content

Instantly share code, notes, and snippets.

@Norwae
Created December 10, 2015 08:51
package experiment
import akka.actor.ActorSystem
import akka.util.Timeout
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import spray.http.HttpHeaders.Accept
import spray.http.MediaTypes._
import spray.http.{ContentType, HttpResponse}
import spray.httpx.marshalling.{ToResponseMarshaller, ToResponseMarshallingContext}
import spray.routing.{ExceptionHandler, HttpService, Route}
import spray.testkit.ScalatestRouteTest
import scala.concurrent.Future
import scala.util.Failure
import scala.util.control.NonFatal
class ErrorHandlerOddnessSpec extends FlatSpec with HttpService with ScalatestRouteTest with ShouldMatchers {
val actorRefFactory = ActorSystem("test-actor-system")
case class ErrorReport(message: String)
implicit val errorMarshaller = ToResponseMarshaller.oneOf(`text/plain`, `application/json`)(
ToResponseMarshaller.of[ErrorReport](`text/plain`) { (report: ErrorReport, contentType: ContentType, context: ToResponseMarshallingContext ) =>
context.marshalTo(HttpResponse(entity = report.message))
},
ToResponseMarshaller.of[ErrorReport](`application/json`) {
{ (report: ErrorReport, contentType: ContentType, context: ToResponseMarshallingContext ) =>
context.marshalTo(HttpResponse(entity = s"""{"message": "${report.message}"}"""))
}
}
)
implicit val timeout = Timeout(500)
implicit val handler = ExceptionHandler {
case NonFatal(e) => complete(ErrorReport("handle implicit"))
}
val failedFuture: Future[String] = Future.failed(new IllegalArgumentException)
val routing: Route = path("explicitHandle") {
onComplete(failedFuture) {
case Failure(err) => complete(ErrorReport("handle explicit"))
}
} ~ path("implicitHandle") {
onSuccess(failedFuture) { _ =>
complete("wtf?!")
}
}
"Content negotiation" should "report the correct type on an explicit accept" in {
Get("http://localhost/explicitHandle").withHeaders(Accept(`text/plain`)) ~> sealRoute(routing) ~> check {
entity.asString shouldEqual "handle explicit"
}
Get("http://localhost/explicitHandle").withHeaders(Accept(`application/json`)) ~> sealRoute(routing) ~> check {
entity.asString shouldEqual """{"message": "handle explicit"}"""
}
}
it should "use the default handler if no accept header is present" in {
Get("http://localhost/explicitHandle") ~> sealRoute(routing) ~> check {
entity.asString shouldEqual "handle explicit"
}
}
"Error handling" should "report the correct type on an explicit accept" in {
Get("http://localhost/implicitHandle").withHeaders(Accept(`text/plain`)) ~> sealRoute(routing) ~> check {
entity.asString shouldEqual "handle implicit"
}
Get("http://localhost/implicitHandle").withHeaders(Accept(`application/json`)) ~> sealRoute(routing) ~> check {
entity.asString shouldEqual """{"message": "handle implicit"}"""
}
}
it should "use the default handler if no accept header is present" in {
Get("http://localhost/implicitHandle") ~> sealRoute(routing) ~> check {
entity.asString shouldEqual "handle implicit"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment