@Test
public void testCrossSiteRedirect() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpGet get = new HttpGet("/test");
final HttpRequestWrapper request = HttpRequestWrapper.wrap(get);
final HttpClientContext context = HttpClientContext.create();
final AuthState targetAuthState = new AuthState();
targetAuthState.setState(AuthProtocolState.SUCCESS);
targetAuthState.update(new BasicScheme(), new UsernamePasswordCredentials("user:pass"));
final AuthState proxyAuthState = new AuthState();
proxyAuthState.setState(AuthProtocolState.SUCCESS);
proxyAuthState.update(new NTLMScheme(), new NTCredentials("user:pass"));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, targetAuthState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, proxyAuthState);
final CloseableHttpResponse response1 = Mockito.mock(CloseableHttpResponse.class);
final CloseableHttpResponse response2 = Mockito.mock(CloseableHttpResponse.class);
final HttpGet redirect = new HttpGet("http://otherhost/redirect");
Mockito.when(requestExecutor.execute(
Mockito.eq(route),
Mockito.same(request),
Mockito.<HttpClientContext>any(),
Mockito.<HttpExecutionAware>any())).thenReturn(response1);
Mockito.when(requestExecutor.execute(
Mockito.eq(route),
HttpRequestWrapperMatcher.same(redirect),
Mockito.<HttpClientContext>any(),
Mockito.<HttpExecutionAware>any())).thenReturn(response2);
Mockito.when(redirectStrategy.isRedirected(
Mockito.same(request),
Mockito.same(response1),
Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
Mockito.when(redirectStrategy.getRedirect(
Mockito.same(request),
Mockito.same(response1),
Mockito.<HttpClientContext>any())).thenReturn(redirect);
Mockito.when(httpRoutePlanner.determineRoute(
Mockito.eq(target),
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpClientContext>any())).thenReturn(new HttpRoute(new HttpHost("otherhost")));
redirectExec.execute(route, request, context, execAware);
Assert.assertNotNull(context.getTargetAuthState());
Assert.assertEquals(AuthProtocolState.UNCHALLENGED, context.getTargetAuthState().getState());
Assert.assertEquals(null, context.getTargetAuthState().getAuthScheme());
Assert.assertNotNull(context.getProxyAuthState());
Assert.assertEquals(AuthProtocolState.UNCHALLENGED, context.getProxyAuthState().getState());
Assert.assertEquals(null, context.getProxyAuthState().getAuthScheme());
}