use of org.apache.hc.core5.http.protocol.HttpProcessor 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);
}
use of org.apache.hc.core5.http.protocol.HttpProcessor 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);
}
use of org.apache.hc.core5.http.protocol.HttpProcessor 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());
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class JSSEProviderIntegrationTest method testSimpleGetIdentityTransfer.
@Test
public void testSimpleGetIdentityTransfer() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
client.start();
for (int i = 0; i < REQ_NUM; i++) {
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
try (final ClientSessionEndpoint streamEndpoint = connectFuture.get()) {
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
final String entity = result.getBody();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertEquals("Hi there", entity);
}
}
}
use of org.apache.hc.core5.http.protocol.HttpProcessor in project httpcomponents-core by apache.
the class Http1IntegrationTest method testSimpleGetIdentityTransfer.
@Test
public void testSimpleGetIdentityTransfer() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
client.start();
final int reqNo = 5;
for (int i = 0; i < reqNo; i++) {
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
streamEndpoint.close();
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
final String entity = result.getBody();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertEquals("Hi there", entity);
}
}
Aggregations