Polish MockCookie[Tests]

This commit is contained in:
Sam Brannen 2018-10-02 12:41:07 +02:00
parent 1b8421c7fe
commit 49bc746640
2 changed files with 40 additions and 21 deletions

View File

@ -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
* <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>.
*
* @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];
}

View File

@ -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);
}
}