use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method requestMaxStale.
@Test
public void requestMaxStale() throws IOException {
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())));
URLConnection connection = openConnection(server.url("/").url());
connection.addRequestProperty("Cache-Control", "max-stale=180");
assertEquals("A", readAscii(connection));
assertEquals("110 HttpURLConnection \"Response is stale\"", connection.getHeaderField("Warning"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method emptyResponseHeaderNameFromCacheIsLenient.
@Test
public void emptyResponseHeaderNameFromCacheIsLenient() throws Exception {
Headers.Builder headers = new Headers.Builder().add("Cache-Control: max-age=120");
Internal.instance.addLenient(headers, ": A");
server.enqueue(new MockResponse().setHeaders(headers.build()).setBody("body"));
HttpURLConnection connection = openConnection(server.url("/").url());
assertEquals("A", connection.getHeaderField(""));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method clientSuppliedConditionWithoutCachedResult.
@Test
public void clientSuppliedConditionWithoutCachedResult() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
HttpURLConnection connection = openConnection(server.url("/").url());
String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
assertEquals("", readAscii(connection));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method varyMultipleFieldsWithNoMatch.
@Test
public void varyMultipleFieldsWithNoMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
HttpURLConnection frenchConnection = openConnection(url);
frenchConnection.setRequestProperty("Accept-Language", "fr-CA");
frenchConnection.setRequestProperty("Accept-Charset", "UTF-8");
frenchConnection.setRequestProperty("Accept-Encoding", "identity");
assertEquals("A", readAscii(frenchConnection));
HttpURLConnection englishConnection = openConnection(url);
englishConnection.setRequestProperty("Accept-Language", "en-CA");
englishConnection.setRequestProperty("Accept-Charset", "UTF-8");
englishConnection.setRequestProperty("Accept-Encoding", "identity");
assertEquals("B", readAscii(englishConnection));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method notModifiedSpecifiesEncoding.
@Test
public void notModifiedSpecifiesEncoding() throws Exception {
server.enqueue(new MockResponse().setBody(gzip("ABCABCABC")).addHeader("Content-Encoding: gzip").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse().setBody("DEFDEFDEF"));
assertEquals("ABCABCABC", readAscii(openConnection(server.url("/").url())));
assertEquals("ABCABCABC", readAscii(openConnection(server.url("/").url())));
assertEquals("DEFDEFDEF", readAscii(openConnection(server.url("/").url())));
}
Aggregations