Search in sources :

Example 11 with Response

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

the class AsyncMiddleManServletTest method testServerResponseContentKnownLengthGzipped.

@Test
public void testServerResponseContentKnownLengthGzipped() throws Exception {
    byte[] bytes = new byte[1024];
    new Random().nextBytes(bytes);
    final byte[] gzipBytes = gzip(bytes);
    startServer(new HttpServlet() {

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

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new GZIPContentTransformer(ContentTransformer.IDENTITY);
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) Test(org.junit.Test)

Example 12 with Response

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

the class AsyncMiddleManServletTest method testTransformGzippedHead.

@Test
public void testTransformGzippedHead() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            String sample = "<a href=\"http://webtide.com/\">Webtide</a>\n<a href=\"http://google.com\">Google</a>\n";
            byte[] bytes = sample.getBytes(StandardCharsets.UTF_8);
            ServletOutputStream out = response.getOutputStream();
            out.write(gzip(bytes));
            // create a byte buffer larger enough to create 2 (or more) transforms.
            byte[] randomFiller = new byte[64 * 1024];
            /* fill with nonsense
                 * Using random data to ensure compressed buffer size is large
                 * enough to trigger at least 2 transform() events.
                 */
            new Random().nextBytes(randomFiller);
            out.write(gzip(randomFiller));
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new GZIPContentTransformer(new HeadTransformer());
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    String expectedStr = "<a href=\"http://webtide.com/\">Webtide</a>";
    byte[] expected = expectedStr.getBytes(StandardCharsets.UTF_8);
    Assert.assertArrayEquals(expected, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) Test(org.junit.Test)

Example 13 with Response

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

the class AsyncMiddleManServletTest method testDownstreamTransformationBufferedGzipped.

@Test
public void testDownstreamTransformationBufferedGzipped() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            ServletInputStream input = request.getInputStream();
            ServletOutputStream output = response.getOutputStream();
            int read;
            while ((read = input.read()) >= 0) {
                output.write(read);
                output.flush();
            }
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new GZIPContentTransformer(new BufferingContentTransformer());
        }
    });
    startClient();
    byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 14 with Response

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

the class AsyncMiddleManServletTest method testDownstreamTransformationKnownContentLengthDroppingLastChunk.

@Test
public void testDownstreamTransformationKnownContentLengthDroppingLastChunk() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            byte[] chunk = new byte[1024];
            int contentLength = 2 * chunk.length;
            response.setContentLength(contentLength);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk);
            output.flush();
            sleep(1000);
            output.write(chunk);
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new ContentTransformer() {

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    if (!finished)
                        output.add(input);
                }
            };
        }
    });
    startClient();
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 15 with Response

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

the class HttpClientTest method test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded.

@Test
public void test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded() throws Exception {
    start(new EmptyServerHandler());
    client.setMaxConnectionsPerDestination(1);
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(2);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
        try {
            latch.await();
        } catch (InterruptedException x) {
            x.printStackTrace();
        }
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            Assert.assertEquals(200, response.getStatus());
            successLatch.countDown();
        }
    });
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(request -> latch.countDown()).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            Assert.assertEquals(200, response.getStatus());
            successLatch.countDown();
        }
    });
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)105 Test (org.junit.Test)89 HttpServletResponse (javax.servlet.http.HttpServletResponse)88 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)83 IOException (java.io.IOException)72 HttpServletRequest (javax.servlet.http.HttpServletRequest)69 ServletException (javax.servlet.ServletException)67 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)33 InputStream (java.io.InputStream)32 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 Request (org.eclipse.jetty.server.Request)26 Request (org.eclipse.jetty.client.api.Request)25 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)24 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22