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 d9b7e8c5f1..b70b3ea606 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 @@ -22,7 +22,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * Extension of {@code Cookie} with extra directives, as defined in + * Extension of {@code Cookie} with extra attributes, as defined in * RFC 6265. * * @author Vedran Pavic @@ -75,40 +75,41 @@ public class MockCookie extends Cookie { */ public static MockCookie parse(String setCookieHeader) { String[] cookieParts = setCookieHeader.split("\\s*=\\s*", 2); - Assert.isTrue(cookieParts.length == 2, "Invalid Set-Cookie header value"); + Assert.isTrue(cookieParts.length == 2, () -> "Invalid Set-Cookie header value '" + setCookieHeader + "'"); String name = cookieParts[0]; - String[] valueAndDirectives = cookieParts[1].split("\\s*;\\s*", 2); - String value = valueAndDirectives[0]; - String[] directives = valueAndDirectives.length > 1 ? valueAndDirectives[1].split("\\s*;\\s*") : new String[0]; + String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2); + String value = valueAndAttributes[0]; + String[] attributes = valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0]; MockCookie cookie = new MockCookie(name, value); - for (String directive : directives) { - if (directive.startsWith("Domain")) { - cookie.setDomain(extractDirectiveValue(directive)); + for (String attribute : attributes) { + if (attribute.startsWith("Domain")) { + cookie.setDomain(extractAttributeValue(attribute, setCookieHeader)); } - else if (directive.startsWith("Max-Age")) { - cookie.setMaxAge(Integer.parseInt(extractDirectiveValue(directive))); + else if (attribute.startsWith("Max-Age")) { + cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader))); } - else if (directive.startsWith("Path")) { - cookie.setPath(extractDirectiveValue(directive)); + else if (attribute.startsWith("Path")) { + cookie.setPath(extractAttributeValue(attribute, setCookieHeader)); } - else if (directive.startsWith("Secure")) { + else if (attribute.startsWith("Secure")) { cookie.setSecure(true); } - else if (directive.startsWith("HttpOnly")) { + else if (attribute.startsWith("HttpOnly")) { cookie.setHttpOnly(true); } - else if (directive.startsWith("SameSite")) { - cookie.setSameSite(extractDirectiveValue(directive)); + else if (attribute.startsWith("SameSite")) { + cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader)); } } return cookie; } - private static String extractDirectiveValue(String directive) { - String[] nameAndValue = directive.split("="); - Assert.isTrue(nameAndValue.length == 2, () -> "No value in directive: '" + directive + "'"); + private static String extractAttributeValue(String attribute, String header) { + String[] nameAndValue = attribute.split("="); + Assert.isTrue(nameAndValue.length == 2, + () -> "No value in attribute '" + nameAndValue[0] + "' for Set-Cookie header '" + header + "'"); return nameAndValue[1]; } 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 7af2c8a9f3..77c0be4b7f 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 @@ -16,7 +16,9 @@ package org.springframework.mock.web; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.*; @@ -24,8 +26,13 @@ import static org.junit.Assert.*; * Unit tests for {@link MockCookie}. * * @author Vedran Pavic + * @author Sam Brannen + * @since 5.1 */ public class MockCookieTests { + + @Rule + public ExpectedException exception = ExpectedException.none(); @Test public void constructCookie() { @@ -58,9 +65,20 @@ public class MockCookieTests { assertEquals("Lax", cookie.getSameSite()); } - @Test(expected = IllegalArgumentException.class) + @Test public void parseInvalidHeader() { - MockCookie.parse("invalid"); + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Invalid Set-Cookie header value 'BOOM'"); + MockCookie.parse("BOOM"); + } + + @Test + public void parseInvalidAttribute() { + String header = "foo=bar; Path="; + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("No value in attribute 'Path' for Set-Cookie header '" + header + "'"); + MockCookie.parse(header); } }