我试图用这样的HttpClient创建一个Ktor (1.3.1) JsonFeature模拟:
@Test
fun mockFailure() = runBlocking {
val mock = MockEngine { call ->
respond("{}",
HttpStatusCode.OK,
headersOf("Content-Type", ContentType.Application.Json.toString()))
}
val client = HttpClient(mock) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
val resp = client.get<JsonObject>("dsf")
}它似乎正确地处理了它,但随后我得到了以下错误:
io.ktor.client.call.NoTransformationFoundException: No transformation found: class kotlinx.coroutines.io.ByteBufferChannel -> class kotlinx.serialization.json.JsonObject
with response from http://localhost/dsf:
status: 200 OK
response headers:
Content-Type: application/json
at io.ktor.client.call.HttpClientCall.receive(HttpClientCall.kt:79)发布于 2022-09-20 07:56:09
尝试在HttpClient块中安装客户端内容协商插件:
val client = HttpClient(mock) {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
//Here
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
}所需依赖项implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
Json序列化implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
还将最后一行更改为val resp = client.get<JsonObject>("dsf").body()
https://stackoverflow.com/questions/60385365
复制相似问题