So, I have a set of Akka Http routes written in scala. Which looks like this
val route: Route = {
handleRejections(PrimaryRejectionHandler.handler) {
handleExceptions(PrimaryExceptionHandler.handler) {
cors() {
encodeResponseWith(Gzip) {
pathPrefix("v1") {
new v1Routes().handler
} ~
path("ping") {
complete("pong")
}
}
}
}
}
}
Now I want to test this using scala-test and akka testkit.
class HttpRouteTest extends WordSpec with Matchers with ScalatestRouteTest {
"GET /ping" should {
"return 200 pong" in new Context {
Get("/ping") ~> httpRoute ~> check {
responseAs[String] shouldBe "pong"
status.intValue() shouldBe 200
}
}
}
trait Context {
val httpRoute: Route = new HttpRoute().route
}
}
Now since, I am encoding my responses with gzip in the route, the test is getting gibberish when it's trying to convert to string. As a result the test does not pass.
Is there any solution to this? Thanks in advance.
For anyone who bumps into this.
This is how I solved the problem. Firstly I structured the unit test packages with the same name of the module it is testing.
I made a BaseService that will be used in all tests which looks like this
trait BaseServiceTest extends WordSpec with Matchers with ScalatestRouteTest with MockitoSugar {
def awaitForResult[T](futureResult: Future[T]): T =
Await.result(futureResult, 5.seconds)
def decodeResponse(response: HttpResponse): HttpResponse = {
val decoder = response.encoding match {
case HttpEncodings.gzip ⇒
Gzip
case HttpEncodings.deflate ⇒
Deflate
case HttpEncodings.identity ⇒
NoCoding
}
decoder.decodeMessage(response)
}
}
Then using this I wrote my tests like so
class UserTest extends BaseServiceTest {
"GET /user" should {
"return user details with 200 code" in new Context {
Get("/") ~> userRoute ~> check {
val decodedResponse = getBody(decodeResponse(response))
decodedResponse.user.name.isDefined shouldBe true
decodedResponse.user.age.isDefined shouldBe true
decodedResponse.user.city.isDefined shouldBe true
status.intValue() shouldBe 200
}
}
}
trait Context {
val userRoute: Route = UserRoute.route
}
def getBody(resp: HttpResponse): UserResponse = {
import UserResponseJsonProtocol._ // Using spray-json for marshalling protocols
Await.result(Unmarshal(resp).to[UserResponse], 10.seconds)
}
}
Hope this helps. Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With