Search in sources :

Example 6 with Connection

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

the class HttpClientTest method testCONNECTWithHTTP10.

@Test
public void testCONNECTWithHTTP10() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        startClient();
        String host = "localhost";
        int port = server.getLocalPort();
        Request request = client.newRequest(host, port).method(HttpMethod.CONNECT).version(HttpVersion.HTTP_1_0);
        FuturePromise<Connection> promise = new FuturePromise<>();
        client.getDestination("http", host, port).newConnection(promise);
        Connection connection = promise.get(5, TimeUnit.SECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        try (Socket socket = server.accept()) {
            InputStream input = socket.getInputStream();
            consume(input, false);
            // HTTP/1.0 response, the client must not close the connection.
            String httpResponse = "" + "HTTP/1.0 200 OK\r\n" + "\r\n";
            OutputStream output = socket.getOutputStream();
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            ContentResponse response = listener.get(5, TimeUnit.SECONDS);
            Assert.assertEquals(200, response.getStatus());
            // Because the tunnel was successful, this connection will be
            // upgraded to an SslConnection, so it will not be fill interested.
            // This test doesn't upgrade, so it needs to restore the fill interest.
            ((AbstractConnection) connection).fillInterested();
            // Test that I can send another request on the same connection.
            request = client.newRequest(host, port);
            listener = new FutureResponseListener(request);
            connection.send(request, listener);
            consume(input, false);
            httpResponse = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            listener.get(5, TimeUnit.SECONDS);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) ServerSocket(java.net.ServerSocket) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 7 with Connection

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

the class HttpDestinationOverHTTPTest method test_IdleConnection_IdleTimeout.

@Test
public void test_IdleConnection_IdleTimeout() throws Exception {
    long idleTimeout = 1000;
    client.setIdleTimeout(idleTimeout);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", connector.getLocalPort()));
    destination.start();
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Connection connection1 = connectionPool.acquire();
    if (connection1 == null) {
        // There are no queued requests, so the newly created connection will be idle
        long start = System.nanoTime();
        while (connection1 == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
            TimeUnit.MILLISECONDS.sleep(50);
            connection1 = connectionPool.getIdleConnections().peek();
        }
        Assert.assertNotNull(connection1);
        TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
        connection1 = connectionPool.getIdleConnections().poll();
        Assert.assertNull(connection1);
    }
}
Also used : Origin(org.eclipse.jetty.client.Origin) DuplexConnectionPool(org.eclipse.jetty.client.DuplexConnectionPool) Connection(org.eclipse.jetty.client.api.Connection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 8 with Connection

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_IncompleteFlush.

@Slow
@Test
public void test_Send_NoRequestContent_IncompleteFlush() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint("", 16);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    connection.send(request, null);
    // This take will free space in the buffer and allow for the write to complete
    StringBuilder builder = new StringBuilder(endPoint.takeOutputString());
    // Wait for the write to complete
    TimeUnit.SECONDS.sleep(1);
    String chunk = endPoint.takeOutputString();
    while (chunk.length() > 0) {
        builder.append(chunk);
        chunk = endPoint.takeOutputString();
    }
    String requestString = builder.toString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertTrue(requestString.endsWith("\r\n\r\n"));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 9 with Connection

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

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_Exception.

@Test
public void test_Send_NoRequestContent_Exception() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    // Shutdown output to trigger the exception on write
    endPoint.shutdownOutput();
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    final CountDownLatch failureLatch = new CountDownLatch(2);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onFailure(Request request, Throwable x) {
            failureLatch.countDown();
        }
    });
    connection.send(request, new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            failureLatch.countDown();
        }
    });
    Assert.assertTrue(failureLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test)

Example 10 with Connection

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

the class HttpClientTimeoutTest method testTimeoutOnListenerWithExplicitConnection.

@Slow
@Test
public void testTimeoutOnListenerWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(2 * 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("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Assert.assertTrue(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
    }
}
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

Connection (org.eclipse.jetty.client.api.Connection)40 Test (org.junit.Test)33 CountDownLatch (java.util.concurrent.CountDownLatch)23 Request (org.eclipse.jetty.client.api.Request)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 Origin (org.eclipse.jetty.client.Origin)13 Result (org.eclipse.jetty.client.api.Result)13 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)13 Promise (org.eclipse.jetty.util.Promise)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)9 Destination (org.eclipse.jetty.client.api.Destination)9 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)9 ServletException (javax.servlet.ServletException)8 Response (org.eclipse.jetty.client.api.Response)8 TimeUnit (java.util.concurrent.TimeUnit)7 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)7 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)7