Search in sources :

Example 1 with AbstractResponseCache

use of okhttp3.AbstractResponseCache 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) {
    }
}
Also used : ProtocolException(java.net.ProtocolException) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) AbstractResponseCache(okhttp3.AbstractResponseCache) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with AbstractResponseCache

use of okhttp3.AbstractResponseCache in project okhttp by square.

the class CacheAdapterTest method put_httpsGet.

@Test
public void put_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpsURLConnection);
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection;
                HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection;
                assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite());
                assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal());
                assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates());
                assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates());
                assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal());
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 3 with AbstractResponseCache

use of okhttp3.AbstractResponseCache in project okhttp by square.

the class CacheAdapterTest method get_httpGet.

@Test
public void get_httpGet() throws Exception {
    final URL serverUrl = configureServer(new MockResponse());
    assertEquals("http", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 4 with AbstractResponseCache

use of okhttp3.AbstractResponseCache in project okhttp by square.

the class CacheAdapterTest method get_httpsGet.

@Test
public void get_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals("https", uri.getScheme());
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 5 with AbstractResponseCache

use of okhttp3.AbstractResponseCache 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)

Aggregations

URI (java.net.URI)9 AbstractResponseCache (okhttp3.AbstractResponseCache)9 Test (org.junit.Test)9 MockResponse (okhttp3.mockwebserver.MockResponse)8 HttpURLConnection (java.net.HttpURLConnection)7 URL (java.net.URL)7 URLConnection (java.net.URLConnection)6 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)6 IOException (java.io.IOException)5 ResponseCache (java.net.ResponseCache)5 OkUrlFactory (okhttp3.OkUrlFactory)5 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 InputStream (java.io.InputStream)1 CacheRequest (java.net.CacheRequest)1 CacheResponse (java.net.CacheResponse)1 ProtocolException (java.net.ProtocolException)1 SecureCacheResponse (java.net.SecureCacheResponse)1 ArrayList (java.util.ArrayList)1