Test for rejction
ssemichev opened this issue · 2 comments
I have added validation to the case class and changed the tests to use Route.seal or inside(rejection). But the test fails with error
- should respond with bad request on incorrect IP format *** FAILED ***
[info] java.lang.IllegalArgumentException: requirement failed: wrong IP address
It's similar to this question but I Post doesn't accept JsObject
case class IpPairSummaryRequest(ip1: String, ip2: String) { require(ip2.split('.').map(oktet => Try(oktet.toInt)).map(x => x.getOrElse(-1)).map({s => s >= 0 && s <= 255}).forall(x => x), "wrong IP address") }
tests
it should "respond with bad request on incorrect IP format" in {
Post(s"/ip", IpPairSummaryRequest(ip1Info.query, "asdfg")) ~> Route.seal(routes) ~> check {
status shouldBe BadRequest
responseAs[String].length should be > 0
}
Post(s"/ip", IpPairSummaryRequest(ip1Info.query, "asdfg")) ~> routes ~> check {
inside(rejection) {
case ValidationRejection("requirement failed: wrong IP address", _) =>
}
}
}
Hi,
Thanks for reaching out.
The problem with code presented is that exception is being thrown here: Post(s"/ip", IpPairSummaryRequest(ip1Info.query, "asdfg"))
making test failed. One way to avoid is to put JSON as a string in there. This way it will be deserialized to case class just inside route where exception will be thrown.
Hope that helps. If it's still unclear, please reopen.
It's working, thank you!
Post(s"/ip", HttpEntity(`application/json`, s"""{ "ip1": "${ip1Info.query}", "ip2": "asdfg" }""")) ~> routes ~> check {
inside(rejection) {
case ValidationRejection("requirement failed: wrong IP address", _) =>
}
}