Search in sources :

Example 11 with Destination

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

the class HttpClientTest method test_DestinationCount.

@Test
public void test_DestinationCount() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    client.GET(scheme + "://" + host + ":" + port);
    List<Destination> destinations = client.getDestinations();
    Assert.assertNotNull(destinations);
    Assert.assertEquals(1, destinations.size());
    Destination destination = destinations.get(0);
    Assert.assertNotNull(destination);
    Assert.assertEquals(scheme, destination.getScheme());
    Assert.assertEquals(host, destination.getHost());
    Assert.assertEquals(port, destination.getPort());
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Example 12 with Destination

use of org.eclipse.jetty.client.api.Destination 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)

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