|
34 | 34 |
|
35 | 35 | import org.apache.hc.client5.http.AuthenticationStrategy; |
36 | 36 | import org.apache.hc.client5.http.HttpRoute; |
| 37 | +import org.apache.hc.client5.http.RouteInfo; |
37 | 38 | import org.apache.hc.client5.http.auth.AuthExchange; |
38 | 39 | import org.apache.hc.client5.http.auth.AuthScope; |
39 | 40 | import org.apache.hc.client5.http.auth.ChallengeType; |
|
43 | 44 | import org.apache.hc.client5.http.classic.methods.HttpGet; |
44 | 45 | import org.apache.hc.client5.http.classic.methods.HttpPost; |
45 | 46 | import org.apache.hc.client5.http.entity.EntityBuilder; |
| 47 | +import org.apache.hc.client5.http.impl.auth.BasicAuthCache; |
46 | 48 | import org.apache.hc.client5.http.impl.auth.BasicScheme; |
47 | 49 | import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder; |
48 | 50 | import org.apache.hc.client5.http.protocol.HttpClientContext; |
|
58 | 60 | import org.junit.jupiter.api.BeforeEach; |
59 | 61 | import org.junit.jupiter.api.Test; |
60 | 62 | import org.junit.jupiter.api.extension.ExtendWith; |
| 63 | +import org.mockito.ArgumentCaptor; |
61 | 64 | import org.mockito.Mock; |
62 | 65 | import org.mockito.Mockito; |
63 | 66 | import org.mockito.junit.jupiter.MockitoExtension; |
@@ -295,4 +298,105 @@ void testAuthExchangePathPrefixRestoredAfterChallenge() throws Exception { |
295 | 298 | final AuthExchange authExchange = context.getAuthExchange(target); |
296 | 299 | Assertions.assertEquals("/blah/", authExchange.getPathPrefix()); |
297 | 300 | } |
| 301 | + |
| 302 | + @Test |
| 303 | + void testProxyChallengeThroughTunnelIsReturnedToCaller() throws Exception { |
| 304 | + final HttpHost httpsTarget = new HttpHost("https", "foo", 443); |
| 305 | + final HttpHost proxy = new HttpHost("http", "proxy", 8080); |
| 306 | + final HttpRoute route = new HttpRoute(httpsTarget, null, proxy, true, |
| 307 | + RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED); |
| 308 | + final HttpClientContext context = HttpClientContext.create(); |
| 309 | + context.setCredentialsProvider(CredentialsProviderBuilder.create() |
| 310 | + .add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray()) |
| 311 | + .build()); |
| 312 | + final ClassicHttpRequest request = new HttpGet("https://foo/test"); |
| 313 | + final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?"); |
| 314 | + response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test"); |
| 315 | + |
| 316 | + Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response); |
| 317 | + |
| 318 | + final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context); |
| 319 | + final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain); |
| 320 | + Assertions.assertEquals(407, finalResponse.getCode()); |
| 321 | + Mockito.verify(chain, Mockito.times(1)).proceed(request, scope); |
| 322 | + Mockito.verifyNoInteractions(proxyAuthStrategy); |
| 323 | + Assertions.assertNull(context.getAuthExchange(proxy).getAuthScheme()); |
| 324 | + } |
| 325 | + |
| 326 | + @Test |
| 327 | + void testProxyChallengeWithoutProxyIsReturnedToCaller() throws Exception { |
| 328 | + final HttpRoute route = new HttpRoute(target); |
| 329 | + final HttpClientContext context = HttpClientContext.create(); |
| 330 | + context.setCredentialsProvider(CredentialsProviderBuilder.create() |
| 331 | + .add(new AuthScope(target), "user", "pass".toCharArray()) |
| 332 | + .build()); |
| 333 | + final ClassicHttpRequest request = new HttpGet("http://foo/test"); |
| 334 | + final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?"); |
| 335 | + response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test"); |
| 336 | + |
| 337 | + Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response); |
| 338 | + |
| 339 | + final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context); |
| 340 | + final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain); |
| 341 | + Assertions.assertEquals(407, finalResponse.getCode()); |
| 342 | + Mockito.verify(chain, Mockito.times(1)).proceed(request, scope); |
| 343 | + Mockito.verifyNoInteractions(proxyAuthStrategy); |
| 344 | + } |
| 345 | + |
| 346 | + @Test |
| 347 | + void testProxyChallengeThroughTunnelWithAuthCacheIsReturnedToCaller() throws Exception { |
| 348 | + final ProtocolExec cachingExec = new ProtocolExec(targetAuthStrategy, proxyAuthStrategy, null, false); |
| 349 | + final HttpHost httpsTarget = new HttpHost("https", "foo", 443); |
| 350 | + final HttpHost proxy = new HttpHost("http", "proxy", 8080); |
| 351 | + final HttpRoute route = new HttpRoute(httpsTarget, null, proxy, true, |
| 352 | + RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED); |
| 353 | + final BasicAuthCache authCache = new BasicAuthCache(); |
| 354 | + final HttpClientContext context = HttpClientContext.create(); |
| 355 | + context.setAuthCache(authCache); |
| 356 | + context.setCredentialsProvider(CredentialsProviderBuilder.create() |
| 357 | + .add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray()) |
| 358 | + .build()); |
| 359 | + final ClassicHttpRequest request = new HttpGet("https://foo/test"); |
| 360 | + final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?"); |
| 361 | + response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test"); |
| 362 | + |
| 363 | + Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response); |
| 364 | + |
| 365 | + final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context); |
| 366 | + final ClassicHttpResponse finalResponse = cachingExec.execute(request, scope, chain); |
| 367 | + Assertions.assertSame(response, finalResponse); |
| 368 | + Mockito.verify(chain, Mockito.times(1)).proceed(request, scope); |
| 369 | + Mockito.verifyNoInteractions(proxyAuthStrategy); |
| 370 | + Assertions.assertNull(request.getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION)); |
| 371 | + Assertions.assertEquals(AuthExchange.State.UNCHALLENGED, context.getAuthExchange(proxy).getState()); |
| 372 | + Assertions.assertNull(authCache.get(proxy)); |
| 373 | + } |
| 374 | + |
| 375 | + @Test |
| 376 | + void testProxyChallengeOnPlainRouteStillAuthenticates() throws Exception { |
| 377 | + final HttpHost proxy = new HttpHost("http", "proxy", 8080); |
| 378 | + final HttpRoute route = new HttpRoute(target, null, proxy, false); |
| 379 | + final HttpClientContext context = HttpClientContext.create(); |
| 380 | + context.setCredentialsProvider(CredentialsProviderBuilder.create() |
| 381 | + .add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray()) |
| 382 | + .build()); |
| 383 | + final ClassicHttpRequest request = new HttpGet("http://foo/test"); |
| 384 | + final ClassicHttpResponse response1 = new BasicClassicHttpResponse(407, "Huh?"); |
| 385 | + response1.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test"); |
| 386 | + final ClassicHttpResponse response2 = new BasicClassicHttpResponse(200, "OK"); |
| 387 | + |
| 388 | + Mockito.when(chain.proceed(Mockito.any(), Mockito.any())).thenReturn(response1, response2); |
| 389 | + Mockito.when(proxyAuthStrategy.select( |
| 390 | + Mockito.eq(ChallengeType.PROXY), |
| 391 | + Mockito.any(), |
| 392 | + Mockito.<HttpClientContext>any())).thenReturn(Collections.singletonList(new BasicScheme())); |
| 393 | + Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true); |
| 394 | + |
| 395 | + final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context); |
| 396 | + final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain); |
| 397 | + Assertions.assertEquals(200, finalResponse.getCode()); |
| 398 | + final ArgumentCaptor<ClassicHttpRequest> captor = ArgumentCaptor.forClass(ClassicHttpRequest.class); |
| 399 | + Mockito.verify(chain, Mockito.times(2)).proceed(captor.capture(), Mockito.same(scope)); |
| 400 | + Assertions.assertNotNull(captor.getAllValues().get(1).getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION)); |
| 401 | + } |
298 | 402 | } |
0 commit comments