Search in sources :

Example 41 with CookieManager

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();
}
Also used : MockResponse(mockwebserver3.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 42 with CookieManager

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();
}
Also used : HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 43 with CookieManager

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");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 44 with CookieManager

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);
}
Also used : MockResponse(mockwebserver3.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 45 with CookieManager

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();
}
Also used : MockResponse(mockwebserver3.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

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