13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/* 23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc. 33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License"); 53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License. 63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at 73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * http://www.apache.org/licenses/LICENSE-2.0 93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * 103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software 113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS, 123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and 143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License. 153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage com.squareup.okhttp.internal.http; 173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.OkHttpClient; 193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.internal.SslContextBuilder; 203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.mockwebserver.MockResponse; 213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport com.squareup.okhttp.mockwebserver.MockWebServer; 223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport org.junit.After; 243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport org.junit.Before; 253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport org.junit.Test; 263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException; 283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.CacheRequest; 293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.CacheResponse; 303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.HttpURLConnection; 313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.ResponseCache; 323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URI; 333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URISyntaxException; 343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URL; 353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URLConnection; 363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.Collections; 373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.List; 383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.util.Map; 393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport javax.net.ssl.HostnameVerifier; 413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport javax.net.ssl.HttpsURLConnection; 423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport javax.net.ssl.SSLContext; 433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport javax.net.ssl.SSLSession; 443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertArrayEquals; 463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertEquals; 473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertFalse; 483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertNotNull; 493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertNull; 503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.assertTrue; 513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport static org.junit.Assert.fail; 523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/** 544944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * A white-box test for {@link ResponseCacheAdapter}. See also: 554944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * <ul> 564944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * <li>{@link ResponseCacheTest} for black-box tests that check that {@link ResponseCache} 574944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * classes are called correctly by OkHttp.</li> 584944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * <li>{@link JavaApiConverterTest} for tests that check Java API classes / OkHttp conversion 594944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * logic. </li> 604944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller * </ul> 613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */ 623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic class ResponseCacheAdapterTest { 633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private static final SSLContext sslContext = SslContextBuilder.localhost(); 653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private static final HostnameVerifier NULL_HOSTNAME_VERIFIER = new HostnameVerifier() { 663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public boolean verify(String hostname, SSLSession session) { 673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return true; 683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private MockWebServer server; 723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 733c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private OkHttpClient client; 743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private HttpURLConnection connection; 763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 774944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Before public void setUp() throws Exception { 783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server = new MockWebServer(); 793c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client = new OkHttpClient(); 803c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 813c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 824944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @After public void tearDown() throws Exception { 833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller if (connection != null) { 843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.disconnect(); 853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 863c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.shutdown(); 873c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 883c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 894944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Test public void get_httpGet() throws Exception { 903c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller final URL serverUrl = configureServer(new MockResponse()); 913c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals("http", serverUrl.getProtocol()); 923c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ResponseCache responseCache = new NoOpResponseCache() { 943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException { 963c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(toUri(serverUrl), uri); 973c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals("GET", method); 983c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent")); 993c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(Collections.singletonList("value1"), headers.get("key1")); 1003c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 1013c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1023c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 1033c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setResponseCache(responseCache); 1043c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1053c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection = client.open(serverUrl); 1063c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.setRequestProperty("key1", "value1"); 1073c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1083c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller executeGet(connection); 1093c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1114944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Test public void get_httpsGet() throws Exception { 1124944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller final URL serverUrl = configureHttpsServer(new MockResponse()); 1134944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("https", serverUrl.getProtocol()); 1143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ResponseCache responseCache = new NoOpResponseCache() { 1163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 1174944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) 1184944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller throws IOException { 1194944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("https", uri.getScheme()); 1203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(toUri(serverUrl), uri); 1214944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("GET", method); 1224944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent")); 1234944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(Collections.singletonList("value1"), headers.get("key1")); 1243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 1253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 1273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setResponseCache(responseCache); 1284944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller client.setSslSocketFactory(sslContext.getSocketFactory()); 1294944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER); 1303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection = client.open(serverUrl); 1324944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller connection.setRequestProperty("key1", "value1"); 1333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller executeGet(connection); 1353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1374944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Test public void put_httpGet() throws Exception { 1383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller final String statusLine = "HTTP/1.1 200 Fantastic"; 1393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller final URL serverUrl = configureServer( 1403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller new MockResponse() 1413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller .setStatus(statusLine) 1424944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller .addHeader("A", "c")); 1433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ResponseCache responseCache = new NoOpResponseCache() { 1453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 1463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { 1474944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertTrue(urlConnection instanceof HttpURLConnection); 1484944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertFalse(urlConnection instanceof HttpsURLConnection); 1494944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller 1504944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(0, urlConnection.getContentLength()); 1514944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller 1523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; 1534944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("GET", httpUrlConnection.getRequestMethod()); 1544944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertTrue(httpUrlConnection.getDoInput()); 1554944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertFalse(httpUrlConnection.getDoOutput()); 1564944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller 1573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals("Fantastic", httpUrlConnection.getResponseMessage()); 1584944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(toUri(serverUrl), uri); 1594944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(serverUrl, urlConnection.getURL()); 1604944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("value", urlConnection.getRequestProperty("key")); 1613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // Check retrieval by string key. 1633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(statusLine, httpUrlConnection.getHeaderField(null)); 1644944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("c", httpUrlConnection.getHeaderField("A")); 1653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller // The RI and OkHttp supports case-insensitive matching for this method. 1664944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("c", httpUrlConnection.getHeaderField("a")); 1673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 1683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 1703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setResponseCache(responseCache); 1713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection = client.open(serverUrl); 1734944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller connection.setRequestProperty("key", "value"); 1743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller executeGet(connection); 1753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 1763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1774944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Test public void put_httpPost() throws Exception { 1784944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller final String statusLine = "HTTP/1.1 200 Fantastic"; 1794944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller final URL serverUrl = configureServer( 1804944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller new MockResponse() 1814944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller .setStatus(statusLine) 1824944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller .addHeader("A", "c")); 1833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ResponseCache responseCache = new NoOpResponseCache() { 1853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 1863c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { 1874944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertTrue(urlConnection instanceof HttpURLConnection); 1884944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertFalse(urlConnection instanceof HttpsURLConnection); 1893c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1904944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(0, urlConnection.getContentLength()); 1913c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1923c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection; 1933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals("POST", httpUrlConnection.getRequestMethod()); 1943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertTrue(httpUrlConnection.getDoInput()); 1953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertTrue(httpUrlConnection.getDoOutput()); 1963c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 1974944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("Fantastic", httpUrlConnection.getResponseMessage()); 1983c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(toUri(serverUrl), uri); 1994944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(serverUrl, urlConnection.getURL()); 2004944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("value", urlConnection.getRequestProperty("key")); 2014944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller 2024944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller // Check retrieval by string key. 2034944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals(statusLine, httpUrlConnection.getHeaderField(null)); 2044944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("c", httpUrlConnection.getHeaderField("A")); 2054944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller // The RI and OkHttp supports case-insensitive matching for this method. 2064944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller assertEquals("c", httpUrlConnection.getHeaderField("a")); 2073c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 2083c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2093c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 2103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setResponseCache(responseCache); 2113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection = client.open(serverUrl); 2133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2144944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller executePost(connection); 2153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2174944713f5c5b141966ac82973d6a31a634e8e01eNeil Fuller @Test public void put_httpsGet() throws Exception { 2183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller final URL serverUrl = configureHttpsServer(new MockResponse()); 2193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals("https", serverUrl.getProtocol()); 2203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller ResponseCache responseCache = new NoOpResponseCache() { 2223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 2233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { 2243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertTrue(urlConnection instanceof HttpsURLConnection); 2253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(toUri(serverUrl), uri); 2263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(serverUrl, urlConnection.getURL()); 2273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) urlConnection; 2293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) connection; 2303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(realHttpsUrlConnection.getCipherSuite(), 2313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller cacheHttpsUrlConnection.getCipherSuite()); 2323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(realHttpsUrlConnection.getPeerPrincipal(), 2333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller cacheHttpsUrlConnection.getPeerPrincipal()); 2343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), 2353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller cacheHttpsUrlConnection.getLocalCertificates()); 2363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), 2373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller cacheHttpsUrlConnection.getServerCertificates()); 2383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller assertEquals(realHttpsUrlConnection.getLocalPrincipal(), 2393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller cacheHttpsUrlConnection.getLocalPrincipal()); 2403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 2413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller }; 2433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setResponseCache(responseCache); 2443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setSslSocketFactory(sslContext.getSocketFactory()); 2453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller client.setHostnameVerifier(NULL_HOSTNAME_VERIFIER); 2463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection = client.open(serverUrl); 2483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller executeGet(connection); 2493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private void executeGet(HttpURLConnection connection) throws IOException { 2523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.connect(); 2533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.getHeaderFields(); 2543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.disconnect(); 2553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private void executePost(HttpURLConnection connection) throws IOException { 2583c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.setDoOutput(true); 2593c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.connect(); 2603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.getOutputStream().write("Hello World".getBytes()); 2613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller connection.disconnect(); 2623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private URL configureServer(MockResponse mockResponse) throws Exception { 2653c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.enqueue(mockResponse); 2663c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.play(); 2673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return server.getUrl("/"); 2683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private URL configureHttpsServer(MockResponse mockResponse) throws Exception { 2713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.useHttps(sslContext.getSocketFactory(), false /* tunnelProxy */); 2723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.enqueue(mockResponse); 2733c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller server.play(); 2743c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return server.getUrl("/"); 2753c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2763c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2773c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private static class NoOpResponseCache extends ResponseCache { 2783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2793c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 2803c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheResponse get(URI uri, String s, Map<String, List<String>> stringListMap) 2813c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller throws IOException { 2823c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 2833c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller @Override 2863c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller public CacheRequest put(URI uri, URLConnection urlConnection) throws IOException { 2873c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 2883c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2893c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2903c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 2913c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller private static URI toUri(URL serverUrl) { 2923c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller try { 2933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return serverUrl.toURI(); 2943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } catch (URISyntaxException e) { 2953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller fail(e.getMessage()); 2963c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return null; 2973c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2983c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 2993c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller} 300