Skip to content

Commit a21ce42

Browse files
committed
MockHttpServletRequest allows for removing registered header entries
Issue: SPR-17295
1 parent e54eb56 commit a21ce42

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
102102
new BufferedReader(new StringReader(""));
103103

104104
/**
105-
* Date formats as specified in the HTTP RFC
105+
* Date formats as specified in the HTTP RFC.
106106
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
107107
*/
108108
private static final String[] DATE_FORMATS = new String[] {
@@ -569,7 +569,7 @@ public void addParameter(String name, String... values) {
569569
}
570570

571571
/**
572-
* Adds all provided parameters <strong>without</strong> replacing any
572+
* Add all provided parameters <strong>without</strong> replacing any
573573
* existing values. To replace existing values, use
574574
* {@link #setParameters(java.util.Map)}.
575575
*/
@@ -598,7 +598,7 @@ public void removeParameter(String name) {
598598
}
599599

600600
/**
601-
* Removes all existing parameters.
601+
* Remove all existing parameters.
602602
*/
603603
public void removeAllParameters() {
604604
this.parameters.clear();
@@ -764,8 +764,8 @@ public void addPreferredLocale(Locale locale) {
764764
/**
765765
* Set the list of preferred locales, in descending order, effectively replacing
766766
* any existing locales.
767-
* @see #addPreferredLocale
768767
* @since 3.2
768+
* @see #addPreferredLocale
769769
*/
770770
public void setPreferredLocales(List<Locale> locales) {
771771
Assert.notEmpty(locales, "Locale list must not be empty");
@@ -966,9 +966,9 @@ public Cookie[] getCookies() {
966966
}
967967

968968
/**
969-
* Add a header entry for the given name.
970-
* <p>While this method can take any {@code Object} as a parameter, it
971-
* is recommended to use the following types:
969+
* Add an HTTP header entry for the given name.
970+
* <p>While this method can take any {@code Object} as a parameter,
971+
* it is recommended to use the following types:
972972
* <ul>
973973
* <li>String or any Object to be converted using {@code toString()}; see {@link #getHeader}.</li>
974974
* <li>String, Number, or Date for date headers; see {@link #getDateHeader}.</li>
@@ -1020,6 +1020,15 @@ else if (value.getClass().isArray()) {
10201020
}
10211021
}
10221022

1023+
/**
1024+
* Remove already registered entries for the specified HTTP header, if any.
1025+
* @since 4.3.20
1026+
*/
1027+
public void removeHeader(String name) {
1028+
Assert.notNull(name, "Header name must not be null");
1029+
this.headers.remove(name);
1030+
}
1031+
10231032
/**
10241033
* Return the long timestamp for the date header with the given {@code name}.
10251034
* <p>If the internal value representation is a String, this method will try
@@ -1264,7 +1273,7 @@ public HttpSession getSession() {
12641273
public String changeSessionId() {
12651274
Assert.isTrue(this.session != null, "The request does not have a session");
12661275
if (this.session instanceof MockHttpSession) {
1267-
return ((MockHttpSession) session).changeSessionId();
1276+
return ((MockHttpSession) this.session).changeSessionId();
12681277
}
12691278
return this.session.getId();
12701279
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
101101
new BufferedReader(new StringReader(""));
102102

103103
/**
104-
* Date formats as specified in the HTTP RFC
104+
* Date formats as specified in the HTTP RFC.
105105
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
106106
*/
107107
private static final String[] DATE_FORMATS = new String[] {
@@ -551,7 +551,7 @@ public void addParameter(String name, String... values) {
551551
}
552552

553553
/**
554-
* Adds all provided parameters <strong>without</strong> replacing any
554+
* Add all provided parameters <strong>without</strong> replacing any
555555
* existing values. To replace existing values, use
556556
* {@link #setParameters(java.util.Map)}.
557557
*/
@@ -581,7 +581,7 @@ public void removeParameter(String name) {
581581
}
582582

583583
/**
584-
* Removes all existing parameters.
584+
* Remove all existing parameters.
585585
*/
586586
public void removeAllParameters() {
587587
this.parameters.clear();
@@ -746,8 +746,8 @@ public void addPreferredLocale(Locale locale) {
746746
/**
747747
* Set the list of preferred locales, in descending order, effectively replacing
748748
* any existing locales.
749-
* @see #addPreferredLocale
750749
* @since 3.2
750+
* @see #addPreferredLocale
751751
*/
752752
public void setPreferredLocales(List<Locale> locales) {
753753
Assert.notEmpty(locales, "Locale list must not be empty");
@@ -945,9 +945,9 @@ public Cookie[] getCookies() {
945945
}
946946

947947
/**
948-
* Add a header entry for the given name.
949-
* <p>While this method can take any {@code Object} as a parameter, it
950-
* is recommended to use the following types:
948+
* Add an HTTP header entry for the given name.
949+
* <p>While this method can take any {@code Object} as a parameter,
950+
* it is recommended to use the following types:
951951
* <ul>
952952
* <li>String or any Object to be converted using {@code toString()}; see {@link #getHeader}.</li>
953953
* <li>String, Number, or Date for date headers; see {@link #getDateHeader}.</li>
@@ -999,6 +999,15 @@ else if (value.getClass().isArray()) {
999999
}
10001000
}
10011001

1002+
/**
1003+
* Remove already registered entries for the specified HTTP header, if any.
1004+
* @since 4.3.20
1005+
*/
1006+
public void removeHeader(String name) {
1007+
Assert.notNull(name, "Header name must not be null");
1008+
this.headers.remove(name);
1009+
}
1010+
10021011
/**
10031012
* Return the long timestamp for the date header with the given {@code name}.
10041013
* <p>If the internal value representation is a String, this method will try
@@ -1232,7 +1241,7 @@ public HttpSession getSession() {
12321241
public String changeSessionId() {
12331242
Assert.isTrue(this.session != null, "The request does not have a session");
12341243
if (this.session instanceof MockHttpSession) {
1235-
return ((MockHttpSession) session).changeSessionId();
1244+
return ((MockHttpSession) this.session).changeSessionId();
12361245
}
12371246
return this.session.getId();
12381247
}

0 commit comments

Comments
 (0)