use of org.apache.http.HttpRequest 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.HttpRequest 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.HttpRequest 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.HttpRequest in project docker-java-api by amihaiemil.
the class UnixHttpClientTestCase method executesRequestWithHost.
/**
* UnixHttpClient can execute the HttpRequest with the given host.
* @throws IOException If something goes wrong.
*/
@Test
public void executesRequestWithHost() throws IOException {
final HttpHost host = new HttpHost("127.0.0.1");
final HttpRequest req = Mockito.mock(HttpRequest.class);
final HttpResponse resp = Mockito.mock(HttpResponse.class);
final HttpClient decorated = Mockito.mock(HttpClient.class);
Mockito.when(decorated.execute(host, req)).thenReturn(resp);
final HttpClient unix = new UnixHttpClient(decorated);
MatcherAssert.assertThat(unix.execute(host, req), Matchers.is(resp));
Mockito.verify(decorated, Mockito.times(1)).execute(host, req);
}
use of org.apache.http.HttpRequest in project docker-java-api by amihaiemil.
the class UnixHttpClientTestCase method executesRequestWithHostAndHandler.
/**
* UnixHttpClient can execute the HttpRequest with the given host and
* response handler.
* @throws IOException If something goes wrong.
*/
@Test
public void executesRequestWithHostAndHandler() 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 HttpClient decorated = Mockito.mock(HttpClient.class);
Mockito.when(decorated.execute(host, req, handler)).thenReturn("executed");
final HttpClient unix = new UnixHttpClient(decorated);
MatcherAssert.assertThat(unix.execute(host, req, handler), Matchers.equalTo("executed"));
Mockito.verify(decorated, Mockito.times(1)).execute(host, req, handler);
}
Aggregations