Search in sources :

Example 26 with BytesContentProvider

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

the class ConnectionStatisticsTest method testConnectionStatistics.

@Test
public void testConnectionStatistics() throws Exception {
    Assume.assumeThat(transport, Matchers.isOneOf(Transport.H2C, Transport.H2));
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    ConnectionStatistics serverStats = new ConnectionStatistics();
    connector.addBean(serverStats);
    serverStats.start();
    ConnectionStatistics clientStats = new ConnectionStatistics();
    client.addBean(clientStats);
    clientStats.start();
    byte[] content = new byte[3072];
    long contentLength = content.length;
    ContentResponse response = client.newRequest(newURI()).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
    // The bytes have already arrived, but give time to
    // the server to finish to run the response logic.
    Thread.sleep(1000);
    // Close all connections.
    stop();
    // Give some time to process the stop event.
    Thread.sleep(1000);
    Assert.assertThat(serverStats.getConnectionsMax(), Matchers.greaterThan(0L));
    Assert.assertThat(serverStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
    Assert.assertThat(serverStats.getSentBytes(), Matchers.greaterThan(contentLength));
    Assert.assertThat(serverStats.getReceivedMessages(), Matchers.greaterThan(0L));
    Assert.assertThat(serverStats.getSentMessages(), Matchers.greaterThan(0L));
    Assert.assertThat(clientStats.getConnectionsMax(), Matchers.greaterThan(0L));
    Assert.assertThat(clientStats.getReceivedBytes(), Matchers.greaterThan(contentLength));
    Assert.assertThat(clientStats.getSentBytes(), Matchers.greaterThan(contentLength));
    Assert.assertThat(clientStats.getReceivedMessages(), Matchers.greaterThan(0L));
    Assert.assertThat(clientStats.getSentMessages(), Matchers.greaterThan(0L));
}
Also used : ConnectionStatistics(org.eclipse.jetty.io.ConnectionStatistics) 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) Test(org.junit.Test)

Example 27 with BytesContentProvider

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

the class HttpClientLoadTest method test.

private void test(String scheme, String host, String method, boolean clientClose, boolean serverClose, int contentLength, final boolean checkContentLength, final CountDownLatch latch, final List<String> failures) {
    long requestId = requestCount.incrementAndGet();
    Request request = client.newRequest(host, connector.getLocalPort()).scheme(scheme).path("/" + requestId).method(method);
    if (clientClose)
        request.header(HttpHeader.CONNECTION, "close");
    else if (serverClose)
        request.header("X-Close", "true");
    switch(method) {
        case "GET":
            request.header("X-Download", String.valueOf(contentLength));
            break;
        case "POST":
            request.header("X-Upload", String.valueOf(contentLength));
            request.content(new BytesContentProvider(new byte[contentLength]));
            break;
    }
    final CountDownLatch requestLatch = new CountDownLatch(1);
    request.send(new Response.Listener.Adapter() {

        private final AtomicInteger contentLength = new AtomicInteger();

        @Override
        public void onHeaders(Response response) {
            if (checkContentLength) {
                String content = response.getHeaders().get("X-Content");
                if (content != null)
                    contentLength.set(Integer.parseInt(content));
            }
        }

        @Override
        public void onContent(Response response, ByteBuffer content) {
            if (checkContentLength)
                contentLength.addAndGet(-content.remaining());
        }

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                result.getFailure().printStackTrace();
                failures.add("Result failed " + result);
            }
            if (checkContentLength && contentLength.get() != 0)
                failures.add("Content length mismatch " + contentLength);
            requestLatch.countDown();
            latch.countDown();
        }
    });
    if (!await(requestLatch, 5, TimeUnit.SECONDS)) {
        logger.warn("Request {} took too long{}{}{}{}", requestId, System.lineSeparator(), server.dump(), System.lineSeparator(), client.dump());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result)

Example 28 with BytesContentProvider

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

the class HttpClientTest method testUploadWithoutResponseContent.

private void testUploadWithoutResponseContent(int length) throws Exception {
    final byte[] bytes = new byte[length];
    new Random().nextBytes(bytes);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            for (int i = 0; i < bytes.length; ++i) Assert.assertEquals(bytes[i] & 0xFF, input.read());
            Assert.assertEquals(-1, input.read());
        }
    });
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(new BytesContentProvider(bytes)).timeout(15, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(0, response.getContent().length);
}
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) 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) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random)

Example 29 with BytesContentProvider

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

the class HttpClientTest method testClientManyWritesSlowServer.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            long sleep = 1024;
            long total = 0;
            ServletInputStream input = request.getInputStream();
            byte[] buffer = new byte[1024];
            while (true) {
                int read = input.read(buffer);
                if (read < 0)
                    break;
                total += read;
                if (total >= sleep) {
                    sleep(250);
                    sleep += 256;
                }
            }
            response.getOutputStream().print(total);
        }
    });
    int chunks = 256;
    int chunkSize = 16;
    byte[][] bytes = IntStream.range(0, chunks).mapToObj(x -> new byte[chunkSize]).toArray(byte[][]::new);
    BytesContentProvider contentProvider = new BytesContentProvider("application/octet-stream", bytes);
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).content(contentProvider).timeout(15, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(chunks * chunkSize, Integer.parseInt(response.getContentAsString()));
}
Also used : IntStream(java.util.stream.IntStream) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) InterruptedIOException(java.io.InterruptedIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Assume(org.junit.Assume) HttpStatus(org.eclipse.jetty.http.HttpStatus) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) EnumSet(java.util.EnumSet) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) IO(org.eclipse.jetty.util.IO) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) HttpMethod(org.eclipse.jetty.http.HttpMethod) InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Assert(org.junit.Assert) InputStream(java.io.InputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 30 with BytesContentProvider

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

the class HttpClientContinueTest method test_Redirect_WithExpect100Continue_WithContent.

@Test
public void test_Redirect_WithExpect100Continue_WithContent() throws Exception {
    // A request with Expect: 100-Continue cannot receive non-final responses like 3xx
    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")) {
                // Send 100-Continue and consume the content
                IO.copy(request.getInputStream(), new ByteArrayOutputStream());
                response.getOutputStream().print(data);
            } else {
                // Send a redirect
                response.sendRedirect("/done");
            }
        }
    });
    byte[] content = new byte[10240];
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).method(HttpMethod.POST).path("/redirect").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.assertNull(result.getResponseFailure());
            Assert.assertEquals(302, 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) 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)

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