diff --git a/playwright/src/test/java/com/microsoft/playwright/Server.java b/playwright/src/test/java/com/microsoft/playwright/Server.java index 280bd456..1408020b 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Server.java +++ b/playwright/src/test/java/com/microsoft/playwright/Server.java @@ -40,6 +40,7 @@ public class Server implements HttpHandler { private final Map> requestSubscribers = Collections.synchronizedMap(new HashMap<>()); private final Map auths = Collections.synchronizedMap(new HashMap<>()); + private final Map csp = Collections.synchronizedMap(new HashMap<>()); private final Map routes = Collections.synchronizedMap(new HashMap<>()); private static class Auth { @@ -90,6 +91,10 @@ public class Server implements HttpHandler { auths.put(path, new Auth(user, password)); } + void setCSP(String path, String csp) { + this.csp.put(path, csp); + } + static class Request { public final String method; // TODO: make a copy to ensure thread safety? @@ -175,6 +180,9 @@ public class Server implements HttpHandler { return; } + if (csp.containsKey(path)) { + exchange.getResponseHeaders().put("Content-Security-Policy", singletonList(csp.get(path))); + } File file = new File(resourcesDir, path.substring(1)); exchange.getResponseHeaders().put("Content-Type", singletonList(mimeType(file))); try (FileInputStream input = new FileInputStream(file)) { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java b/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java index 86d0ca89..6b7025ba 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestWaitForFunction.java @@ -192,15 +192,12 @@ public class TestWaitForFunction { @Test void shouldWorkWithStrictCSPPolicy() { - // TODO: -// server.setCSP("/empty.html", "script-src " + server.PREFIX); -// page.navigate(server.EMPTY_PAGE); -// let error = null; -// -// page.waitForFunction(() => window['__FOO'] === 'hit', {}, {polling: 'raf'}).catch(e => error = e), -// page.evaluate("() => window['__FOO'] = 'hit'") -// -// assertNull(error); + server.setCSP("/empty.html", "script-src " + server.PREFIX); + page.navigate(server.EMPTY_PAGE); + + Deferred result = page.waitForFunction("() => window['__FOO'] === 'hit'"); + page.evaluate("() => window['__FOO'] = 'hit'"); + result.get(); } void shouldThrowOnBadPollingValue() {