Skip to content

Commit e8c122c

Browse files
committed
Fix MockCookie Partitioned support
See gh-31454
1 parent 9cfd455 commit e8c122c

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -104,7 +104,12 @@ public String getSameSite() {
104104
* @see <a href="https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1">The Partitioned attribute spec</a>
105105
*/
106106
public void setPartitioned(boolean partitioned) {
107-
setAttribute("Partitioned", "");
107+
if (partitioned) {
108+
setAttribute("Partitioned", "");
109+
}
110+
else {
111+
setAttribute("Partitioned", null);
112+
}
108113
}
109114

110115
/**

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void constructCookie() {
4646
assertThat(cookie.getMaxAge()).isEqualTo(-1);
4747
assertThat(cookie.getPath()).isNull();
4848
assertThat(cookie.isHttpOnly()).isFalse();
49+
assertThat(cookie.isPartitioned()).isFalse();
4950
assertThat(cookie.getSecure()).isFalse();
5051
assertThat(cookie.getSameSite()).isNull();
5152
}
@@ -207,9 +208,11 @@ void setInvalidAttributeExpiresShouldThrow() {
207208
@Test
208209
void setPartitioned() {
209210
MockCookie cookie = new MockCookie("SESSION", "123");
210-
cookie.setAttribute("Partitioned", "");
211-
211+
assertThat(cookie.isPartitioned()).isFalse();
212+
cookie.setPartitioned(true);
212213
assertThat(cookie.isPartitioned()).isTrue();
214+
cookie.setPartitioned(false);
215+
assertThat(cookie.isPartitioned()).isFalse();
213216
}
214217

215218
}

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -104,7 +104,12 @@ public String getSameSite() {
104104
* @see <a href="https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1">The Partitioned attribute spec</a>
105105
*/
106106
public void setPartitioned(boolean partitioned) {
107-
setAttribute("Partitioned", "");
107+
if (partitioned) {
108+
setAttribute("Partitioned", "");
109+
}
110+
else {
111+
setAttribute("Partitioned", null);
112+
}
108113
}
109114

110115
/**
@@ -197,6 +202,7 @@ public String toString() {
197202
.append("Comment", getComment())
198203
.append("Secure", getSecure())
199204
.append("HttpOnly", isHttpOnly())
205+
.append("Partitioned", isPartitioned())
200206
.append(SAME_SITE, getSameSite())
201207
.append("Max-Age", getMaxAge())
202208
.append(EXPIRES, getAttribute(EXPIRES))

0 commit comments

Comments
 (0)