Search in sources :

Example 26 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpResponseAbortTest method testAbortOnHeader.

@Test
public void testAbortOnHeader() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseHeader(new Response.HeaderListener() {

        @Override
        public boolean onHeader(Response response, HttpField field) {
            response.abort(new Exception());
            return true;
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) HttpField(org.eclipse.jetty.http.HttpField) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 27 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpClientTest method testStoppingClosesConnections.

@Test
public void testStoppingClosesConnections() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    String path = "/";
    Response response = client.GET(scheme + "://" + host + ":" + port + path);
    Assert.assertEquals(200, response.getStatus());
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    long start = System.nanoTime();
    HttpConnectionOverHTTP connection = null;
    while (connection == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
        connection = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
        TimeUnit.MILLISECONDS.sleep(10);
    }
    Assert.assertNotNull(connection);
    String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort();
    client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar"));
    client.stop();
    Assert.assertEquals(0, client.getDestinations().size());
    Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
    Assert.assertFalse(connection.getEndPoint().isOpen());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpCookie(java.net.HttpCookie) EndPoint(org.eclipse.jetty.io.EndPoint) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Example 28 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpClientTest method testResponseListenerForMultipleEventsIsInvokedOncePerEvent.

@Test
public void testResponseListenerForMultipleEventsIsInvokedOncePerEvent() throws Exception {
    start(new EmptyServerHandler());
    final AtomicInteger counter = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(1);
    Response.Listener listener = new Response.Listener() {

        @Override
        public void onBegin(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public boolean onHeader(Response response, HttpField field) {
            // Number of header may vary, so don't count
            return true;
        }

        @Override
        public void onHeaders(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Response response, ByteBuffer content) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onSuccess(Response response) {
            counter.incrementAndGet();
        }

        @Override
        public void onFailure(Response response, Throwable failure) {
            // Should not be invoked
            counter.incrementAndGet();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertEquals(200, result.getResponse().getStatus());
            counter.incrementAndGet();
            latch.countDown();
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(listener).onResponseHeader(listener).onResponseHeaders(listener).onResponseContent(listener).onResponseContentAsync(listener).onResponseSuccess(listener).onResponseFailure(listener).send(listener);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    int expectedEventsTriggeredByOnResponseXXXListeners = 3;
    int expectedEventsTriggeredByCompletionListener = 4;
    int expected = expectedEventsTriggeredByOnResponseXXXListeners + expectedEventsTriggeredByCompletionListener;
    Assert.assertEquals(expected, counter.get());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Callback(org.eclipse.jetty.util.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpField(org.eclipse.jetty.http.HttpField) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 29 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent.

@Test
public void test_Receive_ResponseContent() throws Exception {
    String content = "0123456789ABCDEF";
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + content.length() + "\r\n" + "\r\n" + content);
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("OK", response.getReason());
    Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
    HttpFields headers = response.getHeaders();
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH));
    String received = listener.getContentAsString(StandardCharsets.UTF_8);
    Assert.assertEquals(content, received);
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 30 with Response

use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

Response (org.eclipse.jetty.client.api.Response)104 Test (org.junit.Test)89 HttpServletResponse (javax.servlet.http.HttpServletResponse)88 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)82 IOException (java.io.IOException)71 HttpServletRequest (javax.servlet.http.HttpServletRequest)69 ServletException (javax.servlet.ServletException)67 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)33 InputStream (java.io.InputStream)32 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 Request (org.eclipse.jetty.server.Request)26 Request (org.eclipse.jetty.client.api.Request)24 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22