Allow Set-Cookie header to be overwritten in MockHttpServletResponse
Prior to this commit, there was no way to replace the Set-Cookie header via MockHttpServletResponse. Specifically, an invocation of setHeader() for the Set-Cookie header resulted in an additional Set-Cookie header instead of replacing the existing one, which is in violation of the contract for javax.servlet.http.HttpServletResponse.setHeader(...). This commit refactors the internals of MockHttpServletResponse to ensure that an existing Set-Cookie header is overwritten when set via an invocation of setHeader(). This commit also verifies the expected behavior for addHeader() and addCookie() with regard to multiple cookies. Closes gh-23512
This commit is contained in:
@@ -55,6 +55,7 @@ import org.springframework.web.util.WebUtils;
|
||||
* @author Rod Johnson
|
||||
* @author Brian Clozel
|
||||
* @author Vedran Pavic
|
||||
* @author Sam Brannen
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public class MockHttpServletResponse implements HttpServletResponse {
|
||||
@@ -573,20 +574,22 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
|
||||
private void setHeaderValue(String name, Object value) {
|
||||
if (setSpecialHeader(name, value)) {
|
||||
boolean replaceHeader = true;
|
||||
if (setSpecialHeader(name, value, replaceHeader)) {
|
||||
return;
|
||||
}
|
||||
doAddHeaderValue(name, value, true);
|
||||
doAddHeaderValue(name, value, replaceHeader);
|
||||
}
|
||||
|
||||
private void addHeaderValue(String name, Object value) {
|
||||
if (setSpecialHeader(name, value)) {
|
||||
boolean replaceHeader = false;
|
||||
if (setSpecialHeader(name, value, replaceHeader)) {
|
||||
return;
|
||||
}
|
||||
doAddHeaderValue(name, value, false);
|
||||
doAddHeaderValue(name, value, replaceHeader);
|
||||
}
|
||||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
private boolean setSpecialHeader(String name, Object value, boolean replaceHeader) {
|
||||
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
setContentType(value.toString());
|
||||
return true;
|
||||
@@ -605,7 +608,12 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {
|
||||
MockCookie cookie = MockCookie.parse(value.toString());
|
||||
addCookie(cookie);
|
||||
if (replaceHeader) {
|
||||
setCookie(cookie);
|
||||
}
|
||||
else {
|
||||
addCookie(cookie);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@@ -628,6 +636,20 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@code Set-Cookie} header to the supplied {@link Cookie},
|
||||
* overwriting any previous cookies.
|
||||
* @param cookie the {@code Cookie} to set
|
||||
* @since 5.1.10
|
||||
* @see #addCookie(Cookie)
|
||||
*/
|
||||
private void setCookie(Cookie cookie) {
|
||||
Assert.notNull(cookie, "Cookie must not be null");
|
||||
this.cookies.clear();
|
||||
this.cookies.add(cookie);
|
||||
doAddHeaderValue(HttpHeaders.SET_COOKIE, getCookieHeader(cookie), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStatus(int status) {
|
||||
if (!this.isCommitted()) {
|
||||
|
||||
Reference in New Issue
Block a user