Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ProxySettingTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static java.nio.charset.StandardCharsets.US_ASCII;
21
import static java.nio.charset.StandardCharsets.UTF_8;
22
import static org.assertj.core.api.Assertions.assertThat;
23
import static org.openqa.selenium.remote.CapabilityType.PROXY;
24
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
25
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
26
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
27
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
28
29
import com.google.common.base.Joiner;
30
import com.google.common.net.HostAndPort;
31
import io.netty.handler.codec.http.FullHttpResponse;
32
import io.netty.handler.codec.http.HttpRequest;
33
import java.net.URI;
34
import java.net.URISyntaxException;
35
import java.util.HashSet;
36
import java.util.Set;
37
import org.junit.jupiter.api.Test;
38
import org.openqa.selenium.netty.server.SimpleHttpServer;
39
import org.openqa.selenium.remote.http.HttpMethod;
40
import org.openqa.selenium.testing.Ignore;
41
import org.openqa.selenium.testing.JupiterTestBase;
42
import org.openqa.selenium.testing.NoDriverAfterTest;
43
import org.openqa.selenium.testing.NoDriverBeforeTest;
44
45
class ProxySettingTest extends JupiterTestBase {
46
47
@Test
48
@Ignore(SAFARI)
49
@NoDriverBeforeTest
50
@NoDriverAfterTest
51
public void canConfigureManualHttpProxy() throws URISyntaxException, InterruptedException {
52
try (FakeProxyServer proxyServer = new FakeProxyServer()) {
53
Proxy proxyToUse = proxyServer.asProxy();
54
55
createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));
56
57
driver.get(appServer.whereElseIs("simpleTest.html"));
58
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();
59
}
60
}
61
62
@Test
63
@Ignore(SAFARI)
64
@NoDriverBeforeTest
65
@NoDriverAfterTest
66
public void canConfigureNoProxy() throws URISyntaxException, InterruptedException {
67
try (FakeProxyServer proxyServer = new FakeProxyServer()) {
68
Proxy proxyToUse = proxyServer.asProxy();
69
proxyToUse.setNoProxy("localhost, 127.0.0.1, " + appServer.getHostName());
70
71
createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));
72
73
driver.get(appServer.whereIs("simpleTest.html"));
74
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isFalse();
75
76
driver.get(appServer.whereElseIs("simpleTest.html"));
77
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();
78
}
79
}
80
81
@Test
82
@Ignore(SAFARI)
83
@NoDriverBeforeTest
84
@NoDriverAfterTest
85
public void canConfigureProxyThroughPACFile() throws URISyntaxException, InterruptedException {
86
try (SimpleHttpServer helloServer =
87
createSimpleHttpServer(
88
appServer.whereElseIs("mouseOver.html"),
89
"<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");
90
SimpleHttpServer pacFileServer =
91
createPacfileServer(
92
"/proxy.pac",
93
Joiner.on('\n')
94
.join(
95
"function FindProxyForURL(url, host) {",
96
" return 'PROXY " + getHostAndPort(helloServer) + "';",
97
"}"))) {
98
99
Proxy proxy = new Proxy();
100
proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");
101
102
createNewDriver(new ImmutableCapabilities(PROXY, proxy));
103
104
driver.get(appServer.whereElseIs("mouseOver.html"));
105
assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Hello, world!");
106
}
107
}
108
109
@Test
110
@Ignore(SAFARI)
111
@NoDriverBeforeTest
112
@NoDriverAfterTest
113
@Ignore(value = FIREFOX, travis = true)
114
@Ignore(value = CHROME, reason = "Flaky")
115
@Ignore(value = EDGE, reason = "Flaky")
116
public void canUsePACThatOnlyProxiesCertainHosts()
117
throws URISyntaxException, InterruptedException {
118
try (SimpleHttpServer helloServer =
119
createSimpleHttpServer(
120
"/index.html", "<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");
121
SimpleHttpServer goodbyeServer =
122
createSimpleHttpServer(
123
helloServer.baseUri().resolve("/index.html").toString(),
124
"<!DOCTYPE html><title>Goodbye</title><h3>Goodbye, world!</h3>");
125
SimpleHttpServer pacFileServer =
126
createPacfileServer(
127
"/proxy.pac",
128
Joiner.on('\n')
129
.join(
130
"function FindProxyForURL(url, host) {",
131
" if (url.indexOf('" + getHostAndPort(helloServer) + "') != -1) {",
132
" return 'PROXY " + getHostAndPort(goodbyeServer) + "';",
133
" }",
134
" return 'DIRECT';",
135
"}"))) {
136
137
Proxy proxy = new Proxy();
138
proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");
139
140
createNewDriver(new ImmutableCapabilities(PROXY, proxy));
141
142
driver.get("http://" + getHostAndPort(helloServer) + "/index.html");
143
assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Goodbye, world!");
144
145
driver.get(appServer.whereElseIs("simpleTest.html"));
146
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("Heading");
147
}
148
}
149
150
private SimpleHttpServer createSimpleHttpServer(String requestPath, String responseHtml)
151
throws URISyntaxException, InterruptedException {
152
SimpleHttpServer server = new SimpleHttpServer();
153
byte[] bytes = responseHtml.getBytes(UTF_8);
154
155
server.registerEndpoint(HttpMethod.GET, requestPath, "text/html; charset=utf-8", bytes);
156
157
return server;
158
}
159
160
private SimpleHttpServer createPacfileServer(String requestPath, String responsePac)
161
throws URISyntaxException, InterruptedException {
162
SimpleHttpServer server = new SimpleHttpServer();
163
byte[] bytes = responsePac.getBytes(US_ASCII);
164
165
server.registerEndpoint(
166
HttpMethod.GET, requestPath, "application/x-ns-proxy-autoconfig", bytes);
167
168
return server;
169
}
170
171
private static HostAndPort getHostAndPort(SimpleHttpServer server) {
172
URI baseUri = server.baseUri();
173
return HostAndPort.fromParts(baseUri.getHost(), baseUri.getPort());
174
}
175
176
public static class FakeProxyServer extends SimpleHttpServer {
177
private final Set<String> resources = new HashSet<>();
178
179
public FakeProxyServer() throws URISyntaxException, InterruptedException {}
180
181
@Override
182
protected FullHttpResponse handleRequest(HttpRequest requested) {
183
String[] parts = requested.uri().split("/");
184
185
if (parts.length > 1) {
186
resources.add(parts[parts.length - 1]);
187
}
188
189
return super.handleRequest(requested);
190
}
191
192
/**
193
* Checks if a resource has been requested using the short name of the resource.
194
*
195
* @param resourceName The short name of the resource to check.
196
* @return true if the resource has been called.
197
*/
198
public boolean hasBeenCalled(String resourceName) {
199
return resources.contains(resourceName);
200
}
201
202
public Proxy asProxy() {
203
Proxy proxy = new Proxy();
204
URI baseUri = baseUri();
205
proxy.setHttpProxy(baseUri.getHost() + ":" + baseUri.getPort());
206
return proxy;
207
}
208
}
209
}
210
211