Search in sources :

Example 11 with Cookie

use of okhttp3.Cookie in project keywhiz by square.

the class SessionLogoutResourceIntegrationTest method sendsExpiredCookie.

@Test
public void sendsExpiredCookie() throws Exception {
    Request request = new Request.Builder().post(RequestBody.create(MediaType.parse("text/plain"), "")).url(testUrl("/admin/logout")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.code()).isEqualTo(303);
    List<String> cookies = response.headers(HttpHeaders.SET_COOKIE);
    assertThat(cookies).hasSize(1);
    NewCookie cookie = NewCookie.valueOf(cookies.get(0));
    assertThat(cookie.getName()).isEqualTo("session");
    assertThat(cookie.getValue()).isEqualTo("expired");
    assertThat(cookie.getVersion()).isEqualTo(1);
    assertThat(cookie.getPath()).isEqualTo("/admin");
    assertThat(cookie.isSecure()).isTrue();
    assertThat(cookie.isHttpOnly()).isTrue();
    assertThat(cookie.getExpiry()).isEqualTo(new Date(0));
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test)

Example 12 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class HttpOverHttp2Test method receiveResponseCookies.

@Test
public void receiveResponseCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse().addHeader("set-cookie: a=b"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    cookieJar.assertResponseCookies("a=b; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordingCookieJar(okhttp3.RecordingCookieJar) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 13 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class HttpOverHttp2Test method sendRequestCookies.

@Test
public void sendRequestCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    Cookie requestCookie = new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build();
    cookieJar.enqueueRequestCookies(requestCookie);
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    RecordedRequest request = server.takeRequest();
    assertEquals("a=b", request.getHeader("Cookie"));
}
Also used : Cookie(okhttp3.Cookie) MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordingCookieJar(okhttp3.RecordingCookieJar) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 14 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class URLConnectionTest method disconnectDuringConnect_cookieJar.

@Test
public void disconnectDuringConnect_cookieJar() throws Exception {
    final AtomicReference<HttpURLConnection> connectionHolder = new AtomicReference<>();
    class DisconnectingCookieJar implements CookieJar {

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            connectionHolder.get().disconnect();
            return Collections.emptyList();
        }
    }
    OkHttpClient client = new okhttp3.OkHttpClient.Builder().cookieJar(new DisconnectingCookieJar()).build();
    URL url = server.url("path that should never be accessed").url();
    HttpURLConnection connection = new OkHttpURLConnection(url, client);
    connectionHolder.set(connection);
    try {
        connection.getInputStream();
        fail("Connection should not be established");
    } catch (IOException expected) {
        assertEquals("Canceled", expected.getMessage());
    } finally {
        connection.disconnect();
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with Cookie

use of okhttp3.Cookie in project okhttp by square.

the class CallTest method responseCookies.

@Test
public void responseCookies() throws Exception {
    server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=b; Expires=Thu, 01 Jan 1970 00:00:00 GMT").addHeader("Set-Cookie", "c=d; Expires=Fri, 02 Jan 1970 23:59:59 GMT; path=/bar; secure"));
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    executeSynchronously("/").assertCode(200);
    List<Cookie> responseCookies = cookieJar.takeResponseCookies();
    assertEquals(2, responseCookies.size());
    assertEquals("a=b; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/", responseCookies.get(0).toString());
    assertEquals("c=d; expires=Fri, 02 Jan 1970 23:59:59 GMT; path=/bar; secure", responseCookies.get(1).toString());
}
Also used : HttpCookie(java.net.HttpCookie) MockResponse(okhttp3.mockwebserver.MockResponse) 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