I've created a custom error handler for a scala play application. For that, I implemented the 2 methods: onClientError and onServerError. I created an integration test and I was able to test onClientError, but I can't find a way to force the server error. This is the test:
class MyJsonHttpErrorHandlerTest extends FeatureSpec
with ServiceAcceptanceTestSuite
with PlayService
with FakeApplicationFactory
with Matchers
with WithApplicationComponents {
override def components: BuiltInComponents = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents {
import play.api.mvc.Results
import play.api.routing.Router
import play.api.routing.sird._
lazy val router: Router = Router.from({
case GET(p"/clienterror") => defaultActionBuilder { Results.Forbidden }
case GET(p"/servererror") => throw InternalServerErrorException
})
val optionalSourceMapper: OptionalSourceMapper = new OptionalSourceMapper(Some(devContext.get.sourceMapper))
override lazy val httpErrorHandler: HttpErrorHandler = new MyJsonHttpErrorHandler(environment, optionalSourceMapper)
}
feature("StudentOptionsJsonHttpErrorHandler test") {
scenario("Should use our custom error handler for a client error") {
import play.api.test.Helpers.GET
import play.api.test.Helpers.route
val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/clienterror"))
val json: JsValue = Helpers.contentAsJson(result)
val requestId = (json \ "error" \ "requestId" ).as[Int]
val message = (json \ "error" \ "message" ).as[String]
val uuid = (json \ "error" \ "uuid" ).as[String]
requestId must be(1)
message.isEmpty must be(true)
uuid.matches("[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}") must be(true)
}
scenario("Should use our custom error handler for a server error") {
import play.api.test.Helpers.GET
import play.api.test.Helpers.route
val Some(result: Future[Result]) = route(app, FakeRequest(GET, "/servererror"))
val json: JsValue = Helpers.contentAsJson(result)
val requestId = (json \ "error" \ "requestId" ).as[Int]
val message = (json \ "error" \ "message" ).as[String]
val uuid = (json \ "error" \ "uuid" ).as[String]
requestId must be(2)
message.isEmpty must be(true)
uuid.matches("[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}") must be(true)
}
}
}
So, when I do a request to /servererror, I can see my custom error handler is going to onClientError. How can I force a fake server error so the onServerError of my handler is invoked?
Aucun commentaire:
Enregistrer un commentaire