Search in sources :

Example 16 with FutureResponseListener

use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.

the class HttpClientTest method testRequestWithResponseContentChunked.

private void testRequestWithResponseContentChunked(int length) throws Exception {
    final byte[] chunk1 = new byte[length];
    final byte[] chunk2 = new byte[length];
    Random random = new Random();
    random.nextBytes(chunk1);
    random.nextBytes(chunk2);
    byte[] bytes = new byte[chunk1.length + chunk2.length];
    System.arraycopy(chunk1, 0, bytes, 0, chunk1.length);
    System.arraycopy(chunk2, 0, bytes, chunk1.length, chunk2.length);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            output.write(chunk2);
        }
    });
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, 2 * length);
    request.timeout(10, TimeUnit.SECONDS).send(listener);
    ContentResponse response = listener.get();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 17 with FutureResponseListener

use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.

the class HttpClientTest method testContentDelimitedByEOFWithSlowRequest.

private void testContentDelimitedByEOFWithSlowRequest(final HttpVersion version, int length) throws Exception {
    // This test is crafted in a way that the response completes before the request is fully written.
    // With SSL, the response coming down will close the SSLEngine so it would not be possible to
    // write the last chunk of the request content, and the request will be failed, failing also the
    // test, which is not what we want.
    // This is a limit of Java's SSL implementation that does not allow half closes.
    Assume.assumeTrue(sslContextFactory == null);
    final byte[] data = new byte[length];
    new Random().nextBytes(data);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send Connection: close to avoid that the server chunks the content with HTTP 1.1.
            if (version.compareTo(HttpVersion.HTTP_1_0) > 0)
                response.setHeader("Connection", "close");
            response.getOutputStream().write(data);
        }
    });
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0 }));
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).version(version).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Wait some time to simulate a slow request.
    Thread.sleep(1000);
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) 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) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 18 with FutureResponseListener

use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.

the class HttpClientChunkedContentTest method test_Server_ContentTerminal_Client_ContentDelay.

@Test
public void test_Server_ContentTerminal_Client_ContentDelay() throws Exception {
    startClient();
    try (ServerSocket server = new ServerSocket()) {
        server.bind(new InetSocketAddress("localhost", 0));
        final AtomicReference<Callback> callbackRef = new AtomicReference<>();
        final CountDownLatch firstContentLatch = new CountDownLatch(1);
        final AtomicReference<Result> resultRef = new AtomicReference<>();
        final CountDownLatch completeLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).onResponseContentAsync(new Response.AsyncContentListener() {

            @Override
            public void onContent(Response response, ByteBuffer content, Callback callback) {
                if (callbackRef.compareAndSet(null, callback))
                    firstContentLatch.countDown();
                else
                    callback.succeeded();
            }
        }).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                resultRef.set(result);
                completeLatch.countDown();
            }
        });
        try (Socket socket = server.accept()) {
            consumeRequestHeaders(socket);
            OutputStream output = socket.getOutputStream();
            String response = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "8\r\n" + "01234567\r\n" + "0\r\n" + "\r\n";
            output.write(response.getBytes(StandardCharsets.UTF_8));
            output.flush();
            // Simulate a delay in consuming the content.
            assertTrue(firstContentLatch.await(5, TimeUnit.SECONDS));
            Thread.sleep(1000);
            callbackRef.get().succeeded();
            // Wait for the client to read 0 and become idle.
            Thread.sleep(1000);
            assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
            Result result = resultRef.get();
            assertTrue(result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            // Issue another request to be sure the connection is sane.
            Request request = client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS);
            FutureResponseListener listener = new FutureResponseListener(request);
            request.send(listener);
            consumeRequestHeaders(socket);
            output.write(response.getBytes(StandardCharsets.UTF_8));
            output.flush();
            Assert.assertEquals(200, listener.get(5, TimeUnit.SECONDS).getStatus());
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 19 with FutureResponseListener

use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.

the class HttpClientExplicitConnectionTest method testExplicitConnectionResponseListeners.

@Test
public void testExplicitConnectionResponseListeners() 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);
    CountDownLatch responseLatch = new CountDownLatch(1);
    Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).onResponseSuccess(response -> responseLatch.countDown());
    FutureResponseListener listener = new FutureResponseListener(request);
    connection.send(request, listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 20 with FutureResponseListener

use of org.eclipse.jetty.client.util.FutureResponseListener in project jetty.project by eclipse.

the class HttpClientTest method testRequestWithResponseContent.

private void testRequestWithResponseContent(int length) throws Exception {
    final byte[] bytes = new byte[length];
    new Random().nextBytes(bytes);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setContentLength(length);
            response.getOutputStream().write(bytes);
        }
    });
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, length);
    request.timeout(10, TimeUnit.SECONDS).send(listener);
    ContentResponse response = listener.get();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Aggregations

FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)33 Test (org.junit.Test)24 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)22 Request (org.eclipse.jetty.client.api.Request)20 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)10 ServletException (javax.servlet.ServletException)9 OutputStream (java.io.OutputStream)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpExchange (org.eclipse.jetty.client.HttpExchange)7 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)7 Random (java.util.Random)6 Connection (org.eclipse.jetty.client.api.Connection)6 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)6 InputStream (java.io.InputStream)5 Destination (org.eclipse.jetty.client.api.Destination)5 Response (org.eclipse.jetty.client.api.Response)5 FuturePromise (org.eclipse.jetty.util.FuturePromise)5 ServerSocket (java.net.ServerSocket)4