Testing rejection in akka.http

1,072 views
Skip to first unread message

Alex Ivanov

unread,
Mar 26, 2015, 5:02:48 AM3/26/15
to spray...@googlegroups.com
Good afternoon everyone. 

I've started using akka.http for personal projects and sucked with rejections testing. For example this test:

"reject the request with \"EmailAlreadyInUseRejection\" if the given email is already registered" in withRequest { request =>
 
Post("/register", request) ~> route ~> check {
    handled shouldBe
false
    rejection shouldBe EmailAlreadyInUseRejection(request.email)
    responseAs
[Map[String, String]] shouldBe Map(
     
"email" -> request.email,
     
"reason" -> s"Email ${request.email} is already taken, please provide another one"
    )
 
}
}

And I'm getting a failed test due Request was rejected with rejection EmailAlreadyInUseRejection(te...@gmail.com). I have a custom rejection handler in scope which works as expected when i run the server, but in tests i can't check responseAs part, tried to rewrite it with entityAs, but the problem remains. Don't even know where should i look.


Mathias Doenitz

unread,
Mar 26, 2015, 5:24:51 AM3/26/15
to spray...@googlegroups.com
Alex,

you have to decide whether you want to test your route *including* the rejection handler or *excluding* the rejection handler.
You cannot do both at once.

This is also apparent when you look at the types:

type Route = RequestContext ⇒ Future[RouteResult]
sealed trait RouteResult
object RouteResult {
final case class Complete(response: HttpResponse) extends RouteResult
final case class Rejected(rejections: immutable.Seq[Rejection]) extends RouteResult
}

So, either you test for the presence of a rejection or your test for the presence of a certain response.

It looks like your test is of the form *excluding* the rejection handler.
Like in spray you need to “seal” your route if you want to test the rejection handling as well.
In Akka HTTP you do that by wrapping your route with `Route.seal(…)`.

HTH and 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/c1fc7d24-8f45-4a6d-b20b-30483292bf8e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Alex Ivanov

unread,
Mar 26, 2015, 1:22:58 PM3/26/15
to spray...@googlegroups.com
Thank you, Mathias, missed the "seal" function.

> And I'm getting a failed test due Request was rejected with rejection EmailAlreadyInUseRejection(test...@gmail.com). I have a custom rejection handler in scope which works as expected when i run the server, but in tests i can't check responseAs part, tried to rewrite it with entityAs, but the problem remains. Don't even know where should i look.
Reply all
Reply to author
Forward
0 new messages