Skip to content

Commit 73dc2d6

Browse files
committed
unify logic
1 parent 41bff4d commit 73dc2d6

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

playwright/src/test/java/com/microsoft/playwright/Server.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020

2121
import java.io.*;
2222
import java.net.InetSocketAddress;
23-
import java.nio.file.Files;
24-
import java.nio.file.Path;
2523
import java.util.*;
2624
import java.util.concurrent.CompletableFuture;
2725
import java.util.concurrent.Future;
26+
import java.util.function.Function;
2827
import java.util.zip.GZIPOutputStream;
2928

3029
import static com.microsoft.playwright.Utils.copy;
@@ -42,7 +41,7 @@ public class Server implements HttpHandler {
4241
private final Map<String, String> csp = Collections.synchronizedMap(new HashMap<>());
4342
private final Map<String, HttpHandler> routes = Collections.synchronizedMap(new HashMap<>());
4443
private final Set<String> gzipRoutes = Collections.synchronizedSet(new HashSet<>());
45-
private Path staticFilesDirectory;
44+
private Function<String, InputStream> resourceProvider;
4645

4746
private static class Auth {
4847
public final String user;
@@ -78,6 +77,7 @@ private Server(int port, boolean https) throws IOException {
7877
server.createContext("/", this);
7978
server.setExecutor(null); // creates a default executor
8079
server.start();
80+
resourceProvider = path -> Server.class.getClassLoader().getResourceAsStream("resources" + path);
8181
}
8282

8383
public void stop() {
@@ -96,8 +96,8 @@ void enableGzip(String path) {
9696
gzipRoutes.add(path);
9797
}
9898

99-
void setStaticFilesDirectory(Path staticFilesDirectory) {
100-
this.staticFilesDirectory = staticFilesDirectory;
99+
void setResourceProvider(Function<String, InputStream> resourceProvider) {
100+
this.resourceProvider = resourceProvider;
101101
}
102102

103103
static class Request {
@@ -194,30 +194,16 @@ public void handle(HttpExchange exchange) throws IOException {
194194
path = "/index.html";
195195
}
196196

197-
// If static files directory is set, serve from filesystem first
198-
if (staticFilesDirectory != null) {
199-
Path filePath = staticFilesDirectory.resolve(path.substring(1)); // Remove leading /
200-
if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
201-
exchange.getResponseHeaders().add("Content-Type", mimeType(filePath.toFile()));
202-
exchange.sendResponseHeaders(200, Files.size(filePath));
203-
Files.copy(filePath, exchange.getResponseBody());
204-
exchange.getResponseBody().close();
205-
return;
206-
}
207-
}
208-
209-
// Fallback: Resources from "src/test/resources/" are copied to "resources/" directory in the jar.
210-
String resourcePath = "resources" + path;
211-
InputStream resource = getClass().getClassLoader().getResourceAsStream(resourcePath);
197+
InputStream resource = resourceProvider.apply(path);
212198
if (resource == null) {
213199
exchange.getResponseHeaders().add("Content-Type", "text/plain");
214200
exchange.sendResponseHeaders(404, 0);
215201
try (Writer writer = new OutputStreamWriter(exchange.getResponseBody())) {
216-
writer.write("File not found: " + resourcePath);
202+
writer.write("File not found: " + path);
217203
}
218204
return;
219205
}
220-
exchange.getResponseHeaders().add("Content-Type", mimeType(new File(resourcePath)));
206+
exchange.getResponseHeaders().add("Content-Type", mimeType(new File(path)));
221207
ByteArrayOutputStream body = new ByteArrayOutputStream();
222208
OutputStream output = body;
223209
if (gzipRoutes.contains(path)) {

playwright/src/test/java/com/microsoft/playwright/TestTracing.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,17 @@ private void showTraceViewer(Path tracePath, TraceViewerConsumer callback) throw
368368
Path driverDir = Driver.ensureDriverInstalled(java.util.Collections.emptyMap(), true).driverDir();
369369
Path traceViewerPath = driverDir.resolve("package").resolve("lib").resolve("vite").resolve("traceViewer");
370370
Server traceServer = Server.createHttp(Utils.nextFreePort());
371-
traceServer.setStaticFilesDirectory(traceViewerPath);
371+
traceServer.setResourceProvider(path -> {
372+
Path filePath = traceViewerPath.resolve(path.substring(1));
373+
if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
374+
try {
375+
return Files.newInputStream(filePath);
376+
} catch (IOException e) {
377+
return null;
378+
}
379+
}
380+
return null;
381+
});
372382
traceServer.setRoute("/trace.zip", exchange -> {
373383
exchange.getResponseHeaders().add("Content-Type", "application/zip");
374384
exchange.sendResponseHeaders(200, Files.size(tracePath));

0 commit comments

Comments
 (0)