提交 c1a4f5c0 编写于 作者: J Juergen Hoeller 提交者: unknown

MockHttpServletRequest's getParameter(Values) returns null for null parameter name

Issue: SPR-10192
上级 692ced80
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
......@@ -35,7 +35,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
......@@ -97,8 +96,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
private static final String CHARSET_PREFIX = "charset=";
private boolean active = true;
// ---------------------------------------------------------------------
// ServletRequest properties
// ---------------------------------------------------------------------
......@@ -140,6 +141,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
private int localPort = DEFAULT_SERVER_PORT;
// ---------------------------------------------------------------------
// HttpServletRequest properties
// ---------------------------------------------------------------------
......@@ -235,6 +237,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.locales.add(Locale.ENGLISH);
}
// ---------------------------------------------------------------------
// Lifecycle methods
// ---------------------------------------------------------------------
......@@ -279,6 +282,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
// ---------------------------------------------------------------------
// ServletRequest interface
// ---------------------------------------------------------------------
......@@ -351,7 +355,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* parameter name, they will be replaced.
*/
public void setParameter(String name, String value) {
setParameter(name, new String[] { value });
setParameter(name, new String[] {value});
}
/**
......@@ -373,7 +377,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setParameters(Map params) {
Assert.notNull(params, "Parameter map must not be null");
for (Object key : params.keySet()) {
Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
Assert.isInstanceOf(String.class, key,
"Parameter map key must be of type [" + String.class.getName() + "]");
Object value = params.get(key);
if (value instanceof String) {
this.setParameter((String) key, (String) value);
......@@ -382,8 +387,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.setParameter((String) key, (String[]) value);
}
else {
throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
+ String.class.getName() + "]");
throw new IllegalArgumentException(
"Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
}
}
}
......@@ -394,7 +399,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* parameter name, the given value will be added to the end of the list.
*/
public void addParameter(String name, String value) {
addParameter(name, new String[] { value });
addParameter(name, new String[] {value});
}
/**
......@@ -425,7 +430,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void addParameters(Map params) {
Assert.notNull(params, "Parameter map must not be null");
for (Object key : params.keySet()) {
Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
Assert.isInstanceOf(String.class, key,
"Parameter map key must be of type [" + String.class.getName() + "]");
Object value = params.get(key);
if (value instanceof String) {
this.addParameter((String) key, (String) value);
......@@ -434,8 +440,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.addParameter((String) key, (String[]) value);
}
else {
throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
+ String.class.getName() + "]");
throw new IllegalArgumentException("Parameter map value must be single value " +
" or array of type [" + String.class.getName() + "]");
}
}
}
......@@ -456,8 +462,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public String getParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
String[] arr = this.parameters.get(name);
String[] arr = (name != null ? this.parameters.get(name) : null);
return (arr != null && arr.length > 0 ? arr[0] : null);
}
......@@ -466,8 +471,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public String[] getParameterValues(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.parameters.get(name);
return (name != null ? this.parameters.get(name) : null);
}
public Map<String, String[]> getParameterMap() {
......@@ -509,8 +513,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
public BufferedReader getReader() throws UnsupportedEncodingException {
if (this.content != null) {
InputStream sourceStream = new ByteArrayInputStream(this.content);
Reader sourceReader = (this.characterEncoding != null) ? new InputStreamReader(sourceStream,
this.characterEncoding) : new InputStreamReader(sourceStream);
Reader sourceReader = (this.characterEncoding != null) ?
new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream);
return new BufferedReader(sourceReader);
}
else {
......@@ -574,7 +578,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @since 3.2
*/
public void setPreferredLocales(List<Locale> locales) {
Assert.notEmpty(locales, "preferred locales list must not be empty");
Assert.notEmpty(locales, "Locale list must not be empty");
this.locales.clear();
this.locales.addAll(locales);
}
......@@ -635,6 +639,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
return this.localPort;
}
// ---------------------------------------------------------------------
// HttpServletRequest interface
// ---------------------------------------------------------------------
......@@ -797,8 +802,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
public boolean isUserInRole(String role) {
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains(
role)));
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
}
public void setUserPrincipal(Principal userPrincipal) {
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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,8 +16,6 @@
package org.springframework.mock.web;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
......@@ -29,6 +27,8 @@ import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Unit tests for {@link MockHttpServletRequest}.
*
......@@ -105,6 +105,12 @@ public class MockHttpServletRequestTests {
assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement());
}
@Test
public void nullParameterName() {
assertNull(request.getParameter(null));
assertNull(request.getParameterValues(null));
}
@Test
public void setMultipleParameters() {
request.setParameter("key1", "value1");
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
......@@ -36,7 +36,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
......@@ -110,9 +109,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
private boolean active = true;
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ServletRequest properties
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
......@@ -151,11 +150,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
private int localPort = DEFAULT_SERVER_PORT;
private Map<String, Part> parts = new HashMap<String, Part>();
private final Map<String, Part> parts = new HashMap<String, Part>();
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// HttpServletRequest properties
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
private String authType;
......@@ -200,9 +200,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
private DispatcherType dispatcherType = DispatcherType.REQUEST;
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Constructors
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
/**
* Create a new {@code MockHttpServletRequest} with a default
......@@ -256,9 +256,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
this.locales.add(Locale.ENGLISH);
}
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Lifecycle methods
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
/**
* Return the ServletContext that this request is associated with. (Not
......@@ -302,9 +303,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ServletRequest interface
//---------------------------------------------------------------------
// ---------------------------------------------------------------------
@Override
public Object getAttribute(String name) {
......@@ -414,8 +415,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
else {
throw new IllegalArgumentException(
"Parameter map value must be single value " + " or array of type [" + String.class.getName() +
"]");
"Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
}
}
}
......@@ -490,8 +490,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String getParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
String[] arr = this.parameters.get(name);
String[] arr = (name != null ? this.parameters.get(name) : null);
return (arr != null && arr.length > 0 ? arr[0] : null);
}
......@@ -502,8 +501,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String[] getParameterValues(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.parameters.get(name);
return (name != null ? this.parameters.get(name) : null);
}
@Override
......@@ -620,7 +618,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* @since 3.2
*/
public void setPreferredLocales(List<Locale> locales) {
Assert.notEmpty(locales, "preferred locales list must not be empty");
Assert.notEmpty(locales, "Locale list must not be empty");
this.locales.clear();
this.locales.addAll(locales);
}
......@@ -779,7 +777,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public String getHeader(String name) {
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
return (header != null ? header.getValue().toString() : null);
return (header != null ? header.getStringValue() : null);
}
@Override
......@@ -867,8 +865,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
@Override
public boolean isUserInRole(String role) {
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains(
role)));
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
}
public void setUserPrincipal(Principal userPrincipal) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册