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();
}
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"));
}
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"));
}
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");
}
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"));
}
Aggregations