|
7 | 7 | import org.apache.http.HttpResponse; |
8 | 8 | import org.apache.http.auth.AuthScope; |
9 | 9 | import org.apache.http.auth.UsernamePasswordCredentials; |
| 10 | +import org.apache.http.client.AuthCache; |
10 | 11 | import org.apache.http.client.CredentialsProvider; |
11 | 12 | import org.apache.http.client.HttpClient; |
12 | 13 | import org.apache.http.client.methods.HttpGet; |
13 | 14 | import org.apache.http.client.methods.HttpPost; |
| 15 | +import org.apache.http.client.protocol.HttpClientContext; |
14 | 16 | import org.apache.http.entity.StringEntity; |
| 17 | +import org.apache.http.impl.auth.BasicScheme; |
| 18 | +import org.apache.http.impl.client.BasicAuthCache; |
15 | 19 | import org.apache.http.impl.client.BasicCredentialsProvider; |
16 | 20 | import org.apache.http.impl.client.HttpClients; |
17 | 21 | import org.apache.http.impl.conn.DefaultProxyRoutePlanner; |
@@ -103,34 +107,46 @@ public void givenServerThatIsBehindProxy_whenClientIsConfiguredToSendRequestViaP |
103 | 107 | @Test |
104 | 108 | public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException { |
105 | 109 | //given |
106 | | - proxyMock.stubFor(get(urlMatching(".*")) |
| 110 | + proxyMock.stubFor(get(urlMatching("/private")) |
107 | 111 | .willReturn(aResponse().proxiedFrom("http://localhost:8089/"))); |
108 | | - |
109 | | - serviceMock.stubFor(get(urlEqualTo("/private/username_admin/secret_password")) |
| 112 | + serviceMock.stubFor(get(urlEqualTo("/private")) |
110 | 113 | .willReturn(aResponse().withStatus(200))); |
111 | 114 |
|
112 | 115 |
|
113 | 116 | HttpHost proxy = new HttpHost("localhost", 8090); |
114 | 117 | DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); |
115 | 118 |
|
| 119 | + // Client credentials |
116 | 120 | CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
117 | 121 | credentialsProvider.setCredentials(new AuthScope(proxy), |
118 | 122 | new UsernamePasswordCredentials("username_admin", "secret_password")); |
119 | 123 |
|
| 124 | + |
| 125 | + // Create AuthCache instance |
| 126 | + AuthCache authCache = new BasicAuthCache(); |
| 127 | + |
| 128 | + // Generate BASIC scheme object and add it to the local auth cache |
| 129 | + BasicScheme basicAuth = new BasicScheme(); |
| 130 | + authCache.put(proxy, basicAuth); |
| 131 | + HttpClientContext context = HttpClientContext.create(); |
| 132 | + context.setCredentialsProvider(credentialsProvider); |
| 133 | + context.setAuthCache(authCache); |
| 134 | + |
| 135 | + |
120 | 136 | HttpClient httpclient = HttpClients.custom() |
121 | 137 | .setRoutePlanner(routePlanner) |
122 | 138 | .setDefaultCredentialsProvider(credentialsProvider) |
123 | 139 | .build(); |
124 | 140 |
|
125 | 141 |
|
126 | 142 | //when |
127 | | - final HttpGet httpGet = new HttpGet("http://localhost:8089/private/username_admin/secret_password"); |
128 | | - HttpResponse response = httpclient.execute(httpGet); |
| 143 | + final HttpGet httpGet = new HttpGet("http://localhost:8089/private"); |
| 144 | + HttpResponse response = httpclient.execute(httpGet, context); |
129 | 145 |
|
130 | 146 | //then |
131 | 147 | 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"))); |
| 148 | + proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic"))); |
| 149 | + serviceMock.verify(getRequestedFor(urlEqualTo("/private"))); |
134 | 150 | } |
135 | 151 |
|
136 | 152 |
|
|
0 commit comments