Search in sources :

Example 11 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_EarlyEOF.

@Test
public void test_Receive_ResponseContent_EarlyEOF() throws Exception {
    String content1 = "0123456789";
    String content2 = "ABCDEF";
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + content1);
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    endPoint.addInputEOF();
    connection.getHttpChannel().receive();
    try {
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof EOFException);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) EOFException(java.io.EOFException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 12 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent.

@Test
public void test_Receive_ResponseContent() throws Exception {
    String content = "0123456789ABCDEF";
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: " + content.length() + "\r\n" + "\r\n" + content);
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("OK", response.getReason());
    Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
    HttpFields headers = response.getHeaders();
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH));
    String received = listener.getContentAsString(StandardCharsets.UTF_8);
    Assert.assertEquals(content, received);
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 13 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_Receive_ResponseContent_IdleTimeout.

@Test
public void test_Receive_ResponseContent_IdleTimeout() throws Exception {
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 1\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    // ByteArrayEndPoint has an idle timeout of 0 by default,
    // so to simulate an idle timeout is enough to wait a bit.
    Thread.sleep(100);
    connection.onIdleExpired();
    try {
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 14 with FutureResponseListener

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

the class Usage method testRequestWithExplicitConnectionControl.

@Test
public void testRequestWithExplicitConnectionControl() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Create an explicit connection, and use try-with-resources to manage it
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    client.getDestination("http", "localhost", 8080).newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest("localhost", 8080);
        // Asynchronous send but using FutureResponseListener
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        // Wait for the response on the listener
        Response response = listener.get(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) FuturePromise(org.eclipse.jetty.util.FuturePromise) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 15 with FutureResponseListener

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

the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilContent.

@Test
public void testProxyRequestHeadersNotSentUntilContent() throws Exception {
    startServer(new EchoHttpServlet());
    final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new BufferingContentTransformer();
        }

        @Override
        protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
            proxyRequestLatch.countDown();
            super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
        }
    });
    startClient();
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Send one chunk of content, the proxy request must not be sent.
    ByteBuffer chunk1 = ByteBuffer.allocate(1024);
    content.offer(chunk1);
    Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    // Send another chunk of content, the proxy request must not be sent.
    ByteBuffer chunk2 = ByteBuffer.allocate(512);
    content.offer(chunk2);
    Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    // Finish the content, request must be sent.
    content.close();
    Assert.assertTrue(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

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