Search in sources :

Example 1 with HttpRequestExecutor

use of org.apache.hc.core5.http.impl.io.HttpRequestExecutor in project httpcomponents-core by apache.

the class RequesterBootstrap method create.

public HttpRequester create() {
    final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE, connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE, streamListener);
    final ManagedConnPool<HttpHost, HttpClientConnection> connPool;
    switch(poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
        case LAX:
            connPool = new LaxConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
        case STRICT:
        default:
            connPool = new StrictConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, maxTotal > 0 ? maxTotal : 50, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
    }
    return new HttpRequester(requestExecutor, httpProcessor != null ? httpProcessor : HttpProcessors.client(), connPool, socketConfig != null ? socketConfig : SocketConfig.DEFAULT, connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT), sslSocketFactory, sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(), sslSessionVerifier, DefaultAddressResolver.INSTANCE);
}
Also used : HttpRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) HttpHost(org.apache.hc.core5.http.HttpHost) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) DefaultBHttpClientConnectionFactory(org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory) DefaultTlsSetupHandler(org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)

Example 2 with HttpRequestExecutor

use of org.apache.hc.core5.http.impl.io.HttpRequestExecutor in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testExecutionNoResponseBody.

@Test
public void testExecutionNoResponseBody() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(204, "OK"));
    final ClassicHttpResponse response = executor.execute(request, conn, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn).receiveResponseHeader();
    Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with HttpRequestExecutor

use of org.apache.hc.core5.http.impl.io.HttpRequestExecutor in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testExecutionSkipIntermediateResponses.

@Test
public void testExecutionSkipIntermediateResponses() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
    final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
    final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
    Mockito.verify(conn, Mockito.times(1)).receiveResponseEntity(response);
    final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
    Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
    final List<HttpResponse> infos = responseCaptor.getAllValues();
    Assertions.assertNotNull(infos);
    Assertions.assertEquals(2, infos.size());
    final HttpResponse info1 = infos.get(0);
    Assertions.assertNotNull(info1);
    Assertions.assertEquals(110, info1.getCode());
    final HttpResponse info2 = infos.get(1);
    Assertions.assertNotNull(info2);
    Assertions.assertEquals(111, info2.getCode());
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
    Assertions.assertSame(request, context.getRequest());
    Assertions.assertSame(response, context.getResponse());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponseInformationCallback(org.apache.hc.core5.http.io.HttpResponseInformationCallback) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with HttpRequestExecutor

use of org.apache.hc.core5.http.impl.io.HttpRequestExecutor in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testInvalidInput.

@Test
public void testInvalidInput() throws Exception {
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(null, conn, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(request, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.execute(request, conn, null);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(null, httprocessor, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(request, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.preProcess(request, httprocessor, null);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(null, httprocessor, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(response, null, context);
    });
    Assertions.assertThrows(NullPointerException.class, () -> {
        final HttpRequestExecutor executor = new HttpRequestExecutor();
        executor.postProcess(response, httprocessor, null);
    });
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with HttpRequestExecutor

use of org.apache.hc.core5.http.impl.io.HttpRequestExecutor in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testBasicExecution.

@Test
public void testBasicExecution() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(200, "OK"));
    final ClassicHttpResponse response = executor.execute(request, conn, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn).receiveResponseHeader();
    Mockito.verify(conn).receiveResponseEntity(response);
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
    Assertions.assertSame(request, context.getRequest());
    Assertions.assertSame(response, context.getResponse());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)14 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)12 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)12 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)12 Test (org.junit.jupiter.api.Test)12 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)10 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)10 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)10 HttpEntity (org.apache.hc.core5.http.HttpEntity)5 Timeout (org.apache.hc.core5.util.Timeout)4 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 HttpRequestExecutor (org.apache.hc.core5.http.impl.io.HttpRequestExecutor)2 HttpResponseInformationCallback (org.apache.hc.core5.http.io.HttpResponseInformationCallback)2 IOException (java.io.IOException)1 HttpRequester (org.apache.hc.core5.http.impl.bootstrap.HttpRequester)1 DefaultBHttpClientConnectionFactory (org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory)1 DefaultTlsSetupHandler (org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)1 DefaultDisposalCallback (org.apache.hc.core5.pool.DefaultDisposalCallback)1 StrictConnPool (org.apache.hc.core5.pool.StrictConnPool)1