Skip to content

Commit

Permalink
Fixing cookie tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johanhaleby committed Sep 8, 2023
1 parent 9c3d01e commit 756a91c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@
public class CookieITest extends WithJetty {

@Test
public void cookiesReturnsAMapWhereTheLastValueOfAMultiValueCookieIsUsed() throws Exception {
public void cookiesReturnsAMapWhereTheLastValueOfAMultiValueCookieIsUsed() {
final Map<String,String> cookies = get("/multiCookie").cookies();

assertThat(cookies, hasEntry("cookie1", "cookieValue2"));
}

@Test
public void detailedCookiesAllowsToGetMultiValues() throws Exception {
public void detailedCookiesAllowsToGetMultiValues() {
final Cookies cookies = get("/multiCookie").detailedCookies();

assertThat(cookies.getValues("cookie1"), hasItems("cookieValue1", "cookieValue2"));
}

@Test
public void whenUsingTheDslAndExpectingAMultiValueCookieThenTheLastValueIsUsed() throws Exception {
public void whenUsingTheDslAndExpectingAMultiValueCookieThenTheLastValueIsUsed() {
expect().cookie("cookie1", equalTo("cookieValue2")).when().get("/multiCookie");
}

Expand All @@ -71,12 +71,12 @@ public void supportsDetailedCookieMatcher() {
}

@Test
public void supportsCookieStringMatchingUsingTheDsl() throws Exception {
public void supportsCookieStringMatchingUsingTheDsl() {
expect().cookie("key1", "value1").when().get("/setCookies");
}

@Test
public void canSpecifyMultiValueCookiesUsingByPassingInSeveralValuesToTheCookieMethod() throws Exception {
public void canSpecifyMultiValueCookiesUsingByPassingInSeveralValuesToTheCookieMethod() {
given().
cookie("key1", "value1", "value2").
when().
Expand All @@ -87,27 +87,27 @@ public void canSpecifyMultiValueCookiesUsingByPassingInSeveralValuesToTheCookieM
}

@Test
public void multipleCookieStatementsAreConcatenated() throws Exception {
public void multipleCookieStatementsAreConcatenated() {
expect().response().cookie("key1", "value1").and().cookie("key2", "value2").when().get("/setCookies");
}

@Test
public void multipleCookiesShortVersionUsingPlainStrings() throws Exception {
public void multipleCookiesShortVersionUsingPlainStrings() {
expect().response().cookies("key1", "value1", "key3", "value3").when().get("/setCookies");
}

@Test
public void multipleCookiesShortVersionUsingHamcrestMatching() throws Exception {
public void multipleCookiesShortVersionUsingHamcrestMatching() {
expect().response().cookies("key2", containsString("2"), "key3", equalTo("value3")).when().get("/setCookies");
}

@Test
public void multipleCookiesShortVersionUsingMixOfHamcrestMatchingAndStringMatching() throws Exception {
public void multipleCookiesShortVersionUsingMixOfHamcrestMatchingAndStringMatching() {
expect().response().cookies("key1", containsString("1"), "key2", "value2").when().get("/setCookies");
}

@Test
public void multipleCookiesUsingMap() throws Exception {
public void multipleCookiesUsingMap() {
Map expectedCookies = new HashMap();
expectedCookies.put("key1", "value1");
expectedCookies.put("key2", "value2");
Expand All @@ -116,7 +116,7 @@ public void multipleCookiesUsingMap() throws Exception {
}

@Test
public void multipleCookiesUsingMapWithHamcrestMatcher() throws Exception {
public void multipleCookiesUsingMapWithHamcrestMatcher() {
Map expectedCookies = new HashMap();
expectedCookies.put("key1", containsString("1"));
expectedCookies.put("key3", equalTo("value3"));
Expand All @@ -125,7 +125,7 @@ public void multipleCookiesUsingMapWithHamcrestMatcher() throws Exception {
}

@Test
public void multipleCookiesUsingMapWithMixOfStringAndHamcrestMatcher() throws Exception {
public void multipleCookiesUsingMapWithMixOfStringAndHamcrestMatcher() {
Map expectedCookies = new HashMap();
expectedCookies.put("key1", containsString("1"));
expectedCookies.put("key2", "value2");
Expand All @@ -134,7 +134,7 @@ public void multipleCookiesUsingMapWithMixOfStringAndHamcrestMatcher() throws Ex
}

@Test
public void whenExpectedCookieDoesntMatchAnAssertionThenAssertionErrorIsThrown() throws Exception {
public void whenExpectedCookieDoesntMatchAnAssertionThenAssertionErrorIsThrown() {
exception.expect(AssertionError.class);
exception.expectMessage(equalTo("1 expectation failed.\n" +
"Expected cookie \"key1\" was not \"value2\", was \"value1\".\n"));
Expand All @@ -143,7 +143,7 @@ public void whenExpectedCookieDoesntMatchAnAssertionThenAssertionErrorIsThrown()
}

@Test
public void whenExpectedCookieIsNotFoundThenAnAssertionErrorIsThrown() throws Exception {
public void whenExpectedCookieIsNotFoundThenAnAssertionErrorIsThrown() {
exception.expect(AssertionError.class);
exception.expectMessage(equalTo("1 expectation failed.\n" +
"Cookie \"Not-Defined\" was not defined in the response. Cookies are: \n" +
Expand All @@ -155,30 +155,30 @@ public void whenExpectedCookieIsNotFoundThenAnAssertionErrorIsThrown() throws Ex
}

@Test
public void requestSpecificationAllowsSpecifyingCookieWithNoValue() throws Exception {
public void requestSpecificationAllowsSpecifyingCookieWithNoValue() {
given().cookie("some_cookie").expect().body(equalTo("some_cookie")).when().get("/cookie_with_no_value");
}

@Test
public void responseSpecificationAllowsParsingCookieWithNoValue() throws Exception {
public void responseSpecificationAllowsParsingCookieWithNoValue() {
expect().cookie("PLAY_FLASH").when().get("/response_cookie_with_no_value");
}

@Test
public void requestSpecificationAllowsSpecifyingCookies() throws Exception {
public void requestSpecificationAllowsSpecifyingCookies() {
given().cookies("username", "John", "token", "1234").then().expect().body(equalTo("username, token")).when().get("/cookie");
}

@Test
public void requestSpecificationAllowsSpecifyingCookieUsingMap() throws Exception {
public void requestSpecificationAllowsSpecifyingCookieUsingMap() {
Map<String, String> cookies = new HashMap<String, String>();
cookies.put("username", "John");
cookies.put("token", "1234");
given().cookies(cookies).then().expect().body(equalTo("username, token")).when().get("/cookie");
}

@Test
public void requestSpecificationAllowsSpecifyingMultipleCookies() throws Exception {
public void requestSpecificationAllowsSpecifyingMultipleCookies() {
Map<String, String> cookies = new HashMap<String, String>();
cookies.put("username", "John");
cookies.put("token", "1234");
Expand All @@ -204,7 +204,7 @@ public void canGetCookieDetails() {
}

@Test
public void cookiesSupportEqualCharacterInCookieValue() throws Exception {
public void cookiesSupportEqualCharacterInCookieValue() {
given().
cookie("jsessionid", "9HTaCatOIaiO7ccHojDzuxwVoIU=").
expect().
Expand All @@ -214,7 +214,7 @@ public void cookiesSupportEqualCharacterInCookieValue() throws Exception {
}

@Test
public void cookiesParsingSupportsNoValueCookies() throws Exception {
public void cookiesParsingSupportsNoValueCookies() {
given().
cookie("no-value-cookie").
expect().
Expand All @@ -224,7 +224,7 @@ public void cookiesParsingSupportsNoValueCookies() throws Exception {
}

@Test
public void detailedCookieWorks() throws Exception {
public void detailedCookieWorks() {
final Response response = get("/html_with_cookie");
final Cookie detailedCookieJsessionId = response.detailedCookie("JSESSIONID");

Expand All @@ -234,7 +234,7 @@ public void detailedCookieWorks() throws Exception {
}

@Test
public void getDetailedCookieWorks() throws Exception {
public void getDetailedCookieWorks() {
final Response response = get("/html_with_cookie");
final Cookie detailedCookieJsessionId = response.getDetailedCookie("JSESSIONID");

Expand All @@ -243,15 +243,15 @@ public void getDetailedCookieWorks() throws Exception {
assertThat(detailedCookieJsessionId.getValue(), equalTo("B3134D534F40968A3805968207273EF5"));
}
@Test
public void multipleCookiesWithSameKey() throws Exception {
public void multipleCookiesWithSameKey() {
final Response response = get("/setCommonIdCookies");
Map<String, String> map = new HashMap<String, String>();
map = response.cookies();
assertThat(map.get("key1"), equalTo("value3"));
}

@Test
public void usesCookiesDefinedInAStaticRequestSpecification() throws Exception {
public void usesCookiesDefinedInAStaticRequestSpecification() {
RestAssured.requestSpecification = new RequestSpecBuilder().addCookie("my-cookie", "1234").build();

try {
Expand All @@ -266,7 +266,7 @@ public void usesCookiesDefinedInAStaticRequestSpecification() throws Exception {
}

@Test
public void parsesValidExpiresDateCorrectly() throws Exception {
public void parsesValidExpiresDateCorrectly() {
Cookies cookies =
when().
get("/cookieWithValidExpiresDate").
Expand All @@ -279,7 +279,7 @@ public void parsesValidExpiresDateCorrectly() throws Exception {
}

@Test
public void removesDoubleQuotesFromCookieWithExpiresDate() throws Exception {
public void removesDoubleQuotesFromCookieWithExpiresDate() {
Cookies cookies =
when().
get("/cookieWithDoubleQuoteExpiresDate").
Expand All @@ -292,7 +292,7 @@ public void removesDoubleQuotesFromCookieWithExpiresDate() throws Exception {
}

@Test
public void setsExpiresPropertyToNullWhenCookieHasInvalidExpiresDate() throws Exception {
public void setsExpiresPropertyToNullWhenCookieHasInvalidExpiresDate() {
Cookies cookies =
when().
get("/cookieWithInvalidExpiresDate").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class DetailedCookieAssertion {
cookie = responseCookies.get(cookieName)
} else if (cookie.getExpiryDate() == null && responseCookies.get(cookieName) != null) {
Cookie cookieFromResponse = responseCookies.get(cookieName)
cookie = new Cookie.Builder(cookie).setExpiryDate(cookieFromResponse.getExpiryDate()).build()
def expiryDate = cookieFromResponse.getExpiryDate()
if (expiryDate != null) {
cookie = new Cookie.Builder(cookie).setExpiryDate(expiryDate).build()
}
}

success = matcher.matches(cookie)
Expand Down

0 comments on commit 756a91c

Please sign in to comment.