Search in sources :

Example 21 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP 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 22 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
        start(new AbstractHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                response.setHeader("Connection", "close");
                baseRequest.setHandled(true);
            // Don't read request content; this causes the server parser to be closed
            }
        });
        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());
        Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
        final CountDownLatch latch = new CountDownLatch(1);
        ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
        Arrays.fill(buffer.array(), (byte) 'x');
        client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {

            @Override
            public void onComplete(Result result) {
                Assert.assertEquals(1, latch.getCount());
                Assert.assertEquals(0, idleConnections.size());
                Assert.assertEquals(0, activeConnections.size());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
        Assert.assertEquals(0, idleConnections.size());
        Assert.assertEquals(0, activeConnections.size());
        server.stop();
    }
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 23 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_IdleConnection_IsClosed_OnRemoteClose.

@Slow
@Test
public void test_IdleConnection_IsClosed_OnRemoteClose() 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());
    ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(30, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    connector.stop();
    // Give the connection some time to process the remote close
    TimeUnit.SECONDS.sleep(1);
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : 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) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 24 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setHeader("Connection", "close");
            baseRequest.setHandled(true);
        }
    });
    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 latch = new CountDownLatch(1);
    client.newRequest(host, port).scheme(scheme).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(0, idleConnections.size());
            Assert.assertEquals(0, activeConnections.size());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 25 with HttpDestinationOverHTTP

use of org.eclipse.jetty.client.http.HttpDestinationOverHTTP in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnHeaders.

@Test
public void testAbortOnHeaders() throws Exception {
    start(new EmptyServerHandler());
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch committed = new CountDownLatch(1);
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).listener(new Request.Listener.Adapter() {

            @Override
            public void onHeaders(Request request) {
                aborted.set(request.abort(cause));
                latch.countDown();
            }

            @Override
            public void onCommit(Request request) {
                committed.countDown();
            }
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
        Assert.assertFalse(committed.await(1, TimeUnit.SECONDS));
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)30 Test (org.junit.Test)28 CountDownLatch (java.util.concurrent.CountDownLatch)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)22 IOException (java.io.IOException)21 ServletException (javax.servlet.ServletException)21 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)14 Connection (org.eclipse.jetty.client.api.Connection)13 Result (org.eclipse.jetty.client.api.Result)12 ByteBuffer (java.nio.ByteBuffer)11 TimeUnit (java.util.concurrent.TimeUnit)11 Assert (org.junit.Assert)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 Response (org.eclipse.jetty.client.api.Response)9 ExecutionException (java.util.concurrent.ExecutionException)8 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)8