Search in sources :

Example 31 with Response

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccessWithExplicitConnection.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccessWithExplicitConnection() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme).timeout(2 * timeout, TimeUnit.MILLISECONDS);
        connection.send(request, new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                Response response = result.getResponse();
                Assert.assertEquals(200, response.getStatus());
                Assert.assertFalse(result.isFailed());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
        TimeUnit.MILLISECONDS.sleep(2 * timeout);
        Assert.assertNull(request.getAbortCause());
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Destination(org.eclipse.jetty.client.api.Destination) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 32 with Response

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

the class HttpCookieTest method test_CookieIsSent.

@Test
public void test_CookieIsSent() throws Exception {
    final String name = "foo";
    final String value = "bar";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Cookie[] cookies = request.getCookies();
            Assert.assertNotNull(cookies);
            Assert.assertEquals(1, cookies.length);
            Cookie cookie = cookies[0];
            Assert.assertEquals(name, cookie.getName());
            Assert.assertEquals(value, cookie.getValue());
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    String path = "/path";
    String uri = scheme + "://" + host + ":" + port;
    HttpCookie cookie = new HttpCookie(name, value);
    client.getCookieStore().add(URI.create(uri), cookie);
    Response response = client.GET(scheme + "://" + host + ":" + port + path);
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpCookie(java.net.HttpCookie) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 33 with Response

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

the class HttpResponseAbortTest method testAbortOnContentBeforeRequestTermination.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                OutputStream output = response.getOutputStream();
                output.write(1);
                output.flush();
                output.write(2);
                output.flush();
            } catch (IOException ignored) {
            // The client may have already closed, and we'll get an exception here, but it's expected
            }
        }
    });
    final CountDownLatch abortLatch = new CountDownLatch(1);
    final AtomicInteger completes = new AtomicInteger();
    final CountDownLatch completeLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestSuccess(new org.eclipse.jetty.client.api.Request.SuccessListener() {

        @Override
        public void onSuccess(org.eclipse.jetty.client.api.Request request) {
            try {
                abortLatch.await(5, TimeUnit.SECONDS);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    }).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
            try {
                response.abort(new Exception());
                abortLatch.countDown();
                // Delay to let the request side to finish its processing.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            completes.incrementAndGet();
            Assert.assertTrue(result.isFailed());
            completeLatch.countDown();
        }
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    // Wait to be sure that the complete event is only notified once.
    Thread.sleep(1000);
    Assert.assertEquals(1, completes.get());
}
Also used : OutputStream(java.io.OutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) 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) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 34 with Response

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

the class HttpResponseAbortTest method testAbortOnBegin.

@Test
public void testAbortOnBegin() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseBegin(new Response.BeginListener() {

        @Override
        public void onBegin(Response response) {
            response.abort(new Exception());
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 35 with Response

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

the class HttpResponseAbortTest method testAbortOnHeaders.

@Test
public void testAbortOnHeaders() throws Exception {
    start(new EmptyServerHandler());
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseHeaders(new Response.HeadersListener() {

        @Override
        public void onHeaders(Response response) {
            response.abort(new Exception());
        }
    }).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.client.api.Response)105 Test (org.junit.Test)89 HttpServletResponse (javax.servlet.http.HttpServletResponse)88 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)83 IOException (java.io.IOException)72 HttpServletRequest (javax.servlet.http.HttpServletRequest)69 ServletException (javax.servlet.ServletException)67 CountDownLatch (java.util.concurrent.CountDownLatch)54 Result (org.eclipse.jetty.client.api.Result)51 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)36 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)33 InputStream (java.io.InputStream)32 InterruptedIOException (java.io.InterruptedIOException)29 HttpServlet (javax.servlet.http.HttpServlet)29 Request (org.eclipse.jetty.server.Request)26 Request (org.eclipse.jetty.client.api.Request)25 InputStreamResponseListener (org.eclipse.jetty.client.util.InputStreamResponseListener)24 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)23 Callback (org.eclipse.jetty.util.Callback)22