Search in sources :

Example 21 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CacheTest method assertConditionallyCached.

/** @return the request with the conditional get headers. */
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
    // scenario 1: condition succeeds
    server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    // scenario 2: condition fails
    server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
    HttpUrl valid = server.url("/valid");
    Response response1 = get(valid);
    assertEquals("A", response1.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response1.code());
    assertEquals("A-OK", response1.message());
    Response response2 = get(valid);
    assertEquals("A", response2.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response2.code());
    assertEquals("A-OK", response2.message());
    HttpUrl invalid = server.url("/invalid");
    Response response3 = get(invalid);
    assertEquals("B", response3.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response3.code());
    assertEquals("B-OK", response3.message());
    Response response4 = get(invalid);
    assertEquals("C", response4.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response4.code());
    assertEquals("C-OK", response4.message());
    // regular get
    server.takeRequest();
    // conditional get
    return server.takeRequest();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse)

Example 22 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.

@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
    String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
    MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
    RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
    assertEquals("v1", request.getHeader("If-None-Match"));
    assertNull(request.getHeader("If-Modified-Since"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 23 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CacheTest method cacheControlNoCacheAndExpirationDateInTheFuture.

@Test
public void cacheControlNoCacheAndExpirationDateInTheFuture() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Cache-Control: no-cache"));
    assertEquals(lastModifiedDate, conditionalRequest.getHeader("If-Modified-Since"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 24 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class OkApacheClientTest method postOverrideContentType.

@Test
public void postOverrideContentType() throws Exception {
    server.enqueue(new MockResponse());
    HttpPost httpPost = new HttpPost();
    httpPost.setURI(server.url("/").url().toURI());
    httpPost.addHeader("Content-Type", "application/xml");
    httpPost.setEntity(new StringEntity("<yo/>"));
    client.execute(httpPost);
    RecordedRequest request = server.takeRequest();
    assertEquals(request.getHeader("Content-Type"), "application/xml");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) Test(org.junit.Test)

Example 25 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class ResponseCacheTest method clientSuppliedIfModifiedSinceWithCachedResult.

@Test
public void clientSuppliedIfModifiedSinceWithCachedResult() throws Exception {
    MockResponse response = new MockResponse().addHeader("ETag: v3").addHeader("Cache-Control: max-age=0");
    String ifModifiedSinceDate = formatDate(-24, TimeUnit.HOURS);
    RecordedRequest request = assertClientSuppliedCondition(response, "If-Modified-Since", ifModifiedSinceDate);
    assertEquals(ifModifiedSinceDate, request.getHeader("If-Modified-Since"));
    assertNull(request.getHeader("If-None-Match"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)206 MockResponse (okhttp3.mockwebserver.MockResponse)188 Test (org.junit.Test)168 HttpURLConnection (java.net.HttpURLConnection)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)14 IOException (java.io.IOException)13 URL (java.net.URL)13 OutputStream (java.io.OutputStream)12 Response (okhttp3.Response)12 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)12 AbstractTest (org.openstack4j.api.AbstractTest)12 Test (org.testng.annotations.Test)12 Call (okhttp3.Call)11 Buffer (okio.Buffer)11 Request (okhttp3.Request)8 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)7 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 ByteString (okio.ByteString)6 CookieManager (java.net.CookieManager)5 URLConnection (java.net.URLConnection)5