1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import java.net.URL;
18
19 import org.eclipse.jetty.server.Server;
20 import org.htmlunit.util.UrlUtils;
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28
29
30
31 public class HttpWebConnectionProxyTest extends WebServerTestCase {
32
33 private Server proxyWebServer_;
34
35
36
37
38
39 @BeforeEach
40 public void setup() throws Exception {
41
42 WebDriverTestCase.stopWebServers();
43 startWebServer("src/test/resources/testfiles/noproxyroot/");
44
45 final Server proxyWebServer = createWebServer(PORT_PROXY_SERVER,
46 "src/test/resources/testfiles/proxyroot/", null, null, null);
47 WebServerTestCase.tryStart(PORT_PROXY_SERVER, proxyWebServer);
48 proxyWebServer_ = proxyWebServer;
49
50 final WebClient webClient = getWebClient();
51
52 final ProxyConfig proxy = new ProxyConfig(SOCKS_PROXY_HOST, PORT_PROXY_SERVER, null);
53 proxy.addHostsToProxyBypass("127.0.0.1");
54 webClient.getOptions().setProxyConfig(proxy);
55 }
56
57
58
59
60
61 @Override
62 @AfterEach
63 public void tearDown() throws Exception {
64 if (proxyWebServer_ != null) {
65 proxyWebServer_.stop();
66 proxyWebServer_.destroy();
67 }
68 proxyWebServer_ = null;
69
70 super.tearDown();
71 }
72
73
74
75
76
77
78
79
80 @Test
81 public void proxySettingsAreNotUsedForSubsequentRequestToNonProxyHosts() throws Exception {
82 URL resourceUrl = new URL("http://localhost:" + PORT_PROXY_SERVER + "/response.txt");
83 TextPage page = getWebClient().getPage(UrlUtils.getUrlWithNewPort(resourceUrl, PORT_PROXY_SERVER));
84 assertEquals("proxy should have been used", "proxy-response", page.getContent().trim());
85
86 resourceUrl = new URL("http://127.0.0.1:" + PORT + "/response.txt");
87 page = getWebClient().getPage(resourceUrl);
88 assertEquals("proxy should not be used", "no-proxy-response", page.getContent().trim());
89 }
90 }