use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method varyMultipleFieldValuesWithMatch.
@Test
public void varyMultipleFieldValuesWithMatch() 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 multiConnection1 = openConnection(url);
multiConnection1.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
multiConnection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(multiConnection1));
HttpURLConnection multiConnection2 = openConnection(url);
multiConnection2.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
multiConnection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(multiConnection2));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method assertCached.
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(responseCode).setBody("ABCDE").addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpURLConnection.HTTP_RESET) {
// We forbid bodies for 204 and 205.
mockResponse.setBody("");
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse().setHeader("Cache-Control", "no-store").setBody("FGHIJ"));
}
server.start();
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertEquals(expectedResponseCode, response.code());
// Exhaust the content stream.
response.body().string();
Response cached = cache.get(request);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.body().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
// tearDown() isn't sufficient; this test starts multiple servers
server.shutdown();
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method requestCacheControlNoCache.
@Test
public void requestCacheControlNoCache() throws Exception {
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-120, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
assertEquals("A", get(url).body().string());
Request request = new Request.Builder().url(url).header("Cache-Control", "no-cache").build();
Response response = client.newCall(request).execute();
assertEquals("B", response.body().string());
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method statisticsFullCacheHit.
@Test
public void statisticsFullCacheHit() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
assertEquals("A", get(server.url("/")).body().string());
assertEquals(1, cache.requestCount());
assertEquals(1, cache.networkCount());
assertEquals(0, cache.hitCount());
assertEquals("A", get(server.url("/")).body().string());
assertEquals("A", get(server.url("/")).body().string());
assertEquals(3, cache.requestCount());
assertEquals(1, cache.networkCount());
assertEquals(2, cache.hitCount());
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest 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"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response response1 = client.newCall(request).execute();
assertEquals("A", response1.body().string());
Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response response2 = client.newCall(request1).execute();
assertEquals("A", response2.body().string());
}
Aggregations