How do one use the HttpClient MockEngine when Json...
# ktor
j
How do one use the HttpClient MockEngine when JsonFeature is expected on the engine? Is there any way without having to do the (de)serializing manually? See example failing test. (On Ktor 0.9.4)
e
It looks like you try to set
contentType
header by hand. It's forbidden.
It looks like you could set
Accept
header to
ContentType.Application.Json
in
fetchData
and drop
contentType()
setup
j
But the
contentType(ContentType.Application.Json)
is needed for the
JsonFeature
to kick in. Removing it (or replacing with Accept header) will cause a ClassCastException since
body
is not a
OutgoingContent
e
btw: Where did you install
JsonFeature
?
Yep. We should create OutgoingContent with proper content type here.
j
Production code has (excerpt):
Copy code
HttpClient(Apache) {
  install(JsonFeature) {
    serializer = JacksonSerializer()
  }
}
e
Could you try to set this workaround as body?
Copy code
object Json : OutgoingContent.NoContent() {
    override val contentType: ContentType get() = ContentType.Application.Json
}
Would be nice if you file an github issue. It's an API problem and I'll try to fix it asap.
j
Sure, I'll file a github issue. 🙂
🙏 1
e
Awesome. Could you test the workaround?
j
That leads to another problem which I didn't think of; the MockEngine of course does not know how to transform
MockHttpResponse
into
MyResponse
data class 🤔
e
You could try to make the converting interceptor 🙂