Search in sources :

Example 96 with CookieManager

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

the class CookiesTest method testResponseWithMultipleCookieHeaderLines.

public void testResponseWithMultipleCookieHeaderLines() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com"), cookieHeaders("a=android", "b=banana"));
    List<HttpCookie> cookies = sortedCopy(cookieStore.cookies);
    assertEquals(2, cookies.size());
    HttpCookie cookieA = cookies.get(0);
    assertEquals("a", cookieA.getName());
    assertEquals("android", cookieA.getValue());
    HttpCookie cookieB = cookies.get(1);
    assertEquals("b", cookieB.getName());
    assertEquals("banana", cookieB.getValue());
}
Also used : URI(java.net.URI) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 97 with CookieManager

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

the class CookiesTest method testCookieStoreGet.

public void testCookieStoreGet() throws Exception {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookiePort1 = createCookie("a1", "android", "a.com", "/path1");
    HttpCookie cookiePort2 = createCookie("a2", "android", "a.com", "/path2");
    HttpCookie secureCookie = createCookie("a3", "android", "a.com", "/path3");
    secureCookie.setSecure(true);
    HttpCookie notSecureCookie = createCookie("a4", "android", "a.com", "/path4");
    HttpCookie bCookie = createCookie("b1", "android", "b.com", "/path5");
    cookieStore.add(new URI("http://a.com:443/path1"), cookiePort1);
    cookieStore.add(new URI("http://a.com:8080/path2"), cookiePort2);
    cookieStore.add(new URI("https://a.com:443/path3"), secureCookie);
    cookieStore.add(new URI("https://a.com:443/path4"), notSecureCookie);
    cookieStore.add(new URI("https://b.com:8080/path5"), bCookie);
    List<HttpCookie> expectedStoreCookies = new ArrayList<>();
    expectedStoreCookies.add(cookiePort1);
    expectedStoreCookies.add(cookiePort2);
    expectedStoreCookies.add(secureCookie);
    expectedStoreCookies.add(notSecureCookie);
    // The default CookieStore implementation on Android is currently responsible for matching
    // the host/domain but not handling other cookie rules: it ignores the scheme (e.g. "secure"
    // checks), port and path.
    // The tests below fail on the RI. It looks like in the RI it is CookieStoreImpl that is
    // enforcing "secure" checks.
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:443/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:8080/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("https://a.com/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com/anypath")));
}
Also used : CookieStore(java.net.CookieStore) ArrayList(java.util.ArrayList) HttpCookie(java.net.HttpCookie) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 98 with CookieManager

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

the class CookiesTest method testCookieStoreGetWithSecure.

// TODO(tball): enable when libcore is updated with latest fixes.
/**
     * Regression test for http://b/25682357 /
     * https://code.google.com/p/android/issues/detail?id=193475
     * CookieStoreImpl.get(URI) not handling ports properly in the absence of an explicit cookie
     * Domain.
     *
    public void testCookieStoreGetWithPort() throws Exception {
        CookieStore cookieStore = new CookieManager().getCookieStore();
        HttpCookie cookie = new HttpCookie("theme", "light");
        // Deliberately not setting the cookie domain or path.
        cookieStore.add(new URI("http://a.com:12345"), cookie);

        // CookieStoreImpl must ignore the port during retrieval when domain is not set.
        assertEquals(1, cookieStore.get(new URI("http://a.com:12345/path1")).size());
        assertEquals(1, cookieStore.get(new URI("http://a.com/path1")).size());
    }
    */
public void testCookieStoreGetWithSecure() throws Exception {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookie = createCookie("theme", "light", "a.com", "/path");
    cookie.setSecure(true);
    cookieStore.add(new URI("https://a.com/path"), cookie);
    // CookieStoreImpl on Android ignores the "Secure" attribute. The RI implements the secure
    // check in CookieStoreImpl. For safety / app compatibility, if this is changed Android
    // should probably implement it in both places.
    assertEquals(1, cookieStore.get(new URI("http://a.com/path")).size());
    assertEquals(1, cookieStore.get(new URI("https://a.com/path")).size());
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 99 with CookieManager

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

the class CookiesTest method testCookieStoreEviction.

public void testCookieStoreEviction() throws Exception {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie themeCookie = createCookie("theme", "light", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), themeCookie);
    HttpCookie sidCookie = createCookie("sid", "mysid", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), sidCookie);
    HttpCookie replacementThemeCookie = createCookie("theme", "dark", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), replacementThemeCookie);
    // toString() is used below to avoid confusion with assertEquals():
    // HttpCookie.equals() is implemented so that it only checks name, path and domain
    // attributes but we also want to check the value.
    assertEquals("[sid=\"mysid\";$Path=\"/\";$Domain=\"a.com\", " + "theme=\"dark\";$Path=\"/\";$Domain=\"a.com\"]", cookieStore.get(new URI("http://a.com/")).toString());
    HttpCookie replacementSidCookie = createCookie("sid", "mynewsid", "A.cOm", "/");
    cookieStore.add(new URI("http://a.com/"), replacementSidCookie);
    assertEquals("[theme=\"dark\";$Path=\"/\";$Domain=\"a.com\", " + "sid=\"mynewsid\";$Path=\"/\";$Domain=\"a.com\"]", cookieStore.get(new URI("http://a.com/")).toString());
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 100 with CookieManager

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

the class CookiesTest method testCookieStoreRemoveRequiresUri.

public void testCookieStoreRemoveRequiresUri() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieStore.add(new URI("http://android.com/source/"), cookieA);
    assertFalse(// RI6 fails this
    "Expected remove() to take the cookie URI into account.", cookieStore.remove(new URI("http://code.google.com/"), cookieA));
    assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) URI(java.net.URI) CookieManager(java.net.CookieManager)

Aggregations

CookieManager (java.net.CookieManager)166 URI (java.net.URI)90 HttpCookie (java.net.HttpCookie)83 CookieStore (java.net.CookieStore)50 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.Test)12 Test (org.junit.jupiter.api.Test)12 RecordedRequest (com.google.mockwebserver.RecordedRequest)8 URL (java.net.URL)8 MockResponse (mockwebserver3.MockResponse)8 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 HttpURLConnection (java.net.HttpURLConnection)6 RecordedRequest (mockwebserver3.RecordedRequest)5 JavaNetCookieJar (okhttp3.JavaNetCookieJar)5