Search in sources :

Example 1 with OutputStreamContentProvider

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

the class AsyncRequestContentTest method testEmptyOutputStream.

@Test
public void testEmptyOutputStream() throws Exception {
    start(new ConsumeInputHandler());
    OutputStreamContentProvider contentProvider = new OutputStreamContentProvider();
    CountDownLatch latch = new CountDownLatch(1);
    client.POST(newURI()).content(contentProvider).send(result -> {
        if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
            latch.countDown();
    });
    contentProvider.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with OutputStreamContentProvider

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

the class AsyncRequestContentTest method testOutputStream.

@Test
public void testOutputStream() throws Exception {
    start(new ConsumeInputHandler());
    OutputStreamContentProvider contentProvider = new OutputStreamContentProvider();
    CountDownLatch latch = new CountDownLatch(1);
    client.POST(newURI()).content(contentProvider).send(result -> {
        if (result.isSucceeded() && result.getResponse().getStatus() == HttpStatus.OK_200)
            latch.countDown();
    });
    OutputStream output = contentProvider.getOutputStream();
    output.write(new byte[1]);
    output.flush();
    contentProvider.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with OutputStreamContentProvider

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

the class HttpClientStreamTest method testBigUploadWithOutputStreamFromInputStream.

@Test
public void testBigUploadWithOutputStreamFromInputStream() throws Exception {
    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);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[16 * 1024 * 1024];
    new Random().nextBytes(data);
    final CountDownLatch latch = new CountDownLatch(1);
    OutputStreamContentProvider content = new OutputStreamContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener(data.length) {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertArrayEquals(data, getContent());
            latch.countDown();
        }
    });
    // Make sure we provide the content *after* the request has been "sent".
    Thread.sleep(1000);
    try (InputStream input = new ByteArrayInputStream(data);
        OutputStream output = content.getOutputStream()) {
        byte[] buffer = new byte[1024];
        while (true) {
            int read = input.read(buffer);
            if (read < 0)
                break;
            output.write(buffer, 0, read);
        }
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 4 with OutputStreamContentProvider

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

the class HttpClientStreamTest method testUploadWithOutputStream.

@Test
public void testUploadWithOutputStream() throws Exception {
    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);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    final byte[] data = new byte[512];
    final CountDownLatch latch = new CountDownLatch(1);
    OutputStreamContentProvider content = new OutputStreamContentProvider();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(content).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded() && result.getResponse().getStatus() == 200 && Arrays.equals(data, getContent()))
                latch.countDown();
        }
    });
    // Make sure we provide the content *after* the request has been "sent".
    Thread.sleep(1000);
    try (OutputStream output = content.getOutputStream()) {
        output.write(data);
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 5 with OutputStreamContentProvider

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

the class HttpClientStreamTest method testUploadWithOutputStreamFailureToConnect.

@Test
public void testUploadWithOutputStreamFailureToConnect() throws Exception {
    start(new EmptyServerHandler());
    final byte[] data = new byte[512];
    final CountDownLatch latch = new CountDownLatch(1);
    OutputStreamContentProvider content = new OutputStreamContentProvider();
    client.newRequest("0.0.0.1", connector.getLocalPort()).scheme(getScheme()).content(content).send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    try (OutputStream output = content.getOutputStream()) {
        output.write(data);
        Assert.fail();
    } catch (IOException x) {
    // Expected
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : OutputStreamContentProvider(org.eclipse.jetty.client.util.OutputStreamContentProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

OutputStreamContentProvider (org.eclipse.jetty.client.util.OutputStreamContentProvider)7 OutputStream (java.io.OutputStream)6 Test (org.junit.Test)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 InterruptedIOException (java.io.InterruptedIOException)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Result (org.eclipse.jetty.client.api.Result)2 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)2 Request (org.eclipse.jetty.server.Request)2 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Random (java.util.Random)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 OutboundMessageContext (org.glassfish.jersey.message.internal.OutboundMessageContext)1