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.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   * Tests methods in {@link HttpWebConnection}.
27   *
28   * @author John J Murdoch
29   * @author Ronald Brill
30   */
31  public class HttpWebConnectionProxyTest extends WebServerTestCase {
32  
33      private Server proxyWebServer_;
34  
35      /**
36       * Performs pre-test construction.
37       * @throws Exception if an error occurs
38       */
39      @BeforeEach
40      public void setup() throws Exception {
41          // we have to stop all servers running already to free the port
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       * Performs post-test deconstruction.
59       * @throws Exception if an error occurs
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       * See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e299
75       *
76       * Non client params should not be placed on <code>HttpClient</code>. They
77       * should be carried by the request itself.
78       * @throws Exception if the test fails
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  }