Search in sources :

Example 71 with Result

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

the class HttpRequestAbortTest method testAbortConversation.

@Test
public void testAbortConversation() 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);
            if (!"/done".equals(request.getRequestURI()))
                response.sendRedirect("/done");
        }
    });
    // The test may fail to abort the request in this way:
    // T1 aborts the request, which aborts the sender, which shuts down the output;
    // server reads -1 and closes; T2 reads -1 and the receiver fails the response with an EOFException;
    // T1 tries to abort the receiver, but it's already failed.
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    client.getProtocolHandlers().clear();
    client.getProtocolHandlers().put(new RedirectProtocolHandler(client) {

        @Override
        public void onComplete(Result result) {
            // Abort the request after the 3xx response but before issuing the next request
            if (!result.isFailed()) {
                aborted.set(result.getRequest().abort(cause));
                latch.countDown();
            }
            super.onComplete(result);
        }
    });
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/redirect").timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 72 with Result

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

the class HttpResponseAbortTest method testAbortOnContent.

@Test
public void testAbortOnContent() 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 latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onResponseContent(new Response.ContentListener() {

        @Override
        public void onContent(Response response, ByteBuffer content) {
            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 : OutputStream(java.io.OutputStream) 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) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.eclipse.jetty.client.api.Response) Test(org.junit.Test)

Example 73 with Result

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

the class HttpClientAuthenticationTest method test_Authentication_ThrowsException.

@Test
public void test_Authentication_ThrowsException() throws Exception {
    startBasic(new EmptyServerHandler());
    // Request without Authentication would cause a 401,
    // but the client will throw an exception trying to
    // send the credentials to the server.
    final String cause = "thrown_explicitly_by_test";
    client.getAuthenticationStore().addAuthentication(new Authentication() {

        @Override
        public boolean matches(String type, URI uri, String realm) {
            return true;
        }

        @Override
        public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context) {
            throw new RuntimeException(cause);
        }
    });
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/secure").timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            Assert.assertEquals(cause, result.getFailure().getMessage());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Attributes(org.eclipse.jetty.util.Attributes) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) 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) Authentication(org.eclipse.jetty.client.api.Authentication) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Test(org.junit.Test)

Example 74 with Result

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

the class HttpClientChunkedContentTest method test_Server_HeadersPauseTerminal_Client_Response.

@Test
public void test_Server_HeadersPauseTerminal_Client_Response() throws Exception {
    startClient();
    try (ServerSocket server = new ServerSocket()) {
        server.bind(new InetSocketAddress("localhost", 0));
        final AtomicReference<Result> resultRef = new AtomicReference<>();
        final CountDownLatch completeLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {

            @Override
            public void onComplete(Result result) {
                resultRef.set(result);
                completeLatch.countDown();
            }
        });
        try (Socket socket = server.accept()) {
            consumeRequestHeaders(socket);
            OutputStream output = socket.getOutputStream();
            String headers = "" + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n";
            output.write(headers.getBytes(StandardCharsets.UTF_8));
            output.flush();
            Thread.sleep(1000);
            String terminal = "" + "0\r\n" + "\r\n";
            output.write(terminal.getBytes(StandardCharsets.UTF_8));
            output.flush();
            assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
            Result result = resultRef.get();
            assertTrue(result.isSucceeded());
            Response response = result.getResponse();
            Assert.assertEquals(200, response.getStatus());
        }
    }
}
Also used : Response(org.eclipse.jetty.client.api.Response) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Example 75 with Result

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

the class HttpClientRedirectTest method testHttpRedirector.

@Test
public void testHttpRedirector() throws Exception {
    start(new RedirectHandler());
    final HttpRedirector redirector = new HttpRedirector(client);
    org.eclipse.jetty.client.api.Request request1 = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/302/localhost/done").timeout(5, TimeUnit.SECONDS).followRedirects(false);
    ContentResponse response1 = request1.send();
    Assert.assertEquals(303, response1.getStatus());
    Assert.assertTrue(redirector.isRedirect(response1));
    Result result = redirector.redirect(request1, response1);
    org.eclipse.jetty.client.api.Request request2 = result.getRequest();
    Response response2 = result.getResponse();
    Assert.assertEquals(302, response2.getStatus());
    Assert.assertTrue(redirector.isRedirect(response2));
    final CountDownLatch latch = new CountDownLatch(1);
    redirector.redirect(request2, response2, r -> {
        Response response3 = r.getResponse();
        Assert.assertEquals(200, response3.getStatus());
        Assert.assertFalse(redirector.isRedirect(response3));
        latch.countDown();
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

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