use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method testRfc2109Response.
@Test
public void testRfc2109Response() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "Comment=this cookie is delicious; " + "Domain=" + urlWithIpAddress.host() + "; " + "Max-Age=60; " + "Path=/path; " + "Secure; " + "Version=1"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertThat(cookies.size()).isEqualTo(1);
HttpCookie cookie = cookies.get(0);
assertThat(cookie.getName()).isEqualTo("a");
assertThat(cookie.getValue()).isEqualTo("android");
assertThat(cookie.getCommentURL()).isNull();
assertThat(cookie.getDiscard()).isFalse();
// Converting to a fixed date can cause rounding!
assertThat((double) cookie.getMaxAge()).isCloseTo(60.0, offset(5.0));
assertThat(cookie.getPath()).isEqualTo("/path");
assertThat(cookie.getSecure()).isTrue();
}
use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method acceptOriginalServerDoesNotMatchDifferentServer.
@Test
public void acceptOriginalServerDoesNotMatchDifferentServer() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
HttpUrl url1 = HttpUrl.get("https://api.squareup.com/");
cookieJar.saveFromResponse(url1, asList(Cookie.parse(url1, "a=android; Domain=api.squareup.com")));
HttpUrl url2 = HttpUrl.get("https://www.squareup.com/");
List<Cookie> actualCookies = cookieJar.loadForRequest(url2);
assertThat(actualCookies).isEmpty();
}
use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method testSendingCookiesFromStore.
@Test
public void testSendingCookiesFromStore() throws Exception {
server.enqueue(new MockResponse());
HttpUrl serverUrl = urlWithIpAddress(server, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(serverUrl.host());
cookieA.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(serverUrl.host());
cookieB.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
get(serverUrl);
RecordedRequest request = server.takeRequest();
assertThat(request.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method testNetscapeResponse.
@Test
public void testNetscapeResponse() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; " + "expires=Fri, 31-Dec-9999 23:59:59 GMT; " + "path=/path; " + "domain=" + urlWithIpAddress.host() + "; " + "secure"));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertThat(cookies.size()).isEqualTo(1);
HttpCookie cookie = cookies.get(0);
assertThat(cookie.getName()).isEqualTo("a");
assertThat(cookie.getValue()).isEqualTo("android");
assertThat(cookie.getComment()).isNull();
assertThat(cookie.getCommentURL()).isNull();
assertThat(cookie.getDiscard()).isFalse();
assertThat(cookie.getMaxAge()).isGreaterThan(100000000000L);
assertThat(cookie.getPath()).isEqualTo("/path");
assertThat(cookie.getSecure()).isTrue();
assertThat(cookie.getVersion()).isEqualTo(0);
}
use of java.net.CookieManager in project okhttp by square.
the class CookiesTest method testQuotedAttributeValues.
@Test
public void testQuotedAttributeValues() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
server.enqueue(new MockResponse().addHeader("Set-Cookie: a=\"android\"; " + "Comment=\"this cookie is delicious\"; " + "CommentURL=\"http://google.com/\"; " + "Discard; " + "Domain=" + urlWithIpAddress.host() + "; " + "Max-Age=60; " + "Path=\"/path\"; " + "Port=\"80,443," + server.getPort() + "\"; " + "Secure; " + "Version=\"1\""));
get(urlWithIpAddress);
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertThat(cookies.size()).isEqualTo(1);
HttpCookie cookie = cookies.get(0);
assertThat(cookie.getName()).isEqualTo("a");
assertThat(cookie.getValue()).isEqualTo("android");
// Converting to a fixed date can cause rounding!
assertThat((double) cookie.getMaxAge()).isCloseTo(60.0, offset(1.0));
assertThat(cookie.getPath()).isEqualTo("/path");
assertThat(cookie.getSecure()).isTrue();
}
Aggregations