Skip to content

Commit f3476c6

Browse files
authored
test: fix client-certificate tests (#1669)
1 parent 8fd8f1c commit f3476c6

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ static HttpsConfigurator create() {
4343
public void configure(HttpsParameters params) {
4444
SSLContext sslContext = getSSLContext();
4545
SSLParameters sslParams = sslContext.getDefaultSSLParameters();
46-
sslParams.setNeedClientAuth(true);
47-
params.setNeedClientAuth(true);
46+
sslParams.setWantClientAuth(true);
47+
params.setWantClientAuth(true);
4848
params.setSSLParameters(sslParams);
4949
}
5050

playwright/src/test/java/com/microsoft/playwright/ServerWithClientCertificate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ private static String div(String testId, String message) {
136136
public void handle(HttpExchange exchange) throws IOException {
137137
SSLSession sslSession = ((HttpsExchange) exchange).getSSLSession();
138138
String response = div("servername", sslSession.getPeerHost());
139-
Certificate[] certs = sslSession.getPeerCertificates();
140-
if (certs.length > 0 && certs[0] instanceof X509Certificate) {
139+
try {
140+
Certificate[] certs = sslSession.getPeerCertificates();
141141
X509Certificate cert = (X509Certificate) certs[0];
142142
exchange.getResponseHeaders().add("Content-Type", "text/html");
143143
if (validateCertChain(certs)) {
@@ -149,7 +149,7 @@ public void handle(HttpExchange exchange) throws IOException {
149149
cert.getSubjectX500Principal().getName(), cert.getIssuerX500Principal().getName()));
150150
exchange.sendResponseHeaders(403, 0);
151151
}
152-
} else {
152+
} catch (SSLPeerUnverifiedException e) {
153153
response += div("message", "Sorry, but you need to provide a client certificate to continue.");
154154
exchange.sendResponseHeaders(401, 0);
155155
}

playwright/src/test/java/com/microsoft/playwright/TestClientCertificates.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void stopServer() {
6161
public void shouldFailWithNoClientCertificatesProvided() {
6262
APIRequestContext request = playwright.request().newContext(
6363
new APIRequest.NewContextOptions().setIgnoreHTTPSErrors(true));
64-
PlaywrightException e = assertThrows(PlaywrightException.class, () -> request.get(customServer.url));
65-
assertTrue(e.getMessage().contains("Error: socket hang up"), e.getMessage());
64+
APIResponse response = request.get(customServer.url);
65+
assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text());
6666
request.dispose();
6767
}
6868

@@ -136,8 +136,14 @@ public void shouldWorkWithBrowserNewContext() {
136136

137137
try (BrowserContext context = browser.newContext(options)) {
138138
Page page = context.newPage();
139-
assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin));
140-
assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin));
139+
{
140+
APIResponse response = page.request().get(customServer.crossOrigin);
141+
assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text());
142+
}
143+
{
144+
page.navigate(customServer.crossOrigin);
145+
assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue.");
146+
}
141147
page.navigate(customServer.url);
142148
assertThat(page.getByText("Hello CN=Alice")).isVisible();
143149
APIResponse response = page.request().get(customServer.url);
@@ -156,8 +162,14 @@ public void shouldWorkWithBrowserNewPage() {
156162
.setKeyPath(asset("client-certificates/client/trusted/key.pem"))));
157163

158164
try (Page page = browser.newPage(options)) {
159-
assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin));
160-
assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin));
165+
{
166+
page.navigate(customServer.crossOrigin);
167+
assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue.");
168+
}
169+
{
170+
APIResponse response = page.request().get(customServer.crossOrigin);
171+
assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text());
172+
}
161173
page.navigate(customServer.url);
162174
assertThat(page.getByText("Hello CN=Alice")).isVisible();
163175
APIResponse response = page.request().get(customServer.url);
@@ -176,8 +188,14 @@ public void shouldWorkWithBrowserNewPageWhenPassingAsContent() throws IOExceptio
176188
.setKey(readAllBytes(asset("client-certificates/client/trusted/key.pem")))));
177189

178190
try (Page page = browser.newPage(options)) {
179-
assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin));
180-
assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin));
191+
{
192+
page.navigate(customServer.crossOrigin);
193+
assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue.");
194+
}
195+
{
196+
APIResponse response = page.request().get(customServer.crossOrigin);
197+
assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text());
198+
}
181199
page.navigate(customServer.url);
182200
assertThat(page.getByText("Hello CN=Alice")).isVisible();
183201
APIResponse response = page.request().get(customServer.url);
@@ -197,8 +215,14 @@ public void shouldWorkWithBrowserLaunchPersistentContext(@TempDir Path tmpDir) {
197215

198216
try (BrowserContext context = browser.browserType().launchPersistentContext(tmpDir.resolve("profile") , options)) {
199217
Page page = context.pages().get(0);
200-
assertThrows(PlaywrightException.class, () -> page.navigate(customServer.crossOrigin));
201-
assertThrows(PlaywrightException.class, () -> page.request().get(customServer.crossOrigin));
218+
{
219+
page.navigate(customServer.crossOrigin);
220+
assertThat(page.getByTestId("message")).hasText("Sorry, but you need to provide a client certificate to continue.");
221+
}
222+
{
223+
APIResponse response = page.request().get(customServer.crossOrigin);
224+
assertTrue(response.text().contains("Sorry, but you need to provide a client certificate to continue."), response.text());
225+
}
202226
page.navigate(customServer.url);
203227
assertThat(page.getByText("Hello CN=Alice")).isVisible();
204228
APIResponse response = page.request().get(customServer.url);

playwright/src/test/java/com/microsoft/playwright/TestPageInterception.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ void shouldInterceptAfterAServiceWorker() {
8181
void shouldFulfillInterceptedResponseUsingAlias() {
8282
page.route("**/*", route -> {
8383
APIResponse response = route.fetch();
84-
System.out.println(response.headers().get("content-type"));
8584
route.fulfill(new Route.FulfillOptions().setResponse(response));
8685
});
8786
Response response = page.navigate(server.PREFIX + "/empty.html");

playwright/src/test/java/com/microsoft/playwright/TestPlaywrightCreate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
public class TestPlaywrightCreate {
3636
@Test
3737
void shouldSupportEnvSkipBrowserDownload(@TempDir Path browsersDir) throws IOException, NoSuchFieldException, IllegalAccessException {
38-
System.err.println("shouldSupportEnvSkipBrowserDownload PLAYWRIGHT_BROWSERS_PATH = " + browsersDir);
3938
Map<String, String> env = mapOf("PLAYWRIGHT_BROWSERS_PATH", browsersDir.toString(),
4039
"PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1");
4140
Playwright.CreateOptions options = new Playwright.CreateOptions().setEnv(env);

0 commit comments

Comments
 (0)