Skip to content

Commit

Permalink
Polish 'Decode URL content before passing it to NestedLocation.parse'
Browse files Browse the repository at this point in the history
See gh-39675'

Closes gh-39675'
  • Loading branch information
philwebb committed Feb 22, 2024
1 parent 06569e7 commit a457638
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ void getPermissionReturnsFilePermission() throws Exception {
@Test
void getInputStreamReturnsContentOfNestedJar() throws Exception {
NestedUrlConnection connection = new NestedUrlConnection(this.url);
try (InputStream actual = connection.getInputStream()) {
try (ZipContent zipContent = ZipContent.open(this.jarFile.toPath())) {
try (InputStream expected = zipContent.getEntry("nested.jar").openContent().asInputStream()) {
assertThat(actual).hasSameContentAs(expected);
}
}
}
assertHasSameContentAsNestedJar(connection);
}

@Test
Expand Down Expand Up @@ -163,6 +157,29 @@ void getLastModifiedHeaderReturnsFileModifiedTime() throws IOException {
}
}

@Test
void createDecodesUrlPath() throws Exception {
File withSpace = new File(this.temp, "te st");
withSpace.mkdirs();
this.jarFile = new File(withSpace, "test.jar");
TestJar.create(this.jarFile);
this.url = new URL("nested:" + this.jarFile.toURI().getRawPath() + "/!nested.jar");
assertThat(this.url.toString()).contains("%20");
NestedUrlConnection connection = new NestedUrlConnection(this.url);
assertHasSameContentAsNestedJar(connection);
assertThat(connection.getLastModified()).isEqualTo(this.jarFile.lastModified());
}

private void assertHasSameContentAsNestedJar(NestedUrlConnection connection) throws IOException {
try (InputStream actual = connection.getInputStream()) {
try (ZipContent zipContent = ZipContent.open(this.jarFile.toPath())) {
try (InputStream expected = zipContent.getEntry("nested.jar").openContent().asInputStream()) {
assertThat(actual).hasSameContentAs(expected);
}
}
}
}

private long withoutNanos(long epochMilli) {
return Instant.ofEpochMilli(epochMilli).with(ChronoField.NANO_OF_SECOND, 0).toEpochMilli();
}
Expand Down

0 comments on commit a457638

Please sign in to comment.