Skip to content

Commit 02d727f

Browse files
committed
MockHttpServletRequestBuilder supports multiple locales
Includes revised content type handling. Issue: SPR-15116
1 parent d0e9328 commit 02d727f

File tree

4 files changed

+277
-311
lines changed

4 files changed

+277
-311
lines changed

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -422,6 +422,7 @@ public int getContentLength() {
422422
return (this.content != null ? this.content.length : -1);
423423
}
424424

425+
@Override
425426
public long getContentLengthLong() {
426427
return getContentLength();
427428
}
@@ -475,28 +476,25 @@ public void setParameter(String name, String value) {
475476
* <p>If there are already one or more values registered for the given
476477
* parameter name, they will be replaced.
477478
*/
478-
public void setParameter(String name, String[] values) {
479+
public void setParameter(String name, String... values) {
479480
Assert.notNull(name, "Parameter name must not be null");
480481
this.parameters.put(name, values);
481482
}
482483

483484
/**
484-
* Sets all provided parameters <strong>replacing</strong> any existing
485+
* Set all provided parameters <strong>replacing</strong> any existing
485486
* values for the provided parameter names. To add without replacing
486487
* existing values, use {@link #addParameters(java.util.Map)}.
487488
*/
488-
@SuppressWarnings("rawtypes")
489-
public void setParameters(Map params) {
489+
public void setParameters(Map<String, ?> params) {
490490
Assert.notNull(params, "Parameter map must not be null");
491-
for (Object key : params.keySet()) {
492-
Assert.isInstanceOf(String.class, key,
493-
"Parameter map key must be of type [" + String.class.getName() + "]");
491+
for (String key : params.keySet()) {
494492
Object value = params.get(key);
495493
if (value instanceof String) {
496-
this.setParameter((String) key, (String) value);
494+
setParameter(key, (String) value);
497495
}
498496
else if (value instanceof String[]) {
499-
this.setParameter((String) key, (String[]) value);
497+
setParameter(key, (String[]) value);
500498
}
501499
else {
502500
throw new IllegalArgumentException(
@@ -519,7 +517,7 @@ public void addParameter(String name, String value) {
519517
* <p>If there are already one or more values registered for the given
520518
* parameter name, the given values will be added to the end of the list.
521519
*/
522-
public void addParameter(String name, String[] values) {
520+
public void addParameter(String name, String... values) {
523521
Assert.notNull(name, "Parameter name must not be null");
524522
String[] oldArr = this.parameters.get(name);
525523
if (oldArr != null) {
@@ -538,18 +536,15 @@ public void addParameter(String name, String[] values) {
538536
* existing values. To replace existing values, use
539537
* {@link #setParameters(java.util.Map)}.
540538
*/
541-
@SuppressWarnings("rawtypes")
542-
public void addParameters(Map params) {
539+
public void addParameters(Map<String, ?> params) {
543540
Assert.notNull(params, "Parameter map must not be null");
544-
for (Object key : params.keySet()) {
545-
Assert.isInstanceOf(String.class, key,
546-
"Parameter map key must be of type [" + String.class.getName() + "]");
541+
for (String key : params.keySet()) {
547542
Object value = params.get(key);
548543
if (value instanceof String) {
549-
this.addParameter((String) key, (String) value);
544+
this.addParameter(key, (String) value);
550545
}
551546
else if (value instanceof String[]) {
552-
this.addParameter((String) key, (String[]) value);
547+
this.addParameter(key, (String[]) value);
553548
}
554549
else {
555550
throw new IllegalArgumentException("Parameter map value must be single value " +
@@ -929,14 +924,14 @@ public Cookie[] getCookies() {
929924
* @see #getDateHeader
930925
*/
931926
public void addHeader(String name, Object value) {
932-
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
933-
setContentType((String) value);
934-
return;
927+
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name) && !this.headers.containsKey(CONTENT_TYPE_HEADER)) {
928+
setContentType(value.toString());
929+
}
930+
else {
931+
doAddHeaderValue(name, value, false);
935932
}
936-
doAddHeaderValue(name, value, false);
937933
}
938934

939-
@SuppressWarnings("rawtypes")
940935
private void doAddHeaderValue(String name, Object value, boolean replace) {
941936
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
942937
Assert.notNull(value, "Header value must not be null");

0 commit comments

Comments
 (0)