Search in sources :

Example 81 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

the class CookiesTest method testCookieStoreNullUris.

public void testCookieStoreNullUris() {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieA.setDomain(".android.com");
    cookieA.setPath("/source");
    HttpCookie cookieB = new HttpCookie("b", "banana");
    cookieA.setDomain("code.google.com");
    cookieA.setPath("/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 82 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

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();
    try {
        // RoboVM note: Modified to call server.shutdown() after test finishes.
        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());
    } finally {
        server.shutdown();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 83 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

the class CookiesTest method testSendingCookiesFromStore.

public void testSendingCookiesFromStore() throws Exception {
    MockWebServer server = new MockWebServer();
    try {
        // RoboVM note: Modified to call server.shutdown() after test finishes.
        server.enqueue(new MockResponse());
        server.play();
        CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
        HttpCookie cookieA = new HttpCookie("a", "android");
        cookieA.setDomain(server.getCookieDomain());
        cookieA.setPath("/");
        cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieA);
        HttpCookie cookieB = new HttpCookie("b", "banana");
        cookieB.setDomain(server.getCookieDomain());
        cookieB.setPath("/");
        cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieB);
        CookieHandler.setDefault(cookieManager);
        get(server, "/");
        RecordedRequest request = server.takeRequest();
        List<String> receivedHeaders = request.getHeaders();
        assertContains(receivedHeaders, "Cookie: $Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"");
    } finally {
        server.shutdown();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 84 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

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)

Example 85 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

the class CookiesTest method testCookieStoreRemoveAll.

public void testCookieStoreRemoveAll() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    cookieStore.add(new URI("http://code.google.com/"), new HttpCookie("a", "android"));
    assertTrue(cookieStore.removeAll());
    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
    assertFalse("Expected removeAll() to return false when the call doesn't mutate the store", // RI6 fails this
    cookieStore.removeAll());
}
Also used : CookieStore(java.net.CookieStore) URI(java.net.URI) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Aggregations

CookieManager (java.net.CookieManager)94 HttpCookie (java.net.HttpCookie)53 URI (java.net.URI)50 CookieStore (java.net.CookieStore)28 Test (org.junit.Test)16 MockResponse (com.google.mockwebserver.MockResponse)14 MockWebServer (com.google.mockwebserver.MockWebServer)14 List (java.util.List)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 ArrayList (java.util.ArrayList)7 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 RecordedRequest (com.google.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Project (com.kickstarter.models.Project)3 Headers (com.koushikdutta.async.http.Headers)3 OkHttpClient (okhttp3.OkHttpClient)3 RefTag (com.kickstarter.libs.RefTag)2