Search in sources :

Example 6 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_Respond100Continue.

private void test_Expect100Continue_Respond100Continue(byte[]... contents) throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and copy the content back
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    ContentResponse response = client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(contents)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    int index = 0;
    byte[] responseContent = response.getContent();
    for (byte[] content : contents) {
        for (byte b : content) {
            Assert.assertEquals(b, responseContent[index++]);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Example 7 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithRedirect.

@Test
public void test_Expect100Continue_WithContent_WithRedirect() throws Exception {
    final String data = "success";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (request.getRequestURI().endsWith("/done")) {
                response.getOutputStream().print(data);
            } else {
                // Send 100-Continue and consume the content
                IO.copy(request.getInputStream(), new ByteArrayOutputStream());
                // Send a redirect
                response.sendRedirect("/done");
            }
        }
    });
    byte[] content = new byte[10240];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path("/continue").header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertEquals(200, result.getResponse().getStatus());
            Assert.assertEquals(data, getContentAsString());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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 8 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_After100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_After100Continue() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            // Send 100-Continue and consume the content
            IO.copy(request.getInputStream(), new ByteArrayOutputStream());
            try {
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    client.setIdleTimeout(idleTimeout);
    byte[] content = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNull(result.getRequestFailure());
            Assert.assertNotNull(result.getResponseFailure());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * idleTimeout, TimeUnit.MILLISECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 9 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_RespondError.

private void test_Expect100Continue_WithContent_RespondError(final int error) throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.sendError(error);
        }
    });
    byte[] content1 = new byte[10240];
    byte[] content2 = new byte[16384];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content1, content2)).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertNotNull(result.getRequestFailure());
            Assert.assertNull(result.getResponseFailure());
            byte[] content = getContent();
            Assert.assertNotNull(content);
            Assert.assertTrue(content.length > 0);
            Assert.assertEquals(error, result.getResponse().getStatus());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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)

Example 10 with BytesContentProvider

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

the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeReading.

@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeReading() 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());
        }
    });
    InputStreamResponseListener listener = new InputStreamResponseListener();
    InputStream stream = listener.getInputStream();
    // Close the stream immediately.
    stream.close();
    client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).content(new BytesContentProvider(new byte[] { 0, 1, 2, 3 })).send(listener);
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Throws
    stream.read();
}
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) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) 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) Test(org.junit.Test)

Aggregations

BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)32 IOException (java.io.IOException)31 Test (org.junit.Test)30 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)29 HttpServletResponse (javax.servlet.http.HttpServletResponse)28 ServletException (javax.servlet.ServletException)27 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 Request (org.eclipse.jetty.server.Request)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Response (org.eclipse.jetty.client.api.Response)11 Result (org.eclipse.jetty.client.api.Result)11 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)10 InputStream (java.io.InputStream)9 ByteBuffer (java.nio.ByteBuffer)9 HttpServlet (javax.servlet.http.HttpServlet)9 InterruptedIOException (java.io.InterruptedIOException)8 Random (java.util.Random)8