Skip to content

Commit

Permalink
Avoid reader on empty content to be shared by multiple requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed May 20, 2024
1 parent 97e12bd commit a0f07af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -736,7 +733,7 @@ else if (this.inputStream != null) {
this.reader = new BufferedReader(sourceReader);
}
else {
this.reader = EMPTY_BUFFERED_READER;
this.reader = new BufferedReader(new StringReader(""));
}
return this.reader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ void readEmptyInputStreamWorksAcrossRequests() throws IOException {
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());
Expand Down

0 comments on commit a0f07af

Please sign in to comment.