Kotest Ktor Extension

This page explains the Ktor extension.

The kotest-assertions-ktor module provides response matchers for Ktor applications. If you use server-side test support, there are matchers for TestApplicationResponse, and if you use the Ktor HTTP client, there are matchers for HttpResponse.

To add Ktor matchers, add the following dependency to your project.

io.kotest.extensions:kotest-assertions-ktor:${version}

The following is an example of using matchers with server-side test support:

withTestApplication({ module(testing = true) }) {
   handleRequest(HttpMethod.Get, "/").apply {
      response shouldHaveStatus HttpStatusCode.OK
      response shouldNotHaveContent "failure"
      response.shouldHaveHeader(name = "Authorization", value = "Bearer")
      response.shouldNotHaveCookie(name = "Set-Cookie", cookieValue = "id=1234")
   }
}

And here is an example of using client support:

val client = HttpClient(CIO)
val response = client.post("http://mydomain.com/foo")
response.shouldHaveStatus(HttpStatusCode.OK)
response.shouldHaveHeader(name = "Authorization", value = "Bearer")

References