Search in sources :

Example 21 with CookieManager

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

the class CookiesTest method testNetscapeResponse.

public void testNetscapeResponse() throws Exception {
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    CookieHandler.setDefault(cookieManager);
    MockWebServer server = new MockWebServer();
    server.play();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "expires=Fri, 31-Dec-9999 23:59:59 GMT; " + "path=/path; " + "domain=" + server.getCookieDomain() + "; " + "secure"));
    get(server, "/path/foo");
    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getComment());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    assertEquals(server.getCookieDomain(), cookie.getDomain());
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 22 with CookieManager

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

the class CookiesTest method testCookieStoreNullUris.

public void testCookieStoreNullUris() {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookieA = createCookie("a", "android", ".android.com", "/source");
    HttpCookie cookieB = createCookie("b", "banana", "code.google.com", "/p/android");
    try {
        cookieStore.add(null, cookieA);
    } catch (NullPointerException expected) {
        // the RI crashes even though the cookie does get added to the store; sigh
        expected.printStackTrace();
    }
    assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
    try {
        cookieStore.add(null, cookieB);
    } catch (NullPointerException expected) {
    }
    assertEquals(Arrays.asList(cookieA, cookieB), cookieStore.getCookies());
    try {
        cookieStore.get(null);
        fail();
    } catch (NullPointerException expected) {
    }
    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertTrue(cookieStore.remove(null, cookieA));
    assertEquals(Arrays.asList(cookieB), cookieStore.getCookies());
    assertTrue(cookieStore.removeAll());
    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 23 with CookieManager

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

the class CookiesTest method testPathDefaulting.

public void testPathDefaulting() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com/foo/bar"), cookieHeaders("a=android"));
    assertEquals("/foo/", cookieStore.getCookie("a").getPath());
    cookieManager.put(new URI("http://android.com/"), cookieHeaders("b=banana"));
    assertEquals("/", cookieStore.getCookie("b").getPath());
    cookieManager.put(new URI("http://android.com/foo/"), cookieHeaders("c=carrot"));
    assertEquals("/foo/", cookieStore.getCookie("c").getPath());
}
Also used : URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 24 with CookieManager

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

the class CookiesTest method testCookieManagerGet_schemeChecks.

public void testCookieManagerGet_schemeChecks() throws Exception {
    CookieManager cookieManager = new CookieManager();
    cookieManager.put(new URI("http://a.com/"), cookieHeaders("a1=android"));
    cookieManager.put(new URI("https://a.com/"), cookieHeaders("a2=android"));
    cookieManager.put(new URI("https://a.com/"), cookieHeaders("a3=android; Secure"));
    assertManagerCookiesMatch(cookieManager, "http://a.com/", "a1=android; a2=android");
    assertManagerCookiesMatch(cookieManager, "https://a.com/", "a1=android; a2=android; a3=android");
}
Also used : URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 25 with CookieManager

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

the class CookiesTest method testMatchingDomainsAccepted.

public void testMatchingDomainsAccepted() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://www.android.com/"), cookieHeaders("a=android;domain=.android.com"));
    assertEquals(".android.com", cookieStore.getCookie("a").getDomain());
}
Also used : URI(java.net.URI) CookieManager(java.net.CookieManager)

Aggregations

CookieManager (java.net.CookieManager)162 URI (java.net.URI)89 HttpCookie (java.net.HttpCookie)82 CookieStore (java.net.CookieStore)49 MockResponse (com.google.mockwebserver.MockResponse)20 List (java.util.List)20 ArrayList (java.util.ArrayList)18 MockWebServer (com.google.mockwebserver.MockWebServer)15 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 Test (org.junit.jupiter.api.Test)12 Test (org.junit.Test)11 RecordedRequest (com.google.mockwebserver.RecordedRequest)8 MockResponse (mockwebserver3.MockResponse)8 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 HttpURLConnection (java.net.HttpURLConnection)6 URL (java.net.URL)6 RecordedRequest (mockwebserver3.RecordedRequest)5 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4