Skip to content

Commit 8f01c27

Browse files
committed
Merge branch 'tomekl007-BAEL-12'
2 parents 7f4c715 + 5351ac8 commit 8f01c27

2 files changed

Lines changed: 165 additions & 2 deletions

File tree

‎httpclient/pom.xml‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34
<groupId>com.baeldung</groupId>
45
<artifactId>httpclient</artifactId>
@@ -113,6 +114,13 @@
113114
<version>${mockito.version}</version>
114115
<scope>test</scope>
115116
</dependency>
117+
<dependency>
118+
<groupId>com.github.tomakehurst</groupId>
119+
<artifactId>wiremock</artifactId>
120+
<version>${wiremock.version}</version>
121+
<scope>test</scope>
122+
</dependency>
123+
116124

117125
</dependencies>
118126

@@ -145,7 +153,7 @@
145153
<excludes>
146154
<exclude>**/*LiveTest.java</exclude>
147155
</excludes>
148-
</configuration>
156+
</configuration>
149157
</plugin>
150158

151159
</plugins>
@@ -202,6 +210,7 @@
202210
<org.hamcrest.version>1.3</org.hamcrest.version>
203211
<junit.version>4.12</junit.version>
204212
<mockito.version>1.10.19</mockito.version>
213+
<wiremock.version>2.5.1</wiremock.version>
205214

206215
<httpcore.version>4.4.5</httpcore.version>
207216
<httpclient.version>4.5.2</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package org.baeldung.httpclient.advancedconfig;
2+
3+
4+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
5+
import org.apache.http.HttpHeaders;
6+
import org.apache.http.HttpHost;
7+
import org.apache.http.HttpResponse;
8+
import org.apache.http.auth.AuthScope;
9+
import org.apache.http.auth.UsernamePasswordCredentials;
10+
import org.apache.http.client.AuthCache;
11+
import org.apache.http.client.CredentialsProvider;
12+
import org.apache.http.client.HttpClient;
13+
import org.apache.http.client.methods.HttpGet;
14+
import org.apache.http.client.methods.HttpPost;
15+
import org.apache.http.client.protocol.HttpClientContext;
16+
import org.apache.http.entity.StringEntity;
17+
import org.apache.http.impl.auth.BasicScheme;
18+
import org.apache.http.impl.client.BasicAuthCache;
19+
import org.apache.http.impl.client.BasicCredentialsProvider;
20+
import org.apache.http.impl.client.HttpClients;
21+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
25+
import java.io.IOException;
26+
27+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
28+
import static org.junit.Assert.assertEquals;
29+
30+
public class HttpClientAdvancedConfiguration {
31+
32+
@Rule
33+
public WireMockRule serviceMock = new WireMockRule(8089);
34+
35+
@Rule
36+
public WireMockRule proxyMock = new WireMockRule(8090);
37+
38+
@Test
39+
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
40+
//given
41+
String userAgent = "BaeldungAgent/1.0";
42+
serviceMock.stubFor(get(urlEqualTo("/detail"))
43+
.withHeader("User-Agent", equalTo(userAgent))
44+
.willReturn(aResponse()
45+
.withStatus(200)));
46+
47+
HttpClient httpClient = HttpClients.createDefault();
48+
HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
49+
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
50+
51+
//when
52+
HttpResponse response = httpClient.execute(httpGet);
53+
54+
//then
55+
assertEquals(response.getStatusLine().getStatusCode(), 200);
56+
}
57+
58+
@Test
59+
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
60+
//given
61+
String xmlBody = "<xml><id>1</id></xml>";
62+
serviceMock.stubFor(post(urlEqualTo("/person"))
63+
.withHeader("Content-Type", equalTo("application/xml"))
64+
.withRequestBody(equalTo(xmlBody))
65+
.willReturn(aResponse()
66+
.withStatus(200)));
67+
68+
HttpClient httpClient = HttpClients.createDefault();
69+
HttpPost httpPost = new HttpPost("http://localhost:8089/person");
70+
httpPost.setHeader("Content-Type", "application/xml");
71+
StringEntity xmlEntity = new StringEntity(xmlBody);
72+
httpPost.setEntity(xmlEntity);
73+
74+
//when
75+
HttpResponse response = httpClient.execute(httpPost);
76+
77+
//then
78+
assertEquals(response.getStatusLine().getStatusCode(), 200);
79+
80+
}
81+
82+
@Test
83+
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
84+
//given
85+
proxyMock.stubFor(get(urlMatching(".*"))
86+
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
87+
88+
serviceMock.stubFor(get(urlEqualTo("/private"))
89+
.willReturn(aResponse().withStatus(200)));
90+
91+
92+
HttpHost proxy = new HttpHost("localhost", 8090);
93+
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
94+
HttpClient httpclient = HttpClients.custom()
95+
.setRoutePlanner(routePlanner)
96+
.build();
97+
98+
//when
99+
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
100+
HttpResponse response = httpclient.execute(httpGet);
101+
102+
//then
103+
assertEquals(response.getStatusLine().getStatusCode(), 200);
104+
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
105+
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
106+
}
107+
108+
@Test
109+
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
110+
//given
111+
proxyMock.stubFor(get(urlMatching("/private"))
112+
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
113+
serviceMock.stubFor(get(urlEqualTo("/private"))
114+
.willReturn(aResponse().withStatus(200)));
115+
116+
117+
HttpHost proxy = new HttpHost("localhost", 8090);
118+
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
119+
120+
// Client credentials
121+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
122+
credentialsProvider.setCredentials(new AuthScope(proxy),
123+
new UsernamePasswordCredentials("username_admin", "secret_password"));
124+
125+
126+
// Create AuthCache instance
127+
AuthCache authCache = new BasicAuthCache();
128+
129+
// Generate BASIC scheme object and add it to the local auth cache
130+
BasicScheme basicAuth = new BasicScheme();
131+
authCache.put(proxy, basicAuth);
132+
HttpClientContext context = HttpClientContext.create();
133+
context.setCredentialsProvider(credentialsProvider);
134+
context.setAuthCache(authCache);
135+
136+
137+
HttpClient httpclient = HttpClients.custom()
138+
.setRoutePlanner(routePlanner)
139+
.setDefaultCredentialsProvider(credentialsProvider)
140+
.build();
141+
142+
143+
//when
144+
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
145+
HttpResponse response = httpclient.execute(httpGet, context);
146+
147+
//then
148+
assertEquals(response.getStatusLine().getStatusCode(), 200);
149+
proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
150+
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
151+
}
152+
153+
154+
}

0 commit comments

Comments
 (0)