Search in sources :

Example 81 with Result

use of org.eclipse.jetty.client.api.Result 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 Result

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

the class HttpClientStreamTest method testUploadWithDeferredContentProviderFailsMultipleOffers.

@Test
public void testUploadWithDeferredContentProviderFailsMultipleOffers() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch failLatch = new CountDownLatch(2);
    final Callback callback = new Callback() {

        @Override
        public void failed(Throwable x) {
            failLatch.countDown();
        }
    };
    final CountDownLatch completeLatch = new CountDownLatch(1);
    final DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).onRequestBegin(request -> {
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        content.offer(ByteBuffer.wrap(new byte[256]), callback);
        request.abort(new Exception("explicitly_thrown_by_test"));
    }).send(result -> {
        if (result.isFailed())
            completeLatch.countDown();
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(failLatch.await(5, TimeUnit.SECONDS));
    // Make sure that adding more content results in the callback to be failed.
    final CountDownLatch latch = new CountDownLatch(1);
    content.offer(ByteBuffer.wrap(new byte[128]), new Callback() {

        @Override
        public void failed(Throwable x) {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) 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) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) BufferUtil(org.eclipse.jetty.util.BufferUtil) Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) MavenTestingUtils(org.eclipse.jetty.toolchain.test.MavenTestingUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) DispatcherType(javax.servlet.DispatcherType) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) Callback(org.eclipse.jetty.util.Callback) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServletException(javax.servlet.ServletException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 83 with Result

use of org.eclipse.jetty.client.api.Result 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 Result

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

the class HttpClientStreamTest method testDownloadOfUTF8Content.

@Test
public void testDownloadOfUTF8Content() throws Exception {
    // UTF-8 representation of è
    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 Result

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

the class HttpClientStreamTest method testInputStreamResponseListenerFailedBeforeResponse.

@Test
public void testInputStreamResponseListenerFailedBeforeResponse() throws Exception {
    start(new EmptyServerHandler());
    int port = connector.getLocalPort();
    server.stop();
    InputStreamResponseListener listener = new InputStreamResponseListener();
    // Connect to the wrong port
    client.newRequest("localhost", port).scheme(getScheme()).send(listener);
    Result result = listener.await(5, TimeUnit.SECONDS);
    Assert.assertNotNull(result);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16