use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testBasicExecution.
@Test
public void testBasicExecution() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
httpservice.handleRequest(conn, context);
Assertions.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
Assertions.assertSame(request, context.getRequest());
Assertions.assertSame(response, context.getResponse());
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(response);
Mockito.verify(conn).sendResponseEntity(response);
Mockito.verify(conn).flush();
Mockito.verify(conn).close();
Mockito.verify(response).close();
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testUnsupportedHttpVersionException.
@Test
public void testUnsupportedHttpVersionException() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
Mockito.doThrow(new UnsupportedHttpVersionException()).when(requestHandler).handle(request, response, context);
httpservice.handleRequest(conn, context);
final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
final ClassicHttpResponse error = responseCaptor.getValue();
Assertions.assertNotNull(error);
Assertions.assertSame(request, context.getRequest());
Assertions.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getCode());
Mockito.verify(httprocessor).process(error, error.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(error);
Mockito.verify(conn).sendResponseEntity(error);
Mockito.verify(conn).close();
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testExecutionEntityEnclosingRequest.
@Test
public void testExecutionEntityEnclosingRequest() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
final InputStream inStream = Mockito.mock(InputStream.class);
final InputStreamEntity entity = new InputStreamEntity(inStream, -1, null);
request.setEntity(entity);
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(connReuseStrategy.keepAlive(ArgumentMatchers.eq(request), ArgumentMatchers.argThat(errorResponse -> errorResponse.getCode() == HttpStatus.SC_NOT_IMPLEMENTED), ArgumentMatchers.eq(context))).thenReturn(Boolean.TRUE);
httpservice.handleRequest(conn, context);
final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
final ClassicHttpResponse response = responseCaptor.getValue();
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
Assertions.assertSame(request, context.getRequest());
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.verify(inStream).close();
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(response);
Mockito.verify(conn).sendResponseEntity(response);
Mockito.verify(conn).flush();
Mockito.verify(conn, Mockito.never()).close();
Mockito.verify(response).close();
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class TestHttpService method testProtocolException.
@Test
public void testProtocolException() throws Exception {
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(requestHandler).handle(request, response, context);
httpservice.handleRequest(conn, context);
final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
final ClassicHttpResponse error = responseCaptor.getValue();
Assertions.assertNotNull(error);
Assertions.assertSame(request, context.getRequest());
Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, error.getCode());
Mockito.verify(httprocessor).process(error, error.getEntity(), context);
Mockito.verify(conn).sendResponseHeader(error);
Mockito.verify(conn).sendResponseEntity(error);
Mockito.verify(conn).close();
}
use of org.apache.hc.core5.http.protocol.HttpCoreContext in project httpcomponents-core by apache.
the class ClassicGetExecutionExample 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 String[] requestUris = new String[] { "/", "/ip", "/user-agent", "/headers" };
for (int i = 0; i < requestUris.length; i++) {
final String requestUri = requestUris[i];
final ClassicHttpRequest request = ClassicRequestBuilder.get().setHttpHost(target).setPath(requestUri).build();
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("==============");
}
}
}
Aggregations