Search in sources :

Example 81 with Response

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

the class HttpClientStreamTest method testInputStreamResponseListenerWithRedirect.

@Test
public void testInputStreamResponseListenerWithRedirect() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.startsWith("/303"))
                response.sendRedirect("/200");
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).path("/303").followRedirects(true).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertTrue(result.isSucceeded());
}
Also used : 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) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) 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) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 82 with Response

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

the class HttpClientStreamTest method testInputStreamResponseListenerBufferedRead.

@Test
public void testInputStreamResponseListenerBufferedRead() throws Exception {
    AtomicReference<AsyncContext> asyncContextRef = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    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);
            asyncContextRef.set(request.startAsync());
            latch.countDown();
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).timeout(5, TimeUnit.SECONDS).send(listener);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    AsyncContext asyncContext = asyncContextRef.get();
    Assert.assertNotNull(asyncContext);
    Random random = new Random();
    byte[] chunk = new byte[64];
    random.nextBytes(chunk);
    ServletOutputStream output = asyncContext.getResponse().getOutputStream();
    output.write(chunk);
    output.flush();
    // Use a buffer larger than the data
    // written to test that the read returns.
    byte[] buffer = new byte[2 * chunk.length];
    InputStream stream = listener.getInputStream();
    int totalRead = 0;
    while (totalRead < chunk.length) {
        int read = stream.read(buffer);
        Assert.assertTrue(read > 0);
        totalRead += read;
    }
    asyncContext.complete();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Request(org.eclipse.jetty.server.Request) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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 83 with Response

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

the class HttpClientStreamTest method testDownloadWithFailure.

@Test
public void testDownloadWithFailure() throws Exception {
    final byte[] data = new byte[64 * 1024];
    byte value = 1;
    Arrays.fill(data, value);
    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);
            // Say we want to send this much...
            response.setContentLength(2 * data.length);
            // ...but write only half...
            response.getOutputStream().write(data);
            // ...then shutdown output
            baseRequest.getHttpChannel().getEndPoint().shutdownOutput();
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    Assert.assertNotNull(input);
    int length = 0;
    try {
        length = 0;
        while (input.read() == value) {
            if (length % 100 == 0)
                Thread.sleep(1);
            ++length;
        }
        Assert.fail();
    } catch (IOException x) {
    // Expected.
    }
    Assert.assertThat(length, Matchers.lessThanOrEqualTo(data.length));
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isFailed());
}
Also used : Request(org.eclipse.jetty.server.Request) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) 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 84 with Response

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

the class HttpClientStreamTest method testDownloadOfUTF8Content.

@Test
public void testDownloadOfUTF8Content() throws Exception {
    // UTF-8 representation of &egrave;
    final byte[] data = new byte[] { (byte) 0xC3, (byte) 0xA8 };
    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.getOutputStream().write(data);
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    InputStream input = listener.getInputStream();
    Assert.assertNotNull(input);
    for (byte b : data) {
        int read = input.read();
        Assert.assertTrue(read >= 0);
        Assert.assertEquals(b & 0xFF, read);
    }
    Assert.assertEquals(-1, input.read());
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isFailed());
    Assert.assertSame(response, result.getResponse());
}
Also used : Request(org.eclipse.jetty.server.Request) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) 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 85 with Response

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

the class HttpClientStreamTest method testInputStreamResponseListenerFailedWhileWaiting.

@Test
public void testInputStreamResponseListenerFailedWhileWaiting() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            byte[] data = new byte[1024];
            response.setContentLength(data.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(data);
        }
    });
    CountDownLatch failedLatch = new CountDownLatch(1);
    CountDownLatch contentLatch = new CountDownLatch(1);
    InputStreamResponseListener listener = new InputStreamResponseListener() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            super.onContent(response, content, new Callback() {

                @Override
                public void failed(Throwable x) {
                    failedLatch.countDown();
                    callback.failed(x);
                }
            });
            contentLatch.countDown();
        }
    };
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    // Wait until we get some content.
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    // Abort the response.
    response.abort(new Exception());
    // Make sure that the callback has been invoked.
    Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) 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) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22