use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicPostExecutionExample method main.
public static void main(final String[] args) throws Exception {
final HttpRequester httpRequester = RequesterBootstrap.bootstrap().setStreamListener(new Http1StreamListener() {
@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
System.out.println(connection.getRemoteAddress() + " " + new RequestLine(request));
}
@Override
public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
System.out.println(connection.getRemoteAddress() + " " + new StatusLine(response));
}
@Override
public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
if (keepAlive) {
System.out.println(connection.getRemoteAddress() + " exchange completed (connection kept alive)");
} else {
System.out.println(connection.getRemoteAddress() + " exchange completed (connection closed)");
}
}
}).setSocketConfig(SocketConfig.custom().setSoTimeout(5, TimeUnit.SECONDS).build()).create();
final HttpCoreContext coreContext = HttpCoreContext.create();
final HttpHost target = new HttpHost("httpbin.org");
final HttpEntity[] requestBodies = { HttpEntities.create("This is the first test request", ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)), HttpEntities.create("This is the second test request".getBytes(StandardCharsets.UTF_8), ContentType.APPLICATION_OCTET_STREAM), HttpEntities.create(outStream -> outStream.write(("This is the third test request " + "(streaming)").getBytes(StandardCharsets.UTF_8)), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)), HttpEntities.create("This is the fourth test request " + "(streaming with trailers)", ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8), new BasicHeader("trailer1", "And goodbye")) };
final String requestUri = "/post";
for (int i = 0; i < requestBodies.length; i++) {
final ClassicHttpRequest request = ClassicRequestBuilder.get().setHttpHost(target).setPath(requestUri).build();
request.setEntity(requestBodies[i]);
try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) {
System.out.println(requestUri + "->" + response.getCode());
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
}
}
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext 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);
});
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext 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());
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionEntityEnclosingRequestWithExpectContinueFailure.
@Test
public void testExecutionEntityEnclosingRequestWithExpectContinueFailure() 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.POST, "/");
request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
final HttpEntity entity = Mockito.mock(HttpEntity.class);
request.setEntity(entity);
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(402, "OK"));
Mockito.when(conn.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(Boolean.TRUE);
final ClassicHttpResponse response = executor.execute(request, conn, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn, Mockito.never()).sendRequestEntity(request);
Mockito.verify(conn).terminateRequest(request);
Mockito.verify(conn, Mockito.times(2)).flush();
Mockito.verify(conn).isDataAvailable(Timeout.ofMilliseconds(3000));
Mockito.verify(conn).receiveResponseHeader();
Mockito.verify(conn).receiveResponseEntity(response);
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionRuntimeException.
@Test
public void testExecutionRuntimeException() throws Exception {
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
Mockito.doThrow(new RuntimeException("Oopsie")).when(conn).receiveResponseHeader();
Assertions.assertThrows(RuntimeException.class, () -> executor.execute(request, conn, context));
Mockito.verify(conn).close();
}
Aggregations