Search in sources :

Example 46 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CallTest method requestCookies.

@Test
public void requestCookies() throws Exception {
    server.enqueue(new MockResponse());
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    cookieJar.enqueueRequestCookies(new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build(), new Cookie.Builder().name("c").value("d").domain(server.getHostName()).build());
    client = client.newBuilder().cookieJar(cookieJar).build();
    executeSynchronously("/").assertCode(200);
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("a=b; c=d", recordedRequest.getHeader("Cookie"));
}
Also used : HttpCookie(java.net.HttpCookie) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 47 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CookiesTest method testCookiesSentIgnoresCase.

@Test
public void testCookiesSentIgnoresCase() throws Exception {
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
            Map<String, List<String>> result = new LinkedHashMap<>();
            result.put("COOKIE", Collections.singletonList("Bar=bar"));
            result.put("cooKIE2", Collections.singletonList("Baz=baz"));
            return result;
        }
    })).build();
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    get(server.url("/"));
    RecordedRequest request = server.takeRequest();
    assertEquals("Bar=bar; Baz=baz", request.getHeader("Cookie"));
    assertNull(request.getHeader("Cookie2"));
    assertNull(request.getHeader("Quux"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieManager(java.net.CookieManager) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 48 with Cookie

use of okhttp3.Cookie 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 49 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CookiesTest method cookieHandlerLikeAndroid.

@Test
public void cookieHandlerLikeAndroid() throws Exception {
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    final HttpUrl serverUrl = urlWithIpAddress(server, "/");
    CookieHandler androidCookieHandler = new CookieHandler() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> map) throws IOException {
            return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
        }

        @Override
        public void put(URI uri, Map<String, List<String>> map) throws IOException {
        }
    };
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(androidCookieHandler)).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) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieHandler(java.net.CookieHandler) Test(org.junit.Test)

Example 50 with Cookie

use of okhttp3.Cookie 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)

Aggregations

Response (okhttp3.Response)34 IOException (java.io.IOException)33 Request (okhttp3.Request)33 Call (okhttp3.Call)25 Callback (okhttp3.Callback)22 RequestBody (okhttp3.RequestBody)21 Test (org.junit.Test)18 Cookie (okhttp3.Cookie)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 FormBody (okhttp3.FormBody)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 CookieManager (java.net.CookieManager)9 HttpCookie (java.net.HttpCookie)9 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 OkHttpClient (okhttp3.OkHttpClient)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 HttpUrl (okhttp3.HttpUrl)5 SharedPreferences (android.content.SharedPreferences)4 OnClick (butterknife.OnClick)4