MockHttpSevlet[Request|Response] set cookie headers
Issue: SPR-15225
This commit is contained in:
@@ -27,6 +27,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.security.Principal;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
@@ -55,11 +56,13 @@ import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.HttpUpgradeHandler;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -87,10 +90,6 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private static final String HTTPS = "https";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String HOST_HEADER = "Host";
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
@@ -219,6 +218,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
private Cookie[] cookies;
|
||||
|
||||
private boolean cookieHeaderSet;
|
||||
|
||||
private final Map<String, HeaderValueHolder> headers = new LinkedCaseInsensitiveMap<>();
|
||||
|
||||
private String method;
|
||||
@@ -387,7 +388,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
StringUtils.hasLength(this.characterEncoding)) {
|
||||
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
|
||||
}
|
||||
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -633,7 +634,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
@Override
|
||||
public String getServerName() {
|
||||
String host = getHeader(HOST_HEADER);
|
||||
String host = getHeader(HttpHeaders.HOST);
|
||||
if (host != null) {
|
||||
host = host.trim();
|
||||
if (host.startsWith("[")) {
|
||||
@@ -655,7 +656,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
@Override
|
||||
public int getServerPort() {
|
||||
String host = getHeader(HOST_HEADER);
|
||||
String host = getHeader(HttpHeaders.HOST);
|
||||
if (host != null) {
|
||||
host = host.trim();
|
||||
int idx;
|
||||
@@ -922,6 +923,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
public void setCookies(Cookie... cookies) {
|
||||
this.cookies = cookies;
|
||||
if (!this.cookieHeaderSet && !ObjectUtils.isEmpty(cookies)) {
|
||||
Arrays.stream(cookies)
|
||||
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
|
||||
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -945,10 +951,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
* @see #getDateHeader
|
||||
*/
|
||||
public void addHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name) && !this.headers.containsKey(CONTENT_TYPE_HEADER)) {
|
||||
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name) && !this.headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
|
||||
setContentType(value.toString());
|
||||
}
|
||||
else {
|
||||
this.cookieHeaderSet = HttpHeaders.COOKIE.equalsIgnoreCase(name);
|
||||
doAddHeaderValue(name, value, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,11 @@ import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
/**
|
||||
@@ -56,12 +58,6 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
private static final String CHARSET_PREFIX = "charset=";
|
||||
|
||||
private static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||
|
||||
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||
|
||||
private static final String LOCATION_HEADER = "Location";
|
||||
|
||||
private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||
|
||||
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||
@@ -102,6 +98,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
private final List<Cookie> cookies = new ArrayList<>();
|
||||
|
||||
private boolean cookieHeaderSet;
|
||||
|
||||
private final Map<String, HeaderValueHolder> headers = new LinkedCaseInsensitiveMap<>();
|
||||
|
||||
private int status = HttpServletResponse.SC_OK;
|
||||
@@ -168,7 +166,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
|
||||
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
|
||||
}
|
||||
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +206,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
@Override
|
||||
public void setContentLength(int contentLength) {
|
||||
this.contentLength = contentLength;
|
||||
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
@@ -218,7 +216,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
@Override
|
||||
public void setContentLengthLong(long contentLength) {
|
||||
this.contentLength = contentLength;
|
||||
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_LENGTH, contentLength, true);
|
||||
}
|
||||
|
||||
public long getContentLengthLong() {
|
||||
@@ -322,6 +320,36 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
public void addCookie(Cookie cookie) {
|
||||
Assert.notNull(cookie, "Cookie must not be null");
|
||||
this.cookies.add(cookie);
|
||||
if (!this.cookieHeaderSet) {
|
||||
doAddHeaderValue(HttpHeaders.SET_COOKIE, getCookieHeader(cookie), false);
|
||||
}
|
||||
}
|
||||
|
||||
private String getCookieHeader(Cookie cookie) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(cookie.getName()).append('=').append(cookie.getValue() == null ? "" : cookie.getValue());
|
||||
if (StringUtils.hasText(cookie.getPath())) {
|
||||
buf.append(";Path=").append(cookie.getPath());
|
||||
}
|
||||
if (StringUtils.hasText(cookie.getDomain())) {
|
||||
buf.append(";Domain=").append(cookie.getDomain());
|
||||
}
|
||||
int maxAge = cookie.getMaxAge();
|
||||
if (maxAge >= 0) {
|
||||
buf.append(";Max-Age=").append(maxAge);
|
||||
buf.append(";Expires=");
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setExpires(maxAge > 0 ? System.currentTimeMillis() + 1000L * maxAge : 0);
|
||||
buf.append(headers.getFirst(HttpHeaders.EXPIRES));
|
||||
}
|
||||
|
||||
if (cookie.getSecure()) {
|
||||
buf.append(";Secure");
|
||||
}
|
||||
if (cookie.isHttpOnly()) {
|
||||
buf.append(";HttpOnly");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public Cookie[] getCookies() {
|
||||
@@ -466,13 +494,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
public void sendRedirect(String url) throws IOException {
|
||||
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
|
||||
Assert.notNull(url, "Redirect URL must not be null");
|
||||
setHeader(LOCATION_HEADER, url);
|
||||
setHeader(HttpHeaders.LOCATION, url);
|
||||
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
|
||||
setCommitted(true);
|
||||
}
|
||||
|
||||
public String getRedirectedUrl() {
|
||||
return getHeader(LOCATION_HEADER);
|
||||
return getHeader(HttpHeaders.LOCATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -505,11 +533,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public void setHeader(String name, String value) {
|
||||
this.cookieHeaderSet = HttpHeaders.SET_COOKIE.equalsIgnoreCase(name);
|
||||
setHeaderValue(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addHeader(String name, String value) {
|
||||
this.cookieHeaderSet = HttpHeaders.SET_COOKIE.equalsIgnoreCase(name);
|
||||
addHeaderValue(name, value);
|
||||
}
|
||||
|
||||
@@ -538,11 +568,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
|
||||
private boolean setSpecialHeader(String name, Object value) {
|
||||
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
|
||||
if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name)) {
|
||||
setContentType(value.toString());
|
||||
return true;
|
||||
}
|
||||
else if (CONTENT_LENGTH_HEADER.equalsIgnoreCase(name)) {
|
||||
else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
|
||||
setContentLength(value instanceof Number ? ((Number) value).intValue() :
|
||||
Integer.parseInt(value.toString()));
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user