Return getLastModified result from JarUrlConnection

Update `JarUrlConnection` and `NestedUrlConnection` so that calls
to `getLastModified()` and `getHeaderFieldDate("last-modified", 0)`
always return a result.

Fixes gh-38204
This commit is contained in:
Phillip Webb
2023-11-04 19:43:54 -07:00
parent d6c28b3fc7
commit c0f8b90d31
4 changed files with 116 additions and 0 deletions

View File

@@ -27,6 +27,8 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.Permission;
import java.time.Instant;
import java.time.temporal.ChronoField;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
@@ -490,4 +492,22 @@ class JarUrlConnectionTests {
assertThat(connection).isNotNull();
}
@Test // gh-38204
void getLastModifiedReturnsFileModifiedTime() throws Exception {
JarUrlConnection connection = JarUrlConnection.open(this.url);
assertThat(connection.getLastModified()).isEqualTo(this.file.lastModified());
}
@Test // gh-38204
void getLastModifiedHeaderReturnsFileModifiedTime() throws IOException {
JarUrlConnection connection = JarUrlConnection.open(this.url);
URLConnection fileConnection = this.file.toURI().toURL().openConnection();
assertThat(connection.getHeaderFieldDate("last-modified", 0)).isEqualTo(withoutNanos(this.file.lastModified()))
.isEqualTo(fileConnection.getHeaderFieldDate("last-modified", 0));
}
private long withoutNanos(long epochMilli) {
return Instant.ofEpochMilli(epochMilli).with(ChronoField.NANO_OF_SECOND, 0).toEpochMilli();
}
}

View File

@@ -18,11 +18,15 @@ package org.springframework.boot.loader.net.protocol.nested;
import java.io.File;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Cleaner.Cleanable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.Permission;
import java.time.Instant;
import java.time.temporal.ChronoField;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -148,4 +152,23 @@ class NestedUrlConnectionTests {
actionCaptor.getValue().run();
}
@Test // gh-38204
void getLastModifiedReturnsFileModifiedTime() throws Exception {
NestedUrlConnection connection = new NestedUrlConnection(this.url);
assertThat(connection.getLastModified()).isEqualTo(this.jarFile.lastModified());
}
@Test // gh-38204
void getLastModifiedHeaderReturnsFileModifiedTime() throws IOException {
NestedUrlConnection connection = new NestedUrlConnection(this.url);
URLConnection fileConnection = this.jarFile.toURI().toURL().openConnection();
assertThat(connection.getHeaderFieldDate("last-modified", 0))
.isEqualTo(withoutNanos(this.jarFile.lastModified()))
.isEqualTo(fileConnection.getHeaderFieldDate("last-modified", 0));
}
private long withoutNanos(long epochMilli) {
return Instant.ofEpochMilli(epochMilli).with(ChronoField.NANO_OF_SECOND, 0).toEpochMilli();
}
}