Browse Source

MockHttpServletRequest allows for removing registered header entries

Issue: SPR-17295
pull/1984/merge
Juergen Hoeller 7 years ago
parent
commit
e978f90a19
  1. 27
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java
  2. 27
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@ -101,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -101,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
new BufferedReader(new StringReader(""));
/**
* Date formats as specified in the HTTP RFC
* Date formats as specified in the HTTP RFC.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
*/
private static final String[] DATE_FORMATS = new String[] {
@ -510,7 +510,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -510,7 +510,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Adds all provided parameters <strong>without</strong> replacing any
* Add all provided parameters <strong>without</strong> replacing any
* existing values. To replace existing values, use
* {@link #setParameters(java.util.Map)}.
*/
@ -540,7 +540,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -540,7 +540,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Removes all existing parameters.
* Remove all existing parameters.
*/
public void removeAllParameters() {
this.parameters.clear();
@ -702,8 +702,8 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -702,8 +702,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* Set the list of preferred locales, in descending order, effectively replacing
* any existing locales.
* @see #addPreferredLocale
* @since 3.2
* @see #addPreferredLocale
*/
public void setPreferredLocales(List<Locale> locales) {
Assert.notEmpty(locales, "Locale list must not be empty");
@ -890,9 +890,9 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -890,9 +890,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Add a header entry for the given name.
* <p>While this method can take any {@code Object} as a parameter, it
* is recommended to use the following types:
* Add an HTTP header entry for the given name.
* <p>While this method can take any {@code Object} as a parameter,
* it is recommended to use the following types:
* <ul>
* <li>String or any Object to be converted using {@code toString()}; see {@link #getHeader}.</li>
* <li>String, Number, or Date for date headers; see {@link #getDateHeader}.</li>
@ -931,6 +931,15 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -931,6 +931,15 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
/**
* Remove already registered entries for the specified HTTP header, if any.
* @since 4.3.20
*/
public void removeHeader(String name) {
Assert.notNull(name, "Header name must not be null");
this.headers.remove(name);
}
/**
* Return the long timestamp for the date header with the given {@code name}.
* <p>If the internal value representation is a String, this method will try
@ -1164,7 +1173,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -1164,7 +1173,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) {
return ((MockHttpSession) session).changeSessionId();
return ((MockHttpSession) this.session).changeSessionId();
}
return this.session.getId();
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@ -101,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -101,7 +101,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
new BufferedReader(new StringReader(""));
/**
* Date formats as specified in the HTTP RFC
* Date formats as specified in the HTTP RFC.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
*/
private static final String[] DATE_FORMATS = new String[] {
@ -510,7 +510,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -510,7 +510,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Adds all provided parameters <strong>without</strong> replacing any
* Add all provided parameters <strong>without</strong> replacing any
* existing values. To replace existing values, use
* {@link #setParameters(java.util.Map)}.
*/
@ -540,7 +540,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -540,7 +540,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Removes all existing parameters.
* Remove all existing parameters.
*/
public void removeAllParameters() {
this.parameters.clear();
@ -702,8 +702,8 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -702,8 +702,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
/**
* Set the list of preferred locales, in descending order, effectively replacing
* any existing locales.
* @see #addPreferredLocale
* @since 3.2
* @see #addPreferredLocale
*/
public void setPreferredLocales(List<Locale> locales) {
Assert.notEmpty(locales, "Locale list must not be empty");
@ -890,9 +890,9 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -890,9 +890,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
/**
* Add a header entry for the given name.
* <p>While this method can take any {@code Object} as a parameter, it
* is recommended to use the following types:
* Add an HTTP header entry for the given name.
* <p>While this method can take any {@code Object} as a parameter,
* it is recommended to use the following types:
* <ul>
* <li>String or any Object to be converted using {@code toString()}; see {@link #getHeader}.</li>
* <li>String, Number, or Date for date headers; see {@link #getDateHeader}.</li>
@ -931,6 +931,15 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -931,6 +931,15 @@ public class MockHttpServletRequest implements HttpServletRequest {
}
}
/**
* Remove already registered entries for the specified HTTP header, if any.
* @since 4.3.20
*/
public void removeHeader(String name) {
Assert.notNull(name, "Header name must not be null");
this.headers.remove(name);
}
/**
* Return the long timestamp for the date header with the given {@code name}.
* <p>If the internal value representation is a String, this method will try
@ -1164,7 +1173,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @@ -1164,7 +1173,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) {
return ((MockHttpSession) session).changeSessionId();
return ((MockHttpSession) this.session).changeSessionId();
}
return this.session.getId();
}

Loading…
Cancel
Save