Skip to content

Commit b07d46d

Browse files
committed
MockCookie compares attributes in case-insensitive manner
Closes gh-22786
1 parent 4955747 commit b07d46d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

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

Lines changed: 10 additions & 8 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.
@@ -20,12 +20,14 @@
2020

2121
import org.springframework.lang.Nullable;
2222
import org.springframework.util.Assert;
23+
import org.springframework.util.StringUtils;
2324

2425
/**
2526
* Extension of {@code Cookie} with extra attributes, as defined in
2627
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
2728
*
2829
* @author Vedran Pavic
30+
* @author Juergen Hoeller
2931
* @since 5.1
3032
*/
3133
public class MockCookie extends Cookie {
@@ -39,7 +41,7 @@ public class MockCookie extends Cookie {
3941

4042
/**
4143
* Constructor with the cookie name and value.
42-
* @param name the name
44+
* @param name the name
4345
* @param value the value
4446
* @see Cookie#Cookie(String, String)
4547
*/
@@ -86,22 +88,22 @@ public static MockCookie parse(String setCookieHeader) {
8688

8789
MockCookie cookie = new MockCookie(name, value);
8890
for (String attribute : attributes) {
89-
if (attribute.startsWith("Domain")) {
91+
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
9092
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
9193
}
92-
else if (attribute.startsWith("Max-Age")) {
94+
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
9395
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
9496
}
95-
else if (attribute.startsWith("Path")) {
97+
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
9698
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
9799
}
98-
else if (attribute.startsWith("Secure")) {
100+
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
99101
cookie.setSecure(true);
100102
}
101-
else if (attribute.startsWith("HttpOnly")) {
103+
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
102104
cookie.setHttpOnly(true);
103105
}
104-
else if (attribute.startsWith("SameSite")) {
106+
else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
105107
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
106108
}
107109
}

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

Lines changed: 20 additions & 7 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.
@@ -34,6 +34,7 @@ public class MockCookieTests {
3434
@Rule
3535
public final ExpectedException exception = ExpectedException.none();
3636

37+
3738
@Test
3839
public void constructCookie() {
3940
MockCookie cookie = new MockCookie("SESSION", "123");
@@ -57,9 +58,7 @@ public void setSameSite() {
5758

5859
@Test
5960
public void parseHeaderWithoutAttributes() {
60-
MockCookie cookie;
61-
62-
cookie = MockCookie.parse("SESSION=123");
61+
MockCookie cookie = MockCookie.parse("SESSION=123");
6362
assertCookie(cookie, "SESSION", "123");
6463

6564
cookie = MockCookie.parse("SESSION=123;");
@@ -80,6 +79,11 @@ public void parseHeaderWithAttributes() {
8079
assertEquals("Lax", cookie.getSameSite());
8180
}
8281

82+
private void assertCookie(MockCookie cookie, String name, String value) {
83+
assertEquals(name, cookie.getName());
84+
assertEquals(value, cookie.getValue());
85+
}
86+
8387
@Test
8488
public void parseNullHeader() {
8589
exception.expect(IllegalArgumentException.class);
@@ -103,9 +107,18 @@ public void parseInvalidAttribute() {
103107
MockCookie.parse(header);
104108
}
105109

106-
private void assertCookie(MockCookie cookie, String name, String value) {
107-
assertEquals(name, cookie.getName());
108-
assertEquals(value, cookie.getValue());
110+
@Test
111+
public void parseHeaderWithAttributesCaseSensitivity() {
112+
MockCookie cookie = MockCookie.parse(
113+
"SESSION=123; domain=example.com; max-age=60; path=/; secure; httponly; samesite=Lax");
114+
115+
assertCookie(cookie, "SESSION", "123");
116+
assertEquals("example.com", cookie.getDomain());
117+
assertEquals(60, cookie.getMaxAge());
118+
assertEquals("/", cookie.getPath());
119+
assertTrue(cookie.getSecure());
120+
assertTrue(cookie.isHttpOnly());
121+
assertEquals("Lax", cookie.getSameSite());
109122
}
110123

111124
}

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

Lines changed: 10 additions & 8 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.
@@ -20,12 +20,14 @@
2020

2121
import org.springframework.lang.Nullable;
2222
import org.springframework.util.Assert;
23+
import org.springframework.util.StringUtils;
2324

2425
/**
2526
* Extension of {@code Cookie} with extra attributes, as defined in
2627
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
2728
*
2829
* @author Vedran Pavic
30+
* @author Juergen Hoeller
2931
* @since 5.1
3032
*/
3133
public class MockCookie extends Cookie {
@@ -39,7 +41,7 @@ public class MockCookie extends Cookie {
3941

4042
/**
4143
* Constructor with the cookie name and value.
42-
* @param name the name
44+
* @param name the name
4345
* @param value the value
4446
* @see Cookie#Cookie(String, String)
4547
*/
@@ -86,22 +88,22 @@ public static MockCookie parse(String setCookieHeader) {
8688

8789
MockCookie cookie = new MockCookie(name, value);
8890
for (String attribute : attributes) {
89-
if (attribute.startsWith("Domain")) {
91+
if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
9092
cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
9193
}
92-
else if (attribute.startsWith("Max-Age")) {
94+
else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
9395
cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
9496
}
95-
else if (attribute.startsWith("Path")) {
97+
else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
9698
cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
9799
}
98-
else if (attribute.startsWith("Secure")) {
100+
else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
99101
cookie.setSecure(true);
100102
}
101-
else if (attribute.startsWith("HttpOnly")) {
103+
else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
102104
cookie.setHttpOnly(true);
103105
}
104-
else if (attribute.startsWith("SameSite")) {
106+
else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
105107
cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
106108
}
107109
}

0 commit comments

Comments
 (0)