1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 }