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