From c269d27bde63d3df2fed324ee7009d4dc63e6411 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 6 Jun 2014 11:25:15 -0400 Subject: [PATCH] Improve no content handling in MockHttpServletRequest Issue: SPR-11764 --- .../mock/web/MockHttpServletRequest.java | 7 +++++-- .../mock/web/MockHttpServletRequestTests.java | 21 ++++++++++++++++++- .../mock/web/test/MockHttpServletRequest.java | 5 ++++- 3 files changed, 29 insertions(+), 4 deletions(-) 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 8e1384c09e..fc0199b036 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,6 +104,9 @@ public class MockHttpServletRequest implements HttpServletRequest { private static final String CHARSET_PREFIX = "charset="; + private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM = + new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])); + private boolean active = true; @@ -375,7 +378,7 @@ public class MockHttpServletRequest implements HttpServletRequest { return new DelegatingServletInputStream(new ByteArrayInputStream(this.content)); } else { - return null; + return EMPTY_SERVLET_INPUT_STREAM; } } 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 35900fa6be..c6cfe46c16 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.mock.web; +import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -26,6 +28,7 @@ import java.util.Locale; import java.util.Map; import org.junit.Test; +import org.springframework.util.StreamUtils; import static org.junit.Assert.*; @@ -42,6 +45,22 @@ public class MockHttpServletRequestTests { private MockHttpServletRequest request = new MockHttpServletRequest(); + @Test + public void content() throws IOException { + byte[] bytes = "body".getBytes(Charset.defaultCharset()); + request.setContent(bytes); + assertEquals(bytes.length, request.getContentLength()); + assertNotNull(request.getInputStream()); + assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())); + } + + @Test + public void noContent() throws IOException { + assertEquals(-1, request.getContentLength()); + assertNotNull(request.getInputStream()); + assertEquals(-1, request.getInputStream().read()); + } + @Test public void setContentType() { String contentType = "test/plain"; diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 997af6e09b..44e517f7d5 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -107,6 +107,9 @@ public class MockHttpServletRequest implements HttpServletRequest { private static final String CHARSET_PREFIX = "charset="; + private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM = + new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])); + private boolean active = true; @@ -378,7 +381,7 @@ public class MockHttpServletRequest implements HttpServletRequest { return new DelegatingServletInputStream(new ByteArrayInputStream(this.content)); } else { - return null; + return EMPTY_SERVLET_INPUT_STREAM; } }