|
18 | 18 | import static org.htmlunit.junit.BrowserRunner.TestedBrowser.FF_ESR; |
19 | 19 | import static org.htmlunit.junit.BrowserRunner.TestedBrowser.IE; |
20 | 20 |
|
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.File; |
| 23 | +import java.io.OutputStreamWriter; |
| 24 | +import java.net.URL; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | + |
| 27 | +import org.apache.commons.io.FileUtils; |
| 28 | +import org.htmlunit.CookieManager4Test; |
21 | 29 | import org.htmlunit.WebDriverTestCase; |
22 | 30 | import org.htmlunit.html.HtmlPageTest; |
23 | 31 | import org.htmlunit.junit.BrowserRunner; |
|
28 | 36 | import org.junit.Test; |
29 | 37 | import org.junit.runner.RunWith; |
30 | 38 | import org.openqa.selenium.By; |
| 39 | +import org.openqa.selenium.JavascriptExecutor; |
31 | 40 | import org.openqa.selenium.WebDriver; |
32 | 41 | import org.openqa.selenium.htmlunit.HtmlUnitDriver; |
33 | 42 |
|
@@ -2663,4 +2672,97 @@ public void eventPropertyReplaced() throws Exception { |
2663 | 2672 |
|
2664 | 2673 | loadPageVerifyTitle2(html); |
2665 | 2674 | } |
| 2675 | + |
| 2676 | + /** |
| 2677 | + * @throws Exception if an error occurs |
| 2678 | + */ |
| 2679 | + @Test |
| 2680 | + @Alerts(DEFAULT = "true", |
| 2681 | + IE = "not available") |
| 2682 | + public void isSecureContextLocalhost() throws Exception { |
| 2683 | + final String html |
| 2684 | + = "<html><body><script>\n" |
| 2685 | + + LOG_TITLE_FUNCTION |
| 2686 | + + " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n" |
| 2687 | + + "</script></body></html>"; |
| 2688 | + |
| 2689 | + loadPageVerifyTitle2(html); |
| 2690 | + } |
| 2691 | + |
| 2692 | + /** |
| 2693 | + * @throws Exception if an error occurs |
| 2694 | + */ |
| 2695 | + @Test |
| 2696 | + @Alerts(DEFAULT = "false", |
| 2697 | + IE = "not available") |
| 2698 | + public void isSecureContextHttp() throws Exception { |
| 2699 | + final String html |
| 2700 | + = "<html><body><script>\n" |
| 2701 | + + LOG_TITLE_FUNCTION |
| 2702 | + + " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n" |
| 2703 | + + "</script></body></html>"; |
| 2704 | + |
| 2705 | + final WebDriver driver = loadPage2(html, new URL(CookieManager4Test.URL_HOST1)); |
| 2706 | + verifyTitle2(driver, getExpectedAlerts()); |
| 2707 | + } |
| 2708 | + |
| 2709 | + /** |
| 2710 | + * @throws Exception if an error occurs |
| 2711 | + */ |
| 2712 | + @Test |
| 2713 | + @Alerts(DEFAULT = "true", |
| 2714 | + IE = "null") |
| 2715 | + public void isSecureContextHttpS() throws Exception { |
| 2716 | + final WebDriver driver = loadPage2(new URL("https://www.wetator.org/HtmlUnit"), StandardCharsets.UTF_8); |
| 2717 | + |
| 2718 | + final String script = "return window.isSecureContext"; |
| 2719 | + final Object result = ((JavascriptExecutor) driver).executeScript(script); |
| 2720 | + assertEquals(getExpectedAlerts()[0], "" + result); |
| 2721 | + } |
| 2722 | + |
| 2723 | + /** |
| 2724 | + * @throws Exception if an error occurs |
| 2725 | + */ |
| 2726 | + @Test |
| 2727 | + @Alerts(DEFAULT = "true", |
| 2728 | + IE = "null") |
| 2729 | + public void isSecureContextFile() throws Exception { |
| 2730 | + final String html |
| 2731 | + = "<html><body><script>\n" |
| 2732 | + + LOG_TITLE_FUNCTION |
| 2733 | + + " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n" |
| 2734 | + + "</script></body></html>"; |
| 2735 | + |
| 2736 | + final File currentDirectory = new File((new File("")).getAbsolutePath()); |
| 2737 | + final File tmpFile = File.createTempFile("isSecureContext", ".html", currentDirectory); |
| 2738 | + tmpFile.deleteOnExit(); |
| 2739 | + final String encoding = (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding(); |
| 2740 | + FileUtils.writeStringToFile(tmpFile, html, encoding); |
| 2741 | + |
| 2742 | + final WebDriver driver = getWebDriver(); |
| 2743 | + driver.get("file://" + tmpFile.getCanonicalPath()); |
| 2744 | + |
| 2745 | + final String script = "return window.isSecureContext"; |
| 2746 | + final Object result = ((JavascriptExecutor) driver).executeScript(script); |
| 2747 | + assertEquals(getExpectedAlerts()[0], "" + result); |
| 2748 | + |
| 2749 | + shutDownAll(); |
| 2750 | + } |
| 2751 | + |
| 2752 | + /** |
| 2753 | + * @throws Exception if an error occurs |
| 2754 | + */ |
| 2755 | + @Test |
| 2756 | + @Alerts(DEFAULT = "false", |
| 2757 | + IE = "null") |
| 2758 | + public void isSecureContextAboutBlank() throws Exception { |
| 2759 | + final WebDriver driver = getWebDriver(); |
| 2760 | + driver.get("about:blank"); |
| 2761 | + |
| 2762 | + final String script = "return window.isSecureContext"; |
| 2763 | + final Object result = ((JavascriptExecutor) driver).executeScript(script); |
| 2764 | + assertEquals(getExpectedAlerts()[0], "" + result); |
| 2765 | + |
| 2766 | + shutDownAll(); |
| 2767 | + } |
2666 | 2768 | } |
0 commit comments