2020
2121import java .io .*;
2222import java .net .InetSocketAddress ;
23- import java .nio .file .Files ;
24- import java .nio .file .Path ;
2523import java .util .*;
2624import java .util .concurrent .CompletableFuture ;
2725import java .util .concurrent .Future ;
26+ import java .util .function .Function ;
2827import java .util .zip .GZIPOutputStream ;
2928
3029import 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 )) {
0 commit comments