use of okhttp3.Headers in project okhttp by square.
the class ResponseCacheTest method responseCacheReturnsNullStatusLine.
/**
* Fail if a badly-behaved cache returns a null status line header.
* https://code.google.com/p/android/issues/detail?id=160522
*/
@Test
public void responseCacheReturnsNullStatusLine() throws Exception {
String cachedContentString = "Hello";
final byte[] cachedContent = cachedContentString.getBytes(StandardCharsets.US_ASCII);
setInternalCache(new CacheAdapter(new AbstractResponseCache() {
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
return new CacheResponse() {
@Override
public Map<String, List<String>> getHeaders() throws IOException {
String contentType = "text/plain";
Map<String, List<String>> headers = new LinkedHashMap<>();
headers.put("Content-Length", Arrays.asList(Integer.toString(cachedContent.length)));
headers.put("Content-Type", Arrays.asList(contentType));
headers.put("Expires", Arrays.asList(formatDate(-1, TimeUnit.HOURS)));
headers.put("Cache-Control", Arrays.asList("max-age=60"));
// unusable because OkHttp only caches responses with cacheable response codes.
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream(cachedContent);
}
};
}
}));
HttpURLConnection connection = openConnection(server.url("/").url());
// should be made.
try {
connection.getResponseCode();
fail();
} catch (ProtocolException expected) {
}
}
use of okhttp3.Headers 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.Headers 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.Headers 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.Headers in project okhttp by square.
the class ResponseCacheTest method otherStacks_cacheMissWithVary.
// 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().
@Test
public void otherStacks_cacheMissWithVary() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").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);
connection.setRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection));
URLConnection connection2 = server.url("/").url().openConnection();
assertFalse(connection2 instanceof OkHttpURLConnection);
assertEquals("B", readAscii(connection2));
}
Aggregations