Search in sources :

Example 1 with BytesContentProvider

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

the class HttpClientTest method testPOSTWithContentTracksProgress.

@Test
public void testPOSTWithContentTracksProgress() throws Exception {
    start(new EmptyServerHandler());
    final AtomicInteger progress = new AtomicInteger();
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent(new Request.ContentListener() {

        @Override
        public void onContent(Request request, ByteBuffer buffer) {
            byte[] bytes = new byte[buffer.remaining()];
            Assert.assertEquals(1, bytes.length);
            buffer.get(bytes);
            Assert.assertEquals(bytes[0], progress.getAndIncrement());
        }
    }).content(new BytesContentProvider(new byte[] { 0 }, new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 }, new byte[] { 4 })).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(5, progress.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with BytesContentProvider

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

the class HttpClientTest method testPOSTWithContentNotifiesRequestContentListener.

@Test
public void testPOSTWithContentNotifiesRequestContentListener() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    start(new EmptyServerHandler());
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent(new Request.ContentListener() {

        @Override
        public void onContent(Request request, ByteBuffer buffer) {
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            if (!Arrays.equals(content, bytes))
                request.abort(new Exception());
        }
    }).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) EOFException(java.io.EOFException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with BytesContentProvider

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

the class HttpClientTest method testPOSTWithParametersWithContent.

@Test
public void testPOSTWithParametersWithContent() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    final String paramName = "a";
    final String paramValue = "€";
    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);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/octet-stream");
                IO.copy(request.getInputStream(), response.getOutputStream());
            }
        }
    });
    for (int i = 0; i < 256; ++i) {
        ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertArrayEquals(content, response.getContent());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) 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) Test(org.junit.Test)

Example 4 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue.

@Slow
@Test
public void test_Expect100Continue_WithContent_WithResponseFailure_Before100Continue() 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);
            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.assertNotNull(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) 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 5 with BytesContentProvider

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

the class HttpClientContinueTest method test_Expect100Continue_WithTwoResponsesInOneRead.

@Test
public void test_Expect100Continue_WithTwoResponsesInOneRead() throws Exception {
    Assume.assumeThat(transport, Matchers.isOneOf(Transport.HTTP, Transport.HTTPS));
    // There is a chance that the server replies with the 100 Continue response
    // and immediately after with the "normal" response, say a 200 OK.
    // These may be read by the client in a single read, and must be handled correctly.
    startClient();
    try (ServerSocket server = new ServerSocket()) {
        server.bind(new InetSocketAddress("localhost", 0));
        final CountDownLatch latch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(new byte[] { 0 })).send(result -> {
            Assert.assertTrue(result.toString(), result.isSucceeded());
            Assert.assertEquals(200, result.getResponse().getStatus());
            latch.countDown();
        });
        try (Socket socket = server.accept()) {
            // Read the request headers.
            InputStream input = socket.getInputStream();
            int crlfs = 0;
            while (true) {
                int read = input.read();
                if (read == '\r' || read == '\n')
                    ++crlfs;
                else
                    crlfs = 0;
                if (crlfs == 4)
                    break;
            }
            OutputStream output = socket.getOutputStream();
            String responses = "" + "HTTP/1.1 100 Continue\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "10\r\n" + "0123456789ABCDEF\r\n";
            output.write(responses.getBytes(StandardCharsets.UTF_8));
            output.flush();
            Thread.sleep(1000);
            String content = "" + "10\r\n" + "0123456789ABCDEF\r\n" + "0\r\n" + "\r\n";
            output.write(content.getBytes(StandardCharsets.UTF_8));
            output.flush();
            Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) 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