Search in sources :

Example 16 with CacheResponse

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());
}
Also used : CacheResponse(java.net.CacheResponse) CacheRequest(java.net.CacheRequest) List(java.util.List) ResponseCache(java.net.ResponseCache) URI(java.net.URI) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 17 with CacheResponse

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()));
}
Also used : CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) Request(okhttp3.Request) List(java.util.List) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 18 with CacheResponse

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);
}
Also used : CacheResponse(java.net.CacheResponse) CacheRequest(java.net.CacheRequest)

Example 19 with CacheResponse

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());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) List(java.util.List) ArrayList(java.util.ArrayList) HttpResponseCache(com.android.okhttp.HttpResponseCache) ResponseCache(java.net.ResponseCache)

Example 20 with CacheResponse

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();
    }
}
Also used : CacheResponse(java.net.CacheResponse) List(java.util.List)

Aggregations

CacheResponse (java.net.CacheResponse)20 List (java.util.List)17 URI (java.net.URI)10 SecureCacheResponse (java.net.SecureCacheResponse)9 CacheRequest (java.net.CacheRequest)7 Test (org.junit.Test)7 HttpURLConnection (java.net.HttpURLConnection)6 ResponseCache (java.net.ResponseCache)6 URLConnection (java.net.URLConnection)6 Request (okhttp3.Request)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Response (okhttp3.Response)5 LinkedHashMap (java.util.LinkedHashMap)4 Headers (okhttp3.Headers)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ProtocolException (java.net.ProtocolException)3 Certificate (java.security.cert.Certificate)3 Handshake (okhttp3.Handshake)3 ResponseBody (okhttp3.ResponseBody)3