Search in sources :

Example 71 with Response

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

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

the class HttpClientRedirectTest method test_303.

@Test
public void test_303() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/done").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 73 with Response

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

Example 74 with Response

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

the class HttpClientRedirectTest method test_303_WithConnectionClose_WithBigRequest.

@Test
public void test_303_WithConnectionClose_WithBigRequest() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/done?close=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 75 with Response

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

the class HttpClientRedirectTest method testAbsoluteURIPathWithSpaces.

@Test
public void testAbsoluteURIPathWithSpaces() throws Exception {
    start(new RedirectHandler());
    Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/a+space?decode=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)

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