Alexander Sysoev
07/20/2022, 2:35 PMAleksei Tirman [JB]
07/20/2022, 3:57 PMOutgoingContent
you’re dealing with to convert it to a ByteReadChannel
and then read bytes from the channel:
val client = HttpClient(MockEngine) {
engine {
addHandler { request ->
val content: ByteReadChannel = when (val body = request.body) {
is OutgoingContent.ByteArrayContent -> ByteReadChannel(body.bytes())
is OutgoingContent.NoContent -> ByteReadChannel.Empty
is OutgoingContent.ReadChannelContent -> body.readFrom()
is OutgoingContent.WriteChannelContent -> GlobalScope.writer(coroutineContext, autoFlush = true) {
body.writeTo(channel)
}.channel
else -> error("Not supported OutgoingContent type")
}
// Use content.readRemaining().readText() to read body as String
respondOk()
}
}
}
Alexander Sysoev
07/20/2022, 4:18 PMWriteChannelContent
, why do i need such complicated and unobvious way to read it?Aleksei Tirman [JB]
07/20/2022, 4:51 PMOutgoingContent
in advance then you can just cast request.body
to that type.Alexander Sysoev
07/20/2022, 4:53 PMAleksei Tirman [JB]
07/20/2022, 4:55 PMWriteChannelContent
can only write data to a ByteWriteChannel
.
public abstract class WriteChannelContent : OutgoingContent() {
public abstract suspend fun writeTo(channel: ByteWriteChannel)
}
ByteReadChannel
or a ByteArray
using this class.Alexander Sysoev
07/20/2022, 4:57 PM