diff --git a/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java b/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java new file mode 100644 index 00000000..4e5e8181 --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/HttpsConfiguratorImpl.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.microsoft.playwright; + +import com.sun.net.httpserver.HttpsConfigurator; +import com.sun.net.httpserver.HttpsParameters; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.TrustManagerFactory; +import java.io.FileInputStream; +import java.security.KeyStore; + +class HttpsConfiguratorImpl extends HttpsConfigurator { + + static HttpsConfigurator create() { + return new HttpsConfiguratorImpl(createSSLContext()); + } + + private HttpsConfiguratorImpl(SSLContext context) { + super(context); + } + + @Override + public void configure(HttpsParameters params) { + SSLContext sslContext = getSSLContext(); + SSLParameters sslParams = sslContext.getDefaultSSLParameters(); + sslParams.setNeedClientAuth(true); + params.setNeedClientAuth(true); + params.setSSLParameters(sslParams); + } + + // @see http://rememberjava.com/http/2017/04/29/simple_https_server.html + private static SSLContext createSSLContext() { + try{ + KeyStore ks = KeyStore.getInstance("JKS"); + String password = "password"; + // Generated via + // keytool -genkey -keyalg RSA -validity 36500 -keysize 4096 -dname cn=Playwright,ou=Playwright,o=Playwright,c=US -keystore keystore.jks -storepass password -keypass password + ks.load(new FileInputStream("src/test/resources/keys/keystore.jks"), password.toCharArray()); + + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, password.toCharArray()); + + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + return sslContext; + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/playwright/src/test/java/com/microsoft/playwright/Server.java b/playwright/src/test/java/com/microsoft/playwright/Server.java index 26df7761..280bd456 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Server.java +++ b/playwright/src/test/java/com/microsoft/playwright/Server.java @@ -16,10 +16,7 @@ package com.microsoft.playwright; -import com.sun.net.httpserver.Headers; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.*; import java.io.*; import java.net.InetSocketAddress; @@ -55,13 +52,28 @@ public class Server implements HttpHandler { } } - Server(int port) throws IOException { + static Server createHttp(int port) throws IOException { + return new Server(port, false); + } + + static Server createHttps(int port) throws IOException { + return new Server(port, true); + } + + private Server(int port, boolean https) throws IOException { PORT = port; - PREFIX = "http://localhost:" + PORT; - CROSS_PROCESS_PREFIX = "http://127.0.0.1:" + PORT; + PREFIX = "http" + (https ? "s" : "") + "://localhost:" + PORT; + CROSS_PROCESS_PREFIX = "http" + (https ? "s" : "") + "://127.0.0.1:" + PORT; EMPTY_PAGE = PREFIX + "/empty.html"; - server = HttpServer.create(new InetSocketAddress("localhost", port), 0); + if (https) { + HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress("localhost", port), 0); + httpsServer.setHttpsConfigurator(HttpsConfiguratorImpl.create()); + server = httpsServer; + } else { + server = HttpServer.create(new InetSocketAddress("localhost", port), 0); + } + server.createContext("/", this); server.setExecutor(null); // creates a default executor diff --git a/playwright/src/test/java/com/microsoft/playwright/TestCheck.java b/playwright/src/test/java/com/microsoft/playwright/TestCheck.java index cbd27d73..08f36081 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestCheck.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestCheck.java @@ -45,7 +45,7 @@ public class TestCheck { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestClick.java b/playwright/src/test/java/com/microsoft/playwright/TestClick.java index 47e11255..22febaa7 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestClick.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestClick.java @@ -51,7 +51,7 @@ public class TestClick { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestDialog.java b/playwright/src/test/java/com/microsoft/playwright/TestDialog.java index c21e953e..d76b6f37 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestDialog.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestDialog.java @@ -39,7 +39,7 @@ public class TestDialog { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleClick.java b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleClick.java index c68bc871..beff511c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestElementHandleClick.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestElementHandleClick.java @@ -37,7 +37,7 @@ public class TestElementHandleClick { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelector.java b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelector.java index d2e26c16..72c96268 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelector.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelector.java @@ -46,7 +46,7 @@ public class TestEvalOnSelector { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java index 55863073..c02b4140 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestEvalOnSelectorAll.java @@ -45,7 +45,7 @@ public class TestEvalOnSelectorAll { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestFrameNavigate.java b/playwright/src/test/java/com/microsoft/playwright/TestFrameNavigate.java index ab090954..a0461672 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestFrameNavigate.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestFrameNavigate.java @@ -39,7 +39,7 @@ public class TestFrameNavigate { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java index cd561cd0..75bc8a93 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageBasic.java @@ -50,7 +50,7 @@ public class TestPageBasic { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageFill.java b/playwright/src/test/java/com/microsoft/playwright/TestPageFill.java index ad018e17..d97c85c6 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageFill.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageFill.java @@ -42,7 +42,7 @@ public class TestPageFill { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index 5a559fb0..53b93f17 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -46,7 +46,7 @@ public class TestPageRoute { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java index 2b0373cd..2e6d17f0 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageWaitForNavigation.java @@ -21,11 +21,14 @@ import org.junit.jupiter.api.*; import java.io.IOException; import static com.microsoft.playwright.Page.EventType.FRAMENAVIGATED; +import static com.microsoft.playwright.Utils.expectedSSLError; import static org.junit.jupiter.api.Assertions.*; public class TestPageWaitForNavigation { private static Playwright playwright; private static Server server; + private static Server httpsServer; + private static BrowserType browserType; private static Browser browser; private static boolean isChromium; private static boolean isWebKit; @@ -37,7 +40,8 @@ public class TestPageWaitForNavigation { static void launchBrowser() { playwright = Playwright.create(); BrowserType.LaunchOptions options = new BrowserType.LaunchOptions(); - browser = playwright.chromium().launch(options); + browserType = playwright.chromium(); + browser = browserType.launch(options); isChromium = true; isWebKit = false; headful = false; @@ -45,7 +49,8 @@ public class TestPageWaitForNavigation { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); + httpsServer = Server.createHttps(8908); } @AfterAll @@ -109,17 +114,17 @@ public class TestPageWaitForNavigation { } @Test - void shouldWorkWithClickingOnLinksWhichDoNotCommitNavigation() { - // TODO: https server -// page.navigate(server.EMPTY_PAGE); -// page.setContent("foobar"); -// try { -// page.waitForNavigation(); -// page.click("a"); -// fail("did not throw"); -// } catch (RuntimeException e) { -// assertTrue(e.getMessage().contains(expectedSSLError(browserName))); -// } + void shouldWorkWithClickingOnLinksWhichDoNotCommitNavigation() throws InterruptedException { + page.navigate(server.EMPTY_PAGE); + page.setContent("foobar"); + try { + Deferred event = page.waitForNavigation(); + page.click("a"); + event.get(); + fail("did not throw"); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains(expectedSSLError(browserType.name())), e.getMessage()); + } } @Test diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPopup.java b/playwright/src/test/java/com/microsoft/playwright/TestPopup.java index 6a0b221d..e0db971b 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPopup.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPopup.java @@ -43,7 +43,7 @@ public class TestPopup { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestQuerySelector.java b/playwright/src/test/java/com/microsoft/playwright/TestQuerySelector.java index 1140f223..d5cbcf8e 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestQuerySelector.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestQuerySelector.java @@ -48,7 +48,7 @@ public class TestQuerySelector { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java index 58272c04..a73d140c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java @@ -51,7 +51,7 @@ public class TestRequestContinue { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java b/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java index 57f89bbe..50d6d880 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestWorkers.java @@ -50,7 +50,7 @@ public class TestWorkers { @BeforeAll static void startServer() throws IOException { - server = new Server(8907); + server = Server.createHttp(8907); } @AfterAll diff --git a/playwright/src/test/java/com/microsoft/playwright/Utils.java b/playwright/src/test/java/com/microsoft/playwright/Utils.java index 114c01b1..52a079d6 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Utils.java +++ b/playwright/src/test/java/com/microsoft/playwright/Utils.java @@ -40,4 +40,42 @@ class Utils { return handle.asElement().contentFrame(); } + enum OS { WINDOWS, MAC, LINUX, UNKNOWN } + static OS getOS() { + String name = System.getProperty("os.name").toLowerCase(); + if (name.contains("win")) { + return OS.WINDOWS; + } + if (name.contains("linux")) { + return OS.LINUX; + } + if (name.contains("mac os x")) { + return OS.MAC; + } + return OS.UNKNOWN; + } + + static String expectedSSLError(String browserName) { + switch (browserName) { + case "chromium": + switch (getOS()) { + case MAC: + return "net::ERR_CERT_INVALID"; + default: + return "net::ERR_CERT_AUTHORITY_INVALID"; + } + case "webkit": { + switch (getOS()) { + case MAC: + return "The certificate for this server is invalid"; + case WINDOWS: + return "SSL peer certificate or SSH remote key was not OK"; + default: + return "Unacceptable TLS certificate"; + } + } + default: + return "SSL_ERROR_UNKNOWN"; + } + } } diff --git a/playwright/src/test/resources/keys/keystore.jks b/playwright/src/test/resources/keys/keystore.jks new file mode 100644 index 00000000..2937e1d7 Binary files /dev/null and b/playwright/src/test/resources/keys/keystore.jks differ