Path: blob/trunk/java/test/org/openqa/selenium/ProxySettingTest.java
4012 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import static java.nio.charset.StandardCharsets.US_ASCII;20import static java.nio.charset.StandardCharsets.UTF_8;21import static org.assertj.core.api.Assertions.assertThat;22import static org.openqa.selenium.remote.CapabilityType.PROXY;23import static org.openqa.selenium.testing.drivers.Browser.CHROME;24import static org.openqa.selenium.testing.drivers.Browser.EDGE;25import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;26import static org.openqa.selenium.testing.drivers.Browser.SAFARI;2728import io.netty.handler.codec.http.FullHttpResponse;29import io.netty.handler.codec.http.HttpRequest;30import java.net.URI;31import java.net.URISyntaxException;32import java.util.HashSet;33import java.util.Set;34import org.junit.jupiter.api.Test;35import org.openqa.selenium.netty.server.SimpleHttpServer;36import org.openqa.selenium.remote.http.HttpMethod;37import org.openqa.selenium.testing.Ignore;38import org.openqa.selenium.testing.JupiterTestBase;39import org.openqa.selenium.testing.NoDriverAfterTest;40import org.openqa.selenium.testing.NoDriverBeforeTest;4142class ProxySettingTest extends JupiterTestBase {4344@Test45@Ignore(SAFARI)46@NoDriverBeforeTest47@NoDriverAfterTest48public void canConfigureManualHttpProxy() throws URISyntaxException, InterruptedException {49try (FakeProxyServer proxyServer = new FakeProxyServer()) {50Proxy proxyToUse = proxyServer.asProxy();5152createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));5354driver.get(appServer.whereElseIs("simpleTest.html"));55assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();56}57}5859@Test60@Ignore(SAFARI)61@NoDriverBeforeTest62@NoDriverAfterTest63public void canConfigureNoProxy() throws URISyntaxException, InterruptedException {64try (FakeProxyServer proxyServer = new FakeProxyServer()) {65Proxy proxyToUse = proxyServer.asProxy();66proxyToUse.setNoProxy("localhost, 127.0.0.1, " + appServer.getHostName());6768createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));6970driver.get(appServer.whereIs("simpleTest.html"));71assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isFalse();7273driver.get(appServer.whereElseIs("simpleTest.html"));74assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();75}76}7778@Test79@Ignore(SAFARI)80@NoDriverBeforeTest81@NoDriverAfterTest82public void canConfigureProxyThroughPACFile() throws URISyntaxException, InterruptedException {83try (SimpleHttpServer helloServer =84createSimpleHttpServer(85appServer.whereElseIs("mouseOver.html"),86"<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");87SimpleHttpServer pacFileServer =88createPacfileServer(89"/proxy.pac",90String.join(91"\n",92"function FindProxyForURL(url, host) {",93" return 'PROXY " + getHostAndPort(helloServer) + "';",94"}"))) {9596Proxy proxy = new Proxy();97proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");9899createNewDriver(new ImmutableCapabilities(PROXY, proxy));100101driver.get(appServer.whereElseIs("mouseOver.html"));102assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Hello, world!");103}104}105106@Test107@Ignore(SAFARI)108@NoDriverBeforeTest109@NoDriverAfterTest110@Ignore(value = FIREFOX, reason = "Flaky")111@Ignore(value = CHROME, reason = "Flaky")112@Ignore(value = EDGE, reason = "Flaky")113public void canUsePACThatOnlyProxiesCertainHosts()114throws URISyntaxException, InterruptedException {115try (SimpleHttpServer helloServer =116createSimpleHttpServer(117"/index.html", "<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");118SimpleHttpServer goodbyeServer =119createSimpleHttpServer(120helloServer.baseUri().resolve("/index.html").toString(),121"<!DOCTYPE html><title>Goodbye</title><h3>Goodbye, world!</h3>");122SimpleHttpServer pacFileServer =123createPacfileServer(124"/proxy.pac",125String.join(126"\n",127"function FindProxyForURL(url, host) {",128" if (url.indexOf('" + getHostAndPort(helloServer) + "') != -1) {",129" return 'PROXY " + getHostAndPort(goodbyeServer) + "';",130" }",131" return 'DIRECT';",132"}"))) {133134Proxy proxy = new Proxy();135proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");136137createNewDriver(new ImmutableCapabilities(PROXY, proxy));138139driver.get("http://" + getHostAndPort(helloServer) + "/index.html");140assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Goodbye, world!");141142driver.get(appServer.whereElseIs("simpleTest.html"));143assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("Heading");144}145}146147private SimpleHttpServer createSimpleHttpServer(String requestPath, String responseHtml)148throws URISyntaxException, InterruptedException {149SimpleHttpServer server = new SimpleHttpServer();150byte[] bytes = responseHtml.getBytes(UTF_8);151152server.registerEndpoint(HttpMethod.GET, requestPath, "text/html; charset=utf-8", bytes);153154return server;155}156157private SimpleHttpServer createPacfileServer(String requestPath, String responsePac)158throws URISyntaxException, InterruptedException {159SimpleHttpServer server = new SimpleHttpServer();160byte[] bytes = responsePac.getBytes(US_ASCII);161162server.registerEndpoint(163HttpMethod.GET, requestPath, "application/x-ns-proxy-autoconfig", bytes);164165return server;166}167168private static String getHostAndPort(SimpleHttpServer server) {169URI baseUri = server.baseUri();170return String.format("%s:%s", baseUri.getHost(), baseUri.getPort());171}172173public static class FakeProxyServer extends SimpleHttpServer {174private final Set<String> resources = new HashSet<>();175176public FakeProxyServer() throws URISyntaxException, InterruptedException {}177178@Override179protected FullHttpResponse handleRequest(HttpRequest requested) {180String[] parts = requested.uri().split("/");181182if (parts.length > 1) {183resources.add(parts[parts.length - 1]);184}185186return super.handleRequest(requested);187}188189/**190* Checks if a resource has been requested using the short name of the resource.191*192* @param resourceName The short name of the resource to check.193* @return true if the resource has been called.194*/195public boolean hasBeenCalled(String resourceName) {196return resources.contains(resourceName);197}198199public Proxy asProxy() {200Proxy proxy = new Proxy();201URI baseUri = baseUri();202proxy.setHttpProxy(baseUri.getHost() + ":" + baseUri.getPort());203return proxy;204}205}206}207208209