mirror of
https://github.com/square/okhttp.git
synced 2025-11-27 18:21:14 +03:00
Drop get and set prefixes in OkHttpClient, OkHttpClient.Builder
This commit is contained in:
@@ -36,7 +36,7 @@ class OkHttp extends SynchronousHttpClient {
|
|||||||
@Override public void prepare(Benchmark benchmark) {
|
@Override public void prepare(Benchmark benchmark) {
|
||||||
super.prepare(benchmark);
|
super.prepare(benchmark);
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setProtocols(benchmark.protocols)
|
.protocols(benchmark.protocols)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
if (benchmark.tls) {
|
if (benchmark.tls) {
|
||||||
@@ -48,8 +48,8 @@ class OkHttp extends SynchronousHttpClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setSslSocketFactory(socketFactory)
|
.sslSocketFactory(socketFactory)
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ class OkHttpAsync implements HttpClient {
|
|||||||
targetBacklog = benchmark.targetBacklog;
|
targetBacklog = benchmark.targetBacklog;
|
||||||
|
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setProtocols(benchmark.protocols)
|
.protocols(benchmark.protocols)
|
||||||
.setDispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel,
|
.dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel,
|
||||||
benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>())))
|
benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>())))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -62,8 +62,8 @@ class OkHttpAsync implements HttpClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(socketFactory)
|
.sslSocketFactory(socketFactory)
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,16 +169,16 @@ public class Main extends HelpOption implements Runnable {
|
|||||||
|
|
||||||
private OkHttpClient createClient() {
|
private OkHttpClient createClient() {
|
||||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
builder.setFollowSslRedirects(followRedirects);
|
builder.followSslRedirects(followRedirects);
|
||||||
if (connectTimeout != DEFAULT_TIMEOUT) {
|
if (connectTimeout != DEFAULT_TIMEOUT) {
|
||||||
builder.setConnectTimeout(connectTimeout, SECONDS);
|
builder.connectTimeout(connectTimeout, SECONDS);
|
||||||
}
|
}
|
||||||
if (readTimeout != DEFAULT_TIMEOUT) {
|
if (readTimeout != DEFAULT_TIMEOUT) {
|
||||||
builder.setReadTimeout(readTimeout, SECONDS);
|
builder.readTimeout(readTimeout, SECONDS);
|
||||||
}
|
}
|
||||||
if (allowInsecure) {
|
if (allowInsecure) {
|
||||||
builder.setSslSocketFactory(createInsecureSslSocketFactory());
|
builder.sslSocketFactory(createInsecureSslSocketFactory());
|
||||||
builder.setHostnameVerifier(createInsecureHostnameVerifier());
|
builder.hostnameVerifier(createInsecureHostnameVerifier());
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
@@ -235,7 +235,7 @@ public class Main extends HelpOption implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void close() {
|
private void close() {
|
||||||
client.getConnectionPool().evictAll(); // Close any persistent connections.
|
client.connectionPool().evictAll(); // Close any persistent connections.
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SSLSocketFactory createInsecureSslSocketFactory() {
|
private static SSLSocketFactory createInsecureSslSocketFactory() {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class AndroidInternal {
|
|||||||
// make the ResponseCache look like an InternalCache, we can unwrap the Cache instead.
|
// make the ResponseCache look like an InternalCache, we can unwrap the Cache instead.
|
||||||
// This means that Cache stats will be correctly updated.
|
// This means that Cache stats will be correctly updated.
|
||||||
OkCacheContainer okCacheContainer = (OkCacheContainer) responseCache;
|
OkCacheContainer okCacheContainer = (OkCacheContainer) responseCache;
|
||||||
builder.setCache(okCacheContainer.getCache());
|
builder.cache(okCacheContainer.getCache());
|
||||||
} else {
|
} else {
|
||||||
builder.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
|
builder.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,16 +48,15 @@ import static org.junit.Assert.fail;
|
|||||||
* A port of Android's android.net.http.HttpResponseCacheTest to JUnit4.
|
* A port of Android's android.net.http.HttpResponseCacheTest to JUnit4.
|
||||||
*/
|
*/
|
||||||
public final class HttpResponseCacheTest {
|
public final class HttpResponseCacheTest {
|
||||||
|
|
||||||
@Rule public TemporaryFolder cacheRule = new TemporaryFolder();
|
@Rule public TemporaryFolder cacheRule = new TemporaryFolder();
|
||||||
@Rule public MockWebServer server = new MockWebServer();
|
@Rule public MockWebServer server = new MockWebServer();
|
||||||
|
|
||||||
private File cacheDir;
|
private File cacheDir;
|
||||||
private OkUrlFactory client;
|
private OkUrlFactory urlFactory;
|
||||||
|
|
||||||
@Before public void setUp() throws Exception {
|
@Before public void setUp() throws Exception {
|
||||||
cacheDir = cacheRule.getRoot();
|
cacheDir = cacheRule.getRoot();
|
||||||
client = new OkUrlFactory(new OkHttpClient.Builder().build());
|
urlFactory = new OkUrlFactory(new OkHttpClient.Builder().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After public void tearDown() throws Exception {
|
@After public void tearDown() throws Exception {
|
||||||
@@ -165,8 +164,8 @@ public final class HttpResponseCacheTest {
|
|||||||
// This mimics the Android HttpHandler, which is found in the okhttp3 package.
|
// This mimics the Android HttpHandler, which is found in the okhttp3 package.
|
||||||
private URLConnection openUrl(HttpUrl url) {
|
private URLConnection openUrl(HttpUrl url) {
|
||||||
ResponseCache responseCache = ResponseCache.getDefault();
|
ResponseCache responseCache = ResponseCache.getDefault();
|
||||||
AndroidInternal.setResponseCache(client, responseCache);
|
AndroidInternal.setResponseCache(urlFactory, responseCache);
|
||||||
return client.open(url.url());
|
return urlFactory.open(url.url());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeCache(HttpResponseCache cache) {
|
private void initializeCache(HttpResponseCache cache) {
|
||||||
|
|||||||
@@ -116,8 +116,8 @@ public class CacheAdapterTest {
|
|||||||
};
|
};
|
||||||
setInternalCache(new CacheAdapter(responseCache));
|
setInternalCache(new CacheAdapter(responseCache));
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
connection = new OkUrlFactory(client).open(serverUrl);
|
connection = new OkUrlFactory(client).open(serverUrl);
|
||||||
@@ -234,8 +234,8 @@ public class CacheAdapterTest {
|
|||||||
};
|
};
|
||||||
setInternalCache(new CacheAdapter(responseCache));
|
setInternalCache(new CacheAdapter(responseCache));
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
connection = new OkUrlFactory(client).open(serverUrl);
|
connection = new OkUrlFactory(client).open(serverUrl);
|
||||||
|
|||||||
@@ -346,8 +346,8 @@ public final class ResponseCacheTest {
|
|||||||
.setBody("DEF"));
|
.setBody("DEF"));
|
||||||
|
|
||||||
urlFactory.setClient(urlFactory.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.url("/").url());
|
HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.url("/").url());
|
||||||
@@ -385,8 +385,8 @@ public final class ResponseCacheTest {
|
|||||||
.addHeader("Location: " + server2.url("/").url()));
|
.addHeader("Location: " + server2.url("/").url()));
|
||||||
|
|
||||||
urlFactory.setClient(urlFactory.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
HttpURLConnection connection1 = openConnection(server.url("/").url());
|
HttpURLConnection connection1 = openConnection(server.url("/").url());
|
||||||
@@ -1457,8 +1457,8 @@ public final class ResponseCacheTest {
|
|||||||
.setBody("B"));
|
.setBody("B"));
|
||||||
|
|
||||||
urlFactory.setClient(urlFactory.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ public final class OkApacheClient implements HttpClient {
|
|||||||
private final HttpParams params = new AbstractHttpParams() {
|
private final HttpParams params = new AbstractHttpParams() {
|
||||||
@Override public Object getParameter(String name) {
|
@Override public Object getParameter(String name) {
|
||||||
if (name.equals(ConnRouteParams.DEFAULT_PROXY)) {
|
if (name.equals(ConnRouteParams.DEFAULT_PROXY)) {
|
||||||
Proxy proxy = client.getProxy();
|
Proxy proxy = client.proxy();
|
||||||
if (proxy == null) {
|
if (proxy == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -119,7 +119,7 @@ public final class OkApacheClient implements HttpClient {
|
|||||||
proxy = new Proxy(HTTP, new InetSocketAddress(host.getHostName(), host.getPort()));
|
proxy = new Proxy(HTTP, new InetSocketAddress(host.getHostName(), host.getPort()));
|
||||||
}
|
}
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setProxy(proxy)
|
.proxy(proxy)
|
||||||
.build();
|
.build();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,8 +86,8 @@ public final class CacheTest {
|
|||||||
server.setProtocolNegotiationEnabled(false);
|
server.setProtocolNegotiationEnabled(false);
|
||||||
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,8 +252,8 @@ public final class CacheTest {
|
|||||||
.setBody("ABC"));
|
.setBody("ABC"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -355,8 +355,8 @@ public final class CacheTest {
|
|||||||
.setBody("DEF"));
|
.setBody("DEF"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response1 = get(server.url("/"));
|
Response response1 = get(server.url("/"));
|
||||||
@@ -396,8 +396,8 @@ public final class CacheTest {
|
|||||||
.addHeader("Location: " + server2.url("/")));
|
.addHeader("Location: " + server2.url("/")));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response1 = get(server.url("/"));
|
Response response1 = get(server.url("/"));
|
||||||
@@ -1009,7 +1009,7 @@ public final class CacheTest {
|
|||||||
|
|
||||||
assertEquals("A", get(server.url("/")).body().string());
|
assertEquals("A", get(server.url("/")).body().string());
|
||||||
assertEquals("A", get(server.url("/")).body().string());
|
assertEquals("A", get(server.url("/")).body().string());
|
||||||
assertEquals(1, client.getConnectionPool().getIdleConnectionCount());
|
assertEquals(1, client.connectionPool().getIdleConnectionCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void expiresDateBeforeModifiedDate() throws Exception {
|
@Test public void expiresDateBeforeModifiedDate() throws Exception {
|
||||||
@@ -1659,8 +1659,8 @@ public final class CacheTest {
|
|||||||
.setBody("B"));
|
.setBody("B"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpUrl url = server.url("/");
|
HttpUrl url = server.url("/");
|
||||||
@@ -1682,7 +1682,7 @@ public final class CacheTest {
|
|||||||
@Test public void cachePlusCookies() throws Exception {
|
@Test public void cachePlusCookies() throws Exception {
|
||||||
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
@@ -1937,7 +1937,7 @@ public final class CacheTest {
|
|||||||
writeFile(cache.getDirectory(), "journal", journalBody);
|
writeFile(cache.getDirectory(), "journal", journalBody);
|
||||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = get(url);
|
Response response = get(url);
|
||||||
@@ -1986,7 +1986,7 @@ public final class CacheTest {
|
|||||||
cache.close();
|
cache.close();
|
||||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = get(url);
|
Response response = get(url);
|
||||||
@@ -2035,7 +2035,7 @@ public final class CacheTest {
|
|||||||
cache.close();
|
cache.close();
|
||||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = get(url);
|
Response response = get(url);
|
||||||
@@ -2071,7 +2071,7 @@ public final class CacheTest {
|
|||||||
cache.close();
|
cache.close();
|
||||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = get(url);
|
Response response = get(url);
|
||||||
@@ -2088,8 +2088,8 @@ public final class CacheTest {
|
|||||||
|
|
||||||
HttpUrl url = server.url("/");
|
HttpUrl url = server.url("/");
|
||||||
assertEquals("A", get(url).body().string());
|
assertEquals("A", get(url).body().string());
|
||||||
client.getCache().evictAll();
|
client.cache().evictAll();
|
||||||
assertEquals(0, client.getCache().getSize());
|
assertEquals(0, client.cache().getSize());
|
||||||
assertEquals("B", get(url).body().string());
|
assertEquals("B", get(url).body().string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -382,7 +382,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
String credential = Credentials.basic("jesse", "secret");
|
String credential = Credentials.basic("jesse", "secret");
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setAuthenticator(new RecordingOkAuthenticator(credential))
|
.authenticator(new RecordingOkAuthenticator(credential))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
@@ -407,7 +407,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
String credential = Credentials.basic("jesse", "secret");
|
String credential = Credentials.basic("jesse", "secret");
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setAuthenticator(new RecordingOkAuthenticator(credential))
|
.authenticator(new RecordingOkAuthenticator(credential))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -423,7 +423,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
String credential = Credentials.basic("jesse", "secret");
|
String credential = Credentials.basic("jesse", "secret");
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setAuthenticator(new RecordingOkAuthenticator(credential))
|
.authenticator(new RecordingOkAuthenticator(credential))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -740,13 +740,13 @@ public final class CallTest {
|
|||||||
|
|
||||||
// First request: time out after 1000ms.
|
// First request: time out after 1000ms.
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setReadTimeout(1000, TimeUnit.MILLISECONDS)
|
.readTimeout(1000, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
executeSynchronously(new Request.Builder().url(server.url("/a")).build()).assertBody("abc");
|
executeSynchronously(new Request.Builder().url(server.url("/a")).build()).assertBody("abc");
|
||||||
|
|
||||||
// Second request: time out after 250ms.
|
// Second request: time out after 250ms.
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setReadTimeout(250, TimeUnit.MILLISECONDS)
|
.readTimeout(250, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
Request request = new Request.Builder().url(server.url("/b")).build();
|
Request request = new Request.Builder().url(server.url("/b")).build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
@@ -777,7 +777,7 @@ public final class CallTest {
|
|||||||
.setBody("unreachable!"));
|
.setBody("unreachable!"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setReadTimeout(100, TimeUnit.MILLISECONDS)
|
.readTimeout(100, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -804,8 +804,8 @@ public final class CallTest {
|
|||||||
.setBody("success!"));
|
.setBody("success!"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setProxySelector(proxySelector)
|
.proxySelector(proxySelector)
|
||||||
.setReadTimeout(100, TimeUnit.MILLISECONDS)
|
.readTimeout(100, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url("http://android.com/").build();
|
Request request = new Request.Builder().url("http://android.com/").build();
|
||||||
@@ -927,9 +927,9 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("retry success"));
|
server.enqueue(new MockResponse().setBody("retry success"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDns(new DoubleInetAddressDns())
|
.dns(new DoubleInetAddressDns())
|
||||||
.build();
|
.build();
|
||||||
assertTrue(client.getRetryOnConnectionFailure());
|
assertTrue(client.retryOnConnectionFailure());
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
executeSynchronously(request).assertBody("seed connection pool");
|
executeSynchronously(request).assertBody("seed connection pool");
|
||||||
@@ -942,8 +942,8 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("unreachable!"));
|
server.enqueue(new MockResponse().setBody("unreachable!"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDns(new DoubleInetAddressDns())
|
.dns(new DoubleInetAddressDns())
|
||||||
.setRetryOnConnectionFailure(false)
|
.retryOnConnectionFailure(false)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -962,9 +962,9 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("abc"));
|
server.enqueue(new MockResponse().setBody("abc"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.setDns(new SingleInetAddressDns())
|
.dns(new SingleInetAddressDns())
|
||||||
.setSslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
.sslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
executeSynchronously(new Request.Builder().url(server.url("/")).build())
|
executeSynchronously(new Request.Builder().url(server.url("/")).build())
|
||||||
@@ -986,9 +986,9 @@ public final class CallTest {
|
|||||||
RecordingSSLSocketFactory clientSocketFactory =
|
RecordingSSLSocketFactory clientSocketFactory =
|
||||||
new RecordingSSLSocketFactory(sslContext.getSocketFactory());
|
new RecordingSSLSocketFactory(sslContext.getSocketFactory());
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(clientSocketFactory)
|
.sslSocketFactory(clientSocketFactory)
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.setDns(new SingleInetAddressDns())
|
.dns(new SingleInetAddressDns())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -1011,8 +1011,8 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("abc"));
|
server.enqueue(new MockResponse().setBody("abc"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.setSslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
.sslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -1025,10 +1025,10 @@ public final class CallTest {
|
|||||||
|
|
||||||
@Test public void noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled() throws Exception {
|
@Test public void noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled() throws Exception {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
|
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.setDns(new SingleInetAddressDns())
|
.dns(new SingleInetAddressDns())
|
||||||
.setSslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
.sslSocketFactory(suppressTlsFallbackClientSocketFactory())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
@@ -1048,7 +1048,7 @@ public final class CallTest {
|
|||||||
@Test public void cleartextCallsFailWhenCleartextIsDisabled() throws Exception {
|
@Test public void cleartextCallsFailWhenCleartextIsDisabled() throws Exception {
|
||||||
// Configure the client with only TLS configurations. No cleartext!
|
// Configure the client with only TLS configurations. No cleartext!
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
|
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
@@ -1069,7 +1069,7 @@ public final class CallTest {
|
|||||||
.addHeader("Location: http://square.com"));
|
.addHeader("Location: http://square.com"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setFollowSslRedirects(false)
|
.followSslRedirects(false)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -1094,7 +1094,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
// Make another request with certificate pinning. It should complete normally.
|
// Make another request with certificate pinning. It should complete normally.
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCertificatePinner(certificatePinnerBuilder.build())
|
.certificatePinner(certificatePinnerBuilder.build())
|
||||||
.build();
|
.build();
|
||||||
Request request2 = new Request.Builder().url(server.url("/")).build();
|
Request request2 = new Request.Builder().url(server.url("/")).build();
|
||||||
Response response2 = client.newCall(request2).execute();
|
Response response2 = client.newCall(request2).execute();
|
||||||
@@ -1108,7 +1108,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
// Pin publicobject.com's cert.
|
// Pin publicobject.com's cert.
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCertificatePinner(new CertificatePinner.Builder()
|
.certificatePinner(new CertificatePinner.Builder()
|
||||||
.add(server.getHostName(), "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
|
.add(server.getHostName(), "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
@@ -1179,7 +1179,7 @@ public final class CallTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Store a response in the cache.
|
// Store a response in the cache.
|
||||||
@@ -1233,7 +1233,7 @@ public final class CallTest {
|
|||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Store a response in the cache.
|
// Store a response in the cache.
|
||||||
@@ -1293,7 +1293,7 @@ public final class CallTest {
|
|||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request1 = new Request.Builder()
|
Request request1 = new Request.Builder()
|
||||||
@@ -1322,7 +1322,7 @@ public final class CallTest {
|
|||||||
.setBody("B"));
|
.setBody("B"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request cacheStoreRequest = new Request.Builder()
|
Request cacheStoreRequest = new Request.Builder()
|
||||||
@@ -1369,7 +1369,7 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request1 = new Request.Builder()
|
Request request1 = new Request.Builder()
|
||||||
@@ -1477,7 +1477,7 @@ public final class CallTest {
|
|||||||
|
|
||||||
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -1503,7 +1503,7 @@ public final class CallTest {
|
|||||||
new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build(),
|
new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build(),
|
||||||
new Cookie.Builder().name("c").value("d").domain(server.getHostName()).build());
|
new Cookie.Builder().name("c").value("d").domain(server.getHostName()).build());
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -1530,7 +1530,7 @@ public final class CallTest {
|
|||||||
cookie.setPortlist(portList);
|
cookie.setPortlist(portList);
|
||||||
cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
|
cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = client.newCall(new Request.Builder()
|
Response response = client.newCall(new Request.Builder()
|
||||||
@@ -1554,7 +1554,7 @@ public final class CallTest {
|
|||||||
.addHeader("Location: " + server2.url("/b")));
|
.addHeader("Location: " + server2.url("/b")));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setAuthenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret")))
|
.authenticator(new RecordingOkAuthenticator(Credentials.basic("jesse", "secret")))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/a")).build();
|
Request request = new Request.Builder().url(server.url("/a")).build();
|
||||||
@@ -1802,7 +1802,7 @@ public final class CallTest {
|
|||||||
* I/O takes place.
|
* I/O takes place.
|
||||||
*/
|
*/
|
||||||
@Test public void canceledBeforeIOSignalsOnFailure() throws Exception {
|
@Test public void canceledBeforeIOSignalsOnFailure() throws Exception {
|
||||||
client.getDispatcher().setMaxRequests(1); // Force requests to be executed serially.
|
client.dispatcher().setMaxRequests(1); // Force requests to be executed serially.
|
||||||
server.setDispatcher(new Dispatcher() {
|
server.setDispatcher(new Dispatcher() {
|
||||||
char nextResponse = 'A';
|
char nextResponse = 'A';
|
||||||
|
|
||||||
@@ -1982,7 +1982,7 @@ public final class CallTest {
|
|||||||
.setBody(gzip("abcabcabc"))
|
.setBody(gzip("abcabcabc"))
|
||||||
.addHeader("Content-Encoding: gzip"));
|
.addHeader("Content-Encoding: gzip"));
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setAuthenticator(new RecordingOkAuthenticator("password"))
|
.authenticator(new RecordingOkAuthenticator("password"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -2047,7 +2047,7 @@ public final class CallTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setFollowRedirects(false)
|
.followRedirects(false)
|
||||||
.build();
|
.build();
|
||||||
RecordedResponse recordedResponse = executeSynchronously(
|
RecordedResponse recordedResponse = executeSynchronously(
|
||||||
new Request.Builder().url(server.url("/a")).build());
|
new Request.Builder().url(server.url("/a")).build());
|
||||||
@@ -2112,7 +2112,7 @@ public final class CallTest {
|
|||||||
FakeDns dns = new FakeDns();
|
FakeDns dns = new FakeDns();
|
||||||
dns.addresses(Dns.SYSTEM.lookup(server.url("/").host()));
|
dns.addresses(Dns.SYSTEM.lookup(server.url("/").host()));
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDns(dns)
|
.dns(dns)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
@@ -2164,9 +2164,9 @@ public final class CallTest {
|
|||||||
|
|
||||||
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setProxy(server.toProxyAddress())
|
.proxy(server.toProxyAddress())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -2203,10 +2203,10 @@ public final class CallTest {
|
|||||||
.setBody("response body"));
|
.setBody("response body"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setProxy(server.toProxyAddress())
|
.proxy(server.toProxyAddress())
|
||||||
.setProxyAuthenticator(new RecordingOkAuthenticator("password"))
|
.proxyAuthenticator(new RecordingOkAuthenticator("password"))
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -2242,9 +2242,9 @@ public final class CallTest {
|
|||||||
.setBody("response body"));
|
.setBody("response body"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setProxy(server.toProxyAddress())
|
.proxy(server.toProxyAddress())
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -2276,7 +2276,7 @@ public final class CallTest {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
OkHttpClient nonRetryingClient = client.newBuilder()
|
OkHttpClient nonRetryingClient = client.newBuilder()
|
||||||
.setRetryOnConnectionFailure(false)
|
.retryOnConnectionFailure(false)
|
||||||
.build();
|
.build();
|
||||||
Call call = nonRetryingClient.newCall(new Request.Builder()
|
Call call = nonRetryingClient.newCall(new Request.Builder()
|
||||||
.url(server.url("/"))
|
.url(server.url("/"))
|
||||||
@@ -2302,15 +2302,15 @@ public final class CallTest {
|
|||||||
private void enableProtocol(Protocol protocol) {
|
private void enableProtocol(Protocol protocol) {
|
||||||
enableTls();
|
enableTls();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setProtocols(Arrays.asList(protocol, Protocol.HTTP_1_1))
|
.protocols(Arrays.asList(protocol, Protocol.HTTP_1_1))
|
||||||
.build();
|
.build();
|
||||||
server.setProtocols(client.getProtocols());
|
server.setProtocols(client.protocols());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableTls() {
|
private void enableTls() {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.build();
|
.build();
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public final class ConnectionReuseTest {
|
|||||||
|
|
||||||
@Test public void connectionsAreNotReusedIfPoolIsSizeZero() throws Exception {
|
@Test public void connectionsAreNotReusedIfPoolIsSizeZero() throws Exception {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
.connectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
||||||
.build();
|
.build();
|
||||||
server.enqueue(new MockResponse().setBody("a"));
|
server.enqueue(new MockResponse().setBody("a"));
|
||||||
server.enqueue(new MockResponse().setBody("b"));
|
server.enqueue(new MockResponse().setBody("b"));
|
||||||
@@ -116,7 +116,7 @@ public final class ConnectionReuseTest {
|
|||||||
|
|
||||||
@Test public void connectionsReusedWithRedirectEvenIfPoolIsSizeZero() throws Exception {
|
@Test public void connectionsReusedWithRedirectEvenIfPoolIsSizeZero() throws Exception {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
.connectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
||||||
.build();
|
.build();
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setResponseCode(301)
|
.setResponseCode(301)
|
||||||
@@ -135,7 +135,7 @@ public final class ConnectionReuseTest {
|
|||||||
|
|
||||||
@Test public void connectionsNotReusedWithRedirectIfDiscardingResponseIsSlow() throws Exception {
|
@Test public void connectionsNotReusedWithRedirectIfDiscardingResponseIsSlow() throws Exception {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
.connectionPool(new ConnectionPool(0, 5, TimeUnit.SECONDS))
|
||||||
.build();
|
.build();
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setResponseCode(301)
|
.setResponseCode(301)
|
||||||
@@ -214,7 +214,7 @@ public final class ConnectionReuseTest {
|
|||||||
server.enqueue(new MockResponse().setBody("b"));
|
server.enqueue(new MockResponse().setBody("b"));
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectionPool(new ConnectionPool(5, 250, TimeUnit.MILLISECONDS))
|
.connectionPool(new ConnectionPool(5, 250, TimeUnit.MILLISECONDS))
|
||||||
.build();
|
.build();
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(server.url("/"))
|
.url(server.url("/"))
|
||||||
@@ -235,12 +235,12 @@ public final class ConnectionReuseTest {
|
|||||||
|
|
||||||
private void enableHttp2() {
|
private void enableHttp2() {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.setProtocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
.protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
||||||
.build();
|
.build();
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
server.setProtocols(client.getProtocols());
|
server.setProtocols(client.protocols());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertConnectionReused(Request... requests) throws Exception {
|
private void assertConnectionReused(Request... requests) throws Exception {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public final class DispatcherTest {
|
|||||||
RecordingCallback callback = new RecordingCallback();
|
RecordingCallback callback = new RecordingCallback();
|
||||||
Dispatcher dispatcher = new Dispatcher(executor);
|
Dispatcher dispatcher = new Dispatcher(executor);
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setDispatcher(dispatcher)
|
.dispatcher(dispatcher)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@Before public void setUp() throws Exception {
|
@Before public void setUp() throws Exception {
|
||||||
|
|||||||
@@ -560,7 +560,7 @@ public final class InterceptorTest {
|
|||||||
|
|
||||||
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDispatcher(new Dispatcher(executor))
|
.dispatcher(new Dispatcher(executor))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -586,7 +586,7 @@ public final class InterceptorTest {
|
|||||||
|
|
||||||
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDispatcher(new Dispatcher(executor))
|
.dispatcher(new Dispatcher(executor))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
@@ -616,7 +616,7 @@ public final class InterceptorTest {
|
|||||||
|
|
||||||
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setDispatcher(new Dispatcher(executor))
|
.dispatcher(new Dispatcher(executor))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
|
|||||||
@@ -44,35 +44,35 @@ public final class OkHttpClientTest {
|
|||||||
|
|
||||||
@Test public void timeoutDefaults() {
|
@Test public void timeoutDefaults() {
|
||||||
OkHttpClient client = defaultClient();
|
OkHttpClient client = defaultClient();
|
||||||
assertEquals(10_000, client.getConnectTimeout());
|
assertEquals(10_000, client.connectTimeoutMillis());
|
||||||
assertEquals(10_000, client.getReadTimeout());
|
assertEquals(10_000, client.readTimeoutMillis());
|
||||||
assertEquals(10_000, client.getWriteTimeout());
|
assertEquals(10_000, client.writeTimeoutMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void timeoutValidRange() {
|
@Test public void timeoutValidRange() {
|
||||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
try {
|
try {
|
||||||
builder.setConnectTimeout(1, TimeUnit.NANOSECONDS);
|
builder.connectTimeout(1, TimeUnit.NANOSECONDS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
builder.setWriteTimeout(1, TimeUnit.NANOSECONDS);
|
builder.writeTimeout(1, TimeUnit.NANOSECONDS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
builder.setReadTimeout(1, TimeUnit.NANOSECONDS);
|
builder.readTimeout(1, TimeUnit.NANOSECONDS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
builder.setConnectTimeout(365, TimeUnit.DAYS);
|
builder.connectTimeout(365, TimeUnit.DAYS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
builder.setWriteTimeout(365, TimeUnit.DAYS);
|
builder.writeTimeout(365, TimeUnit.DAYS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
builder.setReadTimeout(365, TimeUnit.DAYS);
|
builder.readTimeout(365, TimeUnit.DAYS);
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,21 +101,21 @@ public final class OkHttpClientTest {
|
|||||||
|
|
||||||
// Values should be non-null.
|
// Values should be non-null.
|
||||||
OkHttpClient a = client.newBuilder().build();
|
OkHttpClient a = client.newBuilder().build();
|
||||||
assertNotNull(a.getDispatcher());
|
assertNotNull(a.dispatcher());
|
||||||
assertNotNull(a.getConnectionPool());
|
assertNotNull(a.connectionPool());
|
||||||
assertNotNull(a.getSslSocketFactory());
|
assertNotNull(a.sslSocketFactory());
|
||||||
|
|
||||||
// Multiple clients share the instances.
|
// Multiple clients share the instances.
|
||||||
OkHttpClient b = client.newBuilder().build();
|
OkHttpClient b = client.newBuilder().build();
|
||||||
assertSame(a.getDispatcher(), b.getDispatcher());
|
assertSame(a.dispatcher(), b.dispatcher());
|
||||||
assertSame(a.getConnectionPool(), b.getConnectionPool());
|
assertSame(a.connectionPool(), b.connectionPool());
|
||||||
assertSame(a.getSslSocketFactory(), b.getSslSocketFactory());
|
assertSame(a.sslSocketFactory(), b.sslSocketFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void setProtocolsRejectsHttp10() throws Exception {
|
@Test public void setProtocolsRejectsHttp10() throws Exception {
|
||||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
try {
|
try {
|
||||||
builder.setProtocols(Arrays.asList(Protocol.HTTP_1_0, Protocol.HTTP_1_1));
|
builder.protocols(Arrays.asList(Protocol.HTTP_1_0, Protocol.HTTP_1_1));
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public final class SocksProxyTest {
|
|||||||
server.enqueue(new MockResponse().setBody("def"));
|
server.enqueue(new MockResponse().setBody("def"));
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setProxy(socksProxy.proxy())
|
.proxy(socksProxy.proxy())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request1 = new Request.Builder().url(server.url("/")).build();
|
Request request1 = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -78,7 +78,7 @@ public final class SocksProxyTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setProxySelector(proxySelector)
|
.proxySelector(proxySelector)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder().url(server.url("/")).build();
|
Request request = new Request.Builder().url(server.url("/")).build();
|
||||||
@@ -93,7 +93,7 @@ public final class SocksProxyTest {
|
|||||||
server.enqueue(new MockResponse().setBody("abc"));
|
server.enqueue(new MockResponse().setBody("abc"));
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setProxy(socksProxy.proxy())
|
.proxy(socksProxy.proxy())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpUrl url = server.url("/")
|
HttpUrl url = server.url("/")
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ public final class TestUtil {
|
|||||||
*/
|
*/
|
||||||
public static OkHttpClient defaultClient() {
|
public static OkHttpClient defaultClient() {
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.setConnectionPool(connectionPool)
|
.connectionPool(connectionPool)
|
||||||
.setDns(new SingleInetAddressDns()) // Prevent unexpected fallback addresses.
|
.dns(new SingleInetAddressDns()) // Prevent unexpected fallback addresses.
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -41,7 +41,7 @@ public class HttpOverHttp2Test extends HttpOverSpdyTest {
|
|||||||
.withPush(pushPromise);
|
.withPush(pushPromise);
|
||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||||
assertEquals(200, connection.getResponseCode());
|
assertEquals(200, connection.getResponseCode());
|
||||||
assertEquals("Sweet", connection.getResponseMessage());
|
assertEquals("Sweet", connection.getResponseMessage());
|
||||||
@@ -65,7 +65,7 @@ public class HttpOverHttp2Test extends HttpOverSpdyTest {
|
|||||||
.withPush(pushPromise);
|
.withPush(pushPromise);
|
||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||||
assertEquals(200, connection.getResponseCode());
|
assertEquals(200, connection.getResponseCode());
|
||||||
assertEquals("Sweet", connection.getResponseMessage());
|
assertEquals("Sweet", connection.getResponseMessage());
|
||||||
@@ -90,18 +90,18 @@ public class HttpOverHttp2Test extends HttpOverSpdyTest {
|
|||||||
|
|
||||||
// Read & write a full request to confirm settings are accepted.
|
// Read & write a full request to confirm settings are accepted.
|
||||||
server.enqueue(new MockResponse().withSettings(settings));
|
server.enqueue(new MockResponse().withSettings(settings));
|
||||||
HttpURLConnection settingsConnection = client.open(server.url("/").url());
|
HttpURLConnection settingsConnection = urlFactory.open(server.url("/").url());
|
||||||
assertContent("", settingsConnection, Integer.MAX_VALUE);
|
assertContent("", settingsConnection, Integer.MAX_VALUE);
|
||||||
|
|
||||||
server.enqueue(new MockResponse().setBody("ABC"));
|
server.enqueue(new MockResponse().setBody("ABC"));
|
||||||
server.enqueue(new MockResponse().setBody("DEF"));
|
server.enqueue(new MockResponse().setBody("DEF"));
|
||||||
server.enqueue(new MockResponse().setBody("GHI"));
|
server.enqueue(new MockResponse().setBody("GHI"));
|
||||||
|
|
||||||
HttpURLConnection connection1 = client.open(server.url("/").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
connection1.connect();
|
connection1.connect();
|
||||||
HttpURLConnection connection2 = client.open(server.url("/").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
connection2.connect();
|
connection2.connect();
|
||||||
HttpURLConnection connection3 = client.open(server.url("/").url());
|
HttpURLConnection connection3 = urlFactory.open(server.url("/").url());
|
||||||
connection3.connect();
|
connection3.connect();
|
||||||
assertContent("ABC", connection1, Integer.MAX_VALUE);
|
assertContent("ABC", connection1, Integer.MAX_VALUE);
|
||||||
assertContent("DEF", connection2, Integer.MAX_VALUE);
|
assertContent("DEF", connection2, Integer.MAX_VALUE);
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
|
|
||||||
protected SSLContext sslContext = SslContextBuilder.localhost();
|
protected SSLContext sslContext = SslContextBuilder.localhost();
|
||||||
protected HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
protected HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
|
||||||
protected OkUrlFactory client;
|
protected OkUrlFactory urlFactory;
|
||||||
protected HttpURLConnection connection;
|
protected HttpURLConnection connection;
|
||||||
protected Cache cache;
|
protected Cache cache;
|
||||||
|
|
||||||
@@ -82,10 +82,10 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Before public void setUp() throws Exception {
|
@Before public void setUp() throws Exception {
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
cache = new Cache(tempDir.getRoot(), Integer.MAX_VALUE);
|
cache = new Cache(tempDir.getRoot(), Integer.MAX_VALUE);
|
||||||
client = new OkUrlFactory(new OkHttpClient.Builder()
|
urlFactory = new OkUrlFactory(new OkHttpClient.Builder()
|
||||||
.setProtocols(Arrays.asList(protocol, Protocol.HTTP_1_1))
|
.protocols(Arrays.asList(protocol, Protocol.HTTP_1_1))
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
MockResponse response = new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet");
|
MockResponse response = new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet");
|
||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
||||||
assertEquals(200, connection.getResponseCode());
|
assertEquals(200, connection.getResponseCode());
|
||||||
assertEquals("Sweet", connection.getResponseMessage());
|
assertEquals("Sweet", connection.getResponseMessage());
|
||||||
@@ -111,7 +111,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void emptyResponse() throws IOException {
|
@Test public void emptyResponse() throws IOException {
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
assertEquals(-1, connection.getInputStream().read());
|
assertEquals(-1, connection.getInputStream().read());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +120,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void noDefaultContentLengthOnStreamingPost() throws Exception {
|
@Test public void noDefaultContentLengthOnStreamingPost() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("ABCDE"));
|
server.enqueue(new MockResponse().setBody("ABCDE"));
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.setChunkedStreamingMode(0);
|
connection.setChunkedStreamingMode(0);
|
||||||
connection.getOutputStream().write(postBytes);
|
connection.getOutputStream().write(postBytes);
|
||||||
@@ -135,7 +135,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void userSuppliedContentLengthHeader() throws Exception {
|
@Test public void userSuppliedContentLengthHeader() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("ABCDE"));
|
server.enqueue(new MockResponse().setBody("ABCDE"));
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
|
connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.getOutputStream().write(postBytes);
|
connection.getOutputStream().write(postBytes);
|
||||||
@@ -150,7 +150,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void closeAfterFlush() throws Exception {
|
@Test public void closeAfterFlush() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("ABCDE"));
|
server.enqueue(new MockResponse().setBody("ABCDE"));
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
|
connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.getOutputStream().write(postBytes); // push bytes into SpdyDataOutputStream.buffer
|
connection.getOutputStream().write(postBytes); // push bytes into SpdyDataOutputStream.buffer
|
||||||
@@ -167,7 +167,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void setFixedLengthStreamingModeSetsContentLength() throws Exception {
|
@Test public void setFixedLengthStreamingModeSetsContentLength() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("ABCDE"));
|
server.enqueue(new MockResponse().setBody("ABCDE"));
|
||||||
|
|
||||||
connection = client.open(server.url("/foo").url());
|
connection = urlFactory.open(server.url("/foo").url());
|
||||||
connection.setFixedLengthStreamingMode(postBytes.length);
|
connection.setFixedLengthStreamingMode(postBytes.length);
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
connection.getOutputStream().write(postBytes);
|
connection.getOutputStream().write(postBytes);
|
||||||
@@ -183,8 +183,8 @@ public abstract class HttpOverSpdyTest {
|
|||||||
server.enqueue(new MockResponse().setBody("ABCDEF"));
|
server.enqueue(new MockResponse().setBody("ABCDEF"));
|
||||||
server.enqueue(new MockResponse().setBody("GHIJKL"));
|
server.enqueue(new MockResponse().setBody("GHIJKL"));
|
||||||
|
|
||||||
HttpURLConnection connection1 = client.open(server.url("/r1").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/r1").url());
|
||||||
HttpURLConnection connection2 = client.open(server.url("/r2").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/r2").url());
|
||||||
assertEquals("ABC", readAscii(connection1.getInputStream(), 3));
|
assertEquals("ABC", readAscii(connection1.getInputStream(), 3));
|
||||||
assertEquals("GHI", readAscii(connection2.getInputStream(), 3));
|
assertEquals("GHI", readAscii(connection2.getInputStream(), 3));
|
||||||
assertEquals("DEF", readAscii(connection1.getInputStream(), 3));
|
assertEquals("DEF", readAscii(connection1.getInputStream(), 3));
|
||||||
@@ -209,7 +209,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void gzippedResponseBody() throws Exception {
|
@Test public void gzippedResponseBody() throws Exception {
|
||||||
server.enqueue(
|
server.enqueue(
|
||||||
new MockResponse().addHeader("Content-Encoding: gzip").setBody(gzip("ABCABCABC")));
|
new MockResponse().addHeader("Content-Encoding: gzip").setBody(gzip("ABCABCABC")));
|
||||||
assertContent("ABCABCABC", client.open(server.url("/r1").url()), Integer.MAX_VALUE);
|
assertContent("ABCABCABC", urlFactory.open(server.url("/r1").url()), Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void authenticate() throws Exception {
|
@Test public void authenticate() throws Exception {
|
||||||
@@ -219,10 +219,10 @@ public abstract class HttpOverSpdyTest {
|
|||||||
server.enqueue(new MockResponse().setBody("Successful auth!"));
|
server.enqueue(new MockResponse().setBody("Successful auth!"));
|
||||||
|
|
||||||
Authenticator.setDefault(new RecordingAuthenticator());
|
Authenticator.setDefault(new RecordingAuthenticator());
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setAuthenticator(new JavaNetAuthenticator())
|
.authenticator(new JavaNetAuthenticator())
|
||||||
.build());
|
.build());
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
|
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
|
||||||
|
|
||||||
RecordedRequest denied = server.takeRequest();
|
RecordedRequest denied = server.takeRequest();
|
||||||
@@ -239,7 +239,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
.setBody("This page has moved!"));
|
.setBody("This page has moved!"));
|
||||||
server.enqueue(new MockResponse().setBody("This is the new location!"));
|
server.enqueue(new MockResponse().setBody("This is the new location!"));
|
||||||
|
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
assertContent("This is the new location!", connection, Integer.MAX_VALUE);
|
assertContent("This is the new location!", connection, Integer.MAX_VALUE);
|
||||||
|
|
||||||
RecordedRequest request1 = server.takeRequest();
|
RecordedRequest request1 = server.takeRequest();
|
||||||
@@ -251,7 +251,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
@Test public void readAfterLastByte() throws Exception {
|
@Test public void readAfterLastByte() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("ABC"));
|
server.enqueue(new MockResponse().setBody("ABC"));
|
||||||
|
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
InputStream in = connection.getInputStream();
|
InputStream in = connection.getInputStream();
|
||||||
assertEquals("ABC", readAscii(in, 3));
|
assertEquals("ABC", readAscii(in, 3));
|
||||||
assertEquals(-1, in.read());
|
assertEquals(-1, in.read());
|
||||||
@@ -263,7 +263,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
|
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
|
||||||
server.enqueue(new MockResponse().setBody("A"));
|
server.enqueue(new MockResponse().setBody("A"));
|
||||||
|
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
connection.setReadTimeout(1000);
|
connection.setReadTimeout(1000);
|
||||||
assertContent("A", connection, Integer.MAX_VALUE);
|
assertContent("A", connection, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
server.enqueue(new MockResponse().setBody(new String(body))
|
server.enqueue(new MockResponse().setBody(new String(body))
|
||||||
.throttleBody(1024, 1, SECONDS)); // slow connection 1KiB/second
|
.throttleBody(1024, 1, SECONDS)); // slow connection 1KiB/second
|
||||||
|
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
connection.setReadTimeout(2000); // 2 seconds to read something.
|
connection.setReadTimeout(2000); // 2 seconds to read something.
|
||||||
assertContent(new String(body), connection, Integer.MAX_VALUE);
|
assertContent(new String(body), connection, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
@@ -297,7 +297,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
.setBody(new String(body))
|
.setBody(new String(body))
|
||||||
.throttleBody(1024, 1, SECONDS)); // slow connection 1KiB/second
|
.throttleBody(1024, 1, SECONDS)); // slow connection 1KiB/second
|
||||||
|
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
connection.setReadTimeout(500); // half a second to read something
|
connection.setReadTimeout(500); // half a second to read something
|
||||||
connection.connect();
|
connection.connect();
|
||||||
try {
|
try {
|
||||||
@@ -313,9 +313,9 @@ public abstract class HttpOverSpdyTest {
|
|||||||
response.setBodyDelay(1, TimeUnit.SECONDS);
|
response.setBodyDelay(1, TimeUnit.SECONDS);
|
||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
|
|
||||||
HttpURLConnection connection1 = client.open(server.url("/").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
connection1.setReadTimeout(2000);
|
connection1.setReadTimeout(2000);
|
||||||
HttpURLConnection connection2 = client.open(server.url("/").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
connection2.setReadTimeout(200);
|
connection2.setReadTimeout(200);
|
||||||
connection1.connect();
|
connection1.connect();
|
||||||
connection2.connect();
|
connection2.connect();
|
||||||
@@ -323,55 +323,55 @@ public abstract class HttpOverSpdyTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test public void responsesAreCached() throws IOException {
|
@Test public void responsesAreCached() throws IOException {
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("A"));
|
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("A"));
|
||||||
|
|
||||||
assertContent("A", client.open(server.url("/").url()), Integer.MAX_VALUE);
|
assertContent("A", urlFactory.open(server.url("/").url()), Integer.MAX_VALUE);
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
assertContent("A", client.open(server.url("/").url()), Integer.MAX_VALUE);
|
assertContent("A", urlFactory.open(server.url("/").url()), Integer.MAX_VALUE);
|
||||||
assertContent("A", client.open(server.url("/").url()), Integer.MAX_VALUE);
|
assertContent("A", urlFactory.open(server.url("/").url()), Integer.MAX_VALUE);
|
||||||
assertEquals(3, cache.getRequestCount());
|
assertEquals(3, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(2, cache.getHitCount());
|
assertEquals(2, cache.getHitCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void conditionalCache() throws IOException {
|
@Test public void conditionalCache() throws IOException {
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
|
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
|
||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
assertContent("A", client.open(server.url("/").url()), Integer.MAX_VALUE);
|
assertContent("A", urlFactory.open(server.url("/").url()), Integer.MAX_VALUE);
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
assertContent("A", client.open(server.url("/").url()), Integer.MAX_VALUE);
|
assertContent("A", urlFactory.open(server.url("/").url()), Integer.MAX_VALUE);
|
||||||
assertEquals(2, cache.getRequestCount());
|
assertEquals(2, cache.getRequestCount());
|
||||||
assertEquals(2, cache.getNetworkCount());
|
assertEquals(2, cache.getNetworkCount());
|
||||||
assertEquals(1, cache.getHitCount());
|
assertEquals(1, cache.getHitCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void responseCachedWithoutConsumingFullBody() throws IOException {
|
@Test public void responseCachedWithoutConsumingFullBody() throws IOException {
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("ABCD"));
|
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("ABCD"));
|
||||||
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("EFGH"));
|
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("EFGH"));
|
||||||
|
|
||||||
HttpURLConnection connection1 = client.open(server.url("/").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
InputStream in1 = connection1.getInputStream();
|
InputStream in1 = connection1.getInputStream();
|
||||||
assertEquals("AB", readAscii(in1, 2));
|
assertEquals("AB", readAscii(in1, 2));
|
||||||
in1.close();
|
in1.close();
|
||||||
|
|
||||||
HttpURLConnection connection2 = client.open(server.url("/").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
InputStream in2 = connection2.getInputStream();
|
InputStream in2 = connection2.getInputStream();
|
||||||
assertEquals("ABCD", readAscii(in2, Integer.MAX_VALUE));
|
assertEquals("ABCD", readAscii(in2, Integer.MAX_VALUE));
|
||||||
in2.close();
|
in2.close();
|
||||||
@@ -385,13 +385,13 @@ public abstract class HttpOverSpdyTest {
|
|||||||
.domain(server.getHostName())
|
.domain(server.getHostName())
|
||||||
.build();
|
.build();
|
||||||
cookieJar.enqueueRequestCookies(requestCookie);
|
cookieJar.enqueueRequestCookies(requestCookie);
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
HttpUrl url = server.url("/");
|
HttpUrl url = server.url("/");
|
||||||
assertContent("", client.open(url.url()), Integer.MAX_VALUE);
|
assertContent("", urlFactory.open(url.url()), Integer.MAX_VALUE);
|
||||||
|
|
||||||
RecordedRequest request = server.takeRequest();
|
RecordedRequest request = server.takeRequest();
|
||||||
assertEquals("a=b", request.getHeader("Cookie"));
|
assertEquals("a=b", request.getHeader("Cookie"));
|
||||||
@@ -399,15 +399,15 @@ public abstract class HttpOverSpdyTest {
|
|||||||
|
|
||||||
@Test public void receiveResponseCookies() throws Exception {
|
@Test public void receiveResponseCookies() throws Exception {
|
||||||
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.addHeader("set-cookie: a=b"));
|
.addHeader("set-cookie: a=b"));
|
||||||
|
|
||||||
HttpUrl url = server.url("/");
|
HttpUrl url = server.url("/");
|
||||||
assertContent("", client.open(url.url()), Integer.MAX_VALUE);
|
assertContent("", urlFactory.open(url.url()), Integer.MAX_VALUE);
|
||||||
|
|
||||||
cookieJar.assertResponseCookies("a=b; path=/");
|
cookieJar.assertResponseCookies("a=b; path=/");
|
||||||
}
|
}
|
||||||
@@ -417,13 +417,13 @@ public abstract class HttpOverSpdyTest {
|
|||||||
server.enqueue(new MockResponse().setBody("abc"));
|
server.enqueue(new MockResponse().setBody("abc"));
|
||||||
|
|
||||||
// Disconnect before the stream is created. A connection is still established!
|
// Disconnect before the stream is created. A connection is still established!
|
||||||
HttpURLConnection connection1 = client.open(server.url("/").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
connection1.connect();
|
connection1.connect();
|
||||||
connection1.disconnect();
|
connection1.disconnect();
|
||||||
|
|
||||||
// That connection is pooled, and it works.
|
// That connection is pooled, and it works.
|
||||||
assertEquals(1, client.client().getConnectionPool().getMultiplexedConnectionCount());
|
assertEquals(1, urlFactory.client().connectionPool().getMultiplexedConnectionCount());
|
||||||
HttpURLConnection connection2 = client.open(server.url("/").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertContent("abc", connection2, 3);
|
assertContent("abc", connection2, 3);
|
||||||
assertEquals(0, server.takeRequest().getSequenceNumber());
|
assertEquals(0, server.takeRequest().getSequenceNumber());
|
||||||
}
|
}
|
||||||
@@ -466,7 +466,7 @@ public abstract class HttpOverSpdyTest {
|
|||||||
|
|
||||||
@Override public void run() {
|
@Override public void run() {
|
||||||
try {
|
try {
|
||||||
HttpURLConnection conn = client.open(server.url(path).url());
|
HttpURLConnection conn = urlFactory.open(server.url(path).url());
|
||||||
assertEquals("A", readAscii(conn.getInputStream(), 1));
|
assertEquals("A", readAscii(conn.getInputStream(), 1));
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public class CookiesTest {
|
|||||||
public void testNetscapeResponse() throws Exception {
|
public void testNetscapeResponse() throws Exception {
|
||||||
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
MockWebServer server = new MockWebServer();
|
MockWebServer server = new MockWebServer();
|
||||||
server.start();
|
server.start();
|
||||||
@@ -81,7 +81,7 @@ public class CookiesTest {
|
|||||||
@Test public void testRfc2109Response() throws Exception {
|
@Test public void testRfc2109Response() throws Exception {
|
||||||
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
MockWebServer server = new MockWebServer();
|
MockWebServer server = new MockWebServer();
|
||||||
server.start();
|
server.start();
|
||||||
@@ -111,7 +111,7 @@ public class CookiesTest {
|
|||||||
@Test public void testQuotedAttributeValues() throws Exception {
|
@Test public void testQuotedAttributeValues() throws Exception {
|
||||||
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
MockWebServer server = new MockWebServer();
|
MockWebServer server = new MockWebServer();
|
||||||
server.start();
|
server.start();
|
||||||
@@ -154,7 +154,7 @@ public class CookiesTest {
|
|||||||
cookieB.setPath("/");
|
cookieB.setPath("/");
|
||||||
cookieManager.getCookieStore().add(server.url("/").uri(), cookieB);
|
cookieManager.getCookieStore().add(server.url("/").uri(), cookieB);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
get(server.url("/"));
|
get(server.url("/"));
|
||||||
@@ -182,7 +182,7 @@ public class CookiesTest {
|
|||||||
cookie.setPortlist(portList);
|
cookie.setPortlist(portList);
|
||||||
cookieManager.getCookieStore().add(redirectSource.url("/").uri(), cookie);
|
cookieManager.getCookieStore().add(redirectSource.url("/").uri(), cookie);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
get(redirectSource.url("/"));
|
get(redirectSource.url("/"));
|
||||||
@@ -199,7 +199,7 @@ public class CookiesTest {
|
|||||||
|
|
||||||
@Test public void testCookiesSentIgnoresCase() throws Exception {
|
@Test public void testCookiesSentIgnoresCase() throws Exception {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setCookieJar(new JavaNetCookieJar(new CookieManager() {
|
.cookieJar(new JavaNetCookieJar(new CookieManager() {
|
||||||
@Override public Map<String, List<String>> get(URI uri,
|
@Override public Map<String, List<String>> get(URI uri,
|
||||||
Map<String, List<String>> requestHeaders) throws IOException {
|
Map<String, List<String>> requestHeaders) throws IOException {
|
||||||
Map<String, List<String>> result = new HashMap<>();
|
Map<String, List<String>> result = new HashMap<>();
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ public final class DisconnectTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setSocketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
|
.socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
|
||||||
@Override protected Socket configureSocket(Socket socket) throws IOException {
|
@Override protected Socket configureSocket(Socket socket) throws IOException {
|
||||||
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
|
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
|
||||||
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
|
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public final class ExternalHttp2Example {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
URL url = new URL("https://twitter.com");
|
URL url = new URL("https://twitter.com");
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setProtocols(Util.immutableList(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
.protocols(Util.immutableList(Protocol.HTTP_2, Protocol.HTTP_1_1))
|
||||||
.build();
|
.build();
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(client)
|
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(client)
|
||||||
.open(url);
|
.open(url);
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public final class ExternalSpdyExample {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
URL url = new URL("https://www.google.ca/");
|
URL url = new URL("https://www.google.ca/");
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setProtocols(Util.immutableList(Protocol.SPDY_3, Protocol.HTTP_1_1))
|
.protocols(Util.immutableList(Protocol.SPDY_3, Protocol.HTTP_1_1))
|
||||||
.build();
|
.build();
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(client)
|
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(client)
|
||||||
.open(url);
|
.open(url);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public final class ThreadInterruptTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setSocketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
|
.socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
|
||||||
@Override
|
@Override
|
||||||
protected Socket configureSocket(Socket socket) throws IOException {
|
protected Socket configureSocket(Socket socket) throws IOException {
|
||||||
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
|
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
|
||||||
|
|||||||
@@ -54,13 +54,13 @@ public final class OkUrlFactory implements URLStreamHandlerFactory, Cloneable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HttpURLConnection open(URL url) {
|
public HttpURLConnection open(URL url) {
|
||||||
return open(url, client.getProxy());
|
return open(url, client.proxy());
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpURLConnection open(URL url, Proxy proxy) {
|
HttpURLConnection open(URL url, Proxy proxy) {
|
||||||
String protocol = url.getProtocol();
|
String protocol = url.getProtocol();
|
||||||
OkHttpClient copy = client.newBuilder()
|
OkHttpClient copy = client.newBuilder()
|
||||||
.setProxy(proxy)
|
.proxy(proxy)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy);
|
if (protocol.equals("http")) return new HttpURLConnectionImpl(url, copy);
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
? url.getPort()
|
? url.getPort()
|
||||||
: HttpUrl.defaultPort(url.getProtocol());
|
: HttpUrl.defaultPort(url.getProtocol());
|
||||||
if (usingProxy()) {
|
if (usingProxy()) {
|
||||||
InetSocketAddress proxyAddress = (InetSocketAddress) client.getProxy().address();
|
InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address();
|
||||||
hostName = proxyAddress.getHostName();
|
hostName = proxyAddress.getHostName();
|
||||||
hostPort = proxyAddress.getPort();
|
hostPort = proxyAddress.getPort();
|
||||||
}
|
}
|
||||||
@@ -279,33 +279,33 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
|
|
||||||
@Override public void setConnectTimeout(int timeoutMillis) {
|
@Override public void setConnectTimeout(int timeoutMillis) {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setConnectTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
|
.connectTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInstanceFollowRedirects(boolean followRedirects) {
|
public void setInstanceFollowRedirects(boolean followRedirects) {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setFollowRedirects(followRedirects)
|
.followRedirects(followRedirects)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public boolean getInstanceFollowRedirects() {
|
@Override public boolean getInstanceFollowRedirects() {
|
||||||
return client.getFollowRedirects();
|
return client.followRedirects();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public int getConnectTimeout() {
|
@Override public int getConnectTimeout() {
|
||||||
return client.getConnectTimeout();
|
return client.connectTimeoutMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void setReadTimeout(int timeoutMillis) {
|
@Override public void setReadTimeout(int timeoutMillis) {
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setReadTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
|
.readTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public int getReadTimeout() {
|
@Override public int getReadTimeout() {
|
||||||
return client.getReadTimeout();
|
return client.readTimeoutMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initHttpEngine() throws IOException {
|
private void initHttpEngine() throws IOException {
|
||||||
@@ -377,7 +377,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
OkHttpClient engineClient = client;
|
OkHttpClient engineClient = client;
|
||||||
if (Internal.instance.internalCache(engineClient) != null && !getUseCaches()) {
|
if (Internal.instance.internalCache(engineClient) != null && !getUseCaches()) {
|
||||||
engineClient = client.newBuilder()
|
engineClient = client.newBuilder()
|
||||||
.setCache(null)
|
.cache(null)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -522,7 +522,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
@Override public final boolean usingProxy() {
|
@Override public final boolean usingProxy() {
|
||||||
Proxy proxy = route != null
|
Proxy proxy = route != null
|
||||||
? route.proxy()
|
? route.proxy()
|
||||||
: client.getProxy();
|
: client.proxy();
|
||||||
return proxy != null && proxy.type() != Proxy.Type.DIRECT;
|
return proxy != null && proxy.type() != Proxy.Type.DIRECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -602,7 +602,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
private void setProtocols(String protocolsString, boolean append) {
|
private void setProtocols(String protocolsString, boolean append) {
|
||||||
List<Protocol> protocolsList = new ArrayList<>();
|
List<Protocol> protocolsList = new ArrayList<>();
|
||||||
if (append) {
|
if (append) {
|
||||||
protocolsList.addAll(client.getProtocols());
|
protocolsList.addAll(client.protocols());
|
||||||
}
|
}
|
||||||
for (String protocol : protocolsString.split(",", -1)) {
|
for (String protocol : protocolsString.split(",", -1)) {
|
||||||
try {
|
try {
|
||||||
@@ -612,7 +612,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setProtocols(protocolsList)
|
.protocols(protocolsList)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,22 +49,22 @@ public final class HttpsURLConnectionImpl extends DelegatingHttpsURLConnection {
|
|||||||
|
|
||||||
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
@Override public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||||
delegate.client = delegate.client.newBuilder()
|
delegate.client = delegate.client.newBuilder()
|
||||||
.setHostnameVerifier(hostnameVerifier)
|
.hostnameVerifier(hostnameVerifier)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public HostnameVerifier getHostnameVerifier() {
|
@Override public HostnameVerifier getHostnameVerifier() {
|
||||||
return delegate.client.getHostnameVerifier();
|
return delegate.client.hostnameVerifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
|
@Override public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
|
||||||
delegate.client = delegate.client.newBuilder()
|
delegate.client = delegate.client.newBuilder()
|
||||||
.setSslSocketFactory(sslSocketFactory)
|
.sslSocketFactory(sslSocketFactory)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public SSLSocketFactory getSSLSocketFactory() {
|
@Override public SSLSocketFactory getSSLSocketFactory() {
|
||||||
return delegate.client.getSslSocketFactory();
|
return delegate.client.sslSocketFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public long getContentLengthLong() {
|
@Override public long getContentLengthLong() {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public class OkUrlFactoryTest {
|
|||||||
@Before public void setUp() throws IOException {
|
@Before public void setUp() throws IOException {
|
||||||
cache = new Cache(new File("/cache/"), 10 * 1024 * 1024, fileSystem);
|
cache = new Cache(new File("/cache/"), 10 * 1024 * 1024, fileSystem);
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
factory = new OkUrlFactory(client);
|
factory = new OkUrlFactory(client);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,16 +81,16 @@ public final class UrlConnectionCacheTest {
|
|||||||
@Rule public InMemoryFileSystem fileSystem = new InMemoryFileSystem();
|
@Rule public InMemoryFileSystem fileSystem = new InMemoryFileSystem();
|
||||||
|
|
||||||
private final SSLContext sslContext = SslContextBuilder.localhost();
|
private final SSLContext sslContext = SslContextBuilder.localhost();
|
||||||
private OkUrlFactory client = new OkUrlFactory(new OkHttpClient.Builder().build());
|
private OkUrlFactory urlFactory = new OkUrlFactory(new OkHttpClient.Builder().build());
|
||||||
private Cache cache;
|
private Cache cache;
|
||||||
private final CookieManager cookieManager = new CookieManager();
|
private final CookieManager cookieManager = new CookieManager();
|
||||||
|
|
||||||
@Before public void setUp() throws Exception {
|
@Before public void setUp() throws Exception {
|
||||||
server.setProtocolNegotiationEnabled(false);
|
server.setProtocolNegotiationEnabled(false);
|
||||||
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(new File("/cache/"), Integer.MAX_VALUE, fileSystem);
|
||||||
client = new OkUrlFactory(new OkHttpClient.Builder()
|
urlFactory = new OkUrlFactory(new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.setCookieJar(new JavaNetCookieJar(cookieManager))
|
.cookieJar(new JavaNetCookieJar(cookieManager))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test public void responseCacheAccessWithOkHttpMember() throws IOException {
|
@Test public void responseCacheAccessWithOkHttpMember() throws IOException {
|
||||||
assertSame(cache, client.client().getCache());
|
assertSame(cache, urlFactory.client().cache());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -178,7 +178,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
HttpURLConnection conn = client.open(url);
|
HttpURLConnection conn = urlFactory.open(url);
|
||||||
assertEquals(responseCode, conn.getResponseCode());
|
assertEquals(responseCode, conn.getResponseCode());
|
||||||
|
|
||||||
// exhaust the content stream
|
// exhaust the content stream
|
||||||
@@ -219,7 +219,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
|
|
||||||
// Make sure that calling skip() doesn't omit bytes from the cache.
|
// Make sure that calling skip() doesn't omit bytes from the cache.
|
||||||
HttpURLConnection urlConnection = client.open(server.url("/").url());
|
HttpURLConnection urlConnection = urlFactory.open(server.url("/").url());
|
||||||
InputStream in = urlConnection.getInputStream();
|
InputStream in = urlConnection.getInputStream();
|
||||||
assertEquals("I love ", readAscii(urlConnection, "I love ".length()));
|
assertEquals("I love ", readAscii(urlConnection, "I love ".length()));
|
||||||
reliableSkip(in, "puppies but hate ".length());
|
reliableSkip(in, "puppies but hate ".length());
|
||||||
@@ -229,7 +229,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
assertEquals(1, cache.getWriteSuccessCount());
|
assertEquals(1, cache.getWriteSuccessCount());
|
||||||
assertEquals(0, cache.getWriteAbortCount());
|
assertEquals(0, cache.getWriteAbortCount());
|
||||||
|
|
||||||
urlConnection = client.open(server.url("/").url()); // cached!
|
urlConnection = urlFactory.open(server.url("/").url()); // cached!
|
||||||
in = urlConnection.getInputStream();
|
in = urlConnection.getInputStream();
|
||||||
assertEquals("I love puppies but hate spiders",
|
assertEquals("I love puppies but hate spiders",
|
||||||
readAscii(urlConnection, "I love puppies but hate spiders".length()));
|
readAscii(urlConnection, "I love puppies but hate spiders".length()));
|
||||||
@@ -250,7 +250,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
|
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
|
||||||
.setBody("ABC"));
|
.setBody("ABC"));
|
||||||
|
|
||||||
HttpsURLConnection c1 = (HttpsURLConnection) client.open(server.url("/").url());
|
HttpsURLConnection c1 = (HttpsURLConnection) urlFactory.open(server.url("/").url());
|
||||||
c1.setSSLSocketFactory(sslContext.getSocketFactory());
|
c1.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||||
c1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
c1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||||
assertEquals("ABC", readAscii(c1));
|
assertEquals("ABC", readAscii(c1));
|
||||||
@@ -262,7 +262,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
Principal peerPrincipal = c1.getPeerPrincipal();
|
Principal peerPrincipal = c1.getPeerPrincipal();
|
||||||
Principal localPrincipal = c1.getLocalPrincipal();
|
Principal localPrincipal = c1.getLocalPrincipal();
|
||||||
|
|
||||||
HttpsURLConnection c2 = (HttpsURLConnection) client.open(server.url("/").url()); // cached!
|
HttpsURLConnection c2 = (HttpsURLConnection) urlFactory.open(server.url("/").url()); // cached!
|
||||||
c2.setSSLSocketFactory(sslContext.getSocketFactory());
|
c2.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||||
c2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
c2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||||
assertEquals("ABC", readAscii(c2));
|
assertEquals("ABC", readAscii(c2));
|
||||||
@@ -288,10 +288,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("ABC"));
|
.setBody("ABC"));
|
||||||
server.enqueue(new MockResponse().setBody("DEF"));
|
server.enqueue(new MockResponse().setBody("DEF"));
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("ABC", readAscii(connection));
|
assertEquals("ABC", readAscii(connection));
|
||||||
|
|
||||||
connection = client.open(server.url("/").url()); // cached!
|
connection = urlFactory.open(server.url("/").url()); // cached!
|
||||||
assertEquals("ABC", readAscii(connection));
|
assertEquals("ABC", readAscii(connection));
|
||||||
|
|
||||||
assertEquals(4, cache.getRequestCount()); // 2 requests + 2 redirects
|
assertEquals(4, cache.getRequestCount()); // 2 requests + 2 redirects
|
||||||
@@ -305,18 +305,18 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Location: /foo"));
|
.addHeader("Location: /foo"));
|
||||||
server.enqueue(new MockResponse().setBody("DEF"));
|
server.enqueue(new MockResponse().setBody("DEF"));
|
||||||
|
|
||||||
assertEquals("ABC", readAscii(client.open(server.url("/foo").url())));
|
assertEquals("ABC", readAscii(urlFactory.open(server.url("/foo").url())));
|
||||||
RecordedRequest request1 = server.takeRequest();
|
RecordedRequest request1 = server.takeRequest();
|
||||||
assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
|
assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
|
||||||
assertEquals(0, request1.getSequenceNumber());
|
assertEquals(0, request1.getSequenceNumber());
|
||||||
|
|
||||||
assertEquals("ABC", readAscii(client.open(server.url("/bar").url())));
|
assertEquals("ABC", readAscii(urlFactory.open(server.url("/bar").url())));
|
||||||
RecordedRequest request2 = server.takeRequest();
|
RecordedRequest request2 = server.takeRequest();
|
||||||
assertEquals("GET /bar HTTP/1.1", request2.getRequestLine());
|
assertEquals("GET /bar HTTP/1.1", request2.getRequestLine());
|
||||||
assertEquals(1, request2.getSequenceNumber());
|
assertEquals(1, request2.getSequenceNumber());
|
||||||
|
|
||||||
// an unrelated request should reuse the pooled connection
|
// an unrelated request should reuse the pooled connection
|
||||||
assertEquals("DEF", readAscii(client.open(server.url("/baz").url())));
|
assertEquals("DEF", readAscii(urlFactory.open(server.url("/baz").url())));
|
||||||
RecordedRequest request3 = server.takeRequest();
|
RecordedRequest request3 = server.takeRequest();
|
||||||
assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
|
assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
|
||||||
assertEquals(2, request3.getSequenceNumber());
|
assertEquals(2, request3.getSequenceNumber());
|
||||||
@@ -333,17 +333,17 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("ABC"));
|
.setBody("ABC"));
|
||||||
server.enqueue(new MockResponse().setBody("DEF"));
|
server.enqueue(new MockResponse().setBody("DEF"));
|
||||||
|
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
HttpsURLConnection connection1 = (HttpsURLConnection) client.open(server.url("/").url());
|
HttpsURLConnection connection1 = (HttpsURLConnection) urlFactory.open(server.url("/").url());
|
||||||
assertEquals("ABC", readAscii(connection1));
|
assertEquals("ABC", readAscii(connection1));
|
||||||
assertNotNull(connection1.getCipherSuite());
|
assertNotNull(connection1.getCipherSuite());
|
||||||
|
|
||||||
// Cached!
|
// Cached!
|
||||||
HttpsURLConnection connection2 = (HttpsURLConnection) client.open(server.url("/").url());
|
HttpsURLConnection connection2 = (HttpsURLConnection) urlFactory.open(server.url("/").url());
|
||||||
assertEquals("ABC", readAscii(connection2));
|
assertEquals("ABC", readAscii(connection2));
|
||||||
assertNotNull(connection2.getCipherSuite());
|
assertNotNull(connection2.getCipherSuite());
|
||||||
|
|
||||||
@@ -371,16 +371,16 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
|
.setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
|
||||||
.addHeader("Location: " + server2.url("/").url()));
|
.addHeader("Location: " + server2.url("/").url()));
|
||||||
|
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
.hostnameVerifier(NULL_HOSTNAME_VERIFIER)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
HttpURLConnection connection1 = client.open(server.url("/").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("ABC", readAscii(connection1));
|
assertEquals("ABC", readAscii(connection1));
|
||||||
|
|
||||||
// Cached!
|
// Cached!
|
||||||
HttpURLConnection connection2 = client.open(server.url("/").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("ABC", readAscii(connection2));
|
assertEquals("ABC", readAscii(connection2));
|
||||||
|
|
||||||
assertEquals(4, cache.getRequestCount()); // 2 direct + 2 redirect = 4
|
assertEquals(4, cache.getRequestCount()); // 2 direct + 2 redirect = 4
|
||||||
@@ -408,7 +408,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("Request #2"));
|
server.enqueue(new MockResponse().setBody("Request #2"));
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(
|
BufferedReader reader = new BufferedReader(
|
||||||
new InputStreamReader(client.open(server.url("/").url()).getInputStream()));
|
new InputStreamReader(urlFactory.open(server.url("/").url()).getInputStream()));
|
||||||
assertEquals("ABCDE", reader.readLine());
|
assertEquals("ABCDE", reader.readLine());
|
||||||
try {
|
try {
|
||||||
reader.readLine();
|
reader.readLine();
|
||||||
@@ -420,7 +420,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
assertEquals(1, cache.getWriteAbortCount());
|
assertEquals(1, cache.getWriteAbortCount());
|
||||||
assertEquals(0, cache.getWriteSuccessCount());
|
assertEquals(0, cache.getWriteSuccessCount());
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("Request #2", readAscii(connection));
|
assertEquals("Request #2", readAscii(connection));
|
||||||
assertEquals(1, cache.getWriteAbortCount());
|
assertEquals(1, cache.getWriteAbortCount());
|
||||||
assertEquals(1, cache.getWriteSuccessCount());
|
assertEquals(1, cache.getWriteSuccessCount());
|
||||||
@@ -445,7 +445,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(response);
|
server.enqueue(response);
|
||||||
server.enqueue(new MockResponse().setBody("Request #2"));
|
server.enqueue(new MockResponse().setBody("Request #2"));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
InputStream in = connection.getInputStream();
|
InputStream in = connection.getInputStream();
|
||||||
assertEquals("ABCDE", readAscii(connection, 5));
|
assertEquals("ABCDE", readAscii(connection, 5));
|
||||||
in.close();
|
in.close();
|
||||||
@@ -457,7 +457,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
assertEquals(1, cache.getWriteAbortCount());
|
assertEquals(1, cache.getWriteAbortCount());
|
||||||
assertEquals(0, cache.getWriteSuccessCount());
|
assertEquals(0, cache.getWriteSuccessCount());
|
||||||
connection = client.open(server.url("/").url());
|
connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("Request #2", readAscii(connection));
|
assertEquals("Request #2", readAscii(connection));
|
||||||
assertEquals(1, cache.getWriteAbortCount());
|
assertEquals(1, cache.getWriteAbortCount());
|
||||||
assertEquals(1, cache.getWriteSuccessCount());
|
assertEquals(1, cache.getWriteSuccessCount());
|
||||||
@@ -474,8 +474,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
URLConnection connection = client.open(url);
|
URLConnection connection = urlFactory.open(url);
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertNull(connection.getHeaderField("Warning"));
|
assertNull(connection.getHeaderField("Warning"));
|
||||||
}
|
}
|
||||||
@@ -501,8 +501,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Date: " + formatDate(-5, TimeUnit.DAYS))
|
.addHeader("Date: " + formatDate(-5, TimeUnit.DAYS))
|
||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertEquals("113 HttpURLConnection \"Heuristic expiration\"",
|
assertEquals("113 HttpURLConnection \"Heuristic expiration\"",
|
||||||
connection.getHeaderField("Warning"));
|
connection.getHeaderField("Warning"));
|
||||||
@@ -516,8 +516,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/?foo=bar").url();
|
URL url = server.url("/?foo=bar").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
assertEquals("B", readAscii(client.open(url)));
|
assertEquals("B", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void expirationDateInThePastWithLastModifiedHeader() throws Exception {
|
@Test public void expirationDateInThePastWithLastModifiedHeader() throws Exception {
|
||||||
@@ -631,13 +631,13 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|
||||||
HttpURLConnection request1 = client.open(url);
|
HttpURLConnection request1 = urlFactory.open(url);
|
||||||
request1.setRequestMethod(requestMethod);
|
request1.setRequestMethod(requestMethod);
|
||||||
addRequestBodyIfNecessary(requestMethod, request1);
|
addRequestBodyIfNecessary(requestMethod, request1);
|
||||||
request1.getInputStream().close();
|
request1.getInputStream().close();
|
||||||
assertEquals("1", request1.getHeaderField("X-Response-ID"));
|
assertEquals("1", request1.getHeaderField("X-Response-ID"));
|
||||||
|
|
||||||
URLConnection request2 = client.open(url);
|
URLConnection request2 = urlFactory.open(url);
|
||||||
request2.getInputStream().close();
|
request2.getInputStream().close();
|
||||||
if (expectCached) {
|
if (expectCached) {
|
||||||
assertEquals("1", request2.getHeaderField("X-Response-ID"));
|
assertEquals("1", request2.getHeaderField("X-Response-ID"));
|
||||||
@@ -669,14 +669,14 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
|
|
||||||
HttpURLConnection invalidate = client.open(url);
|
HttpURLConnection invalidate = urlFactory.open(url);
|
||||||
invalidate.setRequestMethod(requestMethod);
|
invalidate.setRequestMethod(requestMethod);
|
||||||
addRequestBodyIfNecessary(requestMethod, invalidate);
|
addRequestBodyIfNecessary(requestMethod, invalidate);
|
||||||
assertEquals("B", readAscii(invalidate));
|
assertEquals("B", readAscii(invalidate));
|
||||||
|
|
||||||
assertEquals("C", readAscii(client.open(url)));
|
assertEquals("C", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
|
@Test public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
|
||||||
@@ -690,14 +690,14 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
|
|
||||||
HttpURLConnection invalidate = client.open(url);
|
HttpURLConnection invalidate = urlFactory.open(url);
|
||||||
invalidate.setRequestMethod("POST");
|
invalidate.setRequestMethod("POST");
|
||||||
addRequestBodyIfNecessary("POST", invalidate);
|
addRequestBodyIfNecessary("POST", invalidate);
|
||||||
assertEquals("B", readAscii(invalidate));
|
assertEquals("B", readAscii(invalidate));
|
||||||
|
|
||||||
assertEquals("C", readAscii(client.open(url)));
|
assertEquals("C", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void etag() throws Exception {
|
@Test public void etag() throws Exception {
|
||||||
@@ -769,11 +769,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|
||||||
URLConnection range = client.open(url);
|
URLConnection range = urlFactory.open(url);
|
||||||
range.addRequestProperty("Range", "bytes=1000-1001");
|
range.addRequestProperty("Range", "bytes=1000-1001");
|
||||||
assertEquals("AA", readAscii(range));
|
assertEquals("AA", readAscii(range));
|
||||||
|
|
||||||
assertEquals("BB", readAscii(client.open(url)));
|
assertEquals("BB", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void serverReturnsDocumentOlderThanCache() throws Exception {
|
@Test public void serverReturnsDocumentOlderThanCache() throws Exception {
|
||||||
@@ -785,8 +785,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void nonIdentityEncodingAndConditionalCache() throws Exception {
|
@Test public void nonIdentityEncodingAndConditionalCache() throws Exception {
|
||||||
@@ -810,9 +810,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
// At least three request/response pairs are required because after the first request is cached
|
// At least three request/response pairs are required because after the first request is cached
|
||||||
// a different execution path might be taken. Thus modifications to the cache applied during
|
// a different execution path might be taken. Thus modifications to the cache applied during
|
||||||
// the second request might not be visible until another request is performed.
|
// the second request might not be visible until another request is performed.
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void notModifiedSpecifiesEncoding() throws Exception {
|
@Test public void notModifiedSpecifiesEncoding() throws Exception {
|
||||||
@@ -827,9 +827,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setBody("DEFDEFDEF"));
|
.setBody("DEFDEFDEF"));
|
||||||
|
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("DEFDEFDEF", readAscii(client.open(server.url("/").url())));
|
assertEquals("DEFDEFDEF", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** https://github.com/square/okhttp/issues/947 */
|
/** https://github.com/square/okhttp/issues/947 */
|
||||||
@@ -841,8 +841,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Cache-Control: max-age=60"));
|
.addHeader("Cache-Control: max-age=60"));
|
||||||
server.enqueue(new MockResponse().setBody("FAIL"));
|
server.enqueue(new MockResponse().setBody("FAIL"));
|
||||||
|
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("ABCABCABC", readAscii(client.open(server.url("/").url())));
|
assertEquals("ABCABCABC", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void conditionalCacheHitIsNotDoublePooled() throws Exception {
|
@Test public void conditionalCacheHitIsNotDoublePooled() throws Exception {
|
||||||
@@ -851,9 +851,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
.clearHeaders()
|
.clearHeaders()
|
||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(1, client.client().getConnectionPool().getIdleConnectionCount());
|
assertEquals(1, urlFactory.client().connectionPool().getIdleConnectionCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void expiresDateBeforeModifiedDate() throws Exception {
|
@Test public void expiresDateBeforeModifiedDate() throws Exception {
|
||||||
@@ -869,9 +869,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
|
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "max-age=30");
|
connection.addRequestProperty("Cache-Control", "max-age=30");
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -882,9 +882,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "min-fresh=120");
|
connection.addRequestProperty("Cache-Control", "min-fresh=120");
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -895,9 +895,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "max-stale=180");
|
connection.addRequestProperty("Cache-Control", "max-stale=180");
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertEquals("110 HttpURLConnection \"Response is stale\"",
|
assertEquals("110 HttpURLConnection \"Response is stale\"",
|
||||||
@@ -910,9 +910,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "max-stale=180");
|
connection.addRequestProperty("Cache-Control", "max-stale=180");
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -920,7 +920,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
@Test public void requestOnlyIfCachedWithNoResponseCached() throws IOException {
|
@Test public void requestOnlyIfCachedWithNoResponseCached() throws IOException {
|
||||||
// (no responses enqueued)
|
// (no responses enqueued)
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
||||||
assertGatewayTimeout(connection);
|
assertGatewayTimeout(connection);
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
@@ -933,8 +933,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Cache-Control: max-age=30")
|
.addHeader("Cache-Control: max-age=30")
|
||||||
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertEquals(2, cache.getRequestCount());
|
assertEquals(2, cache.getRequestCount());
|
||||||
@@ -947,8 +947,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Cache-Control: max-age=30")
|
.addHeader("Cache-Control: max-age=30")
|
||||||
.addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
||||||
assertGatewayTimeout(connection);
|
assertGatewayTimeout(connection);
|
||||||
assertEquals(2, cache.getRequestCount());
|
assertEquals(2, cache.getRequestCount());
|
||||||
@@ -959,8 +959,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
@Test public void requestOnlyIfCachedWithUnhelpfulResponseCached() throws IOException {
|
@Test public void requestOnlyIfCachedWithUnhelpfulResponseCached() throws IOException {
|
||||||
server.enqueue(new MockResponse().setBody("A"));
|
server.enqueue(new MockResponse().setBody("A"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
||||||
assertGatewayTimeout(connection);
|
assertGatewayTimeout(connection);
|
||||||
assertEquals(2, cache.getRequestCount());
|
assertEquals(2, cache.getRequestCount());
|
||||||
@@ -977,8 +977,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
URLConnection connection = client.open(url);
|
URLConnection connection = urlFactory.open(url);
|
||||||
connection.setRequestProperty("Cache-Control", "no-cache");
|
connection.setRequestProperty("Cache-Control", "no-cache");
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -992,8 +992,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
URLConnection connection = client.open(url);
|
URLConnection connection = urlFactory.open(url);
|
||||||
connection.setRequestProperty("Pragma", "no-cache");
|
connection.setRequestProperty("Pragma", "no-cache");
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -1024,9 +1024,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(url);
|
HttpURLConnection connection = urlFactory.open(url);
|
||||||
connection.addRequestProperty(conditionName, conditionValue);
|
connection.addRequestProperty(conditionName, conditionValue);
|
||||||
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
|
||||||
assertEquals("", readAscii(connection));
|
assertEquals("", readAscii(connection));
|
||||||
@@ -1045,7 +1045,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("A"));
|
server.enqueue(new MockResponse().setBody("A"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection = client.open(url);
|
URLConnection connection = urlFactory.open(url);
|
||||||
connection.setIfModifiedSince(1393666200000L);
|
connection.setIfModifiedSince(1393666200000L);
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
RecordedRequest request = server.takeRequest();
|
RecordedRequest request = server.takeRequest();
|
||||||
@@ -1074,8 +1074,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
|
|
||||||
// The first request has no conditions.
|
// The first request has no conditions.
|
||||||
RecordedRequest request1 = server.takeRequest();
|
RecordedRequest request1 = server.takeRequest();
|
||||||
@@ -1089,7 +1089,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
@Test public void clientSuppliedConditionWithoutCachedResult() throws Exception {
|
@Test public void clientSuppliedConditionWithoutCachedResult() throws Exception {
|
||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
|
String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
|
||||||
connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
|
connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
|
||||||
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
|
||||||
@@ -1101,10 +1101,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection = client.open(url);
|
URLConnection connection = urlFactory.open(url);
|
||||||
connection.addRequestProperty("Authorization", "password");
|
connection.addRequestProperty("Authorization", "password");
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void contentLocationDoesNotPopulateCache() throws Exception {
|
@Test public void contentLocationDoesNotPopulateCache() throws Exception {
|
||||||
@@ -1113,8 +1113,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/foo").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/foo").url())));
|
||||||
assertEquals("B", readAscii(client.open(server.url("/bar").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/bar").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void useCachesFalseDoesNotWriteToCache() throws Exception {
|
@Test public void useCachesFalseDoesNotWriteToCache() throws Exception {
|
||||||
@@ -1122,10 +1122,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A").setBody("A"));
|
new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A").setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.setUseCaches(false);
|
connection.setUseCaches(false);
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
assertEquals("B", readAscii(client.open(server.url("/").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void useCachesFalseDoesNotReadFromCache() throws Exception {
|
@Test public void useCachesFalseDoesNotReadFromCache() throws Exception {
|
||||||
@@ -1133,22 +1133,22 @@ public final class UrlConnectionCacheTest {
|
|||||||
new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A").setBody("A"));
|
new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A").setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.setUseCaches(false);
|
connection.setUseCaches(false);
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void defaultUseCachesSetsInitialValueOnly() throws Exception {
|
@Test public void defaultUseCachesSetsInitialValueOnly() throws Exception {
|
||||||
URL url = new URL("http://localhost/");
|
URL url = new URL("http://localhost/");
|
||||||
URLConnection c1 = client.open(url);
|
URLConnection c1 = urlFactory.open(url);
|
||||||
URLConnection c2 = client.open(url);
|
URLConnection c2 = urlFactory.open(url);
|
||||||
assertTrue(c1.getDefaultUseCaches());
|
assertTrue(c1.getDefaultUseCaches());
|
||||||
c1.setDefaultUseCaches(false);
|
c1.setDefaultUseCaches(false);
|
||||||
try {
|
try {
|
||||||
assertTrue(c1.getUseCaches());
|
assertTrue(c1.getUseCaches());
|
||||||
assertTrue(c2.getUseCaches());
|
assertTrue(c2.getUseCaches());
|
||||||
URLConnection c3 = client.open(url);
|
URLConnection c3 = urlFactory.open(url);
|
||||||
assertFalse(c3.getUseCaches());
|
assertFalse(c3.getUseCaches());
|
||||||
} finally {
|
} finally {
|
||||||
c1.setDefaultUseCaches(true);
|
c1.setDefaultUseCaches(true);
|
||||||
@@ -1162,9 +1162,9 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/a").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/a").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/a").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/a").url())));
|
||||||
assertEquals("B", readAscii(client.open(server.url("/b").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/b").url())));
|
||||||
|
|
||||||
assertEquals(0, server.takeRequest().getSequenceNumber());
|
assertEquals(0, server.takeRequest().getSequenceNumber());
|
||||||
assertEquals(1, server.takeRequest().getSequenceNumber());
|
assertEquals(1, server.takeRequest().getSequenceNumber());
|
||||||
@@ -1178,12 +1178,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
server.enqueue(new MockResponse().setBody("C"));
|
server.enqueue(new MockResponse().setBody("C"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
assertEquals("B", readAscii(client.open(server.url("/").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("C", readAscii(client.open(server.url("/").url())));
|
assertEquals("C", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(3, cache.getRequestCount());
|
assertEquals(3, cache.getRequestCount());
|
||||||
assertEquals(3, cache.getNetworkCount());
|
assertEquals(3, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
@@ -1196,12 +1196,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(3, cache.getRequestCount());
|
assertEquals(3, cache.getRequestCount());
|
||||||
assertEquals(3, cache.getNetworkCount());
|
assertEquals(3, cache.getNetworkCount());
|
||||||
assertEquals(2, cache.getHitCount());
|
assertEquals(2, cache.getHitCount());
|
||||||
@@ -1210,12 +1210,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
@Test public void statisticsFullCacheHit() throws Exception {
|
@Test public void statisticsFullCacheHit() throws Exception {
|
||||||
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
|
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(1, cache.getRequestCount());
|
assertEquals(1, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(0, cache.getHitCount());
|
assertEquals(0, cache.getHitCount());
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals(3, cache.getRequestCount());
|
assertEquals(3, cache.getRequestCount());
|
||||||
assertEquals(1, cache.getNetworkCount());
|
assertEquals(1, cache.getNetworkCount());
|
||||||
assertEquals(2, cache.getHitCount());
|
assertEquals(2, cache.getHitCount());
|
||||||
@@ -1228,11 +1228,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
HttpURLConnection frConnection = client.open(url);
|
HttpURLConnection frConnection = urlFactory.open(url);
|
||||||
frConnection.addRequestProperty("Accept-Language", "fr-CA");
|
frConnection.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
assertEquals("A", readAscii(frConnection));
|
assertEquals("A", readAscii(frConnection));
|
||||||
|
|
||||||
HttpURLConnection enConnection = client.open(url);
|
HttpURLConnection enConnection = urlFactory.open(url);
|
||||||
enConnection.addRequestProperty("Accept-Language", "en-US");
|
enConnection.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("B", readAscii(enConnection));
|
assertEquals("B", readAscii(enConnection));
|
||||||
}
|
}
|
||||||
@@ -1244,10 +1244,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection1 = client.open(url);
|
URLConnection connection1 = urlFactory.open(url);
|
||||||
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
URLConnection connection2 = client.open(url);
|
URLConnection connection2 = urlFactory.open(url);
|
||||||
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
}
|
}
|
||||||
@@ -1258,8 +1258,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void varyMatchesAddedRequestHeaderField() throws Exception {
|
@Test public void varyMatchesAddedRequestHeaderField() throws Exception {
|
||||||
@@ -1268,8 +1268,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
URLConnection fooConnection = client.open(server.url("/").url());
|
URLConnection fooConnection = urlFactory.open(server.url("/").url());
|
||||||
fooConnection.addRequestProperty("Foo", "bar");
|
fooConnection.addRequestProperty("Foo", "bar");
|
||||||
assertEquals("B", readAscii(fooConnection));
|
assertEquals("B", readAscii(fooConnection));
|
||||||
}
|
}
|
||||||
@@ -1280,10 +1280,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URLConnection fooConnection = client.open(server.url("/").url());
|
URLConnection fooConnection = urlFactory.open(server.url("/").url());
|
||||||
fooConnection.addRequestProperty("Foo", "bar");
|
fooConnection.addRequestProperty("Foo", "bar");
|
||||||
assertEquals("A", readAscii(fooConnection));
|
assertEquals("A", readAscii(fooConnection));
|
||||||
assertEquals("B", readAscii(client.open(server.url("/").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void varyFieldsAreCaseInsensitive() throws Exception {
|
@Test public void varyFieldsAreCaseInsensitive() throws Exception {
|
||||||
@@ -1293,10 +1293,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection1 = client.open(url);
|
URLConnection connection1 = urlFactory.open(url);
|
||||||
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
URLConnection connection2 = client.open(url);
|
URLConnection connection2 = urlFactory.open(url);
|
||||||
connection2.addRequestProperty("accept-language", "fr-CA");
|
connection2.addRequestProperty("accept-language", "fr-CA");
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
}
|
}
|
||||||
@@ -1309,12 +1309,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection1 = client.open(url);
|
URLConnection connection1 = urlFactory.open(url);
|
||||||
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
connection1.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
connection1.addRequestProperty("Accept-Charset", "UTF-8");
|
connection1.addRequestProperty("Accept-Charset", "UTF-8");
|
||||||
connection1.addRequestProperty("Accept-Encoding", "identity");
|
connection1.addRequestProperty("Accept-Encoding", "identity");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
URLConnection connection2 = client.open(url);
|
URLConnection connection2 = urlFactory.open(url);
|
||||||
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
connection2.addRequestProperty("Accept-Charset", "UTF-8");
|
connection2.addRequestProperty("Accept-Charset", "UTF-8");
|
||||||
connection2.addRequestProperty("Accept-Encoding", "identity");
|
connection2.addRequestProperty("Accept-Encoding", "identity");
|
||||||
@@ -1329,12 +1329,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection frConnection = client.open(url);
|
URLConnection frConnection = urlFactory.open(url);
|
||||||
frConnection.addRequestProperty("Accept-Language", "fr-CA");
|
frConnection.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
frConnection.addRequestProperty("Accept-Charset", "UTF-8");
|
frConnection.addRequestProperty("Accept-Charset", "UTF-8");
|
||||||
frConnection.addRequestProperty("Accept-Encoding", "identity");
|
frConnection.addRequestProperty("Accept-Encoding", "identity");
|
||||||
assertEquals("A", readAscii(frConnection));
|
assertEquals("A", readAscii(frConnection));
|
||||||
URLConnection enConnection = client.open(url);
|
URLConnection enConnection = urlFactory.open(url);
|
||||||
enConnection.addRequestProperty("Accept-Language", "en-CA");
|
enConnection.addRequestProperty("Accept-Language", "en-CA");
|
||||||
enConnection.addRequestProperty("Accept-Charset", "UTF-8");
|
enConnection.addRequestProperty("Accept-Charset", "UTF-8");
|
||||||
enConnection.addRequestProperty("Accept-Encoding", "identity");
|
enConnection.addRequestProperty("Accept-Encoding", "identity");
|
||||||
@@ -1348,12 +1348,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection1 = client.open(url);
|
URLConnection connection1 = urlFactory.open(url);
|
||||||
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
||||||
connection1.addRequestProperty("Accept-Language", "en-US");
|
connection1.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(url);
|
URLConnection connection2 = urlFactory.open(url);
|
||||||
connection2.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
connection2.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
||||||
connection2.addRequestProperty("Accept-Language", "en-US");
|
connection2.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
@@ -1366,12 +1366,12 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
URLConnection connection1 = client.open(url);
|
URLConnection connection1 = urlFactory.open(url);
|
||||||
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
|
||||||
connection1.addRequestProperty("Accept-Language", "en-US");
|
connection1.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(url);
|
URLConnection connection2 = urlFactory.open(url);
|
||||||
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
connection2.addRequestProperty("Accept-Language", "fr-CA");
|
||||||
connection2.addRequestProperty("Accept-Language", "en-US");
|
connection2.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("B", readAscii(connection2));
|
assertEquals("B", readAscii(connection2));
|
||||||
@@ -1383,8 +1383,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
assertEquals("B", readAscii(client.open(server.url("/").url())));
|
assertEquals("B", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void varyAndHttps() throws Exception {
|
@Test public void varyAndHttps() throws Exception {
|
||||||
@@ -1395,13 +1395,13 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
HttpsURLConnection connection1 = (HttpsURLConnection) client.open(url);
|
HttpsURLConnection connection1 = (HttpsURLConnection) urlFactory.open(url);
|
||||||
connection1.setSSLSocketFactory(sslContext.getSocketFactory());
|
connection1.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||||
connection1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
connection1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||||
connection1.addRequestProperty("Accept-Language", "en-US");
|
connection1.addRequestProperty("Accept-Language", "en-US");
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
|
|
||||||
HttpsURLConnection connection2 = (HttpsURLConnection) client.open(url);
|
HttpsURLConnection connection2 = (HttpsURLConnection) urlFactory.open(url);
|
||||||
connection2.setSSLSocketFactory(sslContext.getSocketFactory());
|
connection2.setSSLSocketFactory(sslContext.getSocketFactory());
|
||||||
connection2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
connection2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
|
||||||
connection2.addRequestProperty("Accept-Language", "en-US");
|
connection2.addRequestProperty("Accept-Language", "en-US");
|
||||||
@@ -1410,8 +1410,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
|
|
||||||
@Test public void cachePlusCookies() throws Exception {
|
@Test public void cachePlusCookies() throws Exception {
|
||||||
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
RecordingCookieJar cookieJar = new RecordingCookieJar();
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCookieJar(cookieJar)
|
.cookieJar(cookieJar)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
@@ -1424,10 +1424,10 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
cookieJar.assertResponseCookies("a=FIRST; path=/");
|
cookieJar.assertResponseCookies("a=FIRST; path=/");
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
cookieJar.assertResponseCookies("a=SECOND; path=/");
|
cookieJar.assertResponseCookies("a=SECOND; path=/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1439,11 +1439,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD, PUT")
|
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD, PUT")
|
||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URLConnection connection1 = client.open(server.url("/").url());
|
URLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals("GET, HEAD", connection1.getHeaderField("Allow"));
|
assertEquals("GET, HEAD", connection1.getHeaderField("Allow"));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(server.url("/").url());
|
URLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals("GET, HEAD, PUT", connection2.getHeaderField("Allow"));
|
assertEquals("GET, HEAD, PUT", connection2.getHeaderField("Allow"));
|
||||||
}
|
}
|
||||||
@@ -1456,11 +1456,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none")
|
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none")
|
||||||
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URLConnection connection1 = client.open(server.url("/").url());
|
URLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals("identity", connection1.getHeaderField("Transfer-Encoding"));
|
assertEquals("identity", connection1.getHeaderField("Transfer-Encoding"));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(server.url("/").url());
|
URLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals("identity", connection2.getHeaderField("Transfer-Encoding"));
|
assertEquals("identity", connection2.getHeaderField("Transfer-Encoding"));
|
||||||
}
|
}
|
||||||
@@ -1472,11 +1472,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URLConnection connection1 = client.open(server.url("/").url());
|
URLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals("199 test danger", connection1.getHeaderField("Warning"));
|
assertEquals("199 test danger", connection1.getHeaderField("Warning"));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(server.url("/").url());
|
URLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals(null, connection2.getHeaderField("Warning"));
|
assertEquals(null, connection2.getHeaderField("Warning"));
|
||||||
}
|
}
|
||||||
@@ -1488,11 +1488,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
.setBody("A"));
|
.setBody("A"));
|
||||||
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
|
||||||
|
|
||||||
URLConnection connection1 = client.open(server.url("/").url());
|
URLConnection connection1 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals("299 test danger", connection1.getHeaderField("Warning"));
|
assertEquals("299 test danger", connection1.getHeaderField("Warning"));
|
||||||
|
|
||||||
URLConnection connection2 = client.open(server.url("/").url());
|
URLConnection connection2 = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals("299 test danger", connection2.getHeaderField("Warning"));
|
assertEquals("299 test danger", connection2.getHeaderField("Warning"));
|
||||||
}
|
}
|
||||||
@@ -1514,18 +1514,18 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
// cache miss; seed the cache
|
// cache miss; seed the cache
|
||||||
HttpURLConnection connection1 = client.open(server.url("/a").url());
|
HttpURLConnection connection1 = urlFactory.open(server.url("/a").url());
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals(null, connection1.getHeaderField("Allow"));
|
assertEquals(null, connection1.getHeaderField("Allow"));
|
||||||
|
|
||||||
// conditional cache hit; update the cache
|
// conditional cache hit; update the cache
|
||||||
HttpURLConnection connection2 = client.open(server.url("/a").url());
|
HttpURLConnection connection2 = urlFactory.open(server.url("/a").url());
|
||||||
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals("GET, HEAD", connection2.getHeaderField("Allow"));
|
assertEquals("GET, HEAD", connection2.getHeaderField("Allow"));
|
||||||
|
|
||||||
// full cache hit
|
// full cache hit
|
||||||
HttpURLConnection connection3 = client.open(server.url("/a").url());
|
HttpURLConnection connection3 = urlFactory.open(server.url("/a").url());
|
||||||
assertEquals("A", readAscii(connection3));
|
assertEquals("A", readAscii(connection3));
|
||||||
assertEquals("GET, HEAD", connection3.getHeaderField("Allow"));
|
assertEquals("GET, HEAD", connection3.getHeaderField("Allow"));
|
||||||
|
|
||||||
@@ -1537,8 +1537,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Cache-Control: max-age=30")
|
.addHeader("Cache-Control: max-age=30")
|
||||||
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
connection.addRequestProperty("Cache-Control", "only-if-cached");
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -1551,8 +1551,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Cache-Control: max-age=30")
|
.addHeader("Cache-Control: max-age=30")
|
||||||
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("B", readAscii(connection));
|
assertEquals("B", readAscii(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1562,15 +1562,15 @@ public final class UrlConnectionCacheTest {
|
|||||||
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
.addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
|
||||||
server.enqueue(new MockResponse().setResponseCode(304));
|
server.enqueue(new MockResponse().setResponseCode(304));
|
||||||
|
|
||||||
assertEquals("A", readAscii(client.open(server.url("/").url())));
|
assertEquals("A", readAscii(urlFactory.open(server.url("/").url())));
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void responseSourceHeaderFetched() throws IOException {
|
@Test public void responseSourceHeaderFetched() throws IOException {
|
||||||
server.enqueue(new MockResponse().setBody("A"));
|
server.enqueue(new MockResponse().setBody("A"));
|
||||||
|
|
||||||
URLConnection connection = client.open(server.url("/").url());
|
URLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", readAscii(connection));
|
assertEquals("A", readAscii(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1580,7 +1580,7 @@ public final class UrlConnectionCacheTest {
|
|||||||
Internal.instance.addLenient(headers, ": A");
|
Internal.instance.addLenient(headers, ": A");
|
||||||
server.enqueue(new MockResponse().setHeaders(headers.build()).setBody("body"));
|
server.enqueue(new MockResponse().setHeaders(headers.build()).setBody("body"));
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(server.url("/").url());
|
HttpURLConnection connection = urlFactory.open(server.url("/").url());
|
||||||
assertEquals("A", connection.getHeaderField(""));
|
assertEquals("A", connection.getHeaderField(""));
|
||||||
assertEquals("body", readAscii(connection));
|
assertEquals("body", readAscii(connection));
|
||||||
}
|
}
|
||||||
@@ -1636,11 +1636,11 @@ public final class UrlConnectionCacheTest {
|
|||||||
writeFile(cache.getDirectory(), urlKey + ".1", entryBody);
|
writeFile(cache.getDirectory(), urlKey + ".1", entryBody);
|
||||||
writeFile(cache.getDirectory(), "journal", journalBody);
|
writeFile(cache.getDirectory(), "journal", journalBody);
|
||||||
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
cache = new Cache(cache.getDirectory(), Integer.MAX_VALUE, fileSystem);
|
||||||
client.setClient(client.client().newBuilder()
|
urlFactory.setClient(urlFactory.client().newBuilder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
HttpURLConnection connection = client.open(url);
|
HttpURLConnection connection = urlFactory.open(url);
|
||||||
assertEquals(entryBody, readAscii(connection));
|
assertEquals(entryBody, readAscii(connection));
|
||||||
assertEquals("3", connection.getHeaderField("Content-Length"));
|
assertEquals("3", connection.getHeaderField("Content-Length"));
|
||||||
assertEquals("foo", connection.getHeaderField("etag"));
|
assertEquals("foo", connection.getHeaderField("etag"));
|
||||||
@@ -1681,8 +1681,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setBody("B"));
|
server.enqueue(new MockResponse().setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
assertEquals("B", readAscii(client.open(url)));
|
assertEquals("B", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return the request with the conditional get headers. */
|
/** @return the request with the conditional get headers. */
|
||||||
@@ -1696,21 +1696,21 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
|
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
|
||||||
|
|
||||||
URL valid = server.url("/valid").url();
|
URL valid = server.url("/valid").url();
|
||||||
HttpURLConnection connection1 = client.open(valid);
|
HttpURLConnection connection1 = urlFactory.open(valid);
|
||||||
assertEquals("A", readAscii(connection1));
|
assertEquals("A", readAscii(connection1));
|
||||||
assertEquals(HttpURLConnection.HTTP_OK, connection1.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_OK, connection1.getResponseCode());
|
||||||
assertEquals("A-OK", connection1.getResponseMessage());
|
assertEquals("A-OK", connection1.getResponseMessage());
|
||||||
HttpURLConnection connection2 = client.open(valid);
|
HttpURLConnection connection2 = urlFactory.open(valid);
|
||||||
assertEquals("A", readAscii(connection2));
|
assertEquals("A", readAscii(connection2));
|
||||||
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
|
||||||
assertEquals("A-OK", connection2.getResponseMessage());
|
assertEquals("A-OK", connection2.getResponseMessage());
|
||||||
|
|
||||||
URL invalid = server.url("/invalid").url();
|
URL invalid = server.url("/invalid").url();
|
||||||
HttpURLConnection connection3 = client.open(invalid);
|
HttpURLConnection connection3 = urlFactory.open(invalid);
|
||||||
assertEquals("B", readAscii(connection3));
|
assertEquals("B", readAscii(connection3));
|
||||||
assertEquals(HttpURLConnection.HTTP_OK, connection3.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_OK, connection3.getResponseCode());
|
||||||
assertEquals("B-OK", connection3.getResponseMessage());
|
assertEquals("B-OK", connection3.getResponseMessage());
|
||||||
HttpURLConnection connection4 = client.open(invalid);
|
HttpURLConnection connection4 = urlFactory.open(invalid);
|
||||||
assertEquals("C", readAscii(connection4));
|
assertEquals("C", readAscii(connection4));
|
||||||
assertEquals(HttpURLConnection.HTTP_OK, connection4.getResponseCode());
|
assertEquals(HttpURLConnection.HTTP_OK, connection4.getResponseCode());
|
||||||
assertEquals("C-OK", connection4.getResponseMessage());
|
assertEquals("C-OK", connection4.getResponseMessage());
|
||||||
@@ -1724,8 +1724,8 @@ public final class UrlConnectionCacheTest {
|
|||||||
server.enqueue(response.setBody("B"));
|
server.enqueue(response.setBody("B"));
|
||||||
|
|
||||||
URL url = server.url("/").url();
|
URL url = server.url("/").url();
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
assertEquals("A", readAscii(client.open(url)));
|
assertEquals("A", readAscii(urlFactory.open(url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ public final class AutobahnTester {
|
|||||||
|
|
||||||
updateReports();
|
updateReports();
|
||||||
} finally {
|
} finally {
|
||||||
client.getDispatcher().getExecutorService().shutdown();
|
client.dispatcher().getExecutorService().shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -175,8 +175,8 @@ public final class WebSocketCallTest {
|
|||||||
@Test public void wssScheme() throws IOException {
|
@Test public void wssScheme() throws IOException {
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
websocketScheme("wss");
|
websocketScheme("wss");
|
||||||
@@ -185,8 +185,8 @@ public final class WebSocketCallTest {
|
|||||||
@Test public void httpsScheme() throws IOException {
|
@Test public void httpsScheme() throws IOException {
|
||||||
server.useHttps(sslContext.getSocketFactory(), false);
|
server.useHttps(sslContext.getSocketFactory(), false);
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.setHostnameVerifier(new RecordingHostnameVerifier())
|
.hostnameVerifier(new RecordingHostnameVerifier())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
websocketScheme("https");
|
websocketScheme("https");
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public final class WebSocketCall {
|
|||||||
key = ByteString.of(nonce).base64();
|
key = ByteString.of(nonce).base64();
|
||||||
|
|
||||||
client = client.newBuilder()
|
client = client.newBuilder()
|
||||||
.setProtocols(Collections.singletonList(Protocol.HTTP_1_1))
|
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
request = request.newBuilder()
|
request = request.newBuilder()
|
||||||
@@ -81,7 +81,7 @@ public final class WebSocketCall {
|
|||||||
/**
|
/**
|
||||||
* Schedules the request to be executed at some point in the future.
|
* Schedules the request to be executed at some point in the future.
|
||||||
*
|
*
|
||||||
* <p>The {@link OkHttpClient#getDispatcher dispatcher} defines when the request will run: usually
|
* <p>The {@link OkHttpClient#dispatcher dispatcher} defines when the request will run: usually
|
||||||
* immediately unless there are several other requests currently being executed.
|
* immediately unless there are several other requests currently being executed.
|
||||||
*
|
*
|
||||||
* <p>This client will later call back {@code responseCallback} with either an HTTP response or a
|
* <p>This client will later call back {@code responseCallback} with either an HTTP response or a
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public interface Call {
|
|||||||
/**
|
/**
|
||||||
* Schedules the request to be executed at some point in the future.
|
* Schedules the request to be executed at some point in the future.
|
||||||
*
|
*
|
||||||
* <p>The {@link OkHttpClient#getDispatcher dispatcher} defines when the request will run: usually
|
* <p>The {@link OkHttpClient#dispatcher dispatcher} defines when the request will run: usually
|
||||||
* immediately unless there are several other requests currently being executed.
|
* immediately unless there are several other requests currently being executed.
|
||||||
*
|
*
|
||||||
* <p>This client will later call back {@code responseCallback} with either an HTTP response or a
|
* <p>This client will later call back {@code responseCallback} with either an HTTP response or a
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package okhttp3;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.ProxySelector;
|
import java.net.ProxySelector;
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -174,33 +173,33 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Default connect timeout (in milliseconds). */
|
/** Default connect timeout (in milliseconds). */
|
||||||
public int getConnectTimeout() {
|
public int connectTimeoutMillis() {
|
||||||
return connectTimeout;
|
return connectTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Default read timeout (in milliseconds). */
|
/** Default read timeout (in milliseconds). */
|
||||||
public int getReadTimeout() {
|
public int readTimeoutMillis() {
|
||||||
return readTimeout;
|
return readTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Default write timeout (in milliseconds). */
|
/** Default write timeout (in milliseconds). */
|
||||||
public int getWriteTimeout() {
|
public int writeTimeoutMillis() {
|
||||||
return writeTimeout;
|
return writeTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Proxy getProxy() {
|
public Proxy proxy() {
|
||||||
return proxy;
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProxySelector getProxySelector() {
|
public ProxySelector proxySelector() {
|
||||||
return proxySelector;
|
return proxySelector;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CookieJar getCookieJar() {
|
public CookieJar cookieJar() {
|
||||||
return cookieJar;
|
return cookieJar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cache getCache() {
|
public Cache cache() {
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,59 +207,59 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
return cache != null ? cache.internalCache : internalCache;
|
return cache != null ? cache.internalCache : internalCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dns getDns() {
|
public Dns dns() {
|
||||||
return dns;
|
return dns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketFactory getSocketFactory() {
|
public SocketFactory socketFactory() {
|
||||||
return socketFactory;
|
return socketFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SSLSocketFactory getSslSocketFactory() {
|
public SSLSocketFactory sslSocketFactory() {
|
||||||
return sslSocketFactory;
|
return sslSocketFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HostnameVerifier getHostnameVerifier() {
|
public HostnameVerifier hostnameVerifier() {
|
||||||
return hostnameVerifier;
|
return hostnameVerifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CertificatePinner getCertificatePinner() {
|
public CertificatePinner certificatePinner() {
|
||||||
return certificatePinner;
|
return certificatePinner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Authenticator getAuthenticator() {
|
public Authenticator authenticator() {
|
||||||
return authenticator;
|
return authenticator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Authenticator getProxyAuthenticator() {
|
public Authenticator proxyAuthenticator() {
|
||||||
return proxyAuthenticator;
|
return proxyAuthenticator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionPool getConnectionPool() {
|
public ConnectionPool connectionPool() {
|
||||||
return connectionPool;
|
return connectionPool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getFollowSslRedirects() {
|
public boolean followSslRedirects() {
|
||||||
return followSslRedirects;
|
return followSslRedirects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getFollowRedirects() {
|
public boolean followRedirects() {
|
||||||
return followRedirects;
|
return followRedirects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getRetryOnConnectionFailure() {
|
public boolean retryOnConnectionFailure() {
|
||||||
return retryOnConnectionFailure;
|
return retryOnConnectionFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dispatcher getDispatcher() {
|
public Dispatcher dispatcher() {
|
||||||
return dispatcher;
|
return dispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Protocol> getProtocols() {
|
public List<Protocol> protocols() {
|
||||||
return protocols;
|
return protocols;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ConnectionSpec> getConnectionSpecs() {
|
public List<ConnectionSpec> connectionSpecs() {
|
||||||
return connectionSpecs;
|
return connectionSpecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,7 +293,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* complete cannot be canceled.
|
* complete cannot be canceled.
|
||||||
*/
|
*/
|
||||||
public OkHttpClient cancel(Object tag) {
|
public OkHttpClient cancel(Object tag) {
|
||||||
getDispatcher().cancel(tag);
|
dispatcher().cancel(tag);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,10 +379,8 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
|
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
|
||||||
* otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to
|
* otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to
|
||||||
* milliseconds.
|
* milliseconds.
|
||||||
*
|
|
||||||
* @see URLConnection#setConnectTimeout(int)
|
|
||||||
*/
|
*/
|
||||||
public Builder setConnectTimeout(long timeout, TimeUnit unit) {
|
public Builder connectTimeout(long timeout, TimeUnit unit) {
|
||||||
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
||||||
if (unit == null) throw new IllegalArgumentException("unit == null");
|
if (unit == null) throw new IllegalArgumentException("unit == null");
|
||||||
long millis = unit.toMillis(timeout);
|
long millis = unit.toMillis(timeout);
|
||||||
@@ -396,10 +393,8 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
/**
|
/**
|
||||||
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
|
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
|
||||||
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
||||||
*
|
|
||||||
* @see URLConnection#setReadTimeout(int)
|
|
||||||
*/
|
*/
|
||||||
public Builder setReadTimeout(long timeout, TimeUnit unit) {
|
public Builder readTimeout(long timeout, TimeUnit unit) {
|
||||||
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
||||||
if (unit == null) throw new IllegalArgumentException("unit == null");
|
if (unit == null) throw new IllegalArgumentException("unit == null");
|
||||||
long millis = unit.toMillis(timeout);
|
long millis = unit.toMillis(timeout);
|
||||||
@@ -413,7 +408,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise
|
* Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise
|
||||||
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
||||||
*/
|
*/
|
||||||
public Builder setWriteTimeout(long timeout, TimeUnit unit) {
|
public Builder writeTimeout(long timeout, TimeUnit unit) {
|
||||||
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
if (timeout < 0) throw new IllegalArgumentException("timeout < 0");
|
||||||
if (unit == null) throw new IllegalArgumentException("unit == null");
|
if (unit == null) throw new IllegalArgumentException("unit == null");
|
||||||
long millis = unit.toMillis(timeout);
|
long millis = unit.toMillis(timeout);
|
||||||
@@ -425,24 +420,24 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the HTTP proxy that will be used by connections created by this client. This takes
|
* Sets the HTTP proxy that will be used by connections created by this client. This takes
|
||||||
* precedence over {@link #setProxySelector}, which is only honored when this proxy is null
|
* precedence over {@link #proxySelector}, which is only honored when this proxy is null
|
||||||
* (which it is by default). To disable proxy use completely, call {@code
|
* (which it is by default). To disable proxy use completely, call {@code
|
||||||
* setProxy(Proxy.NO_PROXY)}.
|
* setProxy(Proxy.NO_PROXY)}.
|
||||||
*/
|
*/
|
||||||
public Builder setProxy(Proxy proxy) {
|
public Builder proxy(Proxy proxy) {
|
||||||
this.proxy = proxy;
|
this.proxy = proxy;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the proxy selection policy to be used if no {@link #setProxy proxy} is specified
|
* Sets the proxy selection policy to be used if no {@link #proxy proxy} is specified
|
||||||
* explicitly. The proxy selector may return multiple proxies; in that case they will be tried
|
* explicitly. The proxy selector may return multiple proxies; in that case they will be tried
|
||||||
* in sequence until a successful connection is established.
|
* in sequence until a successful connection is established.
|
||||||
*
|
*
|
||||||
* <p>If unset, the {@link ProxySelector#getDefault() system-wide default} proxy selector will
|
* <p>If unset, the {@link ProxySelector#getDefault() system-wide default} proxy selector will
|
||||||
* be used.
|
* be used.
|
||||||
*/
|
*/
|
||||||
public Builder setProxySelector(ProxySelector proxySelector) {
|
public Builder proxySelector(ProxySelector proxySelector) {
|
||||||
this.proxySelector = proxySelector;
|
this.proxySelector = proxySelector;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -453,7 +448,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
*
|
*
|
||||||
* <p>If unset, {@linkplain CookieJar#NO_COOKIES no cookies} will be accepted nor provided.
|
* <p>If unset, {@linkplain CookieJar#NO_COOKIES no cookies} will be accepted nor provided.
|
||||||
*/
|
*/
|
||||||
public Builder setCookieJar(CookieJar cookieJar) {
|
public Builder cookieJar(CookieJar cookieJar) {
|
||||||
this.cookieJar = cookieJar;
|
this.cookieJar = cookieJar;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -464,7 +459,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
this.cache = null;
|
this.cache = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder setCache(Cache cache) {
|
public Builder cache(Cache cache) {
|
||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
this.internalCache = null;
|
this.internalCache = null;
|
||||||
return this;
|
return this;
|
||||||
@@ -475,7 +470,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
*
|
*
|
||||||
* <p>If unset, the {@link Dns#SYSTEM system-wide default} DNS will be used.
|
* <p>If unset, the {@link Dns#SYSTEM system-wide default} DNS will be used.
|
||||||
*/
|
*/
|
||||||
public Builder setDns(Dns dns) {
|
public Builder dns(Dns dns) {
|
||||||
this.dns = dns;
|
this.dns = dns;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -488,7 +483,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* <p>If unset, the {@link SocketFactory#getDefault() system-wide default} socket factory will
|
* <p>If unset, the {@link SocketFactory#getDefault() system-wide default} socket factory will
|
||||||
* be used.
|
* be used.
|
||||||
*/
|
*/
|
||||||
public Builder setSocketFactory(SocketFactory socketFactory) {
|
public Builder socketFactory(SocketFactory socketFactory) {
|
||||||
this.socketFactory = socketFactory;
|
this.socketFactory = socketFactory;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -498,7 +493,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
*
|
*
|
||||||
* <p>If unset, a lazily created SSL socket factory will be used.
|
* <p>If unset, a lazily created SSL socket factory will be used.
|
||||||
*/
|
*/
|
||||||
public Builder setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
|
public Builder sslSocketFactory(SSLSocketFactory sslSocketFactory) {
|
||||||
this.sslSocketFactory = sslSocketFactory;
|
this.sslSocketFactory = sslSocketFactory;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -509,39 +504,39 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
*
|
*
|
||||||
* <p>If unset, a default hostname verifier will be used.
|
* <p>If unset, a default hostname verifier will be used.
|
||||||
*/
|
*/
|
||||||
public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
|
public Builder hostnameVerifier(HostnameVerifier hostnameVerifier) {
|
||||||
this.hostnameVerifier = hostnameVerifier;
|
this.hostnameVerifier = hostnameVerifier;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the certificate pinner that constrains which certificates are trusted. By default HTTPS
|
* Sets the certificate pinner that constrains which certificates are trusted. By default HTTPS
|
||||||
* connections rely on only the {@link #setSslSocketFactory SSL socket factory} to establish
|
* connections rely on only the {@link #sslSocketFactory SSL socket factory} to establish
|
||||||
* trust. Pinning certificates avoids the need to trust certificate authorities.
|
* trust. Pinning certificates avoids the need to trust certificate authorities.
|
||||||
*/
|
*/
|
||||||
public Builder setCertificatePinner(CertificatePinner certificatePinner) {
|
public Builder certificatePinner(CertificatePinner certificatePinner) {
|
||||||
this.certificatePinner = certificatePinner;
|
this.certificatePinner = certificatePinner;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the authenticator used to respond to challenges from origin servers. Use {@link
|
* Sets the authenticator used to respond to challenges from origin servers. Use {@link
|
||||||
* #setProxyAuthenticator} to set the authenticator for proxy servers.
|
* #proxyAuthenticator} to set the authenticator for proxy servers.
|
||||||
*
|
*
|
||||||
* <p>If unset, the {@linkplain Authenticator#NONE no authentication will be attempted}.
|
* <p>If unset, the {@linkplain Authenticator#NONE no authentication will be attempted}.
|
||||||
*/
|
*/
|
||||||
public Builder setAuthenticator(Authenticator authenticator) {
|
public Builder authenticator(Authenticator authenticator) {
|
||||||
this.authenticator = authenticator;
|
this.authenticator = authenticator;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the authenticator used to respond to challenges from proxy servers. Use {@link
|
* Sets the authenticator used to respond to challenges from proxy servers. Use {@link
|
||||||
* #setAuthenticator} to set the authenticator for origin servers.
|
* #authenticator} to set the authenticator for origin servers.
|
||||||
*
|
*
|
||||||
* <p>If unset, the {@linkplain Authenticator#NONE no authentication will be attempted}.
|
* <p>If unset, the {@linkplain Authenticator#NONE no authentication will be attempted}.
|
||||||
*/
|
*/
|
||||||
public Builder setProxyAuthenticator(Authenticator proxyAuthenticator) {
|
public Builder proxyAuthenticator(Authenticator proxyAuthenticator) {
|
||||||
this.proxyAuthenticator = proxyAuthenticator;
|
this.proxyAuthenticator = proxyAuthenticator;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -551,7 +546,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
*
|
*
|
||||||
* <p>If unset, a new connection pool will be used.
|
* <p>If unset, a new connection pool will be used.
|
||||||
*/
|
*/
|
||||||
public Builder setConnectionPool(ConnectionPool connectionPool) {
|
public Builder connectionPool(ConnectionPool connectionPool) {
|
||||||
if (connectionPool == null) throw new NullPointerException("connectionPool == null");
|
if (connectionPool == null) throw new NullPointerException("connectionPool == null");
|
||||||
this.connectionPool = connectionPool;
|
this.connectionPool = connectionPool;
|
||||||
return this;
|
return this;
|
||||||
@@ -563,13 +558,13 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* <p>If unset, protocol redirects will be followed. This is different than the built-in {@code
|
* <p>If unset, protocol redirects will be followed. This is different than the built-in {@code
|
||||||
* HttpURLConnection}'s default.
|
* HttpURLConnection}'s default.
|
||||||
*/
|
*/
|
||||||
public Builder setFollowSslRedirects(boolean followProtocolRedirects) {
|
public Builder followSslRedirects(boolean followProtocolRedirects) {
|
||||||
this.followSslRedirects = followProtocolRedirects;
|
this.followSslRedirects = followProtocolRedirects;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Configure this client to follow redirects. If unset, redirects be followed. */
|
/** Configure this client to follow redirects. If unset, redirects be followed. */
|
||||||
public Builder setFollowRedirects(boolean followRedirects) {
|
public Builder followRedirects(boolean followRedirects) {
|
||||||
this.followRedirects = followRedirects;
|
this.followRedirects = followRedirects;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -592,7 +587,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* Set this to false to avoid retrying requests when doing so is destructive. In this case the
|
* Set this to false to avoid retrying requests when doing so is destructive. In this case the
|
||||||
* calling application should do its own recovery of connectivity failures.
|
* calling application should do its own recovery of connectivity failures.
|
||||||
*/
|
*/
|
||||||
public Builder setRetryOnConnectionFailure(boolean retryOnConnectionFailure) {
|
public Builder retryOnConnectionFailure(boolean retryOnConnectionFailure) {
|
||||||
this.retryOnConnectionFailure = retryOnConnectionFailure;
|
this.retryOnConnectionFailure = retryOnConnectionFailure;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -600,7 +595,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
/**
|
/**
|
||||||
* Sets the dispatcher used to set policy and execute asynchronous requests. Must not be null.
|
* Sets the dispatcher used to set policy and execute asynchronous requests. Must not be null.
|
||||||
*/
|
*/
|
||||||
public Builder setDispatcher(Dispatcher dispatcher) {
|
public Builder dispatcher(Dispatcher dispatcher) {
|
||||||
if (dispatcher == null) throw new IllegalArgumentException("dispatcher == null");
|
if (dispatcher == null) throw new IllegalArgumentException("dispatcher == null");
|
||||||
this.dispatcher = dispatcher;
|
this.dispatcher = dispatcher;
|
||||||
return this;
|
return this;
|
||||||
@@ -635,7 +630,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
* @param protocols the protocols to use, in order of preference. The list must contain {@link
|
* @param protocols the protocols to use, in order of preference. The list must contain {@link
|
||||||
* Protocol#HTTP_1_1}. It must not contain null or {@link Protocol#HTTP_1_0}.
|
* Protocol#HTTP_1_1}. It must not contain null or {@link Protocol#HTTP_1_0}.
|
||||||
*/
|
*/
|
||||||
public Builder setProtocols(List<Protocol> protocols) {
|
public Builder protocols(List<Protocol> protocols) {
|
||||||
protocols = Util.immutableList(protocols);
|
protocols = Util.immutableList(protocols);
|
||||||
if (!protocols.contains(Protocol.HTTP_1_1)) {
|
if (!protocols.contains(Protocol.HTTP_1_1)) {
|
||||||
throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols);
|
throw new IllegalArgumentException("protocols doesn't contain http/1.1: " + protocols);
|
||||||
@@ -650,7 +645,7 @@ public class OkHttpClient implements Cloneable, Call.Factory {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder setConnectionSpecs(List<ConnectionSpec> connectionSpecs) {
|
public Builder connectionSpecs(List<ConnectionSpec> connectionSpecs) {
|
||||||
this.connectionSpecs = Util.immutableList(connectionSpecs);
|
this.connectionSpecs = Util.immutableList(connectionSpecs);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,12 +53,12 @@ final class RealCall implements Call {
|
|||||||
executed = true;
|
executed = true;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
client.getDispatcher().executed(this);
|
client.dispatcher().executed(this);
|
||||||
Response result = getResponseWithInterceptorChain(false);
|
Response result = getResponseWithInterceptorChain(false);
|
||||||
if (result == null) throw new IOException("Canceled");
|
if (result == null) throw new IOException("Canceled");
|
||||||
return result;
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
client.getDispatcher().finished(this);
|
client.dispatcher().finished(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ final class RealCall implements Call {
|
|||||||
if (executed) throw new IllegalStateException("Already Executed");
|
if (executed) throw new IllegalStateException("Already Executed");
|
||||||
executed = true;
|
executed = true;
|
||||||
}
|
}
|
||||||
client.getDispatcher().enqueue(new AsyncCall(responseCallback, forWebSocket));
|
client.dispatcher().enqueue(new AsyncCall(responseCallback, forWebSocket));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void cancel() {
|
@Override public void cancel() {
|
||||||
@@ -141,7 +141,7 @@ final class RealCall implements Call {
|
|||||||
responseCallback.onFailure(request, e);
|
responseCallback.onFailure(request, e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
client.getDispatcher().finished(this);
|
client.dispatcher().finished(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,8 +133,8 @@ public final class Http2xStream implements HttpStream {
|
|||||||
: spdy3HeadersList(request);
|
: spdy3HeadersList(request);
|
||||||
boolean hasResponseBody = true;
|
boolean hasResponseBody = true;
|
||||||
stream = framedConnection.newStream(requestHeaders, permitsRequestBody, hasResponseBody);
|
stream = framedConnection.newStream(requestHeaders, permitsRequestBody, hasResponseBody);
|
||||||
stream.readTimeout().timeout(httpEngine.client.getReadTimeout(), TimeUnit.MILLISECONDS);
|
stream.readTimeout().timeout(httpEngine.client.readTimeoutMillis(), TimeUnit.MILLISECONDS);
|
||||||
stream.writeTimeout().timeout(httpEngine.client.getWriteTimeout(), TimeUnit.MILLISECONDS);
|
stream.writeTimeout().timeout(httpEngine.client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void writeRequestBody(RetryableSink requestBody) throws IOException {
|
@Override public void writeRequestBody(RetryableSink requestBody) throws IOException {
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ public final class HttpEngine {
|
|||||||
this.forWebSocket = forWebSocket;
|
this.forWebSocket = forWebSocket;
|
||||||
this.streamAllocation = streamAllocation != null
|
this.streamAllocation = streamAllocation != null
|
||||||
? streamAllocation
|
? streamAllocation
|
||||||
: new StreamAllocation(client.getConnectionPool(), createAddress(client, request));
|
: new StreamAllocation(client.connectionPool(), createAddress(client, request));
|
||||||
this.requestBodyOut = requestBodyOut;
|
this.requestBodyOut = requestBodyOut;
|
||||||
this.priorResponse = priorResponse;
|
this.priorResponse = priorResponse;
|
||||||
}
|
}
|
||||||
@@ -270,9 +270,9 @@ public final class HttpEngine {
|
|||||||
|
|
||||||
private HttpStream connect() throws RouteException, RequestException, IOException {
|
private HttpStream connect() throws RouteException, RequestException, IOException {
|
||||||
boolean doExtensiveHealthChecks = !networkRequest.method().equals("GET");
|
boolean doExtensiveHealthChecks = !networkRequest.method().equals("GET");
|
||||||
return streamAllocation.newStream(client.getConnectTimeout(),
|
return streamAllocation.newStream(client.connectTimeoutMillis(),
|
||||||
client.getReadTimeout(), client.getWriteTimeout(),
|
client.readTimeoutMillis(), client.writeTimeoutMillis(),
|
||||||
client.getRetryOnConnectionFailure(), doExtensiveHealthChecks);
|
client.retryOnConnectionFailure(), doExtensiveHealthChecks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Response stripBody(Response response) {
|
private static Response stripBody(Response response) {
|
||||||
@@ -338,7 +338,7 @@ public final class HttpEngine {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!client.getRetryOnConnectionFailure()) {
|
if (!client.retryOnConnectionFailure()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,7 +498,7 @@ public final class HttpEngine {
|
|||||||
result.header("Accept-Encoding", "gzip");
|
result.header("Accept-Encoding", "gzip");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Cookie> cookies = client.getCookieJar().loadForRequest(request.url());
|
List<Cookie> cookies = client.cookieJar().loadForRequest(request.url());
|
||||||
if (!cookies.isEmpty()) {
|
if (!cookies.isEmpty()) {
|
||||||
result.header("Cookie", cookieHeader(cookies));
|
result.header("Cookie", cookieHeader(cookies));
|
||||||
}
|
}
|
||||||
@@ -842,12 +842,12 @@ public final class HttpEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void receiveHeaders(Headers headers) throws IOException {
|
public void receiveHeaders(Headers headers) throws IOException {
|
||||||
if (client.getCookieJar() == CookieJar.NO_COOKIES) return;
|
if (client.cookieJar() == CookieJar.NO_COOKIES) return;
|
||||||
|
|
||||||
List<Cookie> cookies = Cookie.parseAll(userRequest.url(), headers);
|
List<Cookie> cookies = Cookie.parseAll(userRequest.url(), headers);
|
||||||
if (cookies.isEmpty()) return;
|
if (cookies.isEmpty()) return;
|
||||||
|
|
||||||
client.getCookieJar().saveFromResponse(userRequest.url(), cookies);
|
client.cookieJar().saveFromResponse(userRequest.url(), cookies);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -868,13 +868,13 @@ public final class HttpEngine {
|
|||||||
case HTTP_PROXY_AUTH:
|
case HTTP_PROXY_AUTH:
|
||||||
Proxy selectedProxy = route != null
|
Proxy selectedProxy = route != null
|
||||||
? route.proxy()
|
? route.proxy()
|
||||||
: client.getProxy();
|
: client.proxy();
|
||||||
if (selectedProxy.type() != Proxy.Type.HTTP) {
|
if (selectedProxy.type() != Proxy.Type.HTTP) {
|
||||||
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
|
throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
|
||||||
}
|
}
|
||||||
// fall-through
|
// fall-through
|
||||||
case HTTP_UNAUTHORIZED:
|
case HTTP_UNAUTHORIZED:
|
||||||
return client.getAuthenticator().authenticate(route, userResponse);
|
return client.authenticator().authenticate(route, userResponse);
|
||||||
|
|
||||||
case HTTP_PERM_REDIRECT:
|
case HTTP_PERM_REDIRECT:
|
||||||
case HTTP_TEMP_REDIRECT:
|
case HTTP_TEMP_REDIRECT:
|
||||||
@@ -889,7 +889,7 @@ public final class HttpEngine {
|
|||||||
case HTTP_MOVED_TEMP:
|
case HTTP_MOVED_TEMP:
|
||||||
case HTTP_SEE_OTHER:
|
case HTTP_SEE_OTHER:
|
||||||
// Does the client allow redirects?
|
// Does the client allow redirects?
|
||||||
if (!client.getFollowRedirects()) return null;
|
if (!client.followRedirects()) return null;
|
||||||
|
|
||||||
String location = userResponse.header("Location");
|
String location = userResponse.header("Location");
|
||||||
if (location == null) return null;
|
if (location == null) return null;
|
||||||
@@ -900,7 +900,7 @@ public final class HttpEngine {
|
|||||||
|
|
||||||
// If configured, don't follow redirects between SSL and non-SSL.
|
// If configured, don't follow redirects between SSL and non-SSL.
|
||||||
boolean sameScheme = url.scheme().equals(userRequest.url().scheme());
|
boolean sameScheme = url.scheme().equals(userRequest.url().scheme());
|
||||||
if (!sameScheme && !client.getFollowSslRedirects()) return null;
|
if (!sameScheme && !client.followSslRedirects()) return null;
|
||||||
|
|
||||||
// Redirects don't include a request body.
|
// Redirects don't include a request body.
|
||||||
Request.Builder requestBuilder = userRequest.newBuilder();
|
Request.Builder requestBuilder = userRequest.newBuilder();
|
||||||
@@ -945,14 +945,14 @@ public final class HttpEngine {
|
|||||||
HostnameVerifier hostnameVerifier = null;
|
HostnameVerifier hostnameVerifier = null;
|
||||||
CertificatePinner certificatePinner = null;
|
CertificatePinner certificatePinner = null;
|
||||||
if (request.isHttps()) {
|
if (request.isHttps()) {
|
||||||
sslSocketFactory = client.getSslSocketFactory();
|
sslSocketFactory = client.sslSocketFactory();
|
||||||
hostnameVerifier = client.getHostnameVerifier();
|
hostnameVerifier = client.hostnameVerifier();
|
||||||
certificatePinner = client.getCertificatePinner();
|
certificatePinner = client.certificatePinner();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Address(request.url().host(), request.url().port(), client.getDns(),
|
return new Address(request.url().host(), request.url().port(), client.dns(),
|
||||||
client.getSocketFactory(), sslSocketFactory, hostnameVerifier, certificatePinner,
|
client.socketFactory(), sslSocketFactory, hostnameVerifier, certificatePinner,
|
||||||
client.getProxyAuthenticator(), client.getProxy(), client.getProtocols(),
|
client.proxyAuthenticator(), client.proxy(), client.protocols(),
|
||||||
client.getConnectionSpecs(), client.getProxySelector());
|
client.connectionSpecs(), client.proxySelector());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public final class Crawler {
|
|||||||
|
|
||||||
Cache cache = new Cache(new File(args[0]), cacheByteCount);
|
Cache cache = new Cache(new File(args[0]), cacheByteCount);
|
||||||
OkHttpClient client = new OkHttpClient.Builder()
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Crawler crawler = new Crawler(client);
|
Crawler crawler = new Crawler(client);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import okhttp3.Route;
|
|||||||
|
|
||||||
public final class Authenticate {
|
public final class Authenticate {
|
||||||
private final OkHttpClient client = new OkHttpClient.Builder()
|
private final OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.setAuthenticator(new Authenticator() {
|
.authenticator(new Authenticator() {
|
||||||
@Override public Request authenticate(Route route, Response response) throws IOException {
|
@Override public Request authenticate(Route route, Response response) throws IOException {
|
||||||
System.out.println("Authenticating for response: " + response);
|
System.out.println("Authenticating for response: " + response);
|
||||||
System.out.println("Challenges: " + response.challenges());
|
System.out.println("Challenges: " + response.challenges());
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public final class CacheResponse {
|
|||||||
Cache cache = new Cache(cacheDirectory, cacheSize);
|
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||||
|
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public final class CertificatePinning {
|
|||||||
|
|
||||||
public CertificatePinning() {
|
public CertificatePinning() {
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setCertificatePinner(
|
.certificatePinner(
|
||||||
new CertificatePinner.Builder()
|
new CertificatePinner.Builder()
|
||||||
.add("publicobject.com", "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
|
.add("publicobject.com", "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
|
||||||
.add("publicobject.com", "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=")
|
.add("publicobject.com", "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=")
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ public final class ConfigureTimeouts {
|
|||||||
|
|
||||||
public ConfigureTimeouts() throws Exception {
|
public ConfigureTimeouts() throws Exception {
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setConnectTimeout(10, TimeUnit.SECONDS)
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.setWriteTimeout(10, TimeUnit.SECONDS)
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
.setReadTimeout(30, TimeUnit.SECONDS)
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public final class CustomTrust {
|
|||||||
public CustomTrust() {
|
public CustomTrust() {
|
||||||
SSLContext sslContext = sslContextForTrustedCertificates(trustedCertificatesInputStream());
|
SSLContext sslContext = sslContextForTrustedCertificates(trustedCertificatesInputStream());
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setSslSocketFactory(sslContext.getSocketFactory())
|
.sslSocketFactory(sslContext.getSocketFactory())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public final class PerCallSettings {
|
|||||||
try {
|
try {
|
||||||
// Copy to customize OkHttp for this request.
|
// Copy to customize OkHttp for this request.
|
||||||
OkHttpClient copy = client.newBuilder()
|
OkHttpClient copy = client.newBuilder()
|
||||||
.setReadTimeout(500, TimeUnit.MILLISECONDS)
|
.readTimeout(500, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = copy.newCall(request).execute();
|
Response response = copy.newCall(request).execute();
|
||||||
@@ -44,7 +44,7 @@ public final class PerCallSettings {
|
|||||||
try {
|
try {
|
||||||
// Copy to customize OkHttp for this request.
|
// Copy to customize OkHttp for this request.
|
||||||
OkHttpClient copy = client.newBuilder()
|
OkHttpClient copy = client.newBuilder()
|
||||||
.setReadTimeout(3000, TimeUnit.MILLISECONDS)
|
.readTimeout(3000, TimeUnit.MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = copy.newCall(request).execute();
|
Response response = copy.newCall(request).execute();
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public final class RewriteResponseCacheControl {
|
|||||||
cache.evictAll();
|
cache.evictAll();
|
||||||
|
|
||||||
client = new OkHttpClient.Builder()
|
client = new OkHttpClient.Builder()
|
||||||
.setCache(cache)
|
.cache(cache)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public final class WebSocketEcho implements WebSocketListener {
|
|||||||
WebSocketCall.create(client, request).enqueue(this);
|
WebSocketCall.create(client, request).enqueue(this);
|
||||||
|
|
||||||
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
|
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly.
|
||||||
client.getDispatcher().getExecutorService().shutdown();
|
client.dispatcher().getExecutorService().shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onOpen(final WebSocket webSocket, Response response) {
|
@Override public void onOpen(final WebSocket webSocket, Response response) {
|
||||||
|
|||||||
Reference in New Issue
Block a user