Kotest Ktor Matchers

Explains Ktor matchers.

The code is kept in a separate repository and under a different group, io.kotest.extensions.

Full Dependency

implementation("io.kotest.extensions:kotest-assertions-ktor:version")
implementation "io.kotest.extensions:kotest-assertions-ktor:version"

Matchers for Ktor are provided by the kotest-assertions-ktor module.

Test Application Response

The following matchers are used when testing through the Ktor server test kit.

Matcher Description
TestApplicationResponse.shouldHaveStatus(HttpStatusCode) Checks that the response has the given HTTP status code.
TestApplicationResponse.shouldHaveContent(content) Checks that the response has the given body.
TestApplicationResponse.shouldHaveContentType(ContentType) Checks that the response has the given content type.
TestApplicationResponse.shouldHaveHeader(name, value) Checks that the response contains the given name=value header.
TestApplicationResponse.shouldHaveCookie(name, value) Checks that the response contains the given cookie.
TestApplicationResponse.shouldHaveCacheControl(value) Checks that the response contains the given cache-control header.
TestApplicationResponse.shouldHaveETag(value) Checks that the response contains the given ETag header.
TestApplicationResponse.shouldHaveContentEncoding(value) Checks that the response contains the specified content-encoding header.

HttpResponse

The following matchers can be used for responses from the Ktor HTTP client.

Matcher Description
HttpResponse.shouldHaveStatus(HttpStatusCode) Checks that the response has the given HTTP status code.
HttpResponse.shouldHaveContentType(ContentType) Checks that the response has the given content type.
HttpResponse.shouldHaveHeader(name, value) Checks that the response contains the given name=value header.
HttpResponse.shouldHaveVersion(HttpProtocolVersion) Checks that the response used the given protocol version.
HttpResponse.shouldHaveCacheControl(value) Checks that the response contains the given cache-control header.
HttpResponse.shouldHaveETag(value) Checks that the response contains the given ETag header.
HttpResponse.shouldHaveContentEncoding(value) Checks that the response contains the specified content-encoding header.

References