Skip to content

Commit

Permalink
Polish MockCookie contribution
Browse files Browse the repository at this point in the history
Issue: SPR-17321, #1974
  • Loading branch information
sbrannen committed Oct 2, 2018
1 parent 31095f1 commit 77205ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public String getSameSite() {
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@
* @since 5.1
*/
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
Expand All @@ -51,12 +56,22 @@ public void setSameSite() {
}

@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());
Expand All @@ -68,40 +83,22 @@ public void parseValidHeader() {
@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());
}

}

0 comments on commit 77205ca

Please sign in to comment.