MockHttpServletRequest returns a single InputStream or Reader
Issue: SPR-16505 Issue: SPR-16499
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -52,7 +52,7 @@ public class ObjectToStringHttpMessageConverterTests {
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setup() {
|
||||
ConversionService conversionService = new DefaultConversionService();
|
||||
this.converter = new ObjectToStringHttpMessageConverter(conversionService);
|
||||
|
||||
@@ -60,6 +60,7 @@ public class ObjectToStringHttpMessageConverterTests {
|
||||
this.response = new ServletServerHttpResponse(this.servletResponse);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void canRead() {
|
||||
assertFalse(this.converter.canRead(Math.class, null));
|
||||
@@ -121,20 +122,22 @@ public class ObjectToStringHttpMessageConverterTests {
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
Short shortValue = Short.valueOf((short) 781);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
|
||||
Short shortValue = Short.valueOf((short) 781);
|
||||
request.setContent(shortValue.toString().getBytes(
|
||||
StringHttpMessageConverter.DEFAULT_CHARSET));
|
||||
request.setContent(shortValue.toString().getBytes(StringHttpMessageConverter.DEFAULT_CHARSET));
|
||||
assertEquals(shortValue, this.converter.read(Short.class, new ServletServerHttpRequest(request)));
|
||||
|
||||
Float floatValue = Float.valueOf(123);
|
||||
request = new MockHttpServletRequest();
|
||||
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
request.setCharacterEncoding("UTF-16");
|
||||
request.setContent(floatValue.toString().getBytes("UTF-16"));
|
||||
assertEquals(floatValue, this.converter.read(Float.class, new ServletServerHttpRequest(request)));
|
||||
|
||||
Long longValue = Long.valueOf(55819182821331L);
|
||||
request = new MockHttpServletRequest();
|
||||
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
request.setCharacterEncoding("UTF-8");
|
||||
request.setContent(longValue.toString().getBytes("UTF-8"));
|
||||
assertEquals(longValue, this.converter.read(Long.class, new ServletServerHttpRequest(request)));
|
||||
|
||||
@@ -174,6 +174,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private String contentType;
|
||||
|
||||
private ServletInputStream inputStream;
|
||||
|
||||
private BufferedReader reader;
|
||||
|
||||
private final Map<String, String[]> parameters = new LinkedHashMap<>(16);
|
||||
|
||||
private String protocol = DEFAULT_PROTOCOL;
|
||||
@@ -473,12 +477,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -507,8 +517,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
*/
|
||||
public void setParameters(Map<String, ?> params) {
|
||||
Assert.notNull(params, "Parameter map must not be null");
|
||||
for (String key : params.keySet()) {
|
||||
Object value = params.get(key);
|
||||
params.forEach((key, value) -> {
|
||||
if (value instanceof String) {
|
||||
setParameter(key, (String) value);
|
||||
}
|
||||
@@ -519,7 +528,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
throw new IllegalArgumentException(
|
||||
"Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -557,8 +566,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
*/
|
||||
public void addParameters(Map<String, ?> params) {
|
||||
Assert.notNull(params, "Parameter map must not be null");
|
||||
for (String key : params.keySet()) {
|
||||
Object value = params.get(key);
|
||||
params.forEach((key, value) -> {
|
||||
if (value instanceof String) {
|
||||
addParameter(key, (String) value);
|
||||
}
|
||||
@@ -569,7 +577,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
throw new IllegalArgumentException("Parameter map value must be single value " +
|
||||
" or array of type [" + String.class.getName() + "]");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -677,16 +685,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) {
|
||||
|
||||
@@ -50,7 +50,6 @@ public class FormContentFilterTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.request = new MockHttpServletRequest("PUT", "/");
|
||||
this.request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
|
||||
this.request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.filterChain = new MockFilterChain();
|
||||
@@ -59,29 +58,31 @@ public class FormContentFilterTests {
|
||||
|
||||
@Test
|
||||
public void wrapPutPatchAndDeleteOnly() throws Exception {
|
||||
this.request.setContent("foo=bar".getBytes("ISO-8859-1"));
|
||||
for (HttpMethod method : HttpMethod.values()) {
|
||||
this.request.setMethod(method.name());
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
|
||||
request.setContent("foo=bar".getBytes("ISO-8859-1"));
|
||||
request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
|
||||
this.filterChain = new MockFilterChain();
|
||||
this.filter.doFilter(this.request, this.response, this.filterChain);
|
||||
this.filter.doFilter(request, this.response, this.filterChain);
|
||||
if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
|
||||
assertNotSame(this.request, this.filterChain.getRequest());
|
||||
assertNotSame(request, this.filterChain.getRequest());
|
||||
}
|
||||
else {
|
||||
assertSame(this.request, this.filterChain.getRequest());
|
||||
assertSame(request, this.filterChain.getRequest());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapFormEncodedOnly() throws Exception {
|
||||
this.request.setContent("".getBytes("ISO-8859-1"));
|
||||
String[] contentTypes = new String[] {"text/plain", "multipart/form-data"};
|
||||
for (String contentType : contentTypes) {
|
||||
this.request.setContentType(contentType);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/");
|
||||
request.setContent("".getBytes("ISO-8859-1"));
|
||||
request.setContentType(contentType);
|
||||
this.filterChain = new MockFilterChain();
|
||||
this.filter.doFilter(this.request, this.response, this.filterChain);
|
||||
assertSame(this.request, this.filterChain.getRequest());
|
||||
this.filter.doFilter(request, this.response, this.filterChain);
|
||||
assertSame(request, this.filterChain.getRequest());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +147,7 @@ public class FormContentFilterTests {
|
||||
String[] values = this.filterChain.getRequest().getParameterValues("name");
|
||||
|
||||
assertNotSame("Request not wrapped", this.request, filterChain.getRequest());
|
||||
assertArrayEquals(new String[]{"value1", "value2", "value3", "value4"}, values);
|
||||
assertArrayEquals(new String[] {"value1", "value2", "value3", "value4"}, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -160,7 +161,7 @@ public class FormContentFilterTests {
|
||||
String[] values = this.filterChain.getRequest().getParameterValues("name");
|
||||
|
||||
assertNotSame("Request not wrapped", this.request, this.filterChain.getRequest());
|
||||
assertArrayEquals(new String[]{"value1", "value2"}, values);
|
||||
assertArrayEquals(new String[] {"value1", "value2"}, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,7 +174,7 @@ public class FormContentFilterTests {
|
||||
String[] values = this.filterChain.getRequest().getParameterValues("anotherName");
|
||||
|
||||
assertNotSame("Request not wrapped", this.request, this.filterChain.getRequest());
|
||||
assertArrayEquals(new String[]{"anotherValue"}, values);
|
||||
assertArrayEquals(new String[] {"anotherValue"}, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -211,7 +212,7 @@ public class FormContentFilterTests {
|
||||
this.request.addParameter("hiddenField", "testHidden");
|
||||
this.filter.doFilter(this.request, this.response, this.filterChain);
|
||||
|
||||
assertArrayEquals(new String[]{"testHidden"},
|
||||
assertArrayEquals(new String[] {"testHidden"},
|
||||
this.filterChain.getRequest().getParameterValues("hiddenField"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user