Search in sources :

Example 16 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method defaultExpirationDateFullyCachedForLessThan24Hours.

@Test
public void defaultExpirationDateFullyCachedForLessThan24Hours() throws Exception {
    //      last modified: 105 seconds ago
    //             served:   5 seconds ago
    //   default lifetime: (105 - 5) / 10 = 10 seconds
    //            expires:  10 seconds from served date = 5 seconds from now
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(-5, TimeUnit.SECONDS)).setBody("A"));
    HttpUrl url = server.url("/");
    Response response1 = get(url);
    assertEquals("A", response1.body().string());
    Response response2 = get(url);
    assertEquals("A", response2.body().string());
    assertNull(response2.header("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 17 with HttpUrl

use of okhttp3.HttpUrl in project okhttp by square.

the class CacheTest method varyAndHttps.

@Test
public void varyAndHttps() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
    HttpUrl url = server.url("/");
    Request request1 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("A", response1.body().string());
    Request request2 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("A", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 18 with HttpUrl

use of okhttp3.HttpUrl 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 19 with HttpUrl

use of okhttp3.HttpUrl 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 20 with HttpUrl

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

Aggregations

HttpUrl (okhttp3.HttpUrl)64 Test (org.junit.Test)51 MockResponse (okhttp3.mockwebserver.MockResponse)49 Request (okhttp3.Request)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)19 Response (okhttp3.Response)15 RequestBody (okhttp3.RequestBody)12 MockWebServer (okhttp3.mockwebserver.MockWebServer)10 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 QueryRequest (zipkin.storage.QueryRequest)5 ResponseCache (java.net.ResponseCache)4 Map (java.util.Map)4 Call (okhttp3.Call)4