From 6b708bcac999e209d2f2a55ff33782e2e83974a8 Mon Sep 17 00:00:00 2001 From: eheimbuch Date: Wed, 17 Jun 2020 08:43:05 +0200 Subject: [PATCH] use standard format for weak entity tag (RFC7232) (#1) * use standard format for weak entity tag (RFC7232) * add unit test * fix broken unit test --- .../sdorra/webresources/WebResources.java | 4 ++-- .../sdorra/webresources/WebResourcesTest.java | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/sdorra/webresources/WebResources.java b/src/main/java/com/github/sdorra/webresources/WebResources.java index fec38c9..fc71343 100644 --- a/src/main/java/com/github/sdorra/webresources/WebResources.java +++ b/src/main/java/com/github/sdorra/webresources/WebResources.java @@ -143,8 +143,8 @@ static String etag(Path path) throws IOException { return etag(name, size, lastModified); } - private static String etag(String name, long size, long lastModifid) { - return String.format("%s_%s_%s", name, size, lastModifid); + private static String etag(String name, long size, long lastModified) { + return String.format("W/\"%s_%s_%s\"", name, size, lastModified); } /** diff --git a/src/test/java/com/github/sdorra/webresources/WebResourcesTest.java b/src/test/java/com/github/sdorra/webresources/WebResourcesTest.java index 68543cb..d33333d 100644 --- a/src/test/java/com/github/sdorra/webresources/WebResourcesTest.java +++ b/src/test/java/com/github/sdorra/webresources/WebResourcesTest.java @@ -32,6 +32,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Instant; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; @@ -81,12 +82,24 @@ void testOfJarURL(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = new URL(String.format("jar:file:%s!/%s", jarPath.toString(), "test.txt")); WebResource resource = WebResources.of(url); - assertThat(resource.getContentType()).contains("text/plain"); assertThat(resource.getContentLength()).contains(Files.size(path)); assertThat(Streams.toString(resource.getContent())).isEqualTo("awesome"); - assertThat(resource.getETag()).contains(WebResources.etag(path)); - assertThat(resource.getLastModifiedDate()).contains(Files.getLastModifiedTime(jarPath).toInstant()); + assertThat(resource.getETag().isPresent()).isTrue(); + long lastModified = url.openConnection().getLastModified(); + assertThat(resource.getLastModifiedDate()).contains(Instant.ofEpochMilli(lastModified)); + } + + @Test + void shouldCreateWeakEtag(@TempDirectory.TempDir Path tempDir) throws IOException { + Path path = createSamplePath(tempDir); + + WebResource resource = WebResources.of(path); + + assertThat(resource.getETag().isPresent()).isTrue(); + String etag = resource.getETag().get(); + assertThat(etag).startsWith("W/\""); + assertThat(etag).endsWith("\""); }