Skip to content

Commit 17b09c1

Browse files
authored
Merge pull request #1125 from sigstore/jetty-12-updater-test
ref(deps): Migrate UpdaterTest from Jetty to MockWebServer
2 parents 11c29e1 + 52b38e5 commit 17b09c1

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

sigstore-java/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ dependencies {
5454
testImplementation("no.nav.security:mock-oauth2-server:0.5.10")
5555
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
5656
testImplementation("net.sourceforge.htmlunit:htmlunit:2.70.0")
57-
testImplementation("org.eclipse.jetty:jetty-server:11.0.26")
5857

5958
testImplementation("io.github.netmikey.logunit:logunit-core:2.0.0")
6059
testRuntimeOnly("io.github.netmikey.logunit:logunit-jul:2.0.0")

sigstore-java/src/test/java/dev/sigstore/tuf/UpdaterTest.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@
5555
import java.time.ZonedDateTime;
5656
import java.util.List;
5757
import java.util.Map;
58+
import okhttp3.mockwebserver.Dispatcher;
59+
import okhttp3.mockwebserver.MockResponse;
60+
import okhttp3.mockwebserver.MockWebServer;
61+
import okhttp3.mockwebserver.RecordedRequest;
62+
import okio.Buffer;
5863
import org.apache.commons.io.FileUtils;
5964
import org.apache.commons.lang3.tuple.Pair;
60-
import org.eclipse.jetty.server.Server;
61-
import org.eclipse.jetty.server.ServerConnector;
62-
import org.eclipse.jetty.server.SymlinkAllowedResourceAliasChecker;
63-
import org.eclipse.jetty.server.handler.ContextHandler;
64-
import org.eclipse.jetty.server.handler.ResourceHandler;
65-
import org.eclipse.jetty.util.resource.Resource;
6665
import org.hamcrest.CoreMatchers;
6766
import org.hamcrest.MatcherAssert;
6867
import org.jetbrains.annotations.NotNull;
@@ -80,7 +79,7 @@ class UpdaterTest {
8079

8180
public static final String TEST_STATIC_UPDATE_TIME = "2022-09-09T13:37:00.00Z";
8281

83-
static Server remote;
82+
static MockWebServer remote;
8483
static String remoteUrl;
8584
@TempDir Path localStorePath;
8685
@TempDir static Path localMirrorPath;
@@ -89,32 +88,28 @@ class UpdaterTest {
8988
LogCapturer logs = LogCapturer.create().captureForType(Updater.class, Level.DEBUG);
9089

9190
@BeforeAll
92-
static void startRemoteResourceServer() throws Exception {
93-
remote = new Server();
94-
ServerConnector connector = new ServerConnector(remote);
95-
connector.setHost("127.0.0.1");
96-
remote.addConnector(connector);
97-
98-
ResourceHandler resourceHandler = new ResourceHandler();
99-
Resource resourceBase = Resource.newResource(localMirrorPath.toAbsolutePath());
100-
resourceHandler.setBaseResource(resourceBase);
101-
resourceHandler.setDirectoriesListed(true);
102-
resourceHandler.setDirAllowed(true);
103-
resourceHandler.setAcceptRanges(true);
104-
ContextHandler symlinkAllowingHandler = new ContextHandler();
105-
symlinkAllowingHandler.setContextPath("/");
106-
symlinkAllowingHandler.setAllowNullPathInfo(true);
107-
symlinkAllowingHandler.setHandler(resourceHandler);
108-
symlinkAllowingHandler.setBaseResource(resourceBase);
109-
// the @TempDir locations on OS X are under /var/.. which is a symlink to /private/var and are
110-
// not followed by default in Jetty for security reasons.
111-
symlinkAllowingHandler.clearAliasChecks();
112-
symlinkAllowingHandler.addAliasCheck(
113-
new SymlinkAllowedResourceAliasChecker(symlinkAllowingHandler));
114-
remote.setHandler(symlinkAllowingHandler);
91+
static void startRemoteResourceServer() throws IOException {
92+
remote = new MockWebServer();
93+
remote.setDispatcher(
94+
new Dispatcher() {
95+
@Override
96+
public MockResponse dispatch(RecordedRequest request) {
97+
try {
98+
String path = request.getPath().substring(1);
99+
Path file = localMirrorPath.resolve(path);
100+
if (Files.exists(file) && !Files.isDirectory(file)) {
101+
return new MockResponse()
102+
.setResponseCode(200)
103+
.setBody(new Buffer().write(Files.readAllBytes(file)));
104+
}
105+
return new MockResponse().setResponseCode(404);
106+
} catch (IOException e) {
107+
return new MockResponse().setResponseCode(500);
108+
}
109+
}
110+
});
115111
remote.start();
116-
remoteUrl = "http://" + connector.getHost() + ":" + connector.getLocalPort() + "/";
117-
System.out.println("TUF local server listening on: " + remoteUrl);
112+
remoteUrl = remote.url("/").toString();
118113
}
119114

120115
@Test
@@ -983,8 +978,8 @@ void clearLocalMirror() throws IOException {
983978
}
984979

985980
@AfterAll
986-
static void shutdownRemoteResourceServer() throws Exception {
987-
remote.stop();
981+
static void shutdownRemoteResourceServer() throws IOException {
982+
remote.shutdown();
988983
}
989984

990985
public static final Verifiers.Supplier ALWAYS_VERIFIES =

0 commit comments

Comments
 (0)