Search in sources :

Example 46 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpCookieTest method test_CookieWithoutValue.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.addHeader("Set-Cookie", "");
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(client.getCookieStore().getCookies().isEmpty());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 47 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler 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 48 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler 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 49 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientURITest method testPathWithQueryAndParam.

@Test
public void testPathWithQueryAndParam() throws Exception {
    String name1 = "a";
    String value1 = "1";
    String name2 = "b";
    String value2 = "2";
    final String query = name1 + "=" + value1 + "&" + name2 + "=" + value2;
    final String path = "/path";
    String pathQuery = path + "?" + query;
    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);
            Assert.assertEquals(path, request.getRequestURI());
            Assert.assertEquals(query, request.getQueryString());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path + "?" + name1 + "=" + value1).param(name2, value2);
    Assert.assertEquals(path, request.getPath());
    Assert.assertEquals(query, request.getQuery());
    Assert.assertTrue(request.getURI().toString().endsWith(pathQuery));
    Fields params = request.getParams();
    Assert.assertEquals(2, params.getSize());
    Assert.assertEquals(value1, params.get(name1).getValue());
    Assert.assertEquals(value2, params.get(name2).getValue());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Example 50 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientURITest method testPath.

@Test
public void testPath() throws Exception {
    final String path = "/path";
    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);
            Assert.assertEquals(path, request.getRequestURI());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).path(path);
    Assert.assertEquals(path, request.getPath());
    Assert.assertNull(request.getQuery());
    Fields params = request.getParams();
    Assert.assertEquals(0, params.getSize());
    Assert.assertTrue(request.getURI().toString().endsWith(path));
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)219 HttpServletRequest (javax.servlet.http.HttpServletRequest)216 HttpServletResponse (javax.servlet.http.HttpServletResponse)216 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)188 Request (org.eclipse.jetty.server.Request)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24