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());
}
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();
}
}
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();
}
}
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());
}
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());
}
Aggregations