Kotest Ktor拡張
Ktor拡張について説明する。
kotest-assertions-ktorモジュールは、Ktorアプリケーションに対するレスポンスmatcherを提供する。サーバーサイドテストサポートを使用する場合はTestApplicationResponse向けのmatcherがあり、Ktor HTTPクライアントを使用する場合はHttpResponse向けのmatcherがある。
Ktor matcherを追加するには、プロジェクトに次の依存関係を追加する必要がある。
io.kotest.extensions:kotest-assertions-ktor:${version}
サーバーサイドテストサポートとともにmatcherを使用する例である。
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")
}
}
そして、クライアントサポートの使用例である。
val client = HttpClient(CIO)
val response = client.post("http://mydomain.com/foo")
response.shouldHaveStatus(HttpStatusCode.OK)
response.shouldHaveHeader(name = "Authorization", value = "Bearer")