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.
Comment From: snicoll
Thanks for the report and it's interesting to see this was never reported before. I guess if you don't have content, you don't exhaust the body in the test (in particular closing the reader, which is what this method does). Also getInputStream
does not have this problem as it creates a new stream, even for an empty body.