Search in sources :

Example 81 with Request

use of org.eclipse.jetty.client.api.Request 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 82 with Request

use of org.eclipse.jetty.client.api.Request 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 83 with Request

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

Example 84 with Request

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

the class HttpClientTimeoutTest method retryAfterConnectTimeout.

@Test
public void retryAfterConnectTimeout() throws Exception {
    final String host = "10.255.255.1";
    final int port = 80;
    int connectTimeout = 1000;
    assumeConnectTimeout(host, port, connectTimeout);
    start(new EmptyServerHandler());
    client.stop();
    client.setConnectTimeout(connectTimeout);
    client.start();
    final CountDownLatch latch = new CountDownLatch(1);
    Request request = client.newRequest(host, port);
    request.scheme(scheme).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                // Retry
                client.newRequest(host, port).scheme(scheme).send(new Response.CompleteListener() {

                    @Override
                    public void onComplete(Result result) {
                        if (result.isFailed())
                            latch.countDown();
                    }
                });
            }
        }
    });
    Assert.assertTrue(latch.await(333 * connectTimeout, TimeUnit.MILLISECONDS));
    Assert.assertNotNull(request.getAbortCause());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 85 with Request

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

the class HttpClientTimeoutTest method testTimeoutOnListener.

@Slow
@Test
public void testTimeoutOnListener() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(2 * timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
    request.send(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) 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

Request (org.eclipse.jetty.client.api.Request)223 Test (org.junit.Test)137 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 HttpServletResponse (javax.servlet.http.HttpServletResponse)63 IOException (java.io.IOException)62 CountDownLatch (java.util.concurrent.CountDownLatch)58 ServletException (javax.servlet.ServletException)53 HttpClient (org.eclipse.jetty.client.HttpClient)48 Response (org.eclipse.jetty.client.api.Response)37 Result (org.eclipse.jetty.client.api.Result)37 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)28 ByteBuffer (java.nio.ByteBuffer)27 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)26 Connection (org.eclipse.jetty.client.api.Connection)26 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)25 URI (java.net.URI)24