use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method requestOnlyIfCachedWithConditionalResponseCached.
@Test
public void requestOnlyIfCachedWithConditionalResponseCached() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=30").addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
HttpURLConnection connection = openConnection(server.url("/").url());
connection.addRequestProperty("Cache-Control", "only-if-cached");
assertGatewayTimeout(connection);
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method requestMaxStaleDirectiveWithNoValue.
@Test
public void requestMaxStaleDirectiveWithNoValue() throws IOException {
// Add a stale response to the cache.
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120").addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
// With max-stale, we'll return that stale response.
URLConnection maxStaleConnection = openConnection(server.url("/").url());
maxStaleConnection.setRequestProperty("Cache-Control", "max-stale");
assertEquals("A", readAscii(maxStaleConnection));
assertEquals("110 HttpURLConnection \"Response is stale\"", maxStaleConnection.getHeaderField("Warning"));
}
use of okhttp3.mockwebserver.MockResponse 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.MockResponse in project okhttp by square.
the class CacheTest method clientSideNoStore.
@Test
public void clientSideNoStore() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("B"));
Request request1 = new Request.Builder().url(server.url("/")).cacheControl(new CacheControl.Builder().noStore().build()).build();
Response response1 = client.newCall(request1).execute();
assertEquals("A", response1.body().string());
Request request2 = new Request.Builder().url(server.url("/")).build();
Response response2 = client.newCall(request2).execute();
assertEquals("B", response2.body().string());
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method getHeadersReturnsNetworkEndToEndHeaders.
@Test
public void getHeadersReturnsNetworkEndToEndHeaders() throws Exception {
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD, PUT").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Response response1 = get(server.url("/"));
assertEquals("A", response1.body().string());
assertEquals("GET, HEAD", response1.header("Allow"));
Response response2 = get(server.url("/"));
assertEquals("A", response2.body().string());
assertEquals("GET, HEAD, PUT", response2.header("Allow"));
}
Aggregations