use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method requestCacheControlNoCache.
@Test
public void requestCacheControlNoCache() throws Exception {
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-120, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
assertEquals("A", readAscii(urlFactory.open(url)));
URLConnection connection = urlFactory.open(url);
connection.setRequestProperty("Cache-Control", "no-cache");
assertEquals("B", readAscii(connection));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method varyMultipleFieldValuesWithNoMatch.
@Test
public void varyMultipleFieldValuesWithNoMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
URLConnection connection1 = urlFactory.open(url);
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
connection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection1));
URLConnection connection2 = urlFactory.open(url);
connection2.addRequestProperty("Accept-Language", "fr-CA");
connection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("B", readAscii(connection2));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method setIfModifiedSince.
/**
* Confirm that {@link URLConnection#setIfModifiedSince} causes an If-Modified-Since header with a
* GMT timestamp.
*
* https://code.google.com/p/android/issues/detail?id=66135
*/
@Test
public void setIfModifiedSince() throws Exception {
server.enqueue(new MockResponse().setBody("A"));
URL url = server.url("/").url();
URLConnection connection = urlFactory.open(url);
connection.setIfModifiedSince(1393666200000L);
assertEquals("A", readAscii(connection));
RecordedRequest request = server.takeRequest();
String ifModifiedSinceHeader = request.getHeader("If-Modified-Since");
assertEquals("Sat, 01 Mar 2014 09:30:00 GMT", ifModifiedSinceHeader);
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method getHeadersReturnsCachedHopByHopHeaders.
@Test
public void getHeadersReturnsCachedHopByHopHeaders() throws Exception {
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: identity").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
URLConnection connection1 = urlFactory.open(server.url("/").url());
assertEquals("A", readAscii(connection1));
assertEquals("identity", connection1.getHeaderField("Transfer-Encoding"));
URLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals("A", readAscii(connection2));
assertEquals("identity", connection2.getHeaderField("Transfer-Encoding"));
}
use of java.net.URLConnection in project okhttp by square.
the class UrlConnectionCacheTest method defaultExpirationDateFullyCachedForLessThan24Hours.
@Test
public void defaultExpirationDateFullyCachedForLessThan24Hours() throws Exception {
// last modified: 105 seconds ago
// served: 5 seconds ago
// default lifetime: (105 - 5) / 10 = 10 seconds
// expires: 10 seconds from served date = 5 seconds from now
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(-5, TimeUnit.SECONDS)).setBody("A"));
URL url = server.url("/").url();
assertEquals("A", readAscii(urlFactory.open(url)));
URLConnection connection = urlFactory.open(url);
assertEquals("A", readAscii(connection));
assertNull(connection.getHeaderField("Warning"));
}
Aggregations