Search in sources :

Example 76 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CookiesTest method receiveAndSendMultipleCookies.

@Test
public void receiveAndSendMultipleCookies() throws Exception {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=android").addHeader("Set-Cookie", "b=banana"));
    server.enqueue(new MockResponse());
    server.start();
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    get(urlWithIpAddress(server, "/"));
    RecordedRequest request1 = server.takeRequest();
    assertNull(request1.getHeader("Cookie"));
    get(urlWithIpAddress(server, "/"));
    RecordedRequest request2 = server.takeRequest();
    assertEquals("a=android; b=banana", request2.getHeader("Cookie"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 77 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class CallTest method redirectsDoNotIncludeTooManyCookies.

@Test
public void redirectsDoNotIncludeTooManyCookies() throws Exception {
    server2.enqueue(new MockResponse().setBody("Page 2"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/")));
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookie = new HttpCookie("c", "cookie");
    cookie.setDomain(server.getHostName());
    cookie.setPath("/");
    String portList = Integer.toString(server.getPort());
    cookie.setPortlist(portList);
    cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    Response response = client.newCall(new Request.Builder().url(server.url("/page1")).build()).execute();
    assertEquals("Page 2", response.body().string());
    RecordedRequest request1 = server.takeRequest();
    assertEquals("c=cookie", request1.getHeader("Cookie"));
    RecordedRequest request2 = server2.takeRequest();
    assertNull(request2.getHeader("Cookie"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 78 with CookieManager

use of java.net.CookieManager in project okhttp by square.

the class URLConnectionTest method connectViaHttpsReusingConnections.

private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    // The pool will only reuse sockets if the SSL socket factories are the same.
    SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
    ConnectionPool connectionPool = new ConnectionPool();
    urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    connection = urlFactory.open(server.url("/").url());
    assertContent("this response comes via HTTPS", connection);
    if (rebuildClient) {
        urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    }
    connection = urlFactory.open(server.url("/").url());
    assertContent("another response via HTTPS", connection);
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 79 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

the class CookiesTest method testCookieStoreUriDropsFragment.

public void testCookieStoreUriDropsFragment() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    cookieStore.add(new URI("http://a.com/a/foo#fragment"), new HttpCookie("a", "android"));
    assertEquals(Arrays.asList(new URI("http://a.com")), cookieStore.getURIs());
}
Also used : CookieStore(java.net.CookieStore) URI(java.net.URI) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 80 with CookieManager

use of java.net.CookieManager in project robovm by robovm.

the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.

public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
    MockWebServer redirectTarget = new MockWebServer();
    try {
        // RoboVM note: Modified to call redirectTarget.shutdown() after test finishes.
        redirectTarget.enqueue(new MockResponse().setBody("A"));
        redirectTarget.play();
        MockWebServer redirectSource = new MockWebServer();
        try {
            // RoboVM note: Modified to call redirectSource.shutdown() after test finishes.
            redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTarget.getUrl("/")));
            redirectSource.play();
            CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
            HttpCookie cookie = new HttpCookie("c", "cookie");
            cookie.setDomain(redirectSource.getCookieDomain());
            cookie.setPath("/");
            String portList = Integer.toString(redirectSource.getPort());
            cookie.setPortlist(portList);
            cookieManager.getCookieStore().add(redirectSource.getUrl("/").toURI(), cookie);
            CookieHandler.setDefault(cookieManager);
            get(redirectSource, "/");
            RecordedRequest request = redirectSource.takeRequest();
            assertContains(request.getHeaders(), "Cookie: $Version=\"1\"; " + "c=\"cookie\";$Path=\"/\";$Domain=\"" + redirectSource.getCookieDomain() + "\";$Port=\"" + portList + "\"");
            for (String header : redirectTarget.takeRequest().getHeaders()) {
                if (header.startsWith("Cookie")) {
                    fail(header);
                }
            }
        } finally {
            redirectSource.shutdown();
        }
    } finally {
        redirectTarget.shutdown();
    }
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Aggregations

CookieManager (java.net.CookieManager)94 HttpCookie (java.net.HttpCookie)53 URI (java.net.URI)50 CookieStore (java.net.CookieStore)28 Test (org.junit.Test)16 MockResponse (com.google.mockwebserver.MockResponse)14 MockWebServer (com.google.mockwebserver.MockWebServer)14 List (java.util.List)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 ArrayList (java.util.ArrayList)7 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 RecordedRequest (com.google.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Project (com.kickstarter.models.Project)3 Headers (com.koushikdutta.async.http.Headers)3 OkHttpClient (okhttp3.OkHttpClient)3 RefTag (com.kickstarter.libs.RefTag)2