Java Code Examples for java.net.CookieManager#get()

The following examples show how to use java.net.CookieManager#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private static void assertManagerCookiesMatch(CookieManager cookieManager, String url,
    String expectedCookieRequestHeader) throws Exception {

    Map<String, List<String>> cookieHeaders =
            cookieManager.get(new URI(url), EMPTY_COOKIES_MAP);
    if (expectedCookieRequestHeader == null) {
        assertTrue(cookieHeaders.isEmpty());
        return;
    }

    assertEquals(1, cookieHeaders.size());
    List<String> actualCookieHeaderStrings = cookieHeaders.get("Cookie");

    // For simplicity, we concatenate the cookie header strings if there are multiple ones.
    String actualCookieRequestHeader = actualCookieHeaderStrings.get(0);
    for (int i = 1; i < actualCookieHeaderStrings.size(); i++) {
        actualCookieRequestHeader += "; " + actualCookieHeaderStrings.get(i);
    }
    assertEquals(expectedCookieRequestHeader, actualCookieRequestHeader);
}
 
Example 2
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testCookieWithNullPath() throws Exception {
    FakeSingleCookieStore fscs = new FakeSingleCookieStore();
    CookieManager cm = new CookieManager(fscs, CookiePolicy.ACCEPT_ALL);

    HttpCookie cookie = new HttpCookie("foo", "bar");
    cookie.setDomain("http://www.foo.com");
    cookie.setVersion(0);

    fscs.setNextCookie(cookie);

    Map<String, List<String>> cookieHeaders = cm.get(
            new URI("http://www.foo.com/log/me/in"), Collections.EMPTY_MAP);

    List<String> cookies = cookieHeaders.get("Cookie");
    assertEquals("foo=bar", cookies.get(0));
}
 
Example 3
Source File: CookieFilter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void request(HttpRequestImpl r, MultiExchange<?,?> e) throws IOException {
    HttpClientImpl client = e.client();
    Optional<CookieManager> cookieManOpt = client.cookieManager();
    if (cookieManOpt.isPresent()) {
        CookieManager cookieMan = cookieManOpt.get();
        Map<String,List<String>> userheaders = r.getUserHeaders().map();
        Map<String,List<String>> cookies = cookieMan.get(r.uri(), userheaders);

        // add the returned cookies
        HttpHeadersImpl systemHeaders = r.getSystemHeaders();
        if (cookies.isEmpty()) {
            Log.logTrace("Request: no cookie to add for {0}",
                         r.uri());
        } else {
            Log.logTrace("Request: adding cookies for {0}",
                         r.uri());
        }
        for (String hdrname : cookies.keySet()) {
            List<String> vals = cookies.get(hdrname);
            for (String val : vals) {
                systemHeaders.addHeader(hdrname, val);
            }
        }
    } else {
        Log.logTrace("Request: No cookie manager found for {0}",
                     r.uri());
    }
}
 
Example 4
Source File: CookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookiesWithLeadingPeriod() throws Exception {
    CookieManager cm = new CookieManager(createCookieStore(), null);
    Map<String, List<String>> responseHeaders = Collections.singletonMap("Set-Cookie",
            Collections.singletonList("foo=bar"));

    URI uri = new URI("http://chargepoint.com");
    cm.put(uri, responseHeaders);

    Map<String, List<String>> cookies = cm.get(
            new URI("https://webservices.chargepoint.com/backend.php/mobileapi/"),
            responseHeaders);

    List<String> cookieList = cookies.values().iterator().next();
    assertEquals(Collections.singletonList("foo=bar"), cookieList);
}
 
Example 5
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNoCookieHeaderSentIfNoCookiesMatch() throws IOException, URISyntaxException {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    Map<String, List<String>> cookieHeaders = cookieManager.get(
            new URI("http://android.com/foo/bar/"), EMPTY_COOKIES_MAP);
    assertTrue(cookieHeaders.toString(), cookieHeaders.isEmpty()
            || (cookieHeaders.size() == 1 && cookieHeaders.get("Cookie").isEmpty()));
}
 
Example 6
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void checkValidParams4Get(URI uri,
                                         Map<String, List<String>> map) throws IOException {
    CookieManager manager = new CookieManager(createCookieStore(), null);
    try {
        manager.get(uri, map);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }

}
 
Example 7
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieWithNoPeriod() throws Exception {
    CookieManager cm = new CookieManager(createCookieStore(), null);
    Map<String, List<String>> responseHeaders = Collections.singletonMap("Set-Cookie",
            Collections.singletonList("foo=bar"));

    URI uri = new URI("http://localhost");
    cm.put(uri, responseHeaders);

    Map<String, List<String>> cookies = cm.get(
            new URI("https://localhost/log/me/in"),
            responseHeaders);

    List<String> cookieList = cookies.values().iterator().next();
    assertEquals(Collections.singletonList("foo=bar"), cookieList);
}