Fix MockHttpServletRequest InputStream for empty body

Prior to this commit, a recent change applied in gh-29125 changed the
behavior of `MockHttpServletRequest` instances. In case of an empty
request body, the returned `InputStream` would be static and could not
be reused across requests.
This could result in `java.io.IOException: Stream closed` exceptions if
a previous request was read.

This commit ensures that a new instance of an empty stream is returned
for each request instance.

Fixes gh-29901
This commit is contained in:
Brian Clozel
2023-01-31 18:45:07 +01:00
parent 16937c7ce5
commit 5fd73f0bae
2 changed files with 14 additions and 6 deletions

View File

@@ -82,6 +82,17 @@ class MockHttpServletRequestTests {
assertThat(StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())).isEqualTo("body");
}
@Test
void readEmptyInputStreamWorksAcrossRequests() throws IOException {
MockHttpServletRequest firstRequest = new MockHttpServletRequest();
firstRequest.getInputStream().readAllBytes();
firstRequest.getInputStream().close();
MockHttpServletRequest secondRequest = new MockHttpServletRequest();
secondRequest.getInputStream().readAllBytes();
secondRequest.getInputStream().close();
}
@Test
void setContentAndGetReader() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());