Skip to content

Commit d1d2d59

Browse files
committed
Polish contribution
See gh-30263
1 parent 281736f commit d1d2d59

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockCookie.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,14 +39,15 @@
3939
@SuppressWarnings("removal")
4040
public class MockCookie extends Cookie {
4141

42-
private static final long serialVersionUID = 1198809317225300389L;
42+
private static final long serialVersionUID = 4312531139502726325L;
4343

4444
private static final String SAME_SITE = "SameSite";
4545
private static final String EXPIRES = "Expires";
4646

4747
@Nullable
4848
private ZonedDateTime expires;
4949

50+
5051
/**
5152
* Construct a new {@link MockCookie} with the supplied name and value.
5253
* @param name the name
@@ -62,7 +63,7 @@ public MockCookie(String name, String value) {
6263
* @since 5.1.11
6364
*/
6465
public void setExpires(@Nullable ZonedDateTime expires) {
65-
setAttribute(EXPIRES, expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null);
66+
setAttribute(EXPIRES, (expires != null ? expires.format(DateTimeFormatter.RFC_1123_DATE_TIME) : null));
6667
}
6768

6869
/**
@@ -156,8 +157,8 @@ private static String extractAttributeValue(String attribute, String header) {
156157

157158
@Override
158159
public void setAttribute(String name, @Nullable String value) {
159-
if(EXPIRES.equalsIgnoreCase(name)) {
160-
this.expires = value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null;
160+
if (EXPIRES.equalsIgnoreCase(name)) {
161+
this.expires = (value != null ? ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME) : null);
161162
}
162163
super.setAttribute(name, value);
163164
}
@@ -175,8 +176,7 @@ public String toString() {
175176
.append("HttpOnly", isHttpOnly())
176177
.append(SAME_SITE, getSameSite())
177178
.append("Max-Age", getMaxAge())
178-
.append(EXPIRES, (this.expires != null ?
179-
DateTimeFormatter.RFC_1123_DATE_TIME.format(this.expires) : null))
179+
.append(EXPIRES, getAttribute(EXPIRES))
180180
.toString();
181181
}
182182

spring-test/src/test/java/org/springframework/mock/web/MockCookieTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -148,11 +148,11 @@ void setSameSiteShouldSetAttribute() {
148148

149149
@Test
150150
void setExpiresShouldSetAttribute() {
151+
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
151152
MockCookie cookie = new MockCookie("SESSION", "123");
152-
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
153-
DateTimeFormatter.RFC_1123_DATE_TIME));
153+
cookie.setExpires(ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));
154154

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

158158
@Test
@@ -168,11 +168,11 @@ void setSameSiteNullShouldClear() {
168168

169169
@Test
170170
void setExpiresNullShouldClear() {
171+
ZonedDateTime expiresDateTime = ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
172+
DateTimeFormatter.RFC_1123_DATE_TIME);
171173
MockCookie cookie = new MockCookie("SESSION", "123");
172-
cookie.setExpires(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
173-
DateTimeFormatter.RFC_1123_DATE_TIME));
174-
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
175-
DateTimeFormatter.RFC_1123_DATE_TIME));
174+
cookie.setExpires(expiresDateTime);
175+
assertThat(cookie.getExpires()).isEqualTo(expiresDateTime);
176176

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

190190
@Test
191191
void setAttributeExpiresShouldSetExpires() {
192+
String expiresText = "Tue, 8 Oct 2019 19:50:00 GMT";
192193
MockCookie cookie = new MockCookie("SESSION", "123");
193-
cookie.setAttribute("expires", "Tue, 8 Oct 2019 19:50:00 GMT");
194+
cookie.setAttribute("expires", expiresText);
194195

195-
assertThat(cookie.getExpires()).isEqualTo(ZonedDateTime.parse("Tue, 8 Oct 2019 19:50:00 GMT",
196-
DateTimeFormatter.RFC_1123_DATE_TIME));
196+
assertThat(cookie.getExpires()).isEqualTo(
197+
ZonedDateTime.parse(expiresText, DateTimeFormatter.RFC_1123_DATE_TIME));
197198
}
198199

199200
@Test
200201
void setInvalidAttributeExpiresShouldThrow() {
201202
MockCookie cookie = new MockCookie("SESSION", "123");
202203
assertThatThrownBy(() -> cookie.setAttribute("expires", "12345")).isInstanceOf(DateTimeParseException.class);
203204
}
205+
204206
}

0 commit comments

Comments
 (0)