diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index f9eaf0208bbb..ed1bf185be6d 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -175,6 +175,9 @@ public class MockHttpServletRequest implements HttpServletRequest { @Nullable private byte[] content; + @Nullable + private ContentExtractMethod contentExtractMethod; + @Nullable private String contentType; @@ -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)); } @@ -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) ? @@ -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; } @@ -1345,4 +1363,15 @@ public T upgrade(Class handlerClass) throws IO throw new UnsupportedOperationException(); } + + private enum ContentExtractMethod { + READER("getReader"), INPUT_STREAM("getInputStream"); + + final String methodName; + + ContentExtractMethod(String methodName) { + this.methodName = methodName; + } + } + } diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index f3cc3a7a55d5..1dbf0e676053 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -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";