Search in sources :

Example 21 with FutureResponseListener

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

the class FastCGIProxyServletTest method testGETWithResponseContent.

private void testGETWithResponseContent(int length, final long delay) throws Exception {
    final byte[] data = new byte[length];
    new Random().nextBytes(data);
    final String path = "/foo/index.php";
    prepare(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Assert.assertTrue(request.getRequestURI().endsWith(path));
            response.setContentLength(data.length);
            response.getOutputStream().write(data);
        }
    });
    Request request = client.newRequest("localhost", httpConnector.getLocalPort()).onResponseContentAsync((response, content, callback) -> {
        try {
            if (delay > 0)
                TimeUnit.MILLISECONDS.sleep(delay);
            callback.succeeded();
        } catch (InterruptedException x) {
            callback.failed(x);
        }
    }).path(path);
    FutureResponseListener listener = new FutureResponseListener(request, length);
    request.send(listener);
    ContentResponse response = listener.get(30, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletException(javax.servlet.ServletException) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) After(org.junit.After) HttpStatus(org.eclipse.jetty.http.HttpStatus) Server(org.eclipse.jetty.server.Server) Parameterized(org.junit.runners.Parameterized) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) TimeUnit(java.util.concurrent.TimeUnit) ServerConnector(org.eclipse.jetty.server.ServerConnector) ServerFCGIConnectionFactory(org.eclipse.jetty.fcgi.server.ServerFCGIConnectionFactory) FCGI(org.eclipse.jetty.fcgi.FCGI) Assert(org.junit.Assert) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 22 with FutureResponseListener

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

the class HttpClientTest method testGETResponseWithBigContent.

@Test
public void testGETResponseWithBigContent() throws Exception {
    final byte[] data = new byte[16 * 1024 * 1024];
    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 {
            // Setting the Content-Length triggers the HTTP
            // content mode for response content parsing,
            // otherwise the RAW content mode is used.
            response.setContentLength(data.length);
            response.getOutputStream().write(data);
            baseRequest.setHandled(true);
        }
    });
    Request request = client.newRequest(scheme + "://localhost:" + connector.getLocalPort());
    FutureResponseListener listener = new FutureResponseListener(request, data.length);
    request.send(listener);
    ContentResponse response = listener.get(15, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    byte[] content = response.getContent();
    Assert.assertArrayEquals(data, content);
}
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) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 23 with FutureResponseListener

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

the class HttpClientTest method testContentDelimitedByEOFWithSlowRequest.

private void testContentDelimitedByEOFWithSlowRequest(int length) throws Exception {
    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);
            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).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 24 with FutureResponseListener

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

the class AsyncMiddleManServletTest method testProxyRequestHeadersSentWhenDiscardingContent.

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

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

        @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(0, 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)

Example 25 with FutureResponseListener

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

the class AsyncMiddleManServletTest method testUpstreamTransformationBufferedGzipped.

@Test
public void testUpstreamTransformationBufferedGzipped() throws Exception {
    startServer(new EchoHttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            super.service(request, response);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new GZIPContentTransformer(new BufferingContentTransformer());
        }
    });
    startClient();
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort());
    FutureResponseListener listener = new FutureResponseListener(request);
    request.header(HttpHeader.CONTENT_ENCODING, "gzip").content(content).send(listener);
    byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
    content.offer(ByteBuffer.wrap(gzip(bytes)));
    sleep(1000);
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, 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) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) 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