Skip to content

Commit d07ff4d

Browse files
HTTPCLIENT-2411: Use standard HTTP-date format for synthesized Date header (#775)
Add regression test for cache conformance Date formatting
1 parent e0aa5ba commit d07ff4d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/ResponseCacheConformance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.time.Instant;
3131

32+
import org.apache.hc.client5.http.utils.DateUtils;
3233
import org.apache.hc.core5.annotation.Contract;
3334
import org.apache.hc.core5.annotation.ThreadingBehavior;
3435
import org.apache.hc.core5.http.EntityDetails;
@@ -71,7 +72,7 @@ public void process(final HttpResponse response,
7172
}
7273
}
7374
if (!response.containsHeader(HttpHeaders.DATE)) {
74-
response.addHeader(new BasicHeader(HttpHeaders.DATE, Instant.now()));
75+
response.addHeader(new BasicHeader(HttpHeaders.DATE, DateUtils.formatStandardDate(Instant.now())));
7576
}
7677
}
7778

httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestResponseCacheConformance.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
import java.time.Instant;
3232

3333
import org.apache.hc.client5.http.utils.DateUtils;
34+
import org.apache.hc.core5.http.HttpHeaders;
3435
import org.apache.hc.core5.http.HttpResponse;
3536
import org.apache.hc.core5.http.HttpStatus;
3637
import org.apache.hc.core5.http.support.BasicResponseBuilder;
38+
import org.junit.jupiter.api.Assertions;
3739
import org.junit.jupiter.api.BeforeEach;
3840
import org.junit.jupiter.api.Test;
3941

@@ -87,4 +89,14 @@ void shouldStripContentTypeFromOrigin304ResponseToStrongValidation() throws Exce
8789
"Content-Type", "text/html;charset=utf-8");
8890
}
8991

92+
@Test
93+
void shouldAddStandardDateHeaderIfMissing() throws Exception {
94+
final HttpResponse response = BasicResponseBuilder.create(HttpStatus.SC_OK)
95+
.build();
96+
assertFalse(response.containsHeader(HttpHeaders.DATE));
97+
impl.process(response, null, null);
98+
Assertions.assertTrue(response.containsHeader(HttpHeaders.DATE));
99+
Assertions.assertNotNull(DateUtils.parseStandardDate(response, HttpHeaders.DATE));
100+
}
101+
90102
}

0 commit comments

Comments
 (0)