Avoid reader on empty content to be shared by multiple requests

This commit avoids several instances of MockHttpServletRequest to
have a common reader for empty content as closing it will have an
unwanted side effect on the others.

Closes gh-32848
This commit is contained in:
Stéphane Nicoll
2024-05-20 13:32:04 +02:00
parent 97e12bd0e8
commit a0f07af375
2 changed files with 12 additions and 4 deletions

View File

@@ -100,9 +100,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
private static final BufferedReader EMPTY_BUFFERED_READER =
new BufferedReader(new StringReader(""));
/**
* Date formats as specified in the HTTP RFC.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
@@ -736,7 +733,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.reader = new BufferedReader(sourceReader);
}
else {
this.reader = EMPTY_BUFFERED_READER;
this.reader = new BufferedReader(new StringReader(""));
}
return this.reader;
}

View File

@@ -93,6 +93,17 @@ class MockHttpServletRequestTests {
secondRequest.getInputStream().close();
}
@Test // gh-32820
void readEmptyReaderWorksAcrossRequests() throws IOException {
MockHttpServletRequest firstRequest = new MockHttpServletRequest();
firstRequest.getReader().read(new char[256]);
firstRequest.getReader().close();
MockHttpServletRequest secondRequest = new MockHttpServletRequest();
secondRequest.getReader().read(new char[256]);
secondRequest.getReader().close();
}
@Test
void setContentAndGetReader() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());