View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin.httpclient;
25  
26  import com.github.tomakehurst.wiremock.junit.WireMockRule;
27  import org.apache.http.client.HttpClient;
28  import org.apache.http.client.methods.HttpGet;
29  import org.apache.http.util.EntityUtils;
30  import org.apache.maven.settings.Proxy;
31  import org.junit.Rule;
32  import org.junit.Test;
33  import org.junit.jupiter.api.Assertions;
34  
35  import static com.github.tomakehurst.wiremock.client.WireMock.*;
36  
37  public class HttpClientFactoryTest {
38  
39      private final int PROXY_PORT = 9797;
40      private final int TARGET_PORT = 9696;
41      private final String TARGET_URL = "http://localhost:" + TARGET_PORT;
42  
43      @Rule
44      public WireMockRule targetServer = new WireMockRule(TARGET_PORT);
45  
46      @Rule
47      public WireMockRule proxyServer = new WireMockRule(PROXY_PORT);
48  
49  
50      @Test
51      public void testSimpleRequest() throws Exception {
52          targetServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello World!")));
53  
54          HttpClient client = new HttpClientFactory(TARGET_URL).create();
55          String body = EntityUtils.toString(client.execute(new HttpGet(TARGET_URL)).getEntity());
56  
57          Assertions.assertEquals("Hello World!", body);
58      }
59  
60      @Test
61      public void testUnAuthorizedProxyRequest() throws Exception {
62          targetServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello World!")));
63  
64          proxyServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello Proxy!")));
65  
66          Proxy proxy = new Proxy();
67          proxy.setHost("localhost");
68          proxy.setPort(PROXY_PORT);
69          proxy.setProtocol("http");
70  
71          HttpClient client = new HttpClientFactory(TARGET_URL).proxy(proxy).create();
72          String body = EntityUtils.toString(client.execute(new HttpGet(TARGET_URL)).getEntity());
73  
74          Assertions.assertEquals("Hello Proxy!", body);
75      }
76  
77      @Test
78      public void testAuthorixedProxyRequest() throws Exception {
79          targetServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello World!")));
80  
81          proxyServer.stubFor(get(urlMatching(".*")).withHeader("Proxy-Authorization", matching("Basic Zm9vOmJhcg=="))
82                  .willReturn(aResponse().withBody("Hello Proxy!"))
83                  .atPriority(1));
84          proxyServer.stubFor(any(urlMatching(".*"))
85                  .willReturn(aResponse().withStatus(407).withHeader("Proxy-Authenticate", "Basic"))
86                  .atPriority(2));
87  
88          Proxy proxy = new Proxy();
89          proxy.setHost("localhost");
90          proxy.setPort(PROXY_PORT);
91          proxy.setProtocol("http");
92          proxy.setUsername("foo");
93          proxy.setPassword("bar");
94  
95          HttpClient client = new HttpClientFactory(TARGET_URL).proxy(proxy).create();
96          String body = EntityUtils.toString(client.execute(new HttpGet(TARGET_URL)).getEntity());
97  
98          Assertions.assertEquals("Hello Proxy!", body);
99      }
100 
101     @Test
102     public void testNonProxiedHostRequest() throws Exception {
103         targetServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello World!")));
104 
105         proxyServer.stubFor(get(urlMatching(".*")).willReturn(aResponse().withBody("Hello Proxy!")));
106 
107         Proxy proxy = new Proxy();
108         proxy.setHost("localhost");
109         proxy.setPort(PROXY_PORT);
110         proxy.setProtocol("http");
111         proxy.setNonProxyHosts("localhost|example.com");
112 
113         HttpClient client = new HttpClientFactory(TARGET_URL).proxy(proxy).create();
114         String body = EntityUtils.toString(client.execute(new HttpGet(TARGET_URL)).getEntity());
115 
116         Assertions.assertEquals("Hello World!", body);
117     }
118 }