Testing the rejection handler with sealed routes

97 views
Skip to first unread message

Mariano Treb

unread,
May 5, 2015, 7:05:00 AM5/5/15
to spray...@googlegroups.com
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!

Mathias Doenitz

unread,
May 7, 2015, 5:52:01 AM5/7/15
to spray...@googlegroups.com
Mariano,

your custom RejectionHandler should be marked `implicit`,
otherwise the `sealRoute` will not be able to find it.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/e364ab69-3a45-4737-bddb-330daf9865b5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Mariano Treb

unread,
May 7, 2015, 11:34:48 AM5/7/15
to spray...@googlegroups.com
It is working now. 

Thanks!!
Reply all
Reply to author
Forward
0 new messages