MockHttpServletRequest returns a single InputStream or Reader

Issue: SPR-16505
Issue: SPR-16499
This commit is contained in:
Juergen Hoeller
2018-06-13 22:03:16 +02:00
parent 9aed9bf823
commit 3fc8ec498c
5 changed files with 113 additions and 38 deletions

View File

@@ -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) {