Search in sources :

Example 66 with Result

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

the class HttpClientTest method testAllHeadersDiscarded.

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

            @Override
            public boolean onHeader(Response response, HttpField field) {
                return false;
            }

            @Override
            public void onComplete(Result result) {
                if (result.isSucceeded())
                    latch.countDown();
            }
        });
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) HttpField(org.eclipse.jetty.http.HttpField) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 67 with Result

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

the class HttpClientTest method test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete.

@Test
public void test_ExchangeIsComplete_OnlyWhenBothRequestAndResponseAreComplete() throws Exception {
    start(new RespondThenConsumeHandler());
    // Prepare a big file to upload
    Path targetTestsDir = testdir.getEmptyPathDir();
    Files.createDirectories(targetTestsDir);
    Path file = Paths.get(targetTestsDir.toString(), "http_client_conversation.big");
    try (OutputStream output = Files.newOutputStream(file, StandardOpenOption.CREATE)) {
        byte[] kb = new byte[1024];
        for (int i = 0; i < 10 * 1024; ++i) output.write(kb);
    }
    final CountDownLatch latch = new CountDownLatch(3);
    final AtomicLong exchangeTime = new AtomicLong();
    final AtomicLong requestTime = new AtomicLong();
    final AtomicLong responseTime = new AtomicLong();
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).file(file).onRequestSuccess(request -> {
        requestTime.set(System.nanoTime());
        latch.countDown();
    }).send(new Response.Listener.Adapter() {

        @Override
        public void onSuccess(Response response) {
            responseTime.set(System.nanoTime());
            latch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            exchangeTime.set(System.nanoTime());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    Assert.assertTrue(requestTime.get() <= exchangeTime.get());
    Assert.assertTrue(responseTime.get() <= exchangeTime.get());
    // Give some time to the server to consume the request content
    // This is just to avoid exception traces in the test output
    Thread.sleep(1000);
    Files.delete(file);
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) EndPoint(org.eclipse.jetty.io.EndPoint) TestingDir(org.eclipse.jetty.toolchain.test.TestingDir) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) Request(org.eclipse.jetty.client.api.Request) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) ServerSocket(java.net.ServerSocket) HttpCookie(java.net.HttpCookie) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) URI(java.net.URI) Path(java.nio.file.Path) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow) StandardOpenOption(java.nio.file.StandardOpenOption) IO(org.eclipse.jetty.util.IO) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FuturePromise(org.eclipse.jetty.util.FuturePromise) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) BadMessageException(org.eclipse.jetty.http.BadMessageException) Connection(org.eclipse.jetty.client.api.Connection) Result(org.eclipse.jetty.client.api.Result) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) ArrayList(java.util.ArrayList) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) Assume(org.junit.Assume) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) Iterator(java.util.Iterator) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) AtomicLong(java.util.concurrent.atomic.AtomicLong) URLEncoder(java.net.URLEncoder) HttpMethod(org.eclipse.jetty.http.HttpMethod) Rule(org.junit.Rule) Paths(java.nio.file.Paths) HttpField(org.eclipse.jetty.http.HttpField) ContentProvider(org.eclipse.jetty.client.api.ContentProvider) Destination(org.eclipse.jetty.client.api.Destination) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) Exchanger(java.util.concurrent.Exchanger) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 68 with Result

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

the class HttpClientTest method testCompleteNotInvokedUntilContentConsumed.

@Test
public void testCompleteNotInvokedUntilContentConsumed() 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);
            ServletOutputStream output = response.getOutputStream();
            output.write(new byte[1024]);
        }
    });
    final AtomicReference<Callback> callbackRef = new AtomicReference<>();
    final CountDownLatch contentLatch = new CountDownLatch(1);
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send(new Response.Listener.Adapter() {

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            // Do not notify the callback yet.
            callbackRef.set(callback);
            contentLatch.countDown();
        }

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded())
                completeLatch.countDown();
        }
    });
    Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
    // Make sure the complete event is not emitted.
    Assert.assertFalse(completeLatch.await(1, TimeUnit.SECONDS));
    // Consume the content.
    callbackRef.get().succeeded();
    // Now the complete event is emitted.
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) 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) Callback(org.eclipse.jetty.util.Callback) Test(org.junit.Test)

Example 69 with Result

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

the class HttpClientTest method testRequestRetries.

@Test
public void testRequestRetries() throws Exception {
    final int maxRetries = 3;
    final AtomicInteger requests = new AtomicInteger();
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            int count = requests.incrementAndGet();
            if (count == maxRetries)
                baseRequest.setHandled(true);
            consume(request.getInputStream(), true);
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    new RetryListener(client, scheme, "localhost", connector.getLocalPort(), maxRetries) {

        @Override
        protected void completed(Result result) {
            latch.countDown();
        }
    }.perform();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 70 with Result

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

the class HttpClientTimeoutTest method testConnectTimeoutIsCancelledByShorterRequestTimeout.

@Slow
@Test
public void testConnectTimeoutIsCancelledByShorterRequestTimeout() throws Exception {
    String host = "10.255.255.1";
    int port = 80;
    int connectTimeout = 2000;
    assumeConnectTimeout(host, port, connectTimeout);
    start(new EmptyServerHandler());
    client.stop();
    client.setConnectTimeout(connectTimeout);
    client.start();
    final AtomicInteger completes = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(2);
    Request request = client.newRequest(host, port);
    request.scheme(scheme).timeout(connectTimeout / 2, TimeUnit.MILLISECONDS).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completes.incrementAndGet();
            latch.countDown();
        }
    });
    Assert.assertFalse(latch.await(2 * connectTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(1, completes.get());
    Assert.assertNotNull(request.getAbortCause());
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) EndPoint(org.eclipse.jetty.io.EndPoint) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

Result (org.eclipse.jetty.client.api.Result)100 Test (org.junit.Test)80 CountDownLatch (java.util.concurrent.CountDownLatch)79 HttpServletResponse (javax.servlet.http.HttpServletResponse)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)66 Response (org.eclipse.jetty.client.api.Response)62 IOException (java.io.IOException)60 ServletException (javax.servlet.ServletException)56 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)44 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)41 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)39 Request (org.eclipse.jetty.client.api.Request)31 ByteBuffer (java.nio.ByteBuffer)29 Request (org.eclipse.jetty.server.Request)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)24 InterruptedIOException (java.io.InterruptedIOException)21 ServletOutputStream (javax.servlet.ServletOutputStream)20 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)16