Search in sources :

Example 6 with AbstractResponseCache

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

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

use of okhttp3.AbstractResponseCache in project okhttp by square.

the class ResponseCacheTest method responseCacheCallbackApis.

// Android-added tests.
/**
   * Test that we can interrogate the response when the cache is being populated.
   * http://code.google.com/p/android/issues/detail?id=7787
   */
@Test
public void responseCacheCallbackApis() throws Exception {
    final String body = "ABCDE";
    final AtomicInteger cacheCount = new AtomicInteger();
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 Fantastic").addHeader("Content-Type: text/plain").addHeader("fgh: ijk").setBody(body));
    setInternalCache(new CacheAdapter(new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            assertEquals(server.url("/").url(), uri.toURL());
            assertEquals(200, httpURLConnection.getResponseCode());
            InputStream is = httpURLConnection.getInputStream();
            try {
                is.read();
                fail();
            } catch (UnsupportedOperationException expected) {
            }
            assertEquals("5", connection.getHeaderField("Content-Length"));
            assertEquals("text/plain", connection.getHeaderField("Content-Type"));
            assertEquals("ijk", connection.getHeaderField("fgh"));
            cacheCount.incrementAndGet();
            return null;
        }
    }));
    URL url = server.url("/").url();
    HttpURLConnection connection = openConnection(url);
    assertEquals(body, readAscii(connection));
    assertEquals(1, cacheCount.get());
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 9 with AbstractResponseCache

use of okhttp3.AbstractResponseCache in project okhttp by square.

the class ResponseCacheTest method responseCacheRequestHeaders.

@Test
public void responseCacheRequestHeaders() throws IOException, URISyntaxException {
    server.enqueue(new MockResponse().setBody("ABC"));
    final AtomicReference<Map<String, List<String>>> requestHeadersRef = new AtomicReference<>();
    setInternalCache(new CacheAdapter(new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
            requestHeadersRef.set(requestHeaders);
            return null;
        }
    }));
    URL url = server.url("/").url();
    URLConnection urlConnection = openConnection(url);
    urlConnection.addRequestProperty("A", "android");
    readAscii(urlConnection);
    assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) URI(java.net.URI) URL(java.net.URL) 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