Skip to content

Commit 6bd4f76

Browse files
committed
Added logic to skip the Expires value if Max-Age is set to -1 in cookies. Based on the feedback on #51
1 parent ee24617 commit 6bd4f76

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletResponse.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ public void addCookie(Cookie cookie) {
9898
}
9999
cookieData += "; Max-Age=" + cookie.getMaxAge();
100100

101-
// we always set the timezone to GMT
102-
TimeZone gmtTimeZone = TimeZone.getTimeZone(COOKIE_DEFAULT_TIME_ZONE);
103-
Calendar currentTimestamp = Calendar.getInstance(gmtTimeZone);
104-
currentTimestamp.add(Calendar.SECOND, cookie.getMaxAge());
105-
SimpleDateFormat cookieDateFormatter = new SimpleDateFormat(HEADER_DATE_PATTERN);
106-
cookieDateFormatter.setTimeZone(gmtTimeZone);
107-
cookieData += "; Expires=" + cookieDateFormatter.format(currentTimestamp.getTime());
101+
if (cookie.getMaxAge() > 0) {
102+
// we always set the timezone to GMT
103+
TimeZone gmtTimeZone = TimeZone.getTimeZone(COOKIE_DEFAULT_TIME_ZONE);
104+
Calendar currentTimestamp = Calendar.getInstance(gmtTimeZone);
105+
currentTimestamp.add(Calendar.SECOND, cookie.getMaxAge());
106+
SimpleDateFormat cookieDateFormatter = new SimpleDateFormat(HEADER_DATE_PATTERN);
107+
cookieDateFormatter.setTimeZone(gmtTimeZone);
108+
cookieData += "; Expires=" + cookieDateFormatter.format(currentTimestamp.getTime());
109+
}
108110

109111
setHeader(HttpHeaders.SET_COOKIE, cookieData, false);
110112
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletResponseTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ public void cookie_addCookie_positiveMaxAgeExpiresDate() {
128128
assertEquals(dateFormat.format(testExpiration.getTime()), dateFormat.format(expiration.getTime()));
129129
}
130130

131+
@Test
132+
public void cookie_addCookieWithoutMaxAge_expectNoExpires() {
133+
AwsHttpServletResponse resp = new AwsHttpServletResponse(null, null);
134+
Cookie simpleCookie = new Cookie(COOKIE_NAME, COOKIE_VALUE);
135+
resp.addCookie(simpleCookie);
136+
137+
String cookieHeader = resp.getHeader(HttpHeaders.SET_COOKIE);
138+
assertNotNull(cookieHeader);
139+
assertFalse(cookieHeader.contains("Expires"));
140+
}
141+
131142
private int getMaxAge(String header) {
132143
Matcher ageMatcher = MAX_AGE_PATTERN.matcher(header);
133144
assertTrue(ageMatcher.find());

aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/HelloWorldSparkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void multiCookie_setCookieOnResponse_singleHeaderWithMultipleValues() {
7575
assertEquals(200, response.getStatusCode());
7676
assertTrue(response.getHeaders().containsKey(HttpHeaders.SET_COOKIE));
7777
// we compare against 4 because the expiration date of the cookies will also contain a string
78-
assertEquals(4, response.getHeaders().get(HttpHeaders.SET_COOKIE).split(",").length);
78+
assertEquals(2, response.getHeaders().get(HttpHeaders.SET_COOKIE).split(",").length);
7979
assertTrue(response.getHeaders().get(HttpHeaders.SET_COOKIE).contains(COOKIE_NAME + "=" + COOKIE_VALUE));
8080
assertTrue(response.getHeaders().get(HttpHeaders.SET_COOKIE).contains(COOKIE_NAME + "2=" + COOKIE_VALUE + "2"));
8181
assertTrue(response.getHeaders().get(HttpHeaders.SET_COOKIE).contains(COOKIE_DOMAIN));

0 commit comments

Comments
 (0)