spring-framework
spring-framework copied to clipboard
Cannot use reader of two different empty MockHttpServletRequest instances.
Affects: At least from 5.3.31 to latest (2024-05-14)
If you create two different instances of MockHttpServletRequest
without specifying content and try to use the reader of each of them. The second one will raise an exception as all these instances share the same underlying reader (EMPTY_BUFFERED_READER -> https://github.com/spring-projects/spring-framework/blob/main/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java#L741).
Here is a kotlin example of it:
class MockHttpServletRequestReaderTest {
@Test
fun `call once succeeds`() {
MockHttpServletRequest().reader.use { it.readText() }
}
@Test
fun `call twice fails`() {
MockHttpServletRequest().reader.use { it.readText() }
MockHttpServletRequest().reader.use { it.readText() }
}
}
If you run the first test alone in its own JVM, it'll work.
If you run the second test alone, it'll fail on the second MockHttpServletRequest.reader use.
If you repeat the first test at least twice in the same JVM, only the first execution will run fine, all the other ones will fail.