Skip to content

Commit c1a4f5c

Browse files
jhoellerunknown
authored and
unknown
committed
MockHttpServletRequest's getParameter(Values) returns null for null parameter name
Issue: SPR-10192
1 parent 692ced8 commit c1a4f5c

File tree

3 files changed

+54
-45
lines changed

3 files changed

+54
-45
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@
3535
import java.util.Locale;
3636
import java.util.Map;
3737
import java.util.Set;
38-
3938
import javax.servlet.RequestDispatcher;
4039
import javax.servlet.ServletContext;
4140
import javax.servlet.ServletException;
@@ -97,8 +96,10 @@ public class MockHttpServletRequest implements HttpServletRequest {
9796

9897
private static final String CHARSET_PREFIX = "charset=";
9998

99+
100100
private boolean active = true;
101101

102+
102103
// ---------------------------------------------------------------------
103104
// ServletRequest properties
104105
// ---------------------------------------------------------------------
@@ -140,6 +141,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
140141

141142
private int localPort = DEFAULT_SERVER_PORT;
142143

144+
143145
// ---------------------------------------------------------------------
144146
// HttpServletRequest properties
145147
// ---------------------------------------------------------------------
@@ -235,6 +237,7 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
235237
this.locales.add(Locale.ENGLISH);
236238
}
237239

240+
238241
// ---------------------------------------------------------------------
239242
// Lifecycle methods
240243
// ---------------------------------------------------------------------
@@ -279,6 +282,7 @@ protected void checkActive() throws IllegalStateException {
279282
}
280283
}
281284

285+
282286
// ---------------------------------------------------------------------
283287
// ServletRequest interface
284288
// ---------------------------------------------------------------------
@@ -351,7 +355,7 @@ public ServletInputStream getInputStream() {
351355
* parameter name, they will be replaced.
352356
*/
353357
public void setParameter(String name, String value) {
354-
setParameter(name, new String[] { value });
358+
setParameter(name, new String[] {value});
355359
}
356360

357361
/**
@@ -373,7 +377,8 @@ public void setParameter(String name, String[] values) {
373377
public void setParameters(Map params) {
374378
Assert.notNull(params, "Parameter map must not be null");
375379
for (Object key : params.keySet()) {
376-
Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
380+
Assert.isInstanceOf(String.class, key,
381+
"Parameter map key must be of type [" + String.class.getName() + "]");
377382
Object value = params.get(key);
378383
if (value instanceof String) {
379384
this.setParameter((String) key, (String) value);
@@ -382,8 +387,8 @@ else if (value instanceof String[]) {
382387
this.setParameter((String) key, (String[]) value);
383388
}
384389
else {
385-
throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
386-
+ String.class.getName() + "]");
390+
throw new IllegalArgumentException(
391+
"Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
387392
}
388393
}
389394
}
@@ -394,7 +399,7 @@ else if (value instanceof String[]) {
394399
* parameter name, the given value will be added to the end of the list.
395400
*/
396401
public void addParameter(String name, String value) {
397-
addParameter(name, new String[] { value });
402+
addParameter(name, new String[] {value});
398403
}
399404

400405
/**
@@ -425,7 +430,8 @@ public void addParameter(String name, String[] values) {
425430
public void addParameters(Map params) {
426431
Assert.notNull(params, "Parameter map must not be null");
427432
for (Object key : params.keySet()) {
428-
Assert.isInstanceOf(String.class, key, "Parameter map key must be of type [" + String.class.getName() + "]");
433+
Assert.isInstanceOf(String.class, key,
434+
"Parameter map key must be of type [" + String.class.getName() + "]");
429435
Object value = params.get(key);
430436
if (value instanceof String) {
431437
this.addParameter((String) key, (String) value);
@@ -434,8 +440,8 @@ else if (value instanceof String[]) {
434440
this.addParameter((String) key, (String[]) value);
435441
}
436442
else {
437-
throw new IllegalArgumentException("Parameter map value must be single value " + " or array of type ["
438-
+ String.class.getName() + "]");
443+
throw new IllegalArgumentException("Parameter map value must be single value " +
444+
" or array of type [" + String.class.getName() + "]");
439445
}
440446
}
441447
}
@@ -456,8 +462,7 @@ public void removeAllParameters() {
456462
}
457463

458464
public String getParameter(String name) {
459-
Assert.notNull(name, "Parameter name must not be null");
460-
String[] arr = this.parameters.get(name);
465+
String[] arr = (name != null ? this.parameters.get(name) : null);
461466
return (arr != null && arr.length > 0 ? arr[0] : null);
462467
}
463468

@@ -466,8 +471,7 @@ public Enumeration<String> getParameterNames() {
466471
}
467472

468473
public String[] getParameterValues(String name) {
469-
Assert.notNull(name, "Parameter name must not be null");
470-
return this.parameters.get(name);
474+
return (name != null ? this.parameters.get(name) : null);
471475
}
472476

473477
public Map<String, String[]> getParameterMap() {
@@ -509,8 +513,8 @@ public int getServerPort() {
509513
public BufferedReader getReader() throws UnsupportedEncodingException {
510514
if (this.content != null) {
511515
InputStream sourceStream = new ByteArrayInputStream(this.content);
512-
Reader sourceReader = (this.characterEncoding != null) ? new InputStreamReader(sourceStream,
513-
this.characterEncoding) : new InputStreamReader(sourceStream);
516+
Reader sourceReader = (this.characterEncoding != null) ?
517+
new InputStreamReader(sourceStream, this.characterEncoding) : new InputStreamReader(sourceStream);
514518
return new BufferedReader(sourceReader);
515519
}
516520
else {
@@ -574,7 +578,7 @@ public void addPreferredLocale(Locale locale) {
574578
* @since 3.2
575579
*/
576580
public void setPreferredLocales(List<Locale> locales) {
577-
Assert.notEmpty(locales, "preferred locales list must not be empty");
581+
Assert.notEmpty(locales, "Locale list must not be empty");
578582
this.locales.clear();
579583
this.locales.addAll(locales);
580584
}
@@ -635,6 +639,7 @@ public int getLocalPort() {
635639
return this.localPort;
636640
}
637641

642+
638643
// ---------------------------------------------------------------------
639644
// HttpServletRequest interface
640645
// ---------------------------------------------------------------------
@@ -797,8 +802,8 @@ public void addUserRole(String role) {
797802
}
798803

799804
public boolean isUserInRole(String role) {
800-
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains(
801-
role)));
805+
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
806+
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
802807
}
803808

804809
public void setUserPrincipal(Principal userPrincipal) {

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.mock.web;
1818

19-
import static org.junit.Assert.*;
20-
2119
import java.util.ArrayList;
2220
import java.util.Arrays;
2321
import java.util.Collections;
@@ -29,6 +27,8 @@
2927

3028
import org.junit.Test;
3129

30+
import static org.junit.Assert.*;
31+
3232
/**
3333
* Unit tests for {@link MockHttpServletRequest}.
3434
*
@@ -105,6 +105,12 @@ public void httpHeaderNameCasingIsPreserved() throws Exception {
105105
assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement());
106106
}
107107

108+
@Test
109+
public void nullParameterName() {
110+
assertNull(request.getParameter(null));
111+
assertNull(request.getParameterValues(null));
112+
}
113+
108114
@Test
109115
public void setMultipleParameters() {
110116
request.setParameter("key1", "value1");

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,6 @@
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.Set;
39-
4039
import javax.servlet.AsyncContext;
4140
import javax.servlet.DispatcherType;
4241
import javax.servlet.RequestDispatcher;
@@ -110,9 +109,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
110109
private boolean active = true;
111110

112111

113-
//---------------------------------------------------------------------
112+
// ---------------------------------------------------------------------
114113
// ServletRequest properties
115-
//---------------------------------------------------------------------
114+
// ---------------------------------------------------------------------
116115

117116
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
118117

@@ -151,11 +150,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
151150

152151
private int localPort = DEFAULT_SERVER_PORT;
153152

154-
private Map<String, Part> parts = new HashMap<String, Part>();
153+
private final Map<String, Part> parts = new HashMap<String, Part>();
155154

156-
//---------------------------------------------------------------------
155+
156+
// ---------------------------------------------------------------------
157157
// HttpServletRequest properties
158-
//---------------------------------------------------------------------
158+
// ---------------------------------------------------------------------
159159

160160
private String authType;
161161

@@ -200,9 +200,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
200200
private DispatcherType dispatcherType = DispatcherType.REQUEST;
201201

202202

203-
//---------------------------------------------------------------------
203+
// ---------------------------------------------------------------------
204204
// Constructors
205-
//---------------------------------------------------------------------
205+
// ---------------------------------------------------------------------
206206

207207
/**
208208
* Create a new {@code MockHttpServletRequest} with a default
@@ -256,9 +256,10 @@ public MockHttpServletRequest(ServletContext servletContext, String method, Stri
256256
this.locales.add(Locale.ENGLISH);
257257
}
258258

259-
//---------------------------------------------------------------------
259+
260+
// ---------------------------------------------------------------------
260261
// Lifecycle methods
261-
//---------------------------------------------------------------------
262+
// ---------------------------------------------------------------------
262263

263264
/**
264265
* Return the ServletContext that this request is associated with. (Not
@@ -302,9 +303,9 @@ protected void checkActive() throws IllegalStateException {
302303
}
303304

304305

305-
//---------------------------------------------------------------------
306+
// ---------------------------------------------------------------------
306307
// ServletRequest interface
307-
//---------------------------------------------------------------------
308+
// ---------------------------------------------------------------------
308309

309310
@Override
310311
public Object getAttribute(String name) {
@@ -414,8 +415,7 @@ else if (value instanceof String[]) {
414415
}
415416
else {
416417
throw new IllegalArgumentException(
417-
"Parameter map value must be single value " + " or array of type [" + String.class.getName() +
418-
"]");
418+
"Parameter map value must be single value " + " or array of type [" + String.class.getName() + "]");
419419
}
420420
}
421421
}
@@ -490,8 +490,7 @@ public void removeAllParameters() {
490490

491491
@Override
492492
public String getParameter(String name) {
493-
Assert.notNull(name, "Parameter name must not be null");
494-
String[] arr = this.parameters.get(name);
493+
String[] arr = (name != null ? this.parameters.get(name) : null);
495494
return (arr != null && arr.length > 0 ? arr[0] : null);
496495
}
497496

@@ -502,8 +501,7 @@ public Enumeration<String> getParameterNames() {
502501

503502
@Override
504503
public String[] getParameterValues(String name) {
505-
Assert.notNull(name, "Parameter name must not be null");
506-
return this.parameters.get(name);
504+
return (name != null ? this.parameters.get(name) : null);
507505
}
508506

509507
@Override
@@ -620,7 +618,7 @@ public void addPreferredLocale(Locale locale) {
620618
* @since 3.2
621619
*/
622620
public void setPreferredLocales(List<Locale> locales) {
623-
Assert.notEmpty(locales, "preferred locales list must not be empty");
621+
Assert.notEmpty(locales, "Locale list must not be empty");
624622
this.locales.clear();
625623
this.locales.addAll(locales);
626624
}
@@ -779,7 +777,7 @@ else if (value != null) {
779777
@Override
780778
public String getHeader(String name) {
781779
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
782-
return (header != null ? header.getValue().toString() : null);
780+
return (header != null ? header.getStringValue() : null);
783781
}
784782

785783
@Override
@@ -867,8 +865,8 @@ public void addUserRole(String role) {
867865

868866
@Override
869867
public boolean isUserInRole(String role) {
870-
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext && ((MockServletContext) this.servletContext).getDeclaredRoles().contains(
871-
role)));
868+
return (this.userRoles.contains(role) || (this.servletContext instanceof MockServletContext &&
869+
((MockServletContext) this.servletContext).getDeclaredRoles().contains(role)));
872870
}
873871

874872
public void setUserPrincipal(Principal userPrincipal) {

0 commit comments

Comments
 (0)