Path: blob/trunk/java/test/org/openqa/selenium/ProxySettingTest.java
1865 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 com.google.common.base.Joiner;29import com.google.common.net.HostAndPort;30import io.netty.handler.codec.http.FullHttpResponse;31import io.netty.handler.codec.http.HttpRequest;32import java.net.URI;33import java.net.URISyntaxException;34import java.util.HashSet;35import java.util.Set;36import org.junit.jupiter.api.Test;37import org.openqa.selenium.netty.server.SimpleHttpServer;38import org.openqa.selenium.remote.http.HttpMethod;39import org.openqa.selenium.testing.Ignore;40import org.openqa.selenium.testing.JupiterTestBase;41import org.openqa.selenium.testing.NoDriverAfterTest;42import org.openqa.selenium.testing.NoDriverBeforeTest;4344class ProxySettingTest extends JupiterTestBase {4546@Test47@Ignore(SAFARI)48@NoDriverBeforeTest49@NoDriverAfterTest50public void canConfigureManualHttpProxy() throws URISyntaxException, InterruptedException {51try (FakeProxyServer proxyServer = new FakeProxyServer()) {52Proxy proxyToUse = proxyServer.asProxy();5354createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));5556driver.get(appServer.whereElseIs("simpleTest.html"));57assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();58}59}6061@Test62@Ignore(SAFARI)63@NoDriverBeforeTest64@NoDriverAfterTest65public void canConfigureNoProxy() throws URISyntaxException, InterruptedException {66try (FakeProxyServer proxyServer = new FakeProxyServer()) {67Proxy proxyToUse = proxyServer.asProxy();68proxyToUse.setNoProxy("localhost, 127.0.0.1, " + appServer.getHostName());6970createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));7172driver.get(appServer.whereIs("simpleTest.html"));73assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isFalse();7475driver.get(appServer.whereElseIs("simpleTest.html"));76assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();77}78}7980@Test81@Ignore(SAFARI)82@NoDriverBeforeTest83@NoDriverAfterTest84public void canConfigureProxyThroughPACFile() throws URISyntaxException, InterruptedException {85try (SimpleHttpServer helloServer =86createSimpleHttpServer(87appServer.whereElseIs("mouseOver.html"),88"<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");89SimpleHttpServer pacFileServer =90createPacfileServer(91"/proxy.pac",92Joiner.on('\n')93.join(94"function FindProxyForURL(url, host) {",95" return 'PROXY " + getHostAndPort(helloServer) + "';",96"}"))) {9798Proxy proxy = new Proxy();99proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");100101createNewDriver(new ImmutableCapabilities(PROXY, proxy));102103driver.get(appServer.whereElseIs("mouseOver.html"));104assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Hello, world!");105}106}107108@Test109@Ignore(SAFARI)110@NoDriverBeforeTest111@NoDriverAfterTest112@Ignore(value = FIREFOX, travis = true)113@Ignore(value = CHROME, reason = "Flaky")114@Ignore(value = EDGE, reason = "Flaky")115public void canUsePACThatOnlyProxiesCertainHosts()116throws URISyntaxException, InterruptedException {117try (SimpleHttpServer helloServer =118createSimpleHttpServer(119"/index.html", "<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");120SimpleHttpServer goodbyeServer =121createSimpleHttpServer(122helloServer.baseUri().resolve("/index.html").toString(),123"<!DOCTYPE html><title>Goodbye</title><h3>Goodbye, world!</h3>");124SimpleHttpServer pacFileServer =125createPacfileServer(126"/proxy.pac",127Joiner.on('\n')128.join(129"function FindProxyForURL(url, host) {",130" if (url.indexOf('" + getHostAndPort(helloServer) + "') != -1) {",131" return 'PROXY " + getHostAndPort(goodbyeServer) + "';",132" }",133" return 'DIRECT';",134"}"))) {135136Proxy proxy = new Proxy();137proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");138139createNewDriver(new ImmutableCapabilities(PROXY, proxy));140141driver.get("http://" + getHostAndPort(helloServer) + "/index.html");142assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Goodbye, world!");143144driver.get(appServer.whereElseIs("simpleTest.html"));145assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("Heading");146}147}148149private SimpleHttpServer createSimpleHttpServer(String requestPath, String responseHtml)150throws URISyntaxException, InterruptedException {151SimpleHttpServer server = new SimpleHttpServer();152byte[] bytes = responseHtml.getBytes(UTF_8);153154server.registerEndpoint(HttpMethod.GET, requestPath, "text/html; charset=utf-8", bytes);155156return server;157}158159private SimpleHttpServer createPacfileServer(String requestPath, String responsePac)160throws URISyntaxException, InterruptedException {161SimpleHttpServer server = new SimpleHttpServer();162byte[] bytes = responsePac.getBytes(US_ASCII);163164server.registerEndpoint(165HttpMethod.GET, requestPath, "application/x-ns-proxy-autoconfig", bytes);166167return server;168}169170private static HostAndPort getHostAndPort(SimpleHttpServer server) {171URI baseUri = server.baseUri();172return HostAndPort.fromParts(baseUri.getHost(), baseUri.getPort());173}174175public static class FakeProxyServer extends SimpleHttpServer {176private final Set<String> resources = new HashSet<>();177178public FakeProxyServer() throws URISyntaxException, InterruptedException {}179180@Override181protected FullHttpResponse handleRequest(HttpRequest requested) {182String[] parts = requested.uri().split("/");183184if (parts.length > 1) {185resources.add(parts[parts.length - 1]);186}187188return super.handleRequest(requested);189}190191/**192* Checks if a resource has been requested using the short name of the resource.193*194* @param resourceName The short name of the resource to check.195* @return true if the resource has been called.196*/197public boolean hasBeenCalled(String resourceName) {198return resources.contains(resourceName);199}200201public Proxy asProxy() {202Proxy proxy = new Proxy();203URI baseUri = baseUri();204proxy.setHttpProxy(baseUri.getHost() + ":" + baseUri.getPort());205return proxy;206}207}208}209210211