From 70cc228f1e5761c724bbc86dc6665a99a312320b Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 11 Feb 2017 16:53:26 +0100 Subject: [PATCH 1/5] BAEL-12 advanced http client --- httpclient/pom.xml | 13 +- .../HttpClientAdvancedConfiguration.java | 137 ++++++++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java diff --git a/httpclient/pom.xml b/httpclient/pom.xml index be0daae995..eec705b224 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 com.baeldung httpclient @@ -113,6 +114,13 @@ ${mockito.version} test + + com.github.tomakehurst + wiremock + ${wiremock.version} + test + + @@ -145,7 +153,7 @@ **/*LiveTest.java - + @@ -202,6 +210,7 @@ 1.3 4.12 1.10.19 + 2.5.1 4.4.5 4.5.2 diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java new file mode 100644 index 0000000000..5bb86de2ac --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java @@ -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("1")) + .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("1"); + 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"))); + } + + +} From 34bfeed011ad8429d8a72071733a1040e9cc1eac Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 11 Feb 2017 17:15:08 +0100 Subject: [PATCH 2/5] BAEL-12 end-to-end test case for auth proxy --- .../HttpClientAdvancedConfiguration.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java index 5bb86de2ac..1bb176876c 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java @@ -7,11 +7,15 @@ 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.AuthCache; 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.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.DefaultProxyRoutePlanner; @@ -103,20 +107,32 @@ public class HttpClientAdvancedConfiguration { @Test public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException { //given - proxyMock.stubFor(get(urlMatching(".*")) + proxyMock.stubFor(get(urlMatching("/private")) .willReturn(aResponse().proxiedFrom("http://localhost:8089/"))); - - serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password")) + serviceMock.stubFor(get(urlEqualTo("/private")) .willReturn(aResponse().withStatus(200))); HttpHost proxy = new HttpHost("localhost", 8090); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); + // Client credentials CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("username_admin", "secret_password")); + + // Create AuthCache instance + AuthCache authCache = new BasicAuthCache(); + + // Generate BASIC scheme object and add it to the local auth cache + BasicScheme basicAuth = new BasicScheme(); + authCache.put(proxy, basicAuth); + HttpClientContext context = HttpClientContext.create(); + context.setCredentialsProvider(credentialsProvider); + context.setAuthCache(authCache); + + HttpClient httpclient = HttpClients.custom() .setRoutePlanner(routePlanner) .setDefaultCredentialsProvider(credentialsProvider) @@ -124,13 +140,13 @@ public class HttpClientAdvancedConfiguration { //when - final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password"); - HttpResponse response = httpclient.execute(httpGet); + final HttpGet httpGet = new HttpGet("http://localhost:8089/private"); + HttpResponse response = httpclient.execute(httpGet, context); //then assertEquals(response.getStatusLine().getStatusCode(), 200); - proxyMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password"))); - serviceMock.verify(getRequestedFor(urlEqualTo("/private/username_admin/secret_password"))); + proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic"))); + serviceMock.verify(getRequestedFor(urlEqualTo("/private"))); } From 3eb0575708eb3e907a0738c2beee09315791fa57 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 14 Feb 2017 09:59:12 +0100 Subject: [PATCH 3/5] BAEL-12 userAgent header value in one place --- .../advancedconfig/HttpClientAdvancedConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java index 1bb176876c..ac51f75b11 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java @@ -38,12 +38,12 @@ public class HttpClientAdvancedConfiguration { @Test public void givenClientWithCustomUserAgentHeader_whenExecuteRequest_shouldReturn200() throws IOException { //given + String userAgent = "BaeldungAgent/1.0"; serviceMock.stubFor(get(urlEqualTo("/detail")) - .withHeader("User-Agent", equalTo("BaeldungAgent/1.0")) + .withHeader("User-Agent", equalTo(userAgent)) .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); From a985a9fb5a6ce3bfe4a9a213a89dccd138df5f59 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 14 Feb 2017 10:01:09 +0100 Subject: [PATCH 4/5] BAEL-12 remove final --- .../advancedconfig/HttpClientAdvancedConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java index ac51f75b11..99389a001a 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java @@ -45,7 +45,7 @@ public class HttpClientAdvancedConfiguration { .withStatus(200))); HttpClient httpClient = HttpClients.createDefault(); - final HttpGet httpGet = new HttpGet("http://localhost:8089/detail"); + HttpGet httpGet = new HttpGet("http://localhost:8089/detail"); httpGet.setHeader(HttpHeaders.USER_AGENT, userAgent); //when From 24d0acbbd6abc58293ed4b48045adf54f1109cab Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 14 Feb 2017 10:05:16 +0100 Subject: [PATCH 5/5] BAEL-12 xmlBody into a variable --- .../advancedconfig/HttpClientAdvancedConfiguration.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java index 99389a001a..9b5cb3f293 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/advancedconfig/HttpClientAdvancedConfiguration.java @@ -58,16 +58,17 @@ public class HttpClientAdvancedConfiguration { @Test public void givenClientThatSendDataInBody_whenSendXmlInBody_shouldReturn200() throws IOException { //given + String xmlBody = "1"; serviceMock.stubFor(post(urlEqualTo("/person")) .withHeader("Content-Type", equalTo("application/xml")) - .withRequestBody(equalTo("1")) + .withRequestBody(equalTo(xmlBody)) .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("1"); + StringEntity xmlEntity = new StringEntity(xmlBody); httpPost.setEntity(xmlEntity); //when