Search in sources :

Example 36 with Connection

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

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_Chunked_InTwoChunks.

@Test
public void test_Send_SmallRequestContent_Chunked_InTwoChunks() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    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/"));
    String content1 = "0123456789";
    String content2 = "ABCDEF";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))) {

        @Override
        public long getLength() {
            return -1;
        }
    });
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    String content = Integer.toHexString(content1.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content1 + "\r\n";
    content += Integer.toHexString(content2.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content2 + "\r\n";
    content += "0\r\n\r\n";
    Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.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) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 37 with Connection

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

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InTwoBuffers.

@Test
public void test_Send_SmallRequestContent_InTwoBuffers() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    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/"));
    String content1 = "0123456789";
    String content2 = "abcdef";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))));
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertThat(requestString, Matchers.endsWith("\r\n\r\n" + content1 + content2));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.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) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 38 with Connection

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

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InOneBuffer.

@Test
public void test_Send_SmallRequestContent_InOneBuffer() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    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/"));
    String content = "abcdef";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8))));
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.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) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 39 with Connection

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

the class ForwardProxyTLSServerTest method testTwoConcurrentExchanges.

@Test
public void testTwoConcurrentExchanges() throws Exception {
    startTLSServer(new ServerHandler());
    startProxy();
    final HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        final AtomicReference<Connection> connection = new AtomicReference<>();
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        String content1 = "BODY";
        ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(content1, "UTF-8")).onRequestCommit(request -> {
            Destination destination = httpClient.getDestination(HttpScheme.HTTPS.asString(), "localhost", serverConnector.getLocalPort());
            destination.newConnection(new Promise.Adapter<Connection>() {

                @Override
                public void succeeded(Connection result) {
                    connection.set(result);
                    connectionLatch.countDown();
                }
            });
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
        String content = response1.getContentAsString();
        Assert.assertEquals(content1, content);
        Assert.assertTrue(connectionLatch.await(5, TimeUnit.SECONDS));
        String body2 = "body=" + content1;
        org.eclipse.jetty.client.api.Request request2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(body2.length())).content(new StringContentProvider(body2));
        // Make sure the second connection can send the exchange via the tunnel
        FutureResponseListener listener2 = new FutureResponseListener(request2);
        connection.get().send(request2, listener2);
        ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
        Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
        String content2 = response2.getContentAsString();
        Assert.assertEquals(content1, content2);
    } finally {
        httpClient.stop();
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Connection(org.eclipse.jetty.client.api.Connection) HttpConnection(org.eclipse.jetty.server.HttpConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) HttpClient(org.eclipse.jetty.client.HttpClient) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 40 with Connection

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

the class DuplexConnectionPool method activate.

@Override
protected Connection activate() {
    Connection connection;
    lock();
    try {
        connection = idleConnections.poll();
        if (connection == null)
            return null;
        activeConnections.add(connection);
    } finally {
        unlock();
    }
    return active(connection);
}
Also used : Connection(org.eclipse.jetty.client.api.Connection)

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