Search in sources :

Example 86 with HttpCookie

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

the class CookieTest method testViaServletUpgradeRequest.

@Test
public void testViaServletUpgradeRequest() throws Exception {
    // Setup client
    HttpCookie cookie = new HttpCookie("hello", "world");
    cookie.setPath("/");
    cookie.setMaxAge(100000);
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setCookies(Collections.singletonList(cookie));
    // Client connects
    CookieTrackingSocket clientSocket = new CookieTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri(), request);
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    // client confirms upgrade and receipt of frame
    String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
    Assert.assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpCookie(java.net.HttpCookie) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 87 with HttpCookie

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

the class ProxyServletTest method testCookiesFromDifferentClientsAreNotMixed.

@Test
public void testCookiesFromDifferentClientsAreNotMixed() throws Exception {
    final String name = "biscuit";
    startServer(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            String value = req.getHeader(name);
            if (value != null) {
                Cookie cookie = new Cookie(name, value);
                cookie.setMaxAge(3600);
                resp.addCookie(cookie);
            } else {
                Cookie[] cookies = req.getCookies();
                Assert.assertEquals(1, cookies.length);
            }
        }
    });
    startProxy();
    startClient();
    String value1 = "1";
    ContentResponse response1 = client.newRequest("localhost", serverConnector.getLocalPort()).header(name, value1).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response1.getStatus());
    Assert.assertTrue(response1.getHeaders().containsKey(PROXIED_HEADER));
    List<HttpCookie> cookies = client.getCookieStore().getCookies();
    Assert.assertEquals(1, cookies.size());
    Assert.assertEquals(name, cookies.get(0).getName());
    Assert.assertEquals(value1, cookies.get(0).getValue());
    HttpClient client2 = prepareClient();
    try {
        String value2 = "2";
        ContentResponse response2 = client2.newRequest("localhost", serverConnector.getLocalPort()).header(name, value2).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(200, response2.getStatus());
        Assert.assertTrue(response2.getHeaders().containsKey(PROXIED_HEADER));
        cookies = client2.getCookieStore().getCookies();
        Assert.assertEquals(1, cookies.size());
        Assert.assertEquals(name, cookies.get(0).getName());
        Assert.assertEquals(value2, cookies.get(0).getValue());
        // Make a third request to be sure the proxy does not mix cookies
        ContentResponse response3 = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(200, response3.getStatus());
        Assert.assertTrue(response3.getHeaders().containsKey(PROXIED_HEADER));
    } finally {
        client2.stop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpCookie(java.net.HttpCookie) Cookie(javax.servlet.http.Cookie) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 88 with HttpCookie

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

the class RequestHeadersTest method testAccessRequestCookies.

@Test
public void testAccessRequestCookies() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    client.setTimeout(1, TimeUnit.SECONDS);
    try {
        client.connect();
        client.addHeader("Cookie: fruit=Pear; type=Anjou\r\n");
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        UpgradeRequest req = echoCreator.getLastRequest();
        Assert.assertThat("Last Request", req, notNullValue());
        List<HttpCookie> cookies = req.getCookies();
        Assert.assertThat("Request cookies", cookies, notNullValue());
        Assert.assertThat("Request cookies.size", cookies.size(), is(2));
        for (HttpCookie cookie : cookies) {
            Assert.assertThat("Cookie name", cookie.getName(), anyOf(is("fruit"), is("type")));
            Assert.assertThat("Cookie value", cookie.getValue(), anyOf(is("Pear"), is("Anjou")));
        }
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) HttpCookie(java.net.HttpCookie) Test(org.junit.Test)

Example 89 with HttpCookie

use of java.net.HttpCookie in project zeppelin by apache.

the class ZeppelinhubClient method setConnectionrequest.

private ClientUpgradeRequest setConnectionrequest(String token) {
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setCookies(Lists.newArrayList(new HttpCookie(ZeppelinHubRepo.TOKEN_HEADER, token)));
    return request;
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) HttpCookie(java.net.HttpCookie)

Example 90 with HttpCookie

use of java.net.HttpCookie in project hadoop by apache.

the class TestWebAppProxyServlet method isResponseCookiePresent.

private boolean isResponseCookiePresent(HttpURLConnection proxyConn, String expectedName, String expectedValue) {
    Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
    List<String> cookiesHeader = headerFields.get("Set-Cookie");
    if (cookiesHeader != null) {
        for (String cookie : cookiesHeader) {
            HttpCookie c = HttpCookie.parse(cookie).get(0);
            if (c.getName().equals(expectedName) && c.getValue().equals(expectedValue)) {
                return true;
            }
        }
    }
    return false;
}
Also used : AccessControlList(org.apache.hadoop.security.authorize.AccessControlList) List(java.util.List) HttpCookie(java.net.HttpCookie)

Aggregations

HttpCookie (java.net.HttpCookie)151 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)32 Test (org.junit.Test)31 IOException (java.io.IOException)16 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 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 ByteString (com.linkedin.data.ByteString)4