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