From e8c122c00a4655516436c95131ca5a489aeb7130 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 7 Jun 2024 14:49:15 +0200 Subject: [PATCH] Fix MockCookie Partitioned support See gh-31454 --- .../java/org/springframework/mock/web/MockCookie.java | 9 +++++++-- .../org/springframework/mock/web/MockCookieTests.java | 7 +++++-- .../web/testfixture/servlet/MockCookie.java | 10 ++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java b/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java index be119c29071a..e3589d746305 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockCookie.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -104,7 +104,12 @@ public String getSameSite() { * @see The Partitioned attribute spec */ public void setPartitioned(boolean partitioned) { - setAttribute("Partitioned", ""); + if (partitioned) { + setAttribute("Partitioned", ""); + } + else { + setAttribute("Partitioned", null); + } } /** diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java index 4f2e7a019bec..e078b3809405 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java @@ -46,6 +46,7 @@ void constructCookie() { assertThat(cookie.getMaxAge()).isEqualTo(-1); assertThat(cookie.getPath()).isNull(); assertThat(cookie.isHttpOnly()).isFalse(); + assertThat(cookie.isPartitioned()).isFalse(); assertThat(cookie.getSecure()).isFalse(); assertThat(cookie.getSameSite()).isNull(); } @@ -207,9 +208,11 @@ void setInvalidAttributeExpiresShouldThrow() { @Test void setPartitioned() { MockCookie cookie = new MockCookie("SESSION", "123"); - cookie.setAttribute("Partitioned", ""); - + assertThat(cookie.isPartitioned()).isFalse(); + cookie.setPartitioned(true); assertThat(cookie.isPartitioned()).isTrue(); + cookie.setPartitioned(false); + assertThat(cookie.isPartitioned()).isFalse(); } } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java index a2680c0ae5d0..ea0353fb2104 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockCookie.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -104,7 +104,12 @@ public String getSameSite() { * @see The Partitioned attribute spec */ public void setPartitioned(boolean partitioned) { - setAttribute("Partitioned", ""); + if (partitioned) { + setAttribute("Partitioned", ""); + } + else { + setAttribute("Partitioned", null); + } } /** @@ -197,6 +202,7 @@ public String toString() { .append("Comment", getComment()) .append("Secure", getSecure()) .append("HttpOnly", isHttpOnly()) + .append("Partitioned", isPartitioned()) .append(SAME_SITE, getSameSite()) .append("Max-Age", getMaxAge()) .append(EXPIRES, getAttribute(EXPIRES))