Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/ProxySettingTest.java
4012 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 io.netty.handler.codec.http.FullHttpResponse;
30
import io.netty.handler.codec.http.HttpRequest;
31
import java.net.URI;
32
import java.net.URISyntaxException;
33
import java.util.HashSet;
34
import java.util.Set;
35
import org.junit.jupiter.api.Test;
36
import org.openqa.selenium.netty.server.SimpleHttpServer;
37
import org.openqa.selenium.remote.http.HttpMethod;
38
import org.openqa.selenium.testing.Ignore;
39
import org.openqa.selenium.testing.JupiterTestBase;
40
import org.openqa.selenium.testing.NoDriverAfterTest;
41
import org.openqa.selenium.testing.NoDriverBeforeTest;
42
43
class ProxySettingTest extends JupiterTestBase {
44
45
@Test
46
@Ignore(SAFARI)
47
@NoDriverBeforeTest
48
@NoDriverAfterTest
49
public void canConfigureManualHttpProxy() throws URISyntaxException, InterruptedException {
50
try (FakeProxyServer proxyServer = new FakeProxyServer()) {
51
Proxy proxyToUse = proxyServer.asProxy();
52
53
createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));
54
55
driver.get(appServer.whereElseIs("simpleTest.html"));
56
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();
57
}
58
}
59
60
@Test
61
@Ignore(SAFARI)
62
@NoDriverBeforeTest
63
@NoDriverAfterTest
64
public void canConfigureNoProxy() throws URISyntaxException, InterruptedException {
65
try (FakeProxyServer proxyServer = new FakeProxyServer()) {
66
Proxy proxyToUse = proxyServer.asProxy();
67
proxyToUse.setNoProxy("localhost, 127.0.0.1, " + appServer.getHostName());
68
69
createNewDriver(new ImmutableCapabilities(PROXY, proxyToUse));
70
71
driver.get(appServer.whereIs("simpleTest.html"));
72
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isFalse();
73
74
driver.get(appServer.whereElseIs("simpleTest.html"));
75
assertThat(proxyServer.hasBeenCalled("simpleTest.html")).isTrue();
76
}
77
}
78
79
@Test
80
@Ignore(SAFARI)
81
@NoDriverBeforeTest
82
@NoDriverAfterTest
83
public void canConfigureProxyThroughPACFile() throws URISyntaxException, InterruptedException {
84
try (SimpleHttpServer helloServer =
85
createSimpleHttpServer(
86
appServer.whereElseIs("mouseOver.html"),
87
"<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");
88
SimpleHttpServer pacFileServer =
89
createPacfileServer(
90
"/proxy.pac",
91
String.join(
92
"\n",
93
"function FindProxyForURL(url, host) {",
94
" return 'PROXY " + getHostAndPort(helloServer) + "';",
95
"}"))) {
96
97
Proxy proxy = new Proxy();
98
proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");
99
100
createNewDriver(new ImmutableCapabilities(PROXY, proxy));
101
102
driver.get(appServer.whereElseIs("mouseOver.html"));
103
assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Hello, world!");
104
}
105
}
106
107
@Test
108
@Ignore(SAFARI)
109
@NoDriverBeforeTest
110
@NoDriverAfterTest
111
@Ignore(value = FIREFOX, reason = "Flaky")
112
@Ignore(value = CHROME, reason = "Flaky")
113
@Ignore(value = EDGE, reason = "Flaky")
114
public void canUsePACThatOnlyProxiesCertainHosts()
115
throws URISyntaxException, InterruptedException {
116
try (SimpleHttpServer helloServer =
117
createSimpleHttpServer(
118
"/index.html", "<!DOCTYPE html><title>Hello</title><h3>Hello, world!</h3>");
119
SimpleHttpServer goodbyeServer =
120
createSimpleHttpServer(
121
helloServer.baseUri().resolve("/index.html").toString(),
122
"<!DOCTYPE html><title>Goodbye</title><h3>Goodbye, world!</h3>");
123
SimpleHttpServer pacFileServer =
124
createPacfileServer(
125
"/proxy.pac",
126
String.join(
127
"\n",
128
"function FindProxyForURL(url, host) {",
129
" if (url.indexOf('" + getHostAndPort(helloServer) + "') != -1) {",
130
" return 'PROXY " + getHostAndPort(goodbyeServer) + "';",
131
" }",
132
" return 'DIRECT';",
133
"}"))) {
134
135
Proxy proxy = new Proxy();
136
proxy.setProxyAutoconfigUrl("http://" + getHostAndPort(pacFileServer) + "/proxy.pac");
137
138
createNewDriver(new ImmutableCapabilities(PROXY, proxy));
139
140
driver.get("http://" + getHostAndPort(helloServer) + "/index.html");
141
assertThat(driver.findElement(By.tagName("h3")).getText()).isEqualTo("Goodbye, world!");
142
143
driver.get(appServer.whereElseIs("simpleTest.html"));
144
assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("Heading");
145
}
146
}
147
148
private SimpleHttpServer createSimpleHttpServer(String requestPath, String responseHtml)
149
throws URISyntaxException, InterruptedException {
150
SimpleHttpServer server = new SimpleHttpServer();
151
byte[] bytes = responseHtml.getBytes(UTF_8);
152
153
server.registerEndpoint(HttpMethod.GET, requestPath, "text/html; charset=utf-8", bytes);
154
155
return server;
156
}
157
158
private SimpleHttpServer createPacfileServer(String requestPath, String responsePac)
159
throws URISyntaxException, InterruptedException {
160
SimpleHttpServer server = new SimpleHttpServer();
161
byte[] bytes = responsePac.getBytes(US_ASCII);
162
163
server.registerEndpoint(
164
HttpMethod.GET, requestPath, "application/x-ns-proxy-autoconfig", bytes);
165
166
return server;
167
}
168
169
private static String getHostAndPort(SimpleHttpServer server) {
170
URI baseUri = server.baseUri();
171
return String.format("%s:%s", baseUri.getHost(), baseUri.getPort());
172
}
173
174
public static class FakeProxyServer extends SimpleHttpServer {
175
private final Set<String> resources = new HashSet<>();
176
177
public FakeProxyServer() throws URISyntaxException, InterruptedException {}
178
179
@Override
180
protected FullHttpResponse handleRequest(HttpRequest requested) {
181
String[] parts = requested.uri().split("/");
182
183
if (parts.length > 1) {
184
resources.add(parts[parts.length - 1]);
185
}
186
187
return super.handleRequest(requested);
188
}
189
190
/**
191
* Checks if a resource has been requested using the short name of the resource.
192
*
193
* @param resourceName The short name of the resource to check.
194
* @return true if the resource has been called.
195
*/
196
public boolean hasBeenCalled(String resourceName) {
197
return resources.contains(resourceName);
198
}
199
200
public Proxy asProxy() {
201
Proxy proxy = new Proxy();
202
URI baseUri = baseUri();
203
proxy.setHttpProxy(baseUri.getHost() + ":" + baseUri.getPort());
204
return proxy;
205
}
206
}
207
}
208
209