From 7ed49725826ed252e3409faf35e46ee018f09851 Mon Sep 17 00:00:00 2001 From: "i.ilyin" Date: Mon, 12 Feb 2024 16:17:12 +0300 Subject: [PATCH] fix: removed reset() call on HttpBotRequest's input stream --- .../jaicf/channel/http/HttpBotRequest.kt | 17 +++++++------- .../core/test/channel/HttpBotRequestTest.kt | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 core/src/test/kotlin/com/justai/jaicf/core/test/channel/HttpBotRequestTest.kt diff --git a/core/src/main/kotlin/com/justai/jaicf/channel/http/HttpBotRequest.kt b/core/src/main/kotlin/com/justai/jaicf/channel/http/HttpBotRequest.kt index 03c1a2cb..abba2d03 100644 --- a/core/src/main/kotlin/com/justai/jaicf/channel/http/HttpBotRequest.kt +++ b/core/src/main/kotlin/com/justai/jaicf/channel/http/HttpBotRequest.kt @@ -7,7 +7,7 @@ import java.nio.charset.Charset /** * Contains details of the HTTP request to the corresponding [HttpBotChannel] * - * @property stream input stream containing a request data + * @param stream input stream containing a request data * @property headers request HTTP headers * @property parameters HTTP query parameters * @property requestMetadata optional metadata for request processing @@ -18,21 +18,20 @@ class HttpBotRequest( val parameters: Map> = mapOf(), val requestMetadata: String? = null ) { - val stream = stream.buffered() + private val streamBody: ByteArray - fun receiveText(charset: Charset = Charset.forName("UTF-8")) = stream.runAndReset { bufferedReader(charset).readText() } + init { + streamBody = stream.readBytes() + } + + fun receiveText(charset: Charset = Charset.forName("UTF-8")) = streamBody.toString(charset) fun firstHeader(name: String) = headers[name]?.first() fun firstParameter(name: String) = parameters[name]?.first() override fun toString() = - "HttpBotRequest(stream=$stream, headers=$headers, parameters=$parameters, requestMetadata=$requestMetadata" -} - -fun InputStream.runAndReset(action: InputStream.() -> R): R { - mark(0) - return action().also { reset() } + "HttpBotRequest(streamBody=$streamBody, headers=$headers, parameters=$parameters, requestMetadata=$requestMetadata" } fun String.asHttpBotRequest(requestMetadata: String? = null) = HttpBotRequest( diff --git a/core/src/test/kotlin/com/justai/jaicf/core/test/channel/HttpBotRequestTest.kt b/core/src/test/kotlin/com/justai/jaicf/core/test/channel/HttpBotRequestTest.kt new file mode 100644 index 00000000..298bd9ca --- /dev/null +++ b/core/src/test/kotlin/com/justai/jaicf/core/test/channel/HttpBotRequestTest.kt @@ -0,0 +1,22 @@ +package com.justai.jaicf.core.test.channel + +import com.justai.jaicf.channel.http.HttpBotRequest +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import kotlin.random.Random +import kotlin.test.assertEquals + +class HttpBotRequestTest { + @Test + fun `should not throw exception on long request body`() { + val randomBytes = Random.Default.nextBytes(65536) + val request = HttpBotRequest(randomBytes.inputStream()) + val resultString = assertDoesNotThrow { + request.receiveText(charset = Charsets.UTF_8) + } + assertEquals( + randomBytes.decodeToString(), + resultString + ) + } +} \ No newline at end of file