Hello,
I am trying to test my routes (included the rejected ones) so I am using sealed routes in my tests to check if the rejections are converted successfully into Http responses. I have been reading some links
[1] and I see that you must specify the RejectionHandler to use. In my case I am using a custom rejection handler to convert all rejections into a custom json:
def rejectionHandler = RejectionHandler {
case MalformedRequestContentRejection(msg, e) :: _ =>
completeResponseWithError(BadRequest, ErrorCodes.jsonSyntaxError, msg)
....
case _ :: _ =>
completeResponseWithError(InternalServerError, ErrorCodes.serverUnknownError, "Exceptional server error")
}
So here, in the tests I mix with the FailureHandling trait who has the rejectionHandler showed above.
def test_error_emailFormat() = {
val user = new User("John", "john@smithcom")
val result_wrongEmailFormat = new ErrorResponse(StatusCodes.BadRequest.intValue, ErrorCodes.jsonFormatError, "email")
test("Create User - Check email format") {
Post("/users", user) ~> sealRoute(userRoutes) ~> check {
//assert response in custom json format
However, the test is not using my custom rejection handler. Instead is using default spray handler and so the response is not in my custom json format.
I tried to tell Spray the use my handlers in the following ways:
val wrap = handleRejections(rejectionHandler)
And then (like in the example):
Post("/users", user) ~> wrap ~> sealRoute(userRoutes) ~> check
Or
Post("/users", user) ~> rejectionHandler ~> sealRoute(userRoutes) ~> check
But it doesnt work.
Any idea? I don't understand what it does not work because I am doing it like in the example (I am using scalatest, Should I use specs2???)
Thank you!