Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Apr 4, 2023
1 parent 281736f commit d1d2d59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,14 +39,15 @@
@SuppressWarnings("removal")
public class MockCookie extends Cookie {

private static final long serialVersionUID = 1198809317225300389L;
private static final long serialVersionUID = 4312531139502726325L;

private static final String SAME_SITE = "SameSite";
private static final String EXPIRES = "Expires";

@Nullable
private ZonedDateTime expires;


/**
* Construct a new {@link MockCookie} with the supplied name and value.
* @param name the name
Expand All @@ -62,7 +63,7 @@ public MockCookie(String name, String value) {
* @since 5.1.11
*/
public void setExpires(@Nullable ZonedDateTime expires) {
setAttribute(EXPIRES, expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null);
setAttribute(EXPIRES, (expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null));
}

/**
Expand Down Expand Up @@ -156,8 +157,8 @@ private static String extractAttributeValue(String attribute, String header) {

@Override
public void setAttribute(String name, @Nullable String value) {
if(EXPIRES.equalsIgnoreCase(name)) {
this.expires = value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null;
if (EXPIRES.equalsIgnoreCase(name)) {
this.expires = (value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null);
}
super.setAttribute(name, value);
}
Expand All @@ -175,8 +176,7 @@ public String toString() {
.append("HttpOnly", isHttpOnly())
.append(SAME_SITE, getSameSite())
.append("Max-Age", getMaxAge())
.append(EXPIRES, (this.expires != null ?
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
.append(EXPIRES, getAttribute(EXPIRES))
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -148,11 +148,11 @@ void setSameSiteShouldSetAttribute() {

@Test
void setExpiresShouldSetAttribute() {
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
MockCookie cookie = new MockCookie("SESSION", "123");
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
cookie.setExpires(ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));

assertThat(cookie.getAttribute("expires")).isEqualTo("Tue, 8 Oct 2019 19:50:00 GMT");
assertThat(cookie.getAttribute("expires")).isEqualTo(expiresText);
}

@Test
Expand All @@ -168,11 +168,11 @@ void setSameSiteNullShouldClear() {

@Test
void setExpiresNullShouldClear() {
ZonedDateTime expiresDateTime = ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME);
MockCookie cookie = new MockCookie("SESSION", "123");
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
cookie.setExpires(expiresDateTime);
assertThat(cookie.getExpires()).isEqualTo(expiresDateTime);

cookie.setExpires(null);
assertThat(cookie.getExpires()).isNull();
Expand All @@ -189,16 +189,18 @@ void setAttributeSameSiteShouldSetSameSite() {

@Test
void setAttributeExpiresShouldSetExpires() {
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
MockCookie cookie = new MockCookie("SESSION", "123");
cookie.setAttribute("expires", "Tue, 8 Oct 2019 19:50:00 GMT");
cookie.setAttribute("expires", expiresText);

assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
assertThat(cookie.getExpires()).isEqualTo(
ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));
}

@Test
void setInvalidAttributeExpiresShouldThrow() {
MockCookie cookie = new MockCookie("SESSION", "123");
assertThatThrownBy(() -> cookie.setAttribute("expires", "12345")).isInstanceOf(DateTimeParseException.class);
}

}

0 comments on commit d1d2d59

Please sign in to comment.