use of okhttp3.internal.http2.Header 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.internal.http2.Header 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"));
}
use of okhttp3.internal.http2.Header in project okhttp by square.
the class CacheTest method getHeadersReturnsCachedHopByHopHeaders.
@Test
public void getHeadersReturnsCachedHopByHopHeaders() throws Exception {
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: identity").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Response response1 = get(server.url("/"));
assertEquals("A", response1.body().string());
assertEquals("identity", response1.header("Transfer-Encoding"));
Response response2 = get(server.url("/"));
assertEquals("A", response2.body().string());
assertEquals("identity", response2.header("Transfer-Encoding"));
}
use of okhttp3.internal.http2.Header 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.internal.http2.Header 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