Search in sources :

Example 76 with Response

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

the class HttpClientRedirectTest method testRelativeLocation.

@Test
public void testRelativeLocation() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/done?relative=true").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 77 with Response

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

the class HttpClientSynchronizationTest method testSynchronizationOnException.

@Test
public void testSynchronizationOnException() throws Exception {
    start(new EmptyServerHandler());
    int port = connector.getLocalPort();
    server.stop();
    int count = 10;
    final CountDownLatch latch = new CountDownLatch(count);
    for (int i = 0; i < count; ++i) {
        Request request = client.newRequest("localhost", port).scheme(scheme);
        synchronized (this) {
            request.send(new Response.Listener.Adapter() {

                @Override
                public void onFailure(Response response, Throwable failure) {
                    synchronized (HttpClientSynchronizationTest.this) {
                        Assert.assertThat(failure, Matchers.instanceOf(ConnectException.class));
                        latch.countDown();
                    }
                }
            });
        }
    }
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : Response(org.eclipse.jetty.client.api.Response) Request(org.eclipse.jetty.client.api.Request) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 78 with Response

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

the class AsyncIOServletTest method testAsyncWriteLessThanContentLengthFlushed.

@Test
public void testAsyncWriteLessThanContentLengthFlushed() throws Exception {
    CountDownLatch complete = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentLength(10);
            AsyncContext async = request.startAsync();
            ServletOutputStream out = response.getOutputStream();
            AtomicInteger state = new AtomicInteger(0);
            out.setWriteListener(new WriteListener() {

                @Override
                public void onWritePossible() throws IOException {
                    while (true) {
                        if (!out.isReady())
                            return;
                        switch(state.get()) {
                            case 0:
                                state.incrementAndGet();
                                WriteListener listener = this;
                                new Thread(() -> {
                                    try {
                                        Thread.sleep(50);
                                        listener.onWritePossible();
                                    } catch (Exception e) {
                                    }
                                }).start();
                                return;
                            case 1:
                                state.incrementAndGet();
                                out.flush();
                                break;
                            case 2:
                                state.incrementAndGet();
                                out.write("12345".getBytes());
                                break;
                            case 3:
                                async.complete();
                                complete.countDown();
                                return;
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                }
            });
        }
    });
    AtomicBoolean failed = new AtomicBoolean(false);
    CountDownLatch clientLatch = new CountDownLatch(3);
    client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
        if (response.getStatus() == HttpStatus.OK_200)
            clientLatch.countDown();
    }).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
        // System.err.println("Content: "+BufferUtil.toDetailString(content));
        }
    }).onResponseFailure(new Response.FailureListener() {

        @Override
        public void onFailure(Response response, Throwable failure) {
            clientLatch.countDown();
        }
    }).send(result -> {
        failed.set(result.isFailed());
        clientLatch.countDown();
        clientLatch.countDown();
        clientLatch.countDown();
    });
    assertTrue(complete.await(10, TimeUnit.SECONDS));
    assertTrue(clientLatch.await(10, TimeUnit.SECONDS));
    assertTrue(failed.get());
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) Context(org.eclipse.jetty.server.handler.ContextHandler.Context) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) BufferUtil.toArray(org.eclipse.jetty.util.BufferUtil.toArray) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) HttpInput(org.eclipse.jetty.server.HttpInput) Response(org.eclipse.jetty.client.api.Response) HttpServlet(javax.servlet.http.HttpServlet) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Content(org.eclipse.jetty.server.HttpInput.Content) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Matchers.is(org.hamcrest.Matchers.is) Queue(java.util.Queue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BufferUtil(org.eclipse.jetty.util.BufferUtil) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Result(org.eclipse.jetty.client.api.Result) Handler(org.eclipse.jetty.server.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpChannel(org.eclipse.jetty.server.HttpChannel) ServletInputStream(javax.servlet.ServletInputStream) ByteBuffer.wrap(java.nio.ByteBuffer.wrap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Deque(java.util.Deque) InterruptedIOException(java.io.InterruptedIOException) AsyncContext(javax.servlet.AsyncContext) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) WriteListener(javax.servlet.WriteListener) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) HttpConnectionOverHTTP2(org.eclipse.jetty.http2.client.http.HttpConnectionOverHTTP2) Assume(org.junit.Assume) Executor(java.util.concurrent.Executor) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) TimeUnit(java.util.concurrent.TimeUnit) HttpMethod(org.eclipse.jetty.http.HttpMethod) Connection(org.eclipse.jetty.io.Connection) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ReadListener(javax.servlet.ReadListener) DispatcherType(javax.servlet.DispatcherType) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteListener(javax.servlet.WriteListener) Test(org.junit.Test)

Example 79 with Response

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

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

the class HttpClientStreamTest method testDownloadWithCloseMiddleOfContent.

@Test(expected = AsynchronousCloseException.class)
public void testDownloadWithCloseMiddleOfContent() throws Exception {
    final byte[] data1 = new byte[1024];
    final byte[] data2 = new byte[1024];
    final CountDownLatch latch = new CountDownLatch(1);
    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(data1);
            response.flushBuffer();
            try {
                Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                throw new InterruptedIOException();
            }
            response.getOutputStream().write(data2);
        }
    });
    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 datum1 : data1) Assert.assertEquals(datum1, input.read());
    input.close();
    latch.countDown();
    // Throws
    input.read();
}
Also used : Request(org.eclipse.jetty.server.Request) InterruptedIOException(java.io.InterruptedIOException) 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) CountDownLatch(java.util.concurrent.CountDownLatch) 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

Response (org.eclipse.jetty.client.api.Response)113 Test (org.junit.Test)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)89 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)84 IOException (java.io.IOException)77 HttpServletRequest (javax.servlet.http.HttpServletRequest)70 ServletException (javax.servlet.ServletException)68 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ByteBuffer (java.nio.ByteBuffer)34 ServletOutputStream (javax.servlet.ServletOutputStream)34 Request (org.eclipse.jetty.client.api.Request)31 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)29 Request (org.eclipse.jetty.server.Request)26 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22