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,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Nullable
private byte[] content;

@Nullable
private ContentExtractMethod contentExtractMethod;

@Nullable
private String contentType;

Expand Down Expand Up @@ -492,6 +495,8 @@ public String getContentType() {

@Override
public ServletInputStream getInputStream() {
demandContentExtractMethod(ContentExtractMethod.INPUT_STREAM);

if (this.content != null) {
return new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
}
Expand Down Expand Up @@ -697,6 +702,8 @@ public int getServerPort() {

@Override
public BufferedReader getReader() throws UnsupportedEncodingException {
demandContentExtractMethod(ContentExtractMethod.READER);

if (this.content != null) {
InputStream sourceStream = new ByteArrayInputStream(this.content);
Reader sourceReader = (this.characterEncoding != null) ?
Expand All @@ -709,6 +716,17 @@ public BufferedReader getReader() throws UnsupportedEncodingException {
}
}

private void demandContentExtractMethod(ContentExtractMethod methodToDemand) {
if (this.contentExtractMethod != null &&
!this.contentExtractMethod.equals(methodToDemand)) {
throw new IllegalStateException(
"Cannot call " + methodToDemand.methodName + "()" +
" after " + this.contentExtractMethod.methodName + "()" +
" has already been called for the current request");
}
this.contentExtractMethod = methodToDemand;
}

public void setRemoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr;
}
Expand Down Expand Up @@ -1345,4 +1363,15 @@ public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IO
throw new UnsupportedOperationException();
}


private enum ContentExtractMethod {
READER("getReader"), INPUT_STREAM("getInputStream");

final String methodName;

ContentExtractMethod(String methodName) {
this.methodName = methodName;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ public void noContent() throws IOException {
assertNull(request.getContentAsByteArray());
}

@Test
public void getReaderAfterGettingInputStream() throws IOException {
exception.expect(IllegalStateException.class);
exception.expectMessage("Cannot call getReader() after" +
" getInputStream() has already been called for the current request");
try {
request.getInputStream();
}
catch (IllegalStateException e) {
fail("Call to getInputStream() failed unexpectedly.");
}
request.getReader();
}

@Test
public void getInputStreamAfterGettingReader() throws IOException {
exception.expect(IllegalStateException.class);
exception.expectMessage("Cannot call getInputStream() after" +
" getReader() has already been called for the current request");
try {
request.getReader();
}
catch (IllegalStateException e) {
fail("Call to getReader() failed unexpectedly.");
}
request.getInputStream();
}

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