Search in sources :

Example 21 with HttpCoreContext

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();
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.jupiter.api.Test)

Example 22 with HttpCoreContext

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();
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) Test(org.junit.jupiter.api.Test)

Example 23 with HttpCoreContext

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();
}
Also used : HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) UnsupportedHttpVersionException(org.apache.hc.core5.http.UnsupportedHttpVersionException) HttpResponseFactory(org.apache.hc.core5.http.HttpResponseFactory) InputStreamEntity(org.apache.hc.core5.http.io.entity.InputStreamEntity) HttpRequestMapper(org.apache.hc.core5.http.HttpRequestMapper) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) MockitoAnnotations(org.mockito.MockitoAnnotations) ArgumentCaptor(org.mockito.ArgumentCaptor) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Spy(org.mockito.Spy) HttpServerConnection(org.apache.hc.core5.http.io.HttpServerConnection) ProtocolException(org.apache.hc.core5.http.ProtocolException) ConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) Test(org.junit.jupiter.api.Test) HttpHeaders(org.apache.hc.core5.http.HttpHeaders) Mockito(org.mockito.Mockito) List(java.util.List) HttpRequestHandler(org.apache.hc.core5.http.io.HttpRequestHandler) HeaderElements(org.apache.hc.core5.http.HeaderElements) Method(org.apache.hc.core5.http.Method) Assertions(org.junit.jupiter.api.Assertions) HttpStatus(org.apache.hc.core5.http.HttpStatus) InputStream(java.io.InputStream) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) InputStream(java.io.InputStream) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) InputStreamEntity(org.apache.hc.core5.http.io.entity.InputStreamEntity) Test(org.junit.jupiter.api.Test)

Example 24 with HttpCoreContext

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();
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.jupiter.api.Test)

Example 25 with HttpCoreContext

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("==============");
        }
    }
}
Also used : ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpConnection(org.apache.hc.core5.http.HttpConnection) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) StatusLine(org.apache.hc.core5.http.message.StatusLine) RequestLine(org.apache.hc.core5.http.message.RequestLine) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) HttpRequester(org.apache.hc.core5.http.impl.bootstrap.HttpRequester)

Aggregations

HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)56 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)46 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)39 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)33 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)29 HttpHost (org.apache.hc.core5.http.HttpHost)26 Test (org.junit.jupiter.api.Test)25 Test (org.junit.Test)21 HttpEntity (org.apache.hc.core5.http.HttpEntity)16 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)13 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)12 HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)12 AbstractHttpEntity (org.apache.hc.core5.http.io.entity.AbstractHttpEntity)9 Random (java.util.Random)8 Header (org.apache.hc.core5.http.Header)8 ArrayList (java.util.ArrayList)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)6 ProtocolException (org.apache.hc.core5.http.ProtocolException)6 ContentType (org.apache.hc.core5.http.ContentType)5