Skip to content

Commit 70cc228

Browse files
author
Tomasz Lelek
committed
BAEL-12 advanced http client
1 parent 63b042a commit 70cc228

2 files changed

Lines changed: 148 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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.CredentialsProvider;
11+
import org.apache.http.client.HttpClient;
12+
import org.apache.http.client.methods.HttpGet;
13+
import org.apache.http.client.methods.HttpPost;
14+
import org.apache.http.entity.StringEntity;
15+
import org.apache.http.impl.client.BasicCredentialsProvider;
16+
import org.apache.http.impl.client.HttpClients;
17+
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import java.io.IOException;
22+
23+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class HttpClientAdvancedConfiguration {
27+
28+
@Rule
29+
public WireMockRule serviceMock = new WireMockRule(8089);
30+
31+
@Rule
32+
public WireMockRule proxyMock = new WireMockRule(8090);
33+
34+
@Test
35+
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
36+
//given
37+
serviceMock.stubFor(get(urlEqualTo("/detail"))
38+
.withHeader("User-Agent", equalTo("BaeldungAgent/1.0"))
39+
.willReturn(aResponse()
40+
.withStatus(200)));
41+
42+
String userAgent = "BaeldungAgent/1.0";
43+
HttpClient httpClient = HttpClients.createDefault();
44+
final HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
45+
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
46+
47+
//when
48+
HttpResponse response = httpClient.execute(httpGet);
49+
50+
//then
51+
assertEquals(response.getStatusLine().getStatusCode(), 200);
52+
}
53+
54+
@Test
55+
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
56+
//given
57+
serviceMock.stubFor(post(urlEqualTo("/person"))
58+
.withHeader("Content-Type", equalTo("application/xml"))
59+
.withRequestBody(equalTo("<xml><id>1</id></xml>"))
60+
.willReturn(aResponse()
61+
.withStatus(200)));
62+
63+
HttpClient httpClient = HttpClients.createDefault();
64+
HttpPost httpPost = new HttpPost("http://localhost:8089/person");
65+
httpPost.setHeader("Content-Type", "application/xml");
66+
StringEntity xmlEntity = new StringEntity("<xml><id>1</id></xml>");
67+
httpPost.setEntity(xmlEntity);
68+
69+
//when
70+
HttpResponse response = httpClient.execute(httpPost);
71+
72+
//then
73+
assertEquals(response.getStatusLine().getStatusCode(), 200);
74+
75+
}
76+
77+
@Test
78+
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
79+
//given
80+
proxyMock.stubFor(get(urlMatching(".*"))
81+
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
82+
83+
serviceMock.stubFor(get(urlEqualTo("/private"))
84+
.willReturn(aResponse().withStatus(200)));
85+
86+
87+
HttpHost proxy = new HttpHost("localhost", 8090);
88+
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
89+
HttpClient httpclient = HttpClients.custom()
90+
.setRoutePlanner(routePlanner)
91+
.build();
92+
93+
//when
94+
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
95+
HttpResponse response = httpclient.execute(httpGet);
96+
97+
//then
98+
assertEquals(response.getStatusLine().getStatusCode(), 200);
99+
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
100+
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
101+
}
102+
103+
@Test
104+
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
105+
//given
106+
proxyMock.stubFor(get(urlMatching(".*"))
107+
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
108+
109+
serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password"))
110+
.willReturn(aResponse().withStatus(200)));
111+
112+
113+
HttpHost proxy = new HttpHost("localhost", 8090);
114+
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
115+
116+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
117+
credentialsProvider.setCredentials(new AuthScope(proxy),
118+
new UsernamePasswordCredentials("username_admin", "secret_password"));
119+
120+
HttpClient httpclient = HttpClients.custom()
121+
.setRoutePlanner(routePlanner)
122+
.setDefaultCredentialsProvider(credentialsProvider)
123+
.build();
124+
125+
126+
//when
127+
final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password");
128+
HttpResponse response = httpclient.execute(httpGet);
129+
130+
//then
131+
assertEquals(response.getStatusLine().getStatusCode(), 200);
132+
proxyMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
133+
serviceMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
134+
}
135+
136+
137+
}

0 commit comments

Comments
 (0)