Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Nullable
private byte[] content;

@Nullable
private ServletInputStream inputStream;

@Nullable
private BufferedReader reader;

@Nullable
private String contentType;

Expand Down Expand Up @@ -492,12 +498,17 @@ public String getContentType() {

@Override
public ServletInputStream getInputStream() {
if (this.inputStream != null) {
return this.inputStream;
}

if (this.content != null) {
return new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
this.inputStream = new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
}
else {
return EMPTY_SERVLET_INPUT_STREAM;
this.inputStream = EMPTY_SERVLET_INPUT_STREAM;
}
return this.inputStream;
}

/**
Expand Down Expand Up @@ -697,16 +708,21 @@ public int getServerPort() {

@Override
public BufferedReader getReader() throws UnsupportedEncodingException {
if (this.reader != null) {
return this.reader;
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ public void noContent() throws IOException {
assertNull(request.getContentAsByteArray());
}

@Test
public void getReaderTwice() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());
request.setContent(bytes);
assertSame(
request.getReader(),
request.getReader());
}

@Test
public void getInputStreamTwice() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());
request.setContent(bytes);
assertSame(
request.getInputStream(),
request.getInputStream());
}

@Test
public void setContentType() {
String contentType = "test/plain";
Expand Down