Search in sources :

Example 1 with HttpClientCustomizer

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;
}
Also used : HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) ArrayList(java.util.ArrayList) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with HttpClientCustomizer

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;
}
Also used : HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) HttpClientTransport(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 3 with HttpClientCustomizer

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"));
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpClientCustomizer(org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer) Header(org.apache.http.Header) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpClientCustomizer (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientCustomizer)3 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)2 HttpContext (org.apache.http.protocol.HttpContext)2 HttpClientTransport (org.eclipse.epp.internal.mpc.core.transport.httpclient.HttpClientTransport)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1 Header (org.apache.http.Header)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1