Skip to content

Commit

Permalink
test: fix client-certificate tests (#1669)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Sep 9, 2024
1 parent 8fd8f1c commit f3476c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> env = mapOf("PLAYWRIGHT_BROWSERS_PATH", browsersDir.toString(),
"PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1");
Playwright.CreateOptions options = new Playwright.CreateOptions().setEnv(env);
Expand Down

0 comments on commit f3476c6

Please sign in to comment.