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);
}
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);
}
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());
}
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"));
}
Aggregations