/** * Returns a shallow copy of this OkHttpClient that uses the system-wide default for * each field that hasn't been explicitly configured. */ private OkHttpClient copyWithDefaults() { OkHttpClient result = new OkHttpClient(this); result.proxy = proxy; result.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault(); result.cookieHandler = cookieHandler != null ? cookieHandler : CookieHandler.getDefault(); result.responseCache = responseCache != null ? responseCache : ResponseCache.getDefault(); result.sslSocketFactory = sslSocketFactory != null ? sslSocketFactory : HttpsURLConnection.getDefaultSSLSocketFactory(); result.hostnameVerifier = hostnameVerifier != null ? hostnameVerifier : OkHostnameVerifier.INSTANCE; result.authenticator = authenticator != null ? authenticator : HttpAuthenticator.SYSTEM_DEFAULT; result.connectionPool = connectionPool != null ? connectionPool : ConnectionPool.getDefault(); result.followProtocolRedirects = followProtocolRedirects; result.transports = transports != null ? transports : DEFAULT_TRANSPORTS; result.connectTimeout = connectTimeout; result.readTimeout = readTimeout; return result; }
@Test public void get_httpGet() throws Exception { final URL serverUrl = configureServer(new MockResponse()); assertEquals("http", serverUrl.getProtocol()); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheResponse get( URI uri, String method, Map<String, List<String>> headers) throws IOException { try { assertEquals(toUri(serverUrl), uri); assertEquals("GET", method); assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent")); assertEquals(Collections.singletonList("value1"), headers.get("key1")); return null; } catch (Throwable t) { throw new IOException("unexpected cache failure", t); } } }; setInternalCache(new CacheAdapter(responseCache)); connection = new OkUrlFactory(client).open(serverUrl); connection.setRequestProperty("key1", "value1"); executeGet(connection); }
@Test public void otherStacks_cacheHitWithoutVary() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .setBody("A")); server.enqueue(new MockResponse() .setBody("FAIL")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof OkHttpURLConnection); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof OkHttpURLConnection); assertEquals("A", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: Accept-Encoding") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof OkHttpURLConnection); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof OkHttpURLConnection); assertEquals("B", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVary() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: Accept-Language") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof OkHttpURLConnection); connection.setRequestProperty("Accept-Language", "en-US"); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof OkHttpURLConnection); assertEquals("B", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVaryAsterisk() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: *") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof OkHttpURLConnection); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof OkHttpURLConnection); assertEquals("B", readAscii(connection2)); }
@Test public void get_httpGet() throws Exception { final URL serverUrl = configureServer(new MockResponse()); assertEquals("http", serverUrl.getProtocol()); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheResponse get( URI uri, String method, Map<String, List<String>> headers) throws IOException { assertEquals(toUri(serverUrl), uri); assertEquals("GET", method); assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent")); assertEquals(Collections.singletonList("value1"), headers.get("key1")); return null; } }; setInternalCache(new CacheAdapter(responseCache)); connection = new OkUrlFactory(client).open(serverUrl); connection.setRequestProperty("key1", "value1"); executeGet(connection); }
@Test public void get_httpsGet() throws Exception { final URL serverUrl = configureHttpsServer(new MockResponse()); assertEquals("https", serverUrl.getProtocol()); ResponseCache responseCache = new AbstractResponseCache() { @Override public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException { assertEquals("https", uri.getScheme()); assertEquals(toUri(serverUrl), uri); assertEquals("GET", method); assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent")); assertEquals(Collections.singletonList("value1"), headers.get("key1")); return null; } }; setInternalCache(new CacheAdapter(responseCache)); client = client.newBuilder() .sslSocketFactory(sslContext.getSocketFactory()) .hostnameVerifier(hostnameVerifier) .build(); connection = new OkUrlFactory(client).open(serverUrl); connection.setRequestProperty("key1", "value1"); executeGet(connection); }
@Test public void otherStacks_cacheHitWithoutVary() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .setBody("A")); server.enqueue(new MockResponse() .setBody("FAIL")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof HttpURLConnectionImpl); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof HttpURLConnectionImpl); assertEquals("A", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: Accept-Encoding") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof HttpURLConnectionImpl); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof HttpURLConnectionImpl); assertEquals("B", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVary() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: Accept-Language") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof HttpURLConnectionImpl); connection.setRequestProperty("Accept-Language", "en-US"); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof HttpURLConnectionImpl); assertEquals("B", readAscii(connection2)); }
@Test public void otherStacks_cacheMissWithVaryAsterisk() throws Exception { server.enqueue(new MockResponse() .addHeader("Cache-Control: max-age=60") .addHeader("Vary: *") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); // Set the cache as the shared cache. ResponseCache.setDefault(cache); // Use the platform's HTTP stack. URLConnection connection = server.url("/").url().openConnection(); assertFalse(connection instanceof HttpURLConnectionImpl); assertEquals("A", readAscii(connection)); URLConnection connection2 = server.url("/").url().openConnection(); assertFalse(connection2 instanceof HttpURLConnectionImpl); assertEquals("B", readAscii(connection2)); }