https://kotlinlang.org logo
#ktor
Title
# ktor
j

Jonathan Lennox

05/23/2022, 7:52 PM
Another question - I'm porting code from ktor 1, and there's a unit test that tests that some code that uses ktor's HttpClient executes properly. Previously the request.body it would pass to the MockEngine would be of type
TextContent
, so the test could inspect it to make sure it had the right content. With ktor 2, however, the request.body is an
OutputStreamContent
- what's the best way for me to verify that its content is correct?
h

hfhbd

05/23/2022, 8:03 PM
response.bodyAsText()
j

Jonathan Lennox

05/23/2022, 8:03 PM
This is a request not a response.
h

hfhbd

05/23/2022, 8:04 PM
Oh, sorry, missed it.
j

Jonathan Lennox

05/23/2022, 8:08 PM
It looks like
Copy code
suspend fun OutputStreamContent.asText(): String =
    String(ByteChannel().also { writeTo(it) }.toByteArray(), Charsets.UTF_8)
works, but is there a cleaner way?
h

hfhbd

05/23/2022, 8:09 PM
Your could use
.body.toByteReadPacket().readText()
instead, but don't know, which is better 😄
j

Jonathan Lennox

05/23/2022, 8:12 PM
That version doesn't make me presume that the type of
body
is
OutputStreamContent
, so it seems better. Thanks!
h

hfhbd

05/23/2022, 8:13 PM
And it does not enforce utf-8
j

Jonathan Lennox

05/23/2022, 8:14 PM
Indeed - presumably it picks up the right encoding from the request?
h

hfhbd

05/23/2022, 8:14 PM
Yes, from the data
j

Jonathan Lennox

05/23/2022, 8:14 PM
Great
7 Views