From f3476c68ffb9b17a2dce6210511b583f14d9f5d0 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 9 Sep 2024 18:05:37 +0200 Subject: [PATCH] test: fix client-certificate tests (#1669) --- .../playwright/HttpsConfiguratorImpl.java | 4 +- .../ServerWithClientCertificate.java | 6 +-- .../playwright/TestClientCertificates.java | 44 ++++++++++++++----- .../playwright/TestPageInterception.java | 1 - .../playwright/TestPlaywrightCreate.java | 1 - 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java b/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java index 279a53893..50632dc77 100644 --- a/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java +++ b/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java @@ -43,8 +43,8 @@ static HttpsConfigurator create() { public void configure(HttpsParameters params) { SSLContext sslContext = getSSLContext(); SSLParameters sslParams = sslContext.getDefaultSSLParameters(); - sslParams.setNeedClientAuth(true); - params.setNeedClientAuth(true); + sslParams.setWantClientAuth(true); + params.setWantClientAuth(true); params.setSSLParameters(sslParams); } diff --git a/playwright/src/test/java/com/microsoft/playwright/ServerWithClientCertificate.java b/playwright/src/test/java/com/microsoft/playwright/ServerWithClientCertificate.java index f553f0347..ed8916e57 100644 --- a/playwright/src/test/java/com/microsoft/playwright/ServerWithClientCertificate.java +++ b/playwright/src/test/java/com/microsoft/playwright/ServerWithClientCertificate.java @@ -136,8 +136,8 @@ private static String div(String testId, String message) { public void handle(HttpExchange exchange) throws IOException { SSLSession sslSession = ((HttpsExchange) exchange).getSSLSession(); String response = div("servername", sslSession.getPeerHost()); - Certificate[] certs = sslSession.getPeerCertificates(); - if (certs.length > 0 && certs[0] instanceof X509Certificate) { + try { + Certificate[] certs = sslSession.getPeerCertificates(); X509Certificate cert = (X509Certificate) certs[0]; exchange.getResponseHeaders().add("Content-Type", "text/html"); if (validateCertChain(certs)) { @@ -149,7 +149,7 @@ public void handle(HttpExchange exchange) throws IOException { cert.getSubjectX500Principal().getName(), cert.getIssuerX500Principal().getName())); exchange.sendResponseHeaders(403, 0); } - } else { + } catch (SSLPeerUnverifiedException e) { response += div("message", "Sorry, but you need to provide a client certificate to continue."); exchange.sendResponseHeaders(401, 0); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestClientCertificates.java b/playwright/src/test/java/com/microsoft/playwright/TestClientCertificates.java index eac2e7398..98589bf22 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestClientCertificates.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestClientCertificates.java @@ -61,8 +61,8 @@ void stopServer() { public void shouldFailWithNoClientCertificatesProvided() { APIRequestContext request = playwright.request().newContext( new APIRequest.NewContextOptions().setIgnoreHTTPSErrors(true)); - PlaywrightException e = assertThrows(PlaywrightException.class, () -> request.get(customServer.url)); - assertTrue(e.getMessage().contains("Error: socket hang up"), e.getMessage()); + APIResponse response = request.get(customServer.url); + assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text()); request.dispose(); } @@ -136,8 +136,14 @@ public void shouldWorkWithBrowserNewContext() { try (BrowserContext context = browser.newContext(options)) { Page page = context.newPage(); - assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin)); - assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin)); + { + APIResponse response = page.request().get(customServer.crossOrigin); + assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text()); + } + { + page.navigate(customServer.crossOrigin); + assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue."); + } page.navigate(customServer.url); assertThat(page.getByText("Hello CN=Alice")).isVisible(); APIResponse response = page.request().get(customServer.url); @@ -156,8 +162,14 @@ public void shouldWorkWithBrowserNewPage() { .setKeyPath(asset("client-certificates/client/trusted/key.pem")))); try (Page page = browser.newPage(options)) { - assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin)); - assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin)); + { + page.navigate(customServer.crossOrigin); + assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue."); + } + { + APIResponse response = page.request().get(customServer.crossOrigin); + assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text()); + } page.navigate(customServer.url); assertThat(page.getByText("Hello CN=Alice")).isVisible(); APIResponse response = page.request().get(customServer.url); @@ -176,8 +188,14 @@ public void shouldWorkWithBrowserNewPageWhenPassingAsContent() throws IOExceptio .setKey(readAllBytes(asset("client-certificates/client/trusted/key.pem"))))); try (Page page = browser.newPage(options)) { - assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin)); - assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin)); + { + page.navigate(customServer.crossOrigin); + assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue."); + } + { + APIResponse response = page.request().get(customServer.crossOrigin); + assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text()); + } page.navigate(customServer.url); assertThat(page.getByText("Hello CN=Alice")).isVisible(); APIResponse response = page.request().get(customServer.url); @@ -197,8 +215,14 @@ public void shouldWorkWithBrowserLaunchPersistentContext(@TempDir Path tmpDir) { try (BrowserContext context = browser.browserType().launchPersistentContext(tmpDir.resolve("profile") , options)) { Page page = context.pages().get(0); - assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin)); - assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin)); + { + page.navigate(customServer.crossOrigin); + assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue."); + } + { + APIResponse response = page.request().get(customServer.crossOrigin); + assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text()); + } page.navigate(customServer.url); assertThat(page.getByText("Hello CN=Alice")).isVisible(); APIResponse response = page.request().get(customServer.url); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java b/playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java index 66c891d3a..a398ede58 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java @@ -81,7 +81,6 @@ void shouldInterceptAfterAServiceWorker() { void shouldFulfillInterceptedResponseUsingAlias() { page.route("**/*", route -> { APIResponse response = route.fetch(); - System.out.println(response.headers().get("content-type")); route.fulfill(new Route.FulfillOptions().setResponse(response)); }); Response response = page.navigate(server.PREFIX + "/empty.html"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPlaywrightCreate.java b/playwright/src/test/java/com/microsoft/playwright/TestPlaywrightCreate.java index 6afc84129..eb809e8b9 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPlaywrightCreate.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPlaywrightCreate.java @@ -35,7 +35,6 @@ public class TestPlaywrightCreate { @Test void shouldSupportEnvSkipBrowserDownload(@TempDir Path browsersDir) throws IOException, NoSuchFieldException, IllegalAccessException { - System.err.println("shouldSupportEnvSkipBrowserDownload PLAYWRIGHT_BROWSERS_PATH = " + browsersDir); Map env = mapOf("PLAYWRIGHT_BROWSERS_PATH", browsersDir.toString(), "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1"); Playwright.CreateOptions options = new Playwright.CreateOptions().setEnv(env);