Search in sources :

Example 11 with HttpCookie

use of java.net.HttpCookie 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 12 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class Usage method testCookie.

@Test
public void testCookie() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Set a cookie to be sent in requests that match the cookie's domain
    client.getCookieStore().add(URI.create("http://host:8080/path"), new HttpCookie("name", "value"));
    // Send a request for the cookie's domain
    Response response = client.newRequest("host", 8080).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 13 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class ResponseTest method testAddCookie_JavaNet.

/**
     * Testing behavior documented in Chrome bug
     * https://bugs.chromium.org/p/chromium/issues/detail?id=700618
     */
@Test
public void testAddCookie_JavaNet() throws Exception {
    HttpCookie cookie = new HttpCookie("foo", URLEncoder.encode("bar;baz", UTF_8.toString()));
    cookie.setPath("/secure");
    assertEquals("foo=\"bar%3Bbaz\";$Path=\"/secure\"", cookie.toString());
}
Also used : HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 14 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class CookiesTest method testCookiesAreSentToClient.

@Test
public void testCookiesAreSentToClient() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieDomain = "domain";
    final String cookiePath = "/path";
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            String cookieString = cookieName + "=" + cookieValue + ";Domain=" + cookieDomain + ";Path=" + cookiePath;
            response.getHeaders().put("Set-Cookie", Collections.singletonList(cookieString));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void afterResponse(HandshakeResponse response) {
            Map<String, List<String>> headers = response.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("set-cookie"));
            List<String> values = headers.get("Set-Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            List<HttpCookie> cookies = HttpCookie.parse(values.get(0));
            Assert.assertEquals(1, cookies.size());
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Assert.assertEquals(cookieDomain, cookie.getDomain());
            Assert.assertEquals(cookiePath, cookie.getPath());
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) HandshakeResponse(javax.websocket.HandshakeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Map(java.util.Map) HttpCookie(java.net.HttpCookie) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 15 with HttpCookie

use of java.net.HttpCookie in project jetty.project by eclipse.

the class CookiesTest method testCookiesAreSentToServer.

@Test
public void testCookiesAreSentToServer() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieString = cookieName + "=" + cookieValue;
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            List<HttpCookie> cookies = request.getCookies();
            assertThat("Cookies", cookies, notNullValue());
            assertThat("Cookies", cookies.size(), is(1));
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Map<String, List<String>> headers = request.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("cookie"));
            List<String> values = headers.get("Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            Assert.assertEquals(cookieString, values.get(0));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Cookie", Collections.singletonList(cookieString));
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) HttpCookie(java.net.HttpCookie) Map(java.util.Map) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

HttpCookie (java.net.HttpCookie)148 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)31 Test (org.junit.Test)31 IOException (java.io.IOException)14 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 URL (java.net.URL)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5