Polish contribution

See gh-30263
This commit is contained in:
Sam Brannen 2023-04-04 16:29:03 +02:00
parent 281736f14e
commit d1d2d5943e
2 changed files with 20 additions and 18 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -39,7 +39,7 @@ import org.springframework.util.StringUtils;
@SuppressWarnings("removal") @SuppressWarnings("removal")
public class MockCookie extends Cookie { 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 SAME_SITE = "SameSite";
private static final String EXPIRES = "Expires"; private static final String EXPIRES = "Expires";
@ -47,6 +47,7 @@ public class MockCookie extends Cookie {
@Nullable @Nullable
private ZonedDateTime expires; private ZonedDateTime expires;
/** /**
* Construct a new {@link MockCookie} with the supplied name and value. * Construct a new {@link MockCookie} with the supplied name and value.
* @param name the name * @param name the name
@ -62,7 +63,7 @@ public class MockCookie extends Cookie {
* @since 5.1.11 * @since 5.1.11
*/ */
public void setExpires(@Nullable ZonedDateTime expires) { 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));
} }
/** /**
@ -157,7 +158,7 @@ public class MockCookie extends Cookie {
@Override @Override
public void setAttribute(String name, @Nullable String value) { public void setAttribute(String name, @Nullable String value) {
if (EXPIRES.equalsIgnoreCase(name)) { if (EXPIRES.equalsIgnoreCase(name)) {
this.expires = value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null; this.expires = (value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null);
} }
super.setAttribute(name, value); super.setAttribute(name, value);
} }
@ -175,8 +176,7 @@ public class MockCookie extends Cookie {
.append("HttpOnly", isHttpOnly()) .append("HttpOnly", isHttpOnly())
.append(SAME_SITE, getSameSite()) .append(SAME_SITE, getSameSite())
.append("Max-Age", getMaxAge()) .append("Max-Age", getMaxAge())
.append(EXPIRES, (this.expires != null ? .append(EXPIRES, getAttribute(EXPIRES))
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
.toString(); .toString();
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -148,11 +148,11 @@ class MockCookieTests {
@Test @Test
void setExpiresShouldSetAttribute() { void setExpiresShouldSetAttribute() {
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
MockCookie cookie = new MockCookie("SESSION", "123"); MockCookie cookie = new MockCookie("SESSION", "123");
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT", cookie.setExpires(ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));
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 @Test
@ -168,11 +168,11 @@ class MockCookieTests {
@Test @Test
void setExpiresNullShouldClear() { 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"); MockCookie cookie = new MockCookie("SESSION", "123");
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT", cookie.setExpires(expiresDateTime);
DateTimeFormatter.RFC_1123_DATE_TIME)); assertThat(cookie.getExpires()).isEqualTo(expiresDateTime);
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
DateTimeFormatter.RFC_1123_DATE_TIME));
cookie.setExpires(null); cookie.setExpires(null);
assertThat(cookie.getExpires()).isNull(); assertThat(cookie.getExpires()).isNull();
@ -189,11 +189,12 @@ class MockCookieTests {
@Test @Test
void setAttributeExpiresShouldSetExpires() { void setAttributeExpiresShouldSetExpires() {
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
MockCookie cookie = new MockCookie("SESSION", "123"); 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", assertThat(cookie.getExpires()).isEqualTo(
DateTimeFormatter.RFC_1123_DATE_TIME)); ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));
} }
@Test @Test
@ -201,4 +202,5 @@ class MockCookieTests {
MockCookie cookie = new MockCookie("SESSION", "123"); MockCookie cookie = new MockCookie("SESSION", "123");
assertThatThrownBy(() -> cookie.setAttribute("expires", "12345")).isInstanceOf(DateTimeParseException.class); assertThatThrownBy(() -> cookie.setAttribute("expires", "12345")).isInstanceOf(DateTimeParseException.class);
} }
} }