Search in sources :

Example 1 with RecordingCookieJar

use of okhttp3.RecordingCookieJar 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 2 with RecordingCookieJar

use of okhttp3.RecordingCookieJar 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 3 with RecordingCookieJar

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

Example 4 with RecordingCookieJar

use of okhttp3.RecordingCookieJar in project okhttp by square.

the class UrlConnectionCacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    urlFactory.setClient(urlFactory.client().newBuilder().cookieJar(cookieJar).build());
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", readAscii(urlFactory.open(url)));
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) Test(org.junit.Test)

Example 5 with RecordingCookieJar

use of okhttp3.RecordingCookieJar in project okhttp by square.

the class CacheTest method cachePlusCookies.

@Test
public void cachePlusCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=FIRST").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=SECOND").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpUrl url = server.url("/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=FIRST; path=/");
    assertEquals("A", get(url).body().string());
    cookieJar.assertResponseCookies("a=SECOND; path=/");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)6 Test (org.junit.Test)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 HttpCookie (java.net.HttpCookie)2 Call (okhttp3.Call)2 RecordingCookieJar (okhttp3.RecordingCookieJar)2 Request (okhttp3.Request)2 Response (okhttp3.Response)2 URL (java.net.URL)1 Cookie (okhttp3.Cookie)1