Search in sources :

Example 61 with Response

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

the class HttpClientChunkedContentTest method test_Server_ContentTerminal_Client_ContentDelay.

@Test
public void test_Server_ContentTerminal_Client_ContentDelay() throws Exception {
    startClient();
    try (ServerSocket server = new ServerSocket()) {
        server.bind(new InetSocketAddress("localhost", 0));
        final AtomicReference<Callback> callbackRef = new AtomicReference<>();
        final CountDownLatch firstContentLatch = new CountDownLatch(1);
        final AtomicReference<Result> resultRef = new AtomicReference<>();
        final CountDownLatch completeLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).onResponseContentAsync(new Response.AsyncContentListener() {

            @Override
            public void onContent(Response response, ByteBuffer content, Callback callback) {
                if (callbackRef.compareAndSet(null, callback))
                    firstContentLatch.countDown();
                else
                    callback.succeeded();
            }
        }).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                resultRef.set(result);
                completeLatch.countDown();
            }
        });
        try (Socket socket = server.accept()) {
            consumeRequestHeaders(socket);
            OutputStream output = socket.getOutputStream();
            String response = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "8\r\n" + "01234567\r\n" + "0\r\n" + "\r\n";
            output.write(response.getBytes(StandardCharsets.UTF_8));
            output.flush();
            // Simulate a delay in consuming the content.
            assertTrue(firstContentLatch.await(5, TimeUnit.SECONDS));
            Thread.sleep(1000);
            callbackRef.get().succeeded();
            // Wait for the client to read 0 and become idle.
            Thread.sleep(1000);
            assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
            Result result = resultRef.get();
            assertTrue(result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            // Issue another request to be sure the connection is sane.
            Request request = client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS);
            FutureResponseListener listener = new FutureResponseListener(request);
            request.send(listener);
            consumeRequestHeaders(socket);
            output.write(response.getBytes(StandardCharsets.UTF_8));
            output.flush();
            Assert.assertEquals(200, listener.get(5, TimeUnit.SECONDS).getStatus());
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 62 with Response

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

the class HttpConnectionLifecycleTest method testConnectionForHTTP10ResponseIsRemoved.

@Test
public void testConnectionForHTTP10ResponseIsRemoved() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    client.setStrictEventOrdering(false);
    ContentResponse response = client.newRequest(host, port).scheme(scheme).onResponseBegin(response1 -> {
        ((HttpResponse) response1).version(HttpVersion.HTTP_1_0);
    }).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpVersion(org.eclipse.jetty.http.HttpVersion) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) Log(org.eclipse.jetty.util.log.Log) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Queue(java.util.Queue) Assert(org.junit.Assert) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Connection(org.eclipse.jetty.client.api.Connection) Test(org.junit.Test)

Example 63 with Response

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

the class HttpConnectionLifecycleTest method test_SuccessfulRequest_ReturnsConnection.

@Test
public void test_SuccessfulRequest_ReturnsConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(3);
    client.newRequest(host, port).scheme(scheme).onRequestSuccess(request -> successLatch.countDown()).onResponseHeaders(response -> {
        Assert.assertEquals(0, idleConnections.size());
        Assert.assertEquals(1, activeConnections.size());
        headersLatch.countDown();
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            successLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(headersLatch.await(30, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(1, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpVersion(org.eclipse.jetty.http.HttpVersion) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) Log(org.eclipse.jetty.util.log.Log) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Queue(java.util.Queue) Assert(org.junit.Assert) Connection(org.eclipse.jetty.client.api.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) 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) Test(org.junit.Test)

Example 64 with Response

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

the class HttpConnectionLifecycleTest method test_BadRequest_RemovesConnection.

@Test
public void test_BadRequest_RemovesConnection() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    final Queue<Connection> idleConnections = connectionPool.getIdleConnections();
    Assert.assertEquals(0, idleConnections.size());
    final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
    Assert.assertEquals(0, activeConnections.size());
    final CountDownLatch successLatch = new CountDownLatch(3);
    client.newRequest(host, port).scheme(scheme).listener(new Request.Listener.Adapter() {

        @Override
        public void onBegin(Request request) {
            // Remove the host header, this will make the request invalid
            request.header(HttpHeader.HOST, null);
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            Assert.assertEquals(400, response.getStatus());
            // 400 response also come with a Connection: close,
            // so the connection is closed and removed
            successLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(successLatch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) 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) Test(org.junit.Test)

Example 65 with Response

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

the class HttpClientTest method testAllHeadersDiscarded.

@Test
public void testAllHeadersDiscarded() throws Exception {
    start(new EmptyServerHandler());
    int count = 10;
    final CountDownLatch latch = new CountDownLatch(count);
    for (int i = 0; i < count; ++i) {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {

            @Override
            public boolean onHeader(Response response, HttpField field) {
                return false;
            }

            @Override
            public void onComplete(Result result) {
                if (result.isSucceeded())
                    latch.countDown();
            }
        });
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
}
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) HttpField(org.eclipse.jetty.http.HttpField) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22