use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method partialRangeResponsesDoNotCorruptCache.
@Test
public void partialRangeResponsesDoNotCorruptCache() throws Exception {
// 1. request a range
// 2. request a full document, expecting a cache miss
server.enqueue(new MockResponse().setBody("AA").setResponseCode(HttpURLConnection.HTTP_PARTIAL).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Content-Range: bytes 1000-1001/2000"));
server.enqueue(new MockResponse().setBody("BB"));
URL url = server.url("/").url();
URLConnection range = urlFactory.open(url);
range.addRequestProperty("Range", "bytes=1000-1001");
assertEquals("AA", readAscii(range));
assertEquals("BB", readAscii(urlFactory.open(url)));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method requestMinFresh.
@Test
public void requestMinFresh() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=60").addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Cache-Control", "min-fresh=120");
assertEquals("B", readAscii(connection));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method requestOnlyIfCachedWithFullResponseCached.
@Test
public void requestOnlyIfCachedWithFullResponseCached() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=30").addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Cache-Control", "only-if-cached");
assertEquals("A", readAscii(connection));
assertEquals(2, cache.requestCount());
assertEquals(1, cache.networkCount());
assertEquals(1, cache.hitCount());
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method useCachesFalseDoesNotReadFromCache.
@Test
public void useCachesFalseDoesNotReadFromCache() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.setUseCaches(false);
assertEquals("B", readAscii(connection));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method authorizationRequestFullyCached.
@Test
public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection = urlFactory.open(url);
connection.addRequestProperty("Authorization", "password");
assertEquals("A", readAscii(connection));
assertEquals("A", readAscii(urlFactory.open(url)));
}
Aggregations