Search in sources :

Example 76 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 77 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 78 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 79 with HttpCookie

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

the class HttpConnection method convertCookies.

private StringBuilder convertCookies(List<HttpCookie> cookies, StringBuilder builder) {
    for (int i = 0; i < cookies.size(); ++i) {
        if (builder == null)
            builder = new StringBuilder();
        if (builder.length() > 0)
            builder.append("; ");
        HttpCookie cookie = cookies.get(i);
        builder.append(cookie.getName()).append("=").append(cookie.getValue());
    }
    return builder;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 80 with HttpCookie

use of java.net.HttpCookie in project j2objc by google.

the class CookiesTest method testCookieStoreUriDropsUserInfo.

public void testCookieStoreUriDropsUserInfo() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    cookieStore.add(new URI("http://jesse:secret@a.com/"), new HttpCookie("a", "android"));
    assertEquals(Arrays.asList(new URI("http://a.com")), cookieStore.getURIs());
}
Also used : CookieStore(java.net.CookieStore) URI(java.net.URI) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

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