-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7904 Fix Servlet response body is corrupted due to the wrong off…
…set (#4527)
- Loading branch information
Showing
6 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...server-servlet-jakarta/jvm/test/io/ktor/tests/servlet/jakarta/ServletWriterJakartaTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.servlet.jakarta | ||
|
||
import io.ktor.server.servlet.* | ||
import io.ktor.server.servlet.jakarta.servletWriter | ||
import io.ktor.utils.io.* | ||
import io.ktor.utils.io.core.* | ||
import jakarta.servlet.ServletOutputStream | ||
import jakarta.servlet.WriteListener | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.io.readByteArray | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ServletWriterJakartaTest { | ||
|
||
@Test | ||
fun testWriteBufferWithPositivePosition() = runBlocking { | ||
val content = withServletWriter { | ||
val buffer = ByteReadPacket("1234567890".toByteArray()) | ||
buffer.readByte() | ||
|
||
writeBuffer(buffer) | ||
flushAndClose() | ||
} | ||
|
||
assertEquals("234567890", content.decodeToString()) | ||
} | ||
|
||
suspend fun withServletWriter(block: suspend ByteWriteChannel.() -> Unit): ByteArray { | ||
val content = BytePacketBuilder() | ||
val output = object : ServletOutputStream() { | ||
override fun isReady(): Boolean = true | ||
override fun setWriteListener(writeListener: WriteListener) { | ||
writeListener.onWritePossible() | ||
} | ||
|
||
override fun write(b: Int) { | ||
content.writeByte((b and 0xff).toByte()) | ||
} | ||
} | ||
|
||
coroutineScope { | ||
val channel = servletWriter(output).channel | ||
block(channel) | ||
channel.flushAndClose() | ||
} | ||
|
||
return content.build().readByteArray() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ktor-server/ktor-server-servlet/jvm/test/io/ktor/tests/servlet/ServletWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.servlet | ||
|
||
import io.ktor.server.servlet.* | ||
import io.ktor.utils.io.* | ||
import io.ktor.utils.io.core.* | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.io.readByteArray | ||
import javax.servlet.ServletOutputStream | ||
import javax.servlet.WriteListener | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ServletWriterTest { | ||
|
||
@Test | ||
fun testWriteBufferWithPositivePosition() = runBlocking { | ||
val content = withServletWriter { | ||
val buffer = ByteReadPacket("1234567890".toByteArray()) | ||
buffer.readByte() | ||
|
||
writeBuffer(buffer) | ||
flushAndClose() | ||
} | ||
|
||
assertEquals("234567890", content.decodeToString()) | ||
} | ||
|
||
suspend fun withServletWriter(block: suspend ByteWriteChannel.() -> Unit): ByteArray { | ||
val content = BytePacketBuilder() | ||
val output = object : ServletOutputStream() { | ||
override fun isReady(): Boolean = true | ||
|
||
override fun setWriteListener(writeListener: WriteListener) { | ||
writeListener.onWritePossible() | ||
} | ||
|
||
override fun write(b: Int) { | ||
content.writeByte((b and 0xff).toByte()) | ||
} | ||
} | ||
|
||
coroutineScope { | ||
val channel = servletWriter(output).channel | ||
block(channel) | ||
channel.flushAndClose() | ||
} | ||
|
||
return content.build().readByteArray() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters