View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.net.URL;
18  
19  import org.eclipse.jetty.server.Server;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.htmlunit.util.UrlUtils;
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  
27  /**
28   * Tests methods in {@link HttpWebConnection}.
29   *
30   * @author John J Murdoch
31   * @author Ronald Brill
32   */
33  @RunWith(BrowserRunner.class)
34  public class HttpWebConnectionProxyTest extends WebServerTestCase {
35  
36      private Server proxyWebServer_;
37  
38      /**
39       * Performs pre-test construction.
40       * @throws Exception if an error occurs
41       */
42      @Before
43      public void setup() throws Exception {
44          // we have to stop all servers running already to free the port
45          WebDriverTestCase.stopWebServers();
46          startWebServer("src/test/resources/testfiles/noproxyroot/");
47  
48          final Server proxyWebServer = createWebServer(PORT_PROXY_SERVER,
49                                              "src/test/resources/testfiles/proxyroot/", null, null, null);
50          WebServerTestCase.tryStart(PORT_PROXY_SERVER, proxyWebServer);
51          proxyWebServer_ = proxyWebServer;
52  
53          final WebClient webClient = getWebClient();
54  
55          final ProxyConfig proxy = new ProxyConfig(SOCKS_PROXY_HOST, PORT_PROXY_SERVER, null);
56          proxy.addHostsToProxyBypass("127.0.0.1");
57          webClient.getOptions().setProxyConfig(proxy);
58      }
59  
60      /**
61       * Performs post-test deconstruction.
62       * @throws Exception if an error occurs
63       */
64      @Override
65      @After
66      public void tearDown() throws Exception {
67          if (proxyWebServer_ != null) {
68              proxyWebServer_.stop();
69              proxyWebServer_.destroy();
70          }
71          proxyWebServer_ = null;
72  
73          super.tearDown();
74      }
75  
76      /**
77       * See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e299
78       *
79       * Non client params should not be placed on <code>HttpClient</code>. They
80       * should be carried by the request itself.
81       * @throws Exception if the test fails
82       */
83      @Test
84      public void proxySettingsAreNotUsedForSubsequentRequestToNonProxyHosts() throws Exception {
85          URL resourceUrl = new URL("http://localhost:" + PORT_PROXY_SERVER + "/response.txt");
86          TextPage page = getWebClient().getPage(UrlUtils.getUrlWithNewPort(resourceUrl, PORT_PROXY_SERVER));
87          assertEquals("proxy should have been used", "proxy-response", page.getContent().trim());
88  
89          resourceUrl = new URL("http://127.0.0.1:" + PORT + "/response.txt");
90          page = getWebClient().getPage(resourceUrl);
91          assertEquals("proxy should not be used", "no-proxy-response", page.getContent().trim());
92      }
93  }