use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer in project epp.mpc by eclipse.
the class TransportFactoryTest method createClient.
private static HttpClientTransport createClient(HttpClientCustomizer... customizers) {
if (customizers == null || customizers.length == 0) {
return new HttpClientTransport();
}
List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
for (int i = 0; i < customizers.length; i++) {
HttpClientCustomizer customizer = customizers[i];
Dictionary<String, Object> serviceProperties = ServiceUtil.serviceName("org.eclipse.epp.mpc.core.transport.http.test.customizer." + i, ServiceUtil.serviceRanking(1000 + i, null));
ServiceRegistration<?> registration = FrameworkUtil.getBundle(HttpClientCustomizer.class).getBundleContext().registerService(HttpClientCustomizer.class, customizer, serviceProperties);
registrations.add(registration);
}
HttpClientTransport httpClientTransport;
try {
httpClientTransport = new HttpClientTransport();
} finally {
for (ServiceRegistration<?> registration : registrations) {
registration.unregister();
}
}
return httpClientTransport;
}
use of org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer 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.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer 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"));
}
Aggregations