use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.
the class TransportFactoryTest method interceptRequest.
private static AbortRequestCustomizer interceptRequest(HttpClientCustomizer... customizers) throws Exception {
AbortRequestCustomizer abortRequestCustomizer = new AbortRequestCustomizer();
HttpClientCustomizer[] mergedCustomizers;
if (customizers == null || customizers.length == 0) {
mergedCustomizers = new HttpClientCustomizer[] { abortRequestCustomizer };
} else {
mergedCustomizers = new HttpClientCustomizer[customizers.length + 1];
System.arraycopy(customizers, 0, mergedCustomizers, 0, customizers.length);
mergedCustomizers[customizers.length] = abortRequestCustomizer;
}
HttpClientTransport httpClientTransport = createClient(mergedCustomizers);
HttpClient client = httpClientTransport.getClient();
HttpContext context = new BasicHttpContext();
try {
client.execute(new HttpGet("http://localhost/test"), context);
fail("Expected request execution to fail");
} catch (ConnectionClosedException ex) {
// ignore expected exception
}
return abortRequestCustomizer;
}
use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.
the class TransportFactoryTest method testHttpClientCustomizer.
@Test
public void testHttpClientCustomizer() throws Exception {
final HttpClientCustomizer customizer = Mockito.mock(HttpClientCustomizer.class);
Mockito.when(customizer.customizeBuilder(Matchers.any())).thenAnswer(new Answer<HttpClientBuilder>() {
public HttpClientBuilder answer(InvocationOnMock invocation) {
HttpClientBuilder builder = (HttpClientBuilder) invocation.getArguments()[0];
return builder == null ? null : builder.addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.addHeader("X-Customizer-Test", "true");
}
});
}
});
Mockito.when(customizer.customizeCredentialsProvider(Matchers.any())).thenReturn(null);
HttpRequest request = interceptRequest(customizer).getInterceptedRequest();
Mockito.verify(customizer).customizeBuilder(Matchers.any());
Mockito.verify(customizer).customizeCredentialsProvider(Matchers.any());
assertThat(request.getFirstHeader("X-Customizer-Test"), LambdaMatchers.<Header, String>map(x -> x == null ? null : x.getValue()).matches("true"));
}
use of org.apache.http.protocol.HttpContext in project epp.mpc by eclipse.
the class HttpClientFactory method createCredentialsProvider.
private static CredentialsProvider createCredentialsProvider(HttpClientBuilder clientBuilder) {
// TODO we should handle configured proxy passwords and dialogs to prompt for unknown credentials on our own...
CredentialsProvider credentialsProvider = new SystemCredentialsProvider();
credentialsProvider = customizeCredentialsProvider(credentialsProvider);
final CacheCredentialsProvider cacheProvider = new CacheCredentialsProvider();
credentialsProvider = new ChainedCredentialsProvider(cacheProvider, credentialsProvider);
credentialsProvider = new SynchronizedCredentialsProvider(credentialsProvider);
clientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
context.setAttribute(CacheCredentialsAuthenticationStrategy.CREDENTIALS_CACHE_ATTRIBUTE, cacheProvider);
}
});
return credentialsProvider;
}
use of org.apache.http.protocol.HttpContext in project service-proxy by membrane.
the class AssertUtils method getAuthenticatingHttpClient.
private static HttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
BasicCredentialsProvider bcp = new BasicCredentialsProvider();
bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.update(new BasicScheme(), creds);
}
}
}
};
HttpClient hc = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).setDefaultCredentialsProvider(bcp).addInterceptorFirst(preemptiveAuth).build();
return hc;
}
use of org.apache.http.protocol.HttpContext in project docker-java-api by amihaiemil.
the class UnixHttpClientTestCase method executesRequestWithHostHandlerAndContext.
/**
* UnixHttpClient can execute the HttpRequest with the given host,
* response handler and context.
* @throws IOException If something goes wrong.
*/
@Test
public void executesRequestWithHostHandlerAndContext() throws IOException {
final HttpHost host = new HttpHost("127.0.0.1");
final HttpRequest req = Mockito.mock(HttpRequest.class);
final ResponseHandler<String> handler = Mockito.mock(ResponseHandler.class);
final HttpContext context = Mockito.mock(HttpContext.class);
final HttpClient decorated = Mockito.mock(HttpClient.class);
Mockito.when(decorated.execute(host, req, handler, context)).thenReturn("executed");
final HttpClient unix = new UnixHttpClient(decorated);
MatcherAssert.assertThat(unix.execute(host, req, handler, context), Matchers.equalTo("executed"));
Mockito.verify(decorated, Mockito.times(1)).execute(host, req, handler, context);
}
Aggregations