Skip to content

Commit

Permalink
use standard format for weak entity tag (RFC7232) (#1)
Browse files Browse the repository at this point in the history
* use standard format for weak entity tag (RFC7232)
* add unit test
* fix broken unit test
  • Loading branch information
eheimbuch authored Jun 17, 2020
1 parent b27e178 commit 6b708bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
19 changes: 16 additions & 3 deletions src/test/java/com/github/sdorra/webresources/WebResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("\"");
}


Expand Down

0 comments on commit 6b708bc

Please sign in to comment.