test: support setCSP and enable a test (#26)

This commit is contained in:
Yury Semikhatsky 2020-10-21 09:55:39 -07:00 committed by GitHub
parent 28a4be86a4
commit adc49e496a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View File

@ -40,6 +40,7 @@ public class Server implements HttpHandler {
private final Map<String, CompletableFuture<Request>> requestSubscribers = Collections.synchronizedMap(new HashMap<>()); private final Map<String, CompletableFuture<Request>> requestSubscribers = Collections.synchronizedMap(new HashMap<>());
private final Map<String, Auth> auths = Collections.synchronizedMap(new HashMap<>()); private final Map<String, Auth> auths = Collections.synchronizedMap(new HashMap<>());
private final Map<String, String> csp = Collections.synchronizedMap(new HashMap<>());
private final Map<String, HttpHandler> routes = Collections.synchronizedMap(new HashMap<>()); private final Map<String, HttpHandler> routes = Collections.synchronizedMap(new HashMap<>());
private static class Auth { private static class Auth {
@ -90,6 +91,10 @@ public class Server implements HttpHandler {
auths.put(path, new Auth(user, password)); auths.put(path, new Auth(user, password));
} }
void setCSP(String path, String csp) {
this.csp.put(path, csp);
}
static class Request { static class Request {
public final String method; public final String method;
// TODO: make a copy to ensure thread safety? // TODO: make a copy to ensure thread safety?
@ -175,6 +180,9 @@ public class Server implements HttpHandler {
return; return;
} }
if (csp.containsKey(path)) {
exchange.getResponseHeaders().put("Content-Security-Policy", singletonList(csp.get(path)));
}
File file = new File(resourcesDir, path.substring(1)); File file = new File(resourcesDir, path.substring(1));
exchange.getResponseHeaders().put("Content-Type", singletonList(mimeType(file))); exchange.getResponseHeaders().put("Content-Type", singletonList(mimeType(file)));
try (FileInputStream input = new FileInputStream(file)) { try (FileInputStream input = new FileInputStream(file)) {

View File

@ -192,15 +192,12 @@ public class TestWaitForFunction {
@Test @Test
void shouldWorkWithStrictCSPPolicy() { void shouldWorkWithStrictCSPPolicy() {
// TODO: server.setCSP("/empty.html", "script-src " + server.PREFIX);
// server.setCSP("/empty.html", "script-src " + server.PREFIX); page.navigate(server.EMPTY_PAGE);
// page.navigate(server.EMPTY_PAGE);
// let error = null; Deferred<JSHandle> result = page.waitForFunction("() => window['__FOO'] === 'hit'");
// page.evaluate("() => window['__FOO'] = 'hit'");
// page.waitForFunction(() => window['__FOO'] === 'hit', {}, {polling: 'raf'}).catch(e => error = e), result.get();
// page.evaluate("() => window['__FOO'] = 'hit'")
//
// assertNull(error);
} }
void shouldThrowOnBadPollingValue() { void shouldThrowOnBadPollingValue() {