Fix MockHttpServletRequest.setCookies to produce single cookie header

Prior to this commit, MockHttpServletRequest.setCookies() produced one
Cookie header per supplied cookie, resulting in multiple Cookie headers
which violates the specification.

This commit fixes this by ensuring that all cookie name-value pairs are
stored under a single Cookie header, separated by a semicolon.

Closes gh-23074
This commit is contained in:
Ilya Lukyanovich
2019-06-03 15:02:44 +03:00
committed by Sam Brannen
parent 22a3364173
commit 5990548f6f
3 changed files with 93 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -41,6 +41,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
@@ -58,6 +59,7 @@ import javax.servlet.http.Part;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -951,12 +953,18 @@ public class MockHttpServletRequest implements HttpServletRequest {
public void setCookies(@Nullable Cookie... cookies) {
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
this.headers.remove(HttpHeaders.COOKIE);
if (this.cookies != null) {
Arrays.stream(this.cookies)
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false));
if (this.cookies == null) {
removeHeader(HttpHeaders.COOKIE);
}
else {
doAddHeaderValue(HttpHeaders.COOKIE, encodeCookies(this.cookies), true);
}
}
private static String encodeCookies(@NonNull Cookie... cookies) {
return Arrays.stream(cookies)
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
.collect(Collectors.joining("; "));
}
@Override
@@ -1272,6 +1280,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
* Otherwise it simply returns the current session id.
* @since 4.0.3
*/
@Override
public String changeSessionId() {
Assert.isTrue(this.session != null, "The request does not have a session");
if (this.session instanceof MockHttpSession) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -259,7 +259,9 @@ public class MockHttpServletRequestTests {
assertEquals("bar", cookies[0].getValue());
assertEquals("baz", cookies[1].getName());
assertEquals("qux", cookies[1].getValue());
assertEquals(Arrays.asList("foo=bar", "baz=qux"), cookieHeaders);
assertEquals(1, cookieHeaders.size());
assertEquals("foo=bar; baz=qux", cookieHeaders.get(0));
}
@Test