Search in sources :

Example 1 with Destination

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

the class HttpClientExplicitConnectionTest method testExplicitConnection.

@Test
public void testExplicitConnection() throws Exception {
    start(new EmptyServerHandler());
    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);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        ContentResponse response = listener.get(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
        HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP) destination;
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) httpDestination.getConnectionPool();
        Assert.assertTrue(connectionPool.getActiveConnections().isEmpty());
        Assert.assertTrue(connectionPool.getIdleConnections().isEmpty());
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 2 with Destination

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

the class HttpClientExplicitConnectionTest method testExplicitConnectionIsClosedOnRemoteClose.

@Test
public void testExplicitConnectionIsClosedOnRemoteClose() throws Exception {
    start(new EmptyServerHandler());
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
    Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme);
    FutureResponseListener listener = new FutureResponseListener(request);
    connection.send(request, listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Wait some time to have the client is an idle state.
    TimeUnit.SECONDS.sleep(1);
    connector.stop();
    // Give the connection some time to process the remote close.
    TimeUnit.SECONDS.sleep(1);
    HttpConnectionOverHTTP httpConnection = (HttpConnectionOverHTTP) connection;
    Assert.assertFalse(httpConnection.getEndPoint().isOpen());
    HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP) destination;
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) httpDestination.getConnectionPool();
    Assert.assertTrue(connectionPool.getActiveConnections().isEmpty());
    Assert.assertTrue(connectionPool.getIdleConnections().isEmpty());
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 3 with Destination

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

the class AsyncIOServletTest method testAsyncReadEarlyEOF.

@Test
public void testAsyncReadEarlyEOF() throws Exception {
    // SSLEngine receives the close alert from the client, and when
    // the server passes the response to encrypt and write, SSLEngine
    // only generates the close alert back, without encrypting the
    // response, so we need to skip the transports over TLS.
    Assume.assumeThat(transport, Matchers.not(Matchers.isOneOf(Transport.HTTPS, Transport.H2)));
    String content = "jetty";
    int responseCode = HttpStatus.NO_CONTENT_204;
    CountDownLatch readLatch = new CountDownLatch(content.length());
    CountDownLatch errorLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            ServletInputStream input = request.getInputStream();
            input.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    while (input.isReady() && !input.isFinished()) {
                        int read = input.read();
                        // System.err.printf("%x%n", read);
                        readLatch.countDown();
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                }

                @Override
                public void onError(Throwable x) {
                    response.setStatus(responseCode);
                    asyncContext.complete();
                    errorLatch.countDown();
                }
            });
        }
    });
    CountDownLatch responseLatch = new CountDownLatch(1);
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).onResponseSuccess(response -> responseLatch.countDown());
    Destination destination = client.getDestination(getScheme(), "localhost", connector.getLocalPort());
    FuturePromise<org.eclipse.jetty.client.api.Connection> promise = new FuturePromise<>();
    destination.newConnection(promise);
    org.eclipse.jetty.client.api.Connection connection = promise.get(5, TimeUnit.SECONDS);
    CountDownLatch clientLatch = new CountDownLatch(1);
    connection.send(request, result -> {
        assertThat(result.getResponse().getStatus(), Matchers.equalTo(responseCode));
        clientLatch.countDown();
    });
    assertTrue(readLatch.await(5, TimeUnit.SECONDS));
    switch(transport) {
        case HTTP:
        case HTTPS:
            ((HttpConnectionOverHTTP) connection).getEndPoint().shutdownOutput();
            break;
        case H2C:
        case H2:
            // In case of HTTP/2, we not only send the request, but also the preface and
            // SETTINGS frames. SETTINGS frame need to be replied, so we want to wait to
            // write the reply before shutting output down, so that the test does not fail.
            Thread.sleep(1000);
            Session session = ((HttpConnectionOverHTTP2) connection).getSession();
            ((HTTP2Session) session).getEndPoint().shutdownOutput();
            break;
        default:
            Assert.fail();
    }
    // Wait for the response to arrive before finishing the request.
    assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
    contentProvider.close();
    assertTrue(errorLatch.await(5, TimeUnit.SECONDS));
    assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) AsyncContext(javax.servlet.AsyncContext) HttpConnectionOverHTTP2(org.eclipse.jetty.http2.client.http.HttpConnectionOverHTTP2) Matchers.containsString(org.hamcrest.Matchers.containsString) ReadListener(javax.servlet.ReadListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpServlet(javax.servlet.http.HttpServlet) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 4 with Destination

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

the class HttpClientTest method testHTTP10WithKeepAliveAndNoContentLength.

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

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Send the headers at this point, then write the content
            response.flushBuffer();
            response.getOutputStream().print("TEST");
        }
    });
    FuturePromise<Connection> promise = new FuturePromise<>();
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    destination.newConnection(promise);
    try (Connection connection = promise.get(5, TimeUnit.SECONDS)) {
        long timeout = 5000;
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(destination.getScheme()).version(HttpVersion.HTTP_1_0).header(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()).timeout(timeout, TimeUnit.MILLISECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        ContentResponse response = listener.get(2 * timeout, TimeUnit.MILLISECONDS);
        Assert.assertEquals(200, response.getStatus());
        // The parser notifies end-of-content and therefore the CompleteListener
        // before closing the connection, so we need to wait before checking
        // that the connection is closed to avoid races.
        Thread.sleep(1000);
        Assert.assertTrue(connection.isClosed());
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 5 with Destination

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

the class HttpDestinationOverHTTPTest method testDestinationIsRemoved.

@Test
public void testDestinationIsRemoved() throws Exception {
    String host = "localhost";
    int port = connector.getLocalPort();
    Destination destinationBefore = client.getDestination(scheme, host, port);
    ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).send();
    Assert.assertEquals(200, response.getStatus());
    Destination destinationAfter = client.getDestination(scheme, host, port);
    Assert.assertSame(destinationBefore, destinationAfter);
    client.setRemoveIdleDestinations(true);
    response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).send();
    Assert.assertEquals(200, response.getStatus());
    destinationAfter = client.getDestination(scheme, host, port);
    Assert.assertNotSame(destinationBefore, destinationAfter);
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Aggregations

Destination (org.eclipse.jetty.client.api.Destination)12 Test (org.junit.Test)12 Connection (org.eclipse.jetty.client.api.Connection)9 FuturePromise (org.eclipse.jetty.util.FuturePromise)9 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)8 Request (org.eclipse.jetty.client.api.Request)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 IOException (java.io.IOException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)3 EndPoint (org.eclipse.jetty.io.EndPoint)3 Promise (org.eclipse.jetty.util.Promise)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 HttpCookie (java.net.HttpCookie)2 InetSocketAddress (java.net.InetSocketAddress)2 ServerSocket (java.net.ServerSocket)2