MockHttpServletRequest returns a single InputStream or Reader
Issue: SPR-16505 Issue: SPR-16499
This commit is contained in:
@@ -178,6 +178,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
@Nullable
|
||||
private String contentType;
|
||||
|
||||
@Nullable
|
||||
private ServletInputStream inputStream;
|
||||
|
||||
@Nullable
|
||||
private BufferedReader reader;
|
||||
|
||||
private final Map<String, String[]> parameters = new LinkedHashMap<>(16);
|
||||
|
||||
private String protocol = DEFAULT_PROTOCOL;
|
||||
@@ -492,12 +498,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() {
|
||||
if (this.content != null) {
|
||||
return new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
|
||||
if (this.inputStream != null) {
|
||||
return this.inputStream;
|
||||
}
|
||||
else {
|
||||
return EMPTY_SERVLET_INPUT_STREAM;
|
||||
else if (this.reader != null) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot call getInputStream() after getReader() has already been called for the current request") ;
|
||||
}
|
||||
|
||||
this.inputStream = (this.content != null ?
|
||||
new DelegatingServletInputStream(new ByteArrayInputStream(this.content)) :
|
||||
EMPTY_SERVLET_INPUT_STREAM);
|
||||
return this.inputStream;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -695,16 +707,25 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws UnsupportedEncodingException {
|
||||
if (this.reader != null) {
|
||||
return this.reader;
|
||||
}
|
||||
else if (this.inputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot call getReader() after getInputStream() has already been called for the current request") ;
|
||||
}
|
||||
|
||||
if (this.content != null) {
|
||||
InputStream sourceStream = new ByteArrayInputStream(this.content);
|
||||
Reader sourceReader = (this.characterEncoding != null) ?
|
||||
new InputStreamReader(sourceStream, this.characterEncoding) :
|
||||
new InputStreamReader(sourceStream);
|
||||
return new BufferedReader(sourceReader);
|
||||
this.reader = new BufferedReader(sourceReader);
|
||||
}
|
||||
else {
|
||||
return EMPTY_BUFFERED_READER;
|
||||
this.reader = EMPTY_BUFFERED_READER;
|
||||
}
|
||||
return this.reader;
|
||||
}
|
||||
|
||||
public void setRemoteAddr(String remoteAddr) {
|
||||
|
||||
@@ -47,6 +47,7 @@ import static org.junit.Assert.*;
|
||||
* @author Sam Brannen
|
||||
* @author Brian Clozel
|
||||
* @author Jakub Narloch
|
||||
* @author Av Pinzur
|
||||
*/
|
||||
public class MockHttpServletRequestTests {
|
||||
|
||||
@@ -112,6 +113,38 @@ public class MockHttpServletRequestTests {
|
||||
assertNull(request.getContentAsByteArray());
|
||||
}
|
||||
|
||||
@Test // SPR-16505
|
||||
public void getReaderTwice() throws IOException {
|
||||
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
||||
request.setContent(bytes);
|
||||
assertSame(request.getReader(), request.getReader());
|
||||
}
|
||||
|
||||
@Test // SPR-16505
|
||||
public void getInputStreamTwice() throws IOException {
|
||||
byte[] bytes = "body".getBytes(Charset.defaultCharset());
|
||||
request.setContent(bytes);
|
||||
assertSame(request.getInputStream(), request.getInputStream());
|
||||
}
|
||||
|
||||
@Test // SPR-16499
|
||||
public void getReaderAfterGettingInputStream() throws IOException {
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage(
|
||||
"Cannot call getReader() after getInputStream() has already been called for the current request");
|
||||
request.getInputStream();
|
||||
request.getReader();
|
||||
}
|
||||
|
||||
@Test // SPR-16499
|
||||
public void getInputStreamAfterGettingReader() throws IOException {
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage(
|
||||
"Cannot call getInputStream() after getReader() has already been called for the current request");
|
||||
request.getReader();
|
||||
request.getInputStream();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setContentType() {
|
||||
String contentType = "test/plain";
|
||||
|
||||
Reference in New Issue
Block a user