Search in sources :

Example 1 with CookieJar

use of okhttp3.CookieJar 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();
    MockWebServer server = new MockWebServer();
    server.start();
    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();
    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());
    assertTrue(cookie.getMaxAge() > 100000000000L);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
    assertEquals(0, cookie.getVersion());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 2 with CookieJar

use of okhttp3.CookieJar 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();
    MockWebServer server = new MockWebServer();
    server.start();
    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();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    assertEquals(null, cookie.getCommentURL());
    assertEquals(false, cookie.getDiscard());
    // Converting to a fixed date can cause rounding!
    assertEquals(60.0, cookie.getMaxAge(), 1.0);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 3 with CookieJar

use of okhttp3.CookieJar in project okhttp by square.

the class CookiesTest method testSendingCookiesFromStore.

@Test
public void testSendingCookiesFromStore() throws Exception {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    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();
    assertEquals("a=android; b=banana", request.getHeader("Cookie"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 4 with CookieJar

use of okhttp3.CookieJar in project okhttp by square.

the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.

@Test
public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
    MockWebServer redirectTarget = new MockWebServer();
    redirectTarget.enqueue(new MockResponse().setBody("A"));
    redirectTarget.start();
    HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
    MockWebServer redirectSource = new MockWebServer();
    redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTargetUrl));
    redirectSource.start();
    HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookie = new HttpCookie("c", "cookie");
    cookie.setDomain(redirectSourceUrl.host());
    cookie.setPath("/");
    String portList = Integer.toString(redirectSource.getPort());
    cookie.setPortlist(portList);
    cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    get(redirectSourceUrl);
    RecordedRequest request = redirectSource.takeRequest();
    assertEquals("c=cookie", request.getHeader("Cookie"));
    for (String header : redirectTarget.takeRequest().getHeaders().names()) {
        if (header.startsWith("Cookie")) {
            fail(header);
        }
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 5 with CookieJar

use of okhttp3.CookieJar 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();
    MockWebServer server = new MockWebServer();
    server.start();
    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();
    assertEquals(1, cookies.size());
    HttpCookie cookie = cookies.get(0);
    assertEquals("a", cookie.getName());
    assertEquals("android", cookie.getValue());
    // Converting to a fixed date can cause rounding!
    assertEquals(60.0, cookie.getMaxAge(), 1.0);
    assertEquals("/path", cookie.getPath());
    assertEquals(true, cookie.getSecure());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 OkHttpClient (okhttp3.OkHttpClient)14 CookieManager (java.net.CookieManager)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 HttpCookie (java.net.HttpCookie)8 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 Request (okhttp3.Request)6 Response (okhttp3.Response)5 NonNull (android.support.annotation.NonNull)4 IOException (java.io.IOException)4 List (java.util.List)4 CookieHashSet (me.postaddict.instagram.scraper.cookie.CookieHashSet)4 DefaultCookieJar (me.postaddict.instagram.scraper.cookie.DefaultCookieJar)4 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)4 UserAgentInterceptor (me.postaddict.instagram.scraper.interceptor.UserAgentInterceptor)4 Cookie (okhttp3.Cookie)4 JavaNetCookieJar (okhttp3.JavaNetCookieJar)4 ClearableCookieJar (com.franmontiel.persistentcookiejar.ClearableCookieJar)3