|
| 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