use of java.net.URLConnection in project okhttp by square.
the class ResponseCacheTest 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(openConnection(server.url("/").url())));
URLConnection connection = openConnection(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 ResponseCacheTest method otherStacks_cacheMissWithVaryAsterisk.
// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We can't keep the Vary case working, because we can't get to the Vary
// request headers after connect().
@Test
public void otherStacks_cacheMissWithVaryAsterisk() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: *").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
// Set the cache as the shared cache.
ResponseCache.setDefault(cache);
// Use the platform's HTTP stack.
URLConnection connection = server.url("/").url().openConnection();
assertFalse(connection instanceof OkHttpURLConnection);
assertEquals("A", readAscii(connection));
URLConnection connection2 = server.url("/").url().openConnection();
assertFalse(connection2 instanceof OkHttpURLConnection);
assertEquals("B", readAscii(connection2));
}
use of java.net.URLConnection in project okhttp by square.
the class ResponseCacheTest method responseCacheReturnsNullOutputStream.
/** Don't explode if the cache returns a null body. http://b/3373699 */
@Test
public void responseCacheReturnsNullOutputStream() throws Exception {
final AtomicBoolean aborted = new AtomicBoolean();
setInternalCache(new CacheAdapter(new AbstractResponseCache() {
@Override
public CacheRequest put(URI uri, URLConnection connection) {
return new CacheRequest() {
@Override
public void abort() {
aborted.set(true);
}
@Override
public OutputStream getBody() throws IOException {
return null;
}
};
}
}));
server.enqueue(new MockResponse().setBody("abcdef"));
HttpURLConnection connection = openConnection(server.url("/").url());
assertEquals("abc", readAscii(connection, 3));
connection.getInputStream().close();
// The best behavior is ambiguous, but RI 6 doesn't abort here
assertFalse(aborted.get());
}
use of java.net.URLConnection in project okhttp by square.
the class MockWebServerTest method delayResponse.
/** Delay the response body by sleeping 1s. */
@Test
public void delayResponse() throws IOException {
server.enqueue(new MockResponse().setBody("ABCDEF").setBodyDelay(1, SECONDS));
long startNanos = System.nanoTime();
URLConnection connection = server.url("/").url().openConnection();
InputStream in = connection.getInputStream();
assertEquals('A', in.read());
long elapsedNanos = System.nanoTime() - startNanos;
long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000);
in.close();
}
use of java.net.URLConnection in project okhttp by square.
the class MockWebServerTest method redirect.
@Test
public void redirect() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server.url("/new-path")).setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
URLConnection connection = server.url("/").url().openConnection();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
assertEquals("This is the new location!", reader.readLine());
RecordedRequest first = server.takeRequest();
assertEquals("GET / HTTP/1.1", first.getRequestLine());
RecordedRequest redirect = server.takeRequest();
assertEquals("GET /new-path HTTP/1.1", redirect.getRequestLine());
}
Aggregations