Is there any EASY way to get the body's object con...
# ktor
d
Is there any EASY way to get the body's object content from Ktor client's MockEngine's requestHistory?
a
Do you want to get a request body from the history as an object that was passed to the
setBody
?
d
One possibility... or just use the installed json deserializer...
@Aleksei Tirman [JB]?
I'm struggling with MockEngine, which would have been the best way to test, but compare that to: https://github.com/marcinziolo/kotlin-wiremock which seems to be much nicer, and has an easy way to assert things.
k
This is what I did on my project:
Copy code
/**
 * Decode the body content from this request into a [String] in UTF-8.
 */
suspend fun OutgoingContent.decodeContent(): String =
    toByteArray().decodeToString()

/**
 * Decode the body content from a [String] in UTF-8 into a serializable type [T].
 */
@JvmName("decodeContentSerialization")
suspend inline fun <reified T> OutgoingContent.decodeContent(): T =
    Json.decodeFromString(decodeContent())
👍🏼 1
d
It gives me: Type mismatch. Required: DeserializationStrategy<TypeVariable(T)> Found: String No value passed for parameter 'string'
(the second one...)
a
How do you call that method?
k
decodeFromString
is an extension function for
Json
:
Copy code
import kotlinx.serialization.decodeFromString
👍🏼 1
d
Yeah, I would have thought that Intellij was smart enough to auto-import that kind of thing by now...
@Aleksei Tirman [JB] I would use it in a matching framework like Strikt, to see if the expected request was received by the MockEngine
348 Views