Search in sources :

Example 11 with ResponseCache

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

the class HttpResponseCacheTest method openUrl.

// This mimics the Android HttpHandler, which is found in the okhttp3 package.
private URLConnection openUrl(HttpUrl url) {
    ResponseCache responseCache = ResponseCache.getDefault();
    AndroidInternal.setResponseCache(urlFactory, responseCache);
    return urlFactory.open(url.url());
}
Also used : ResponseCache(java.net.ResponseCache)

Example 12 with ResponseCache

use of java.net.ResponseCache 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 13 with ResponseCache

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

the class CacheAdapterTest method put_httpPost.

@Test
public void put_httpPost() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c"));
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpURLConnection);
                assertFalse(connection instanceof HttpsURLConnection);
                assertEquals(0, connection.getContentLength());
                HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
                assertEquals("POST", httpUrlConnection.getRequestMethod());
                assertTrue(httpUrlConnection.getDoInput());
                assertTrue(httpUrlConnection.getDoOutput());
                assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                assertEquals("value", connection.getRequestProperty("key"));
                // Check retrieval by string key.
                assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
                assertEquals("c", httpUrlConnection.getHeaderField("A"));
                // The RI and OkHttp supports case-insensitive matching for this method.
                assertEquals("c", httpUrlConnection.getHeaderField("a"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    executePost(connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 14 with ResponseCache

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

the class CacheAdapterTest method put_httpGet.

@Test
public void put_httpGet() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    final byte[] response = "ResponseString".getBytes(StandardCharsets.UTF_8);
    final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c").setBody(new Buffer().write(response)));
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpURLConnection);
                assertFalse(connection instanceof HttpsURLConnection);
                assertEquals(response.length, connection.getContentLength());
                HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
                assertEquals("GET", httpUrlConnection.getRequestMethod());
                assertTrue(httpUrlConnection.getDoInput());
                assertFalse(httpUrlConnection.getDoOutput());
                assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                assertEquals("value", connection.getRequestProperty("key"));
                // Check retrieval by string key.
                assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
                assertEquals("c", httpUrlConnection.getHeaderField("A"));
                // The RI and OkHttp supports case-insensitive matching for this method.
                assertEquals("c", httpUrlConnection.getHeaderField("a"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key", "value");
    executeGet(connection);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 15 with ResponseCache

use of java.net.ResponseCache 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)

Aggregations

ResponseCache (java.net.ResponseCache)22 URI (java.net.URI)11 URLConnection (java.net.URLConnection)11 IOException (java.io.IOException)9 HttpURLConnection (java.net.HttpURLConnection)7 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)7 CacheRequest (java.net.CacheRequest)6 CacheResponse (java.net.CacheResponse)6 List (java.util.List)6 Test (org.junit.Test)6 AndroidShimResponseCache (com.android.okhttp.AndroidShimResponseCache)5 URL (java.net.URL)5 AbstractResponseCache (okhttp3.AbstractResponseCache)5 OkUrlFactory (okhttp3.OkUrlFactory)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 ArrayList (java.util.ArrayList)4 HttpResponseCache (com.android.okhttp.HttpResponseCache)2 MockResponse (com.google.mockwebserver.MockResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2