Search in sources :

Example 1 with CacheRequest

use of java.net.CacheRequest in project android_frameworks_base by ParanoidAndroid.

the class HttpResponseCacheTest method testGetInstalledWithWrongTypeInstalled.

public void testGetInstalledWithWrongTypeInstalled() {
    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)

Example 2 with CacheRequest

use of java.net.CacheRequest in project robovm by robovm.

the class URLConnectionTest method backdoorUrlToUri.

/**
     * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI,
     * HttpURLConnection recovers from URLs with unescaped but unsupported URI
     * characters like '{' and '|' by escaping these characters.
     */
private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<URI>();
    ResponseCache.setDefault(new ResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            return null;
        }

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
            uriReference.set(uri);
            throw new UnsupportedOperationException();
        }
    });
    try {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.getResponseCode();
    } catch (Exception expected) {
    }
    return uriReference.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpRetryException(java.net.HttpRetryException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) 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 3 with CacheRequest

use of java.net.CacheRequest in project okhttp by square.

the class ResponseCacheTest method responseCacheReturnsNullOutputStream.

/** Don't explode if the cache returns a null body. http://b/3373699 */
@Test
public void responseCacheReturnsNullOutputStream() throws Exception {
    final AtomicBoolean aborted = new AtomicBoolean();
    setInternalCache(new CacheAdapter(new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) {
            return new CacheRequest() {

                @Override
                public void abort() {
                    aborted.set(true);
                }

                @Override
                public OutputStream getBody() throws IOException {
                    return null;
                }
            };
        }
    }));
    server.enqueue(new MockResponse().setBody("abcdef"));
    HttpURLConnection connection = openConnection(server.url("/").url());
    assertEquals("abc", readAscii(connection, 3));
    connection.getInputStream().close();
    // The best behavior is ambiguous, but RI 6 doesn't abort here
    assertFalse(aborted.get());
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with CacheRequest

use of java.net.CacheRequest in project enroscar by stanfy.

the class TestUtils method putCachedContent.

static void putCachedContent(final ImagesManager manager, final String url) throws Exception {
    CacheRequest cacheRequest = manager.getImagesResponseCache().put(new URI(url), fakeConnection(new URL(url)));
    OutputStream out = cacheRequest.getBody();
    out.write(new byte[] { 1 });
    out.close();
}
Also used : CacheRequest(java.net.CacheRequest) OutputStream(java.io.OutputStream) URI(java.net.URI) URL(java.net.URL)

Example 5 with CacheRequest

use of java.net.CacheRequest in project j2objc by google.

the class URLConnectionTest method testResponseCacheReturnsNullOutputStream.

// TODO(tball): b/28067294
//    public void testHostWithNul() throws Exception {
//        URL url = new URL("http://host/");
//        try {
//            url.openStream();
//            fail();
//        } catch (UnknownHostException expected) {
//        } catch (IllegalArgumentException expected) {
//        }
//    }
/**
     * 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) IOException(java.io.IOException) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheResponse(java.net.CacheResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) ArrayList(java.util.ArrayList) List(java.util.List) ResponseCache(java.net.ResponseCache)

Aggregations

CacheRequest (java.net.CacheRequest)9 URI (java.net.URI)9 URLConnection (java.net.URLConnection)8 CacheResponse (java.net.CacheResponse)6 ResponseCache (java.net.ResponseCache)6 List (java.util.List)6 HttpURLConnection (java.net.HttpURLConnection)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 OutputStream (java.io.OutputStream)3 HttpRetryException (java.net.HttpRetryException)3 ProtocolException (java.net.ProtocolException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 HttpResponseCache (com.android.okhttp.HttpResponseCache)2 MockResponse (com.google.mockwebserver.MockResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2