Skip to content

Commit 5990548

Browse files
L00kiansbrannen
authored andcommitted
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
1 parent 22a3364 commit 5990548

File tree

3 files changed

+93
-40
lines changed

3 files changed

+93
-40
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
import java.util.Map;
4242
import java.util.Set;
4343
import java.util.TimeZone;
44+
import java.util.stream.Collectors;
4445
import javax.servlet.AsyncContext;
4546
import javax.servlet.DispatcherType;
4647
import javax.servlet.RequestDispatcher;
@@ -58,6 +59,7 @@
5859

5960
import org.springframework.http.HttpHeaders;
6061
import org.springframework.http.MediaType;
62+
import org.springframework.lang.NonNull;
6163
import org.springframework.lang.Nullable;
6264
import org.springframework.util.Assert;
6365
import org.springframework.util.LinkedCaseInsensitiveMap;
@@ -951,14 +953,20 @@ public String getAuthType() {
951953

952954
public void setCookies(@Nullable Cookie... cookies) {
953955
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
954-
this.headers.remove(HttpHeaders.COOKIE);
955-
if (this.cookies != null) {
956-
Arrays.stream(this.cookies)
957-
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
958-
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false));
956+
if (this.cookies == null) {
957+
removeHeader(HttpHeaders.COOKIE);
958+
}
959+
else {
960+
doAddHeaderValue(HttpHeaders.COOKIE, encodeCookies(this.cookies), true);
959961
}
960962
}
961963

964+
private static String encodeCookies(@NonNull Cookie... cookies) {
965+
return Arrays.stream(cookies)
966+
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
967+
.collect(Collectors.joining("; "));
968+
}
969+
962970
@Override
963971
@Nullable
964972
public Cookie[] getCookies() {
@@ -1272,6 +1280,7 @@ public HttpSession getSession() {
12721280
* Otherwise it simply returns the current session id.
12731281
* @since 4.0.3
12741282
*/
1283+
@Override
12751284
public String changeSessionId() {
12761285
Assert.isTrue(this.session != null, "The request does not have a session");
12771286
if (this.session instanceof MockHttpSession) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -259,7 +259,9 @@ public void cookies() {
259259
assertEquals("bar", cookies[0].getValue());
260260
assertEquals("baz", cookies[1].getName());
261261
assertEquals("qux", cookies[1].getValue());
262-
assertEquals(Arrays.asList("foo=bar", "baz=qux"), cookieHeaders);
262+
263+
assertEquals(1, cookieHeaders.size());
264+
assertEquals("foo=bar; baz=qux", cookieHeaders.get(0));
263265
}
264266

265267
@Test

0 commit comments

Comments
 (0)