use of java.net.CacheResponse in project okhttp by square.
the class HttpResponseCacheTest method getInstalledWithWrongTypeInstalled.
@Test
public void getInstalledWithWrongTypeInstalled() {
ResponseCache.setDefault(new ResponseCache() {
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) {
return null;
}
@Override
public CacheRequest put(URI uri, URLConnection connection) {
return null;
}
});
assertNull(HttpResponseCache.getInstalled());
}
use of java.net.CacheResponse in project okhttp by square.
the class JavaApiConverterTest method createJavaCacheResponse_httpPost.
@Test
public void createJavaCacheResponse_httpPost() throws Exception {
Request okRequest = createArbitraryOkRequest().newBuilder().url("http://insecure/request").post(createRequestBody("RequestBody")).build();
ResponseBody responseBody = createResponseBody("ResponseBody");
Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").body(responseBody).build();
CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
assertFalse(javaCacheResponse instanceof SecureCacheResponse);
Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
}
use of java.net.CacheResponse in project okhttp by square.
the class AndroidShimResponseCache method get.
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
Request okRequest = JavaApiConverter.createOkRequest(uri, requestMethod, requestHeaders);
Response okResponse = delegate.internalCache.get(okRequest);
if (okResponse == null) {
return null;
}
return JavaApiConverter.createJavaCacheResponse(okResponse);
}
use of java.net.CacheResponse in project robovm by robovm.
the class URLConnectionTest method testResponseCacheReturnsNullOutputStream.
/**
* Don't explode if the cache returns a null body. http://b/3373699
*/
public void testResponseCacheReturnsNullOutputStream() throws Exception {
final AtomicBoolean aborted = new AtomicBoolean();
ResponseCache.setDefault(new ResponseCache() {
@Override
public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
return null;
}
@Override
public CacheRequest put(URI uri, URLConnection connection) throws IOException {
return new CacheRequest() {
@Override
public void abort() {
aborted.set(true);
}
@Override
public OutputStream getBody() throws IOException {
return null;
}
};
}
});
server.enqueue(new MockResponse().setBody("abcdef"));
server.play();
HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
InputStream in = connection.getInputStream();
assertEquals("abc", readAscii(in, 3));
in.close();
// The best behavior is ambiguous, but RI 6 doesn't abort here
assertFalse(aborted.get());
}
use of java.net.CacheResponse in project XobotOS by xamarin.
the class HttpEngine method initResponseSource.
/**
* Initialize the source for this response. It may be corrected later if the
* request headers forbids network use.
*/
private void initResponseSource() throws IOException {
responseSource = ResponseSource.NETWORK;
if (!policy.getUseCaches() || responseCache == null) {
return;
}
CacheResponse candidate = responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap());
if (candidate == null) {
return;
}
Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
cachedResponseBody = candidate.getBody();
if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
IoUtils.closeQuietly(cachedResponseBody);
return;
}
RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap);
cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
long now = System.currentTimeMillis();
this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
if (responseSource == ResponseSource.CACHE) {
this.cacheResponse = candidate;
setResponse(cachedResponseHeaders, cachedResponseBody);
} else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
this.cacheResponse = candidate;
} else if (responseSource == ResponseSource.NETWORK) {
IoUtils.closeQuietly(cachedResponseBody);
} else {
throw new AssertionError();
}
}
Aggregations