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-32820
This commit is contained in:
parent
617833bec9
commit
f2605fd9e5
|
@ -100,9 +100,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
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.
|
* 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>
|
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
|
||||||
|
@ -738,7 +735,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
this.reader = new BufferedReader(sourceReader);
|
this.reader = new BufferedReader(sourceReader);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.reader = EMPTY_BUFFERED_READER;
|
this.reader = new BufferedReader(new StringReader(""));
|
||||||
}
|
}
|
||||||
return this.reader;
|
return this.reader;
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,17 @@ class MockHttpServletRequestTests {
|
||||||
secondRequest.getInputStream().close();
|
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
|
@Test
|
||||||
void setContentAndGetReader() throws IOException {
|
void setContentAndGetReader() throws IOException {
|
||||||
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
||||||
|
|
Loading…
Reference in New Issue