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 ResponseCacheTest method temporaryRedirectNotCachedWithoutCachingHeader.
private void temporaryRedirectNotCachedWithoutCachingHeader(int responseCode) throws Exception {
server.enqueue(new MockResponse().setResponseCode(responseCode).addHeader("Location", "/a"));
server.enqueue(new MockResponse().setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
URL url = server.url("/").url();
assertEquals("a", readAscii(openConnection(url)));
assertEquals("b", readAscii(openConnection(url)));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method otherStacks_cacheMissWithVaryAcceptEncoding.
// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We can't keep the Vary case working because we can't get to the Vary
// request headers after connect(). Accept-Encoding has special behavior so we test it explicitly.
@Test
public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Encoding").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
// Set the cache as the shared cache.
ResponseCache.setDefault(cache);
// Use the platform's HTTP stack.
URLConnection connection = server.url("/").url().openConnection();
assertFalse(connection instanceof OkHttpURLConnection);
assertEquals("A", readAscii(connection));
URLConnection connection2 = server.url("/").url().openConnection();
assertFalse(connection2 instanceof OkHttpURLConnection);
assertEquals("B", readAscii(connection2));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method varyMatchesUnchangedRequestHeaderField.
@Test
public void varyMatchesUnchangedRequestHeaderField() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
HttpURLConnection frenchConnection1 = openConnection(url);
frenchConnection1.setRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(frenchConnection1));
HttpURLConnection frenchConnection2 = openConnection(url);
frenchConnection2.setRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(frenchConnection2));
}
Aggregations