Browse Source

Polish MockCookie contribution

Issue: SPR-17321, #1974
pull/1976/merge
Sam Brannen 6 years ago
parent
commit
77205ca165
  1. 2
      spring-test/src/main/java/org/springframework/mock/web/MockCookie.java
  2. 55
      spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java

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

@ -75,7 +75,7 @@ public class MockCookie extends Cookie { @@ -75,7 +75,7 @@ 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 '" + setCookieHeader + "'");
Assert.isTrue(cookieParts.length == 2, () -> "Invalid Set-Cookie header '" + setCookieHeader + "'");
String name = cookieParts[0];
String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2);

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

@ -32,14 +32,19 @@ import static org.junit.Assert.*; @@ -32,14 +32,19 @@ import static org.junit.Assert.*;
public class MockCookieTests {
@Rule
public ExpectedException exception = ExpectedException.none();
public final ExpectedException exception = ExpectedException.none();
@Test
public void constructCookie() {
MockCookie cookie = new MockCookie("SESSION", "123");
assertEquals("SESSION", cookie.getName());
assertEquals("123", cookie.getValue());
assertCookie(cookie, "SESSION", "123");
assertNull(cookie.getDomain());
assertEquals(-1, cookie.getMaxAge());
assertNull(cookie.getPath());
assertFalse(cookie.isHttpOnly());
assertFalse(cookie.getSecure());
assertNull(cookie.getSameSite());
}
@Test
@ -51,12 +56,22 @@ public class MockCookieTests { @@ -51,12 +56,22 @@ public class MockCookieTests {
}
@Test
public void parseValidHeader() {
public void parseHeaderWithoutAttributes() {
MockCookie cookie;
cookie = MockCookie.parse("SESSION=123");
assertCookie(cookie, "SESSION", "123");
cookie = MockCookie.parse("SESSION=123;");
assertCookie(cookie, "SESSION", "123");
}
@Test
public void parseHeaderWithAttributes() {
MockCookie cookie = MockCookie.parse(
"SESSION=123; Domain=example.com; Max-Age=60; Path=/; Secure; HttpOnly; SameSite=Lax");
assertEquals("SESSION", cookie.getName());
assertEquals("123", cookie.getValue());
assertCookie(cookie, "SESSION", "123");
assertEquals("example.com", cookie.getDomain());
assertEquals(60, cookie.getMaxAge());
assertEquals("/", cookie.getPath());
@ -68,40 +83,22 @@ public class MockCookieTests { @@ -68,40 +83,22 @@ public class MockCookieTests {
@Test
public void parseInvalidHeader() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Invalid Set-Cookie header value 'BOOM'");
exception.expectMessage("Invalid Set-Cookie header 'BOOM'");
MockCookie.parse("BOOM");
}
@Test
public void parseInvalidAttribute() {
String header = "foo=bar; Path=";
String header = "SESSION=123; Path=";
exception.expect(IllegalArgumentException.class);
exception.expectMessage("No value in attribute 'Path' for Set-Cookie header '" + header + "'");
MockCookie.parse(header);
}
@Test
public void testCookieParsing() {
MockCookie m = MockCookie.parse("foo=bar");
testCookie("foo", "bar", m);
m = MockCookie.parse("foo=bar;");
assertFalse(m.isHttpOnly());
assertFalse(m.getSecure());
testCookie("foo", "bar", m);
m = MockCookie.parse("foo=bar; HttpOnly");
testCookie("foo", "bar", m);
assertTrue(m.isHttpOnly());
assertFalse(m.getSecure());
m = MockCookie.parse("foo=bar; Secure");
testCookie("foo", "bar", m);
assertTrue(m.getSecure());
assertFalse(m.isHttpOnly());
}
private void testCookie(String name, String value, MockCookie mockCookie) {
assertEquals(name, mockCookie.getName());
assertEquals(value, mockCookie.getValue());
private void assertCookie(MockCookie cookie, String name, String value) {
assertEquals(name, cookie.getName());
assertEquals(value, cookie.getValue());
}
}

Loading…
Cancel
Save