BAEL-12 advanced http client
This commit is contained in:
parent
63b042a9d8
commit
70cc228f1e
|
@ -1,4 +1,5 @@
|
||||||
<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">
|
<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">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
|
@ -113,6 +114,13 @@
|
||||||
<version>${mockito.version}</version>
|
<version>${mockito.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tomakehurst</groupId>
|
||||||
|
<artifactId>wiremock</artifactId>
|
||||||
|
<version>${wiremock.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -145,7 +153,7 @@
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -202,6 +210,7 @@
|
||||||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<mockito.version>1.10.19</mockito.version>
|
<mockito.version>1.10.19</mockito.version>
|
||||||
|
<wiremock.version>2.5.1</wiremock.version>
|
||||||
|
|
||||||
<httpcore.version>4.4.5</httpcore.version>
|
<httpcore.version>4.4.5</httpcore.version>
|
||||||
<httpclient.version>4.5.2</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
<httpclient.version>4.5.2</httpclient.version> <!-- 4.3.6 --> <!-- 4.4-beta1 -->
|
||||||
|
|
|
@ -0,0 +1,137 @@
|
||||||
|
package org.baeldung.httpclient.advancedconfig;
|
||||||
|
|
||||||
|
|
||||||
|
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||||
|
import org.apache.http.HttpHeaders;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.CredentialsProvider;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class HttpClientAdvancedConfiguration {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public WireMockRule serviceMock = new WireMockRule(8089);
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public WireMockRule proxyMock = new WireMockRule(8090);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException {
|
||||||
|
//given
|
||||||
|
serviceMock.stubFor(get(urlEqualTo("/detail"))
|
||||||
|
.withHeader("User-Agent", equalTo("BaeldungAgent/1.0"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)));
|
||||||
|
|
||||||
|
String userAgent = "BaeldungAgent/1.0";
|
||||||
|
HttpClient httpClient = HttpClients.createDefault();
|
||||||
|
final HttpGet httpGet = new HttpGet("http://localhost:8089/detail");
|
||||||
|
httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent);
|
||||||
|
|
||||||
|
//when
|
||||||
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException {
|
||||||
|
//given
|
||||||
|
serviceMock.stubFor(post(urlEqualTo("/person"))
|
||||||
|
.withHeader("Content-Type", equalTo("application/xml"))
|
||||||
|
.withRequestBody(equalTo("<xml><id>1</id></xml>"))
|
||||||
|
.willReturn(aResponse()
|
||||||
|
.withStatus(200)));
|
||||||
|
|
||||||
|
HttpClient httpClient = HttpClients.createDefault();
|
||||||
|
HttpPost httpPost = new HttpPost("http://localhost:8089/person");
|
||||||
|
httpPost.setHeader("Content-Type", "application/xml");
|
||||||
|
StringEntity xmlEntity = new StringEntity("<xml><id>1</id></xml>");
|
||||||
|
httpPost.setEntity(xmlEntity);
|
||||||
|
|
||||||
|
//when
|
||||||
|
HttpResponse response = httpClient.execute(httpPost);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaProxy_shouldReturn200() throws IOException {
|
||||||
|
//given
|
||||||
|
proxyMock.stubFor(get(urlMatching(".*"))
|
||||||
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
|
|
||||||
|
serviceMock.stubFor(get(urlEqualTo("/private"))
|
||||||
|
.willReturn(aResponse().withStatus(200)));
|
||||||
|
|
||||||
|
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8090);
|
||||||
|
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||||
|
HttpClient httpclient = HttpClients.custom()
|
||||||
|
.setRoutePlanner(routePlanner)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//when
|
||||||
|
final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
|
||||||
|
HttpResponse response = httpclient.execute(httpGet);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||||
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
|
||||||
|
//given
|
||||||
|
proxyMock.stubFor(get(urlMatching(".*"))
|
||||||
|
.willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
|
||||||
|
|
||||||
|
serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password"))
|
||||||
|
.willReturn(aResponse().withStatus(200)));
|
||||||
|
|
||||||
|
|
||||||
|
HttpHost proxy = new HttpHost("localhost", 8090);
|
||||||
|
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
|
||||||
|
|
||||||
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||||
|
credentialsProvider.setCredentials(new AuthScope(proxy),
|
||||||
|
new UsernamePasswordCredentials("username_admin", "secret_password"));
|
||||||
|
|
||||||
|
HttpClient httpclient = HttpClients.custom()
|
||||||
|
.setRoutePlanner(routePlanner)
|
||||||
|
.setDefaultCredentialsProvider(credentialsProvider)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
//when
|
||||||
|
final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password");
|
||||||
|
HttpResponse response = httpclient.execute(httpGet);
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(response.getStatusLine().getStatusCode(), 200);
|
||||||
|
proxyMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
|
||||||
|
serviceMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password")));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue