URLConnectionTest.java revision 8ac847a52e72f0cefbb20a6850ae04468d433a9e
1e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes/*
2e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
4e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * you may not use this file except in compliance with the License.
6e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * You may obtain a copy of the License at
7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
8e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
10e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * See the License for the specific language governing permissions and
14e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * limitations under the License.
15e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes */
16e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes
174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.net;
18e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes
1951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.io.BufferedReader;
20c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilsonimport java.io.ByteArrayOutputStream;
216247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.io.IOException;
2251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.io.InputStream;
23e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughesimport java.io.InputStreamReader;
2402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.io.OutputStream;
254557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.Authenticator;
264557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheRequest;
274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheResponse;
284557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpRetryException;
294557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpURLConnection;
3000feece22909b7dc79fc96d666d157390b93858eJesse Wilsonimport java.net.InetAddress;
314557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.PasswordAuthentication;
324557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.ResponseCache;
3337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilsonimport java.net.SecureCacheResponse;
344557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.SocketTimeoutException;
354557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URI;
364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URISyntaxException;
374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URL;
384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URLConnection;
3937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilsonimport java.security.Principal;
4037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilsonimport java.security.cert.Certificate;
41c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.CertificateException;
42c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.X509Certificate;
4351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.ArrayList;
4402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.util.Arrays;
4551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Collections;
4683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.HashSet;
4751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Iterator;
486247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.util.List;
4983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.Map;
5051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Set;
5183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.concurrent.atomic.AtomicReference;
52deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPInputStream;
53deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPOutputStream;
5460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HostnameVerifier;
5560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HttpsURLConnection;
56c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLContext;
57096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilsonimport javax.net.ssl.SSLException;
5860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.SSLSession;
59c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLSocketFactory;
60c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.TrustManager;
61c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.X509TrustManager;
6250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilsonimport libcore.javax.net.ssl.TestSSLContext;
6351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport tests.http.DefaultResponseCache;
6460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.MockResponse;
6560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.MockWebServer;
6660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.RecordedRequest;
67e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes
68e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughespublic class URLConnectionTest extends junit.framework.TestCase {
69b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
70ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private static final Authenticator SIMPLE_AUTHENTICATOR = new Authenticator() {
71ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        protected PasswordAuthentication getPasswordAuthentication() {
72ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            return new PasswordAuthentication("username", "password".toCharArray());
73ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
74ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    };
75ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
7651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private MockWebServer server = new MockWebServer();
7700feece22909b7dc79fc96d666d157390b93858eJesse Wilson    private String hostname;
7800feece22909b7dc79fc96d666d157390b93858eJesse Wilson
7900feece22909b7dc79fc96d666d157390b93858eJesse Wilson    @Override protected void setUp() throws Exception {
8000feece22909b7dc79fc96d666d157390b93858eJesse Wilson        super.setUp();
8100feece22909b7dc79fc96d666d157390b93858eJesse Wilson        hostname = InetAddress.getLocalHost().getHostName();
8200feece22909b7dc79fc96d666d157390b93858eJesse Wilson    }
8351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
8451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    @Override protected void tearDown() throws Exception {
8551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(null);
86ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(null);
87984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("proxyHost");
88984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("proxyPort");
89984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("http.proxyHost");
90984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("http.proxyPort");
91984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("https.proxyHost");
92984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("https.proxyPort");
9351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.shutdown();
9400feece22909b7dc79fc96d666d157390b93858eJesse Wilson        super.tearDown();
9551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
9651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
9783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    public void testRequestHeaders() throws IOException, InterruptedException {
9883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.enqueue(new MockResponse());
9983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.play();
10083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
10183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
10283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("D", "e");
10383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("D", "f");
10483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties();
10583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D")));
10683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
10783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            requestHeaders.put("G", Arrays.asList("h"));
10883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
10983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
11083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
11183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
11283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            requestHeaders.get("D").add("i");
11383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
11483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
11583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
11683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
11783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.setRequestProperty(null, "j");
11883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail();
11983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (NullPointerException expected) {
12083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
12183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
12283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.addRequestProperty(null, "k");
12383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail();
12483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (NullPointerException expected) {
12583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
12683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.setRequestProperty("NullValue", null); // should fail silently!
12783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("AnotherNullValue", null);  // should fail silently!
12883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
12983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.getResponseCode();
13083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        RecordedRequest request = server.takeRequest();
13183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContains(request.getHeaders(), "D: e");
13283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContains(request.getHeaders(), "D: f");
13383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "NullValue.*");
13483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*");
13583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "G:.*");
13683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "null:.*");
13783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
13883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
13983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.addRequestProperty("N", "o");
14083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Set header after connect");
14183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (IllegalStateException expected) {
14283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
14383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
14483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.setRequestProperty("P", "q");
14583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Set header after connect");
14683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (IllegalStateException expected) {
14783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
14883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
14983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
15083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    public void testResponseHeaders() throws IOException, InterruptedException {
15183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.enqueue(new MockResponse()
15283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .setStatus("HTTP/1.0 200 Fantastic")
15383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .addHeader("A: b")
15483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .addHeader("A: c")
15583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8));
15683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.play();
15783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
15883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
15983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(200, urlConnection.getResponseCode());
16083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals("Fantastic", urlConnection.getResponseMessage());
16183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        Map<String, List<String>> responseHeaders = urlConnection.getHeaderFields();
1628ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(Arrays.asList("HTTP/1.0 200 Fantastic"), responseHeaders.get(null));
16383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(newSet("b", "c"), new HashSet<String>(responseHeaders.get("A")));
16483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
16583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            responseHeaders.put("N", Arrays.asList("o"));
16683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
16783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
16883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
16983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
17083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            responseHeaders.get("A").add("d");
17183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
17283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
17383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
17483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
17583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
176e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // Check that if we don't read to the end of a response, the next request on the
177e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // recycled connection doesn't get the unread tail of the first request's response.
178e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // http://code.google.com/p/android/issues/detail?id=2939
179e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    public void test_2939() throws Exception {
180b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        MockResponse response = new MockResponse().setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8);
181b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
182b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
183b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
184b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
185b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
186c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDE", server.getUrl("/").openConnection(), 5);
187c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDE", server.getUrl("/").openConnection(), 5);
1888baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson    }
1898baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson
190977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    // Check that we recognize a few basic mime types by extension.
191977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    // http://code.google.com/p/android/issues/detail?id=10100
192977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    public void test_10100() throws Exception {
193977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes        assertEquals("image/jpeg", URLConnection.guessContentTypeFromName("someFile.jpg"));
194977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes        assertEquals("application/pdf", URLConnection.guessContentTypeFromName("stuff.pdf"));
195977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    }
196977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes
1978baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson    public void testConnectionsArePooled() throws Exception {
198b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        MockResponse response = new MockResponse().setBody("ABCDEFGHIJKLMNOPQR");
199b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
200b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
201b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
202b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
203b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
204b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
205c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
206c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
207c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
208c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
209c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
210c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(2, server.takeRequest().getSequenceNumber());
211c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
212c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
213c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testChunkedConnectionsArePooled() throws Exception {
214c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        MockResponse response = new MockResponse().setChunkedBody("ABCDEFGHIJKLMNOPQR", 5);
215c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
216c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
217c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
218c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
219c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
220c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
221c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
222b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
223c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
224b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
225c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/").openConnection());
226b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(2, server.takeRequest().getSequenceNumber());
227e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    }
22802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
229b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson    enum WriteKind { BYTE_BY_BYTE, SMALL_BUFFERS, LARGE_BUFFERS }
23002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
23102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_byteByByte() throws Exception {
23251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.BYTE_BY_BYTE);
23302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
23402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
23502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_smallBuffers() throws Exception {
23651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.SMALL_BUFFERS);
23702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
23802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
23902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_largeBuffers() throws Exception {
24051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.LARGE_BUFFERS);
24102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
24202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
24302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_byteByByte() throws Exception {
24451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.BYTE_BY_BYTE);
24502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
24602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
24702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_smallBuffers() throws Exception {
24851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.SMALL_BUFFERS);
24902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
25002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
25102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_largeBuffers() throws Exception {
25251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.LARGE_BUFFERS);
25302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
25402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
25551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception {
25602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        int n = 512*1024;
257b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.setBodyLimit(0);
258b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(new MockResponse());
259b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
260b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
261b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) server.getUrl("/").openConnection();
26202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        conn.setDoOutput(true);
26302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        conn.setRequestMethod("POST");
26451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        if (uploadKind == TransferKind.CHUNKED) {
26502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            conn.setChunkedStreamingMode(-1);
26602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        } else {
26702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            conn.setFixedLengthStreamingMode(n);
26802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        }
26902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        OutputStream out = conn.getOutputStream();
27002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        if (writeKind == WriteKind.BYTE_BY_BYTE) {
27102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            for (int i = 0; i < n; ++i) {
27202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes                out.write('x');
27302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            }
27402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        } else {
27502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64*1024];
27602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            Arrays.fill(buf, (byte) 'x');
27702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            for (int i = 0; i < n; i += buf.length) {
27802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes                out.write(buf, 0, Math.min(buf.length, n - i));
27902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            }
28002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        }
28102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        out.close();
2824cb7f05dc68abb23ae54a5891c369062185f2210Elliott Hughes        assertEquals(200, conn.getResponseCode());
283b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        RecordedRequest request = server.takeRequest();
284b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(n, request.getBodySize());
28551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        if (uploadKind == TransferKind.CHUNKED) {
286b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson            assertTrue(request.getChunkSizes().size() > 0);
287b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        } else {
288b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson            assertTrue(request.getChunkSizes().isEmpty());
289b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        }
29002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
2916247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
29251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
29351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * Test that response caching is consistent with the RI and the spec.
29451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
29551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
2966247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes    public void test_responseCaching() throws Exception {
2976247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        // Test each documented HTTP/1.1 code, plus the first unused value in each range.
2986247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
2996247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
3006247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        // We can't test 100 because it's not really a response.
3016247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        // assertCached(false, 100);
3026247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 101);
3036247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 102);
3046247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(true,  200);
3056247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 201);
3066247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 202);
3076247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(true,  203);
3086247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 204);
3096247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 205);
3106247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(true,  206);
3116247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 207);
31251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // (See test_responseCaching_300.)
3136247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(true,  301);
3146247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        for (int i = 302; i <= 308; ++i) {
3156247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes            assertCached(false, i);
3166247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        }
3176247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        for (int i = 400; i <= 406; ++i) {
3186247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes            assertCached(false, i);
3196247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        }
3206247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        // (See test_responseCaching_407.)
3216247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 408);
3226247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertCached(false, 409);
32351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // (See test_responseCaching_410.)
3246247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        for (int i = 411; i <= 418; ++i) {
3256247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes            assertCached(false, i);
3266247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        }
3276247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        for (int i = 500; i <= 506; ++i) {
3286247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes            assertCached(false, i);
3296247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        }
3306247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes    }
3316247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
33251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void test_responseCaching_300() throws Exception {
33351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // TODO: fix this for android
33451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertCached(false, 300);
33551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
33651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
3371f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson    /**
3381f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson     * Response code 407 should only come from proxy servers. Android's client
3391f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson     * throws if it is sent by an origin server.
3401f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson     */
3411f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson    public void testOriginServerSends407() throws Exception {
3421f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        server.enqueue(new MockResponse().setResponseCode(407));
3431f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        server.play();
3441f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson
3451f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        URL url = server.getUrl("/");
3461f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3471f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        try {
3481f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson            conn.getResponseCode();
3491f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson            fail();
3501f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        } catch (IOException expected) {
3511f8243e3d2b5a3f8e0398c304d1dea0395cbc368Jesse Wilson        }
3526247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes    }
3536247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
35451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void test_responseCaching_410() throws Exception {
35551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // the HTTP spec permits caching 410s, but the RI doesn't.
35651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertCached(false, 410);
35751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
35851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
3596247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes    private void assertCached(boolean shouldPut, int responseCode) throws Exception {
36051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server = new MockWebServer();
361b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(new MockResponse()
362b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson                .setResponseCode(responseCode)
36351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .setBody("ABCDE")
364b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson                .addHeader("WWW-Authenticate: challenge"));
365b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
366b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
36751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        DefaultResponseCache responseCache = new DefaultResponseCache();
36851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(responseCache);
36951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        URL url = server.getUrl("/");
37051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3716247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes        assertEquals(responseCode, conn.getResponseCode());
3726247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
37351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // exhaust the content stream
37451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        try {
37551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            // TODO: remove special case once testUnauthorizedResponseHandling() is fixed
37651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            if (responseCode != 401) {
37751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                readAscii(conn.getInputStream(), Integer.MAX_VALUE);
37851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
37951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        } catch (IOException ignored) {
38051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
38151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
38251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        Set<URI> expectedCachedUris = shouldPut
38351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                ? Collections.singleton(url.toURI())
38451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                : Collections.<URI>emptySet();
38551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(Integer.toString(responseCode),
38651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                expectedCachedUris, responseCache.getContents().keySet());
38751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
3886247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes    }
38960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
39060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    public void testConnectViaHttps() throws IOException, InterruptedException {
39160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
39260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
393059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
394c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
39560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        server.play();
39660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
397096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
3984559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
39960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
400c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via HTTPS", connection);
40160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
40260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        RecordedRequest request = server.takeRequest();
40360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
40460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
40560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
406096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson    public void testConnectViaHttpsReusingConnections() throws IOException, InterruptedException {
407096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
408096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
409059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
410096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
411096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.enqueue(new MockResponse().setBody("another response via HTTPS"));
412096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.play();
413096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
414b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
415b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
416b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertContent("this response comes via HTTPS", connection);
417b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
418b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection();
419b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
420b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertContent("another response via HTTPS", connection);
421b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
422b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
423b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
424b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson    }
425b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
4268116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson    public void testConnectViaHttpsReusingConnectionsDifferentFactories()
427b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson            throws IOException, InterruptedException {
428b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
429b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
430b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
431b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
432b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.enqueue(new MockResponse().setBody("another response via HTTPS"));
433b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.play();
434b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
435096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        // install a custom SSL socket factory so the server can be authorized
436096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
437059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
438096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        assertContent("this response comes via HTTPS", connection);
439096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
440096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection();
441096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        try {
442096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
443b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson            fail("without an SSL socket factory, the connection should fail");
444096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        } catch (SSLException expected) {
445096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        }
446096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson    }
447096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
4484559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom    public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
4494559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        TestSSLContext testSSLContext = TestSSLContext.create();
4504559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
4514559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
4524559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.enqueue(new MockResponse().setDisconnectAtStart(true));
4534559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.enqueue(new MockResponse().setBody("this response comes via SSL"));
4544559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.play();
4554559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
4564559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
4574559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
4584559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
4594559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        assertContent("this response comes via SSL", connection);
4604559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
4614559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        RecordedRequest request = server.takeRequest();
4624559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
4634559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom    }
4644559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
465984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingProxyArg() throws Exception {
466984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.CREATE_ARG);
467984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
468984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
469984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingProxySystemProperty() throws Exception {
470984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY);
471984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
472984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
473984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingHttpProxySystemProperty() throws Exception {
474984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY);
475984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
476984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
477984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
478c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockResponse mockResponse = new MockResponse().setBody("this response comes via a proxy");
47951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(mockResponse);
48051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
48160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
482984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        URL url = new URL("http://android.com/foo");
483984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HttpURLConnection connection = proxyConfig.connect(server, url);
484c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via a proxy", connection);
48560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
48651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest request = server.takeRequest();
48760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET http://android.com/foo HTTP/1.1", request.getRequestLine());
48860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(request.getHeaders(), "Host: android.com");
48960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
49060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
491c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testContentDisagreesWithContentLengthHeader() throws IOException {
492c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(new MockResponse()
493c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .setBody("abc\r\nYOU SHOULD NOT SEE THIS")
494c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .clearHeaders()
495c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .addHeader("Content-Length: 3"));
496c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
497c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
498c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("abc", server.getUrl("/").openConnection());
499c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
500c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
501c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testContentDisagreesWithChunkedHeader() throws IOException {
502c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        MockResponse mockResponse = new MockResponse();
503c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.setChunkedBody("abc", 3);
504c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
505c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        bytesOut.write(mockResponse.getBody());
506c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        bytesOut.write("\r\nYOU SHOULD NOT SEE THIS".getBytes());
507c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.setBody(bytesOut.toByteArray());
508c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.clearHeaders();
509c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.addHeader("Transfer-encoding: chunked");
510c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
511c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(mockResponse);
512c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
513c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
514c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("abc", server.getUrl("/").openConnection());
515c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
516c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
517984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingProxyArg() throws Exception {
518984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.CREATE_ARG);
519984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
520984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
521984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    /**
522984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * We weren't honoring all of the appropriate proxy system properties when
523984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * connecting via HTTPS. http://b/3097518
524984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     */
525984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingProxySystemProperty() throws Exception {
526984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY);
527984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
528984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
529984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception {
530984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY);
531984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
532984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
533984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty() throws Exception {
534984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY);
535984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
536984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
537984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    /**
538984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * We were verifying the wrong hostname when connecting to an HTTPS site
539984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * through a proxy. http://b/3097277
540984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     */
541984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
54260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
543984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
54460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
545059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
546c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().clearHeaders()); // for CONNECT
547c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
54851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
54960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
55060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        URL url = new URL("https://android.com/foo");
551984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
552059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
553984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        connection.setHostnameVerifier(hostnameVerifier);
55460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
555c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via a secure proxy", connection);
55660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
55751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest connect = server.takeRequest();
55860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("Connect line failure on proxy",
55960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson                "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
56060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(connect.getHeaders(), "Host: android.com");
56160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
56251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest get = server.takeRequest();
56360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
56460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(get.getHeaders(), "Host: android.com");
565984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
56660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
56760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
568d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    /**
569d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson     * Test which headers are sent unencrypted to the HTTP proxy.
570d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson     */
571d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    public void testProxyConnectIncludesProxyHeadersOnly()
572d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            throws IOException, InterruptedException {
573984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
574d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
575d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
576d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
577d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.enqueue(new MockResponse().clearHeaders()); // for CONNECT
578d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
579d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.play();
580d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
581d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        URL url = new URL("https://android.com/foo");
582d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(
583d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson                server.toProxyAddress());
584d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("Private", "Secret");
585d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("Proxy-Authorization", "bar");
586d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("User-Agent", "baz");
587d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
588984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        connection.setHostnameVerifier(hostnameVerifier);
589d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContent("encrypted response from the origin server", connection);
590d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
591d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        RecordedRequest connect = server.takeRequest();
592d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContainsNoneMatching(connect.getHeaders(), "Private.*");
593d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Proxy-Authorization: bar");
594d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "User-Agent: baz");
595d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Host: android.com");
596d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive");
597d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
598d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        RecordedRequest get = server.takeRequest();
599d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(get.getHeaders(), "Private: Secret");
600984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
601d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    }
602d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
603d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    public void testDisconnectedConnection() throws IOException {
604d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.enqueue(new MockResponse().setBody("ABCDEFGHIJKLMNOPQR"));
605d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.play();
606d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
607d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
608d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        InputStream in = connection.getInputStream();
609d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertEquals('A', (char) in.read());
610d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.disconnect();
611d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        try {
612d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            in.read();
613d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            fail("Expected a connection closed exception");
614d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        } catch (IOException expected) {
615d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        }
616d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    }
617d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
61851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testResponseCachingAndInputStreamSkipWithFixedLength() throws IOException {
61951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testResponseCaching(TransferKind.FIXED_LENGTH);
62051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
62151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
62251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testResponseCachingAndInputStreamSkipWithChunkedEncoding() throws IOException {
62351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testResponseCaching(TransferKind.CHUNKED);
62451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
62551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
62651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testResponseCachingAndInputStreamSkipWithNoLengthHeaders() throws IOException {
62751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testResponseCaching(TransferKind.END_OF_STREAM);
62851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
62951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
63051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
63151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * HttpURLConnection.getInputStream().skip(long) causes ResponseCache corruption
63251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=8175
63351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
63451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void testResponseCaching(TransferKind transferKind) throws IOException {
6358ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        MockResponse response = new MockResponse()
6368ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson                .setStatus("HTTP/1.1 200 Fantastic");
63751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        transferKind.setBody(response, "I love puppies but hate spiders", 1);
63851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
63951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
64051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
64151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
64251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(cache);
64351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
64451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        // Make sure that calling skip() doesn't omit bytes from the cache.
6458ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
64651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        InputStream in = urlConnection.getInputStream();
64751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("I love ", readAscii(in, "I love ".length()));
64851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        reliableSkip(in, "puppies but hate ".length());
64951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("spiders", readAscii(in, "spiders".length()));
65051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(-1, in.read());
65151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        in.close();
65251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getSuccessCount());
653096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        assertEquals(0, cache.getAbortCount());
65451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
6558ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); // cached!
65651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        in = urlConnection.getInputStream();
65751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("I love puppies but hate spiders",
65851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                readAscii(in, "I love puppies but hate spiders".length()));
6598ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(200, urlConnection.getResponseCode());
6608ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals("Fantastic", urlConnection.getResponseMessage());
6618ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
66251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(-1, in.read());
66351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getMissCount());
66451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getHitCount());
665096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        assertEquals(1, cache.getSuccessCount());
666096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        assertEquals(0, cache.getAbortCount());
66751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
66851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
66937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    public void testSecureResponseCaching() throws IOException {
67037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
67137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
67237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
67337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.play();
67437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
67537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
67637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        ResponseCache.setDefault(cache);
67737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
67837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
67937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
68037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
68137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
68237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        // OpenJDK 6 fails on this line, complaining that the connection isn't open yet
68337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        String suite = connection.getCipherSuite();
68437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        List<Certificate> localCerts = toListOrNull(connection.getLocalCertificates());
68537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        List<Certificate> serverCerts = toListOrNull(connection.getServerCertificates());
68637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        Principal peerPrincipal = connection.getPeerPrincipal();
68737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        Principal localPrincipal = connection.getLocalPrincipal();
68837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
68937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection(); // cached!
69037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
69137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
69237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
69337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(1, cache.getMissCount());
69437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(1, cache.getHitCount());
69537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
69637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(suite, connection.getCipherSuite());
69737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(localCerts, toListOrNull(connection.getLocalCertificates()));
69837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(serverCerts, toListOrNull(connection.getServerCertificates()));
69937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(peerPrincipal, connection.getPeerPrincipal());
70037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals(localPrincipal, connection.getLocalPrincipal());
70137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    }
70237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
70337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    public void testCacheReturnsInsecureResponseForSecureRequest() throws IOException {
70437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
70537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
70637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
70737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.enqueue(new MockResponse().setBody("DEF"));
70837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        server.play();
70937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
71037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        ResponseCache insecureResponseCache = new InsecureResponseCache();
71137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        ResponseCache.setDefault(insecureResponseCache);
71237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
71337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
71437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
71537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
71637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
71737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection(); // not cached!
71837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
71937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        assertEquals("DEF", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
72037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    }
72137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
7228ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson    public void testResponseCachingAndRedirects() throws IOException {
7238ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse()
7248ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
7258ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson                .addHeader("Location: /foo"));
7268ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
7278ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse().setBody("DEF"));
7288ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.play();
7298ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7308ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
7318ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        ResponseCache.setDefault(cache);
7328ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7338ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
7348ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
7358ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7368ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        connection = (HttpURLConnection) server.getUrl("/").openConnection(); // cached!
7378ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
7388ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7398ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(2, cache.getMissCount()); // 1 redirect + 1 final response = 2
7408ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(2, cache.getHitCount());
7418ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson    }
7428ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7438ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson    public void testSecureResponseCachingAndRedirects() throws IOException {
7448ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
7458ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
7468ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse()
7478ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
7488ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson                .addHeader("Location: /foo"));
7498ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
7508ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.enqueue(new MockResponse().setBody("DEF"));
7518ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        server.play();
7528ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7538ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
7548ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        ResponseCache.setDefault(cache);
7558ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7568ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
7578ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
7588ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
7598ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7608ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection(); // cached!
7618ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
7628ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals("ABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
7638ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
7648ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(2, cache.getMissCount()); // 1 redirect + 1 final response = 2
7658ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(2, cache.getHitCount());
7668ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson    }
7678ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson
76883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    public void testResponseCacheRequestHeaders() throws IOException, URISyntaxException {
76983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
77083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.play();
77183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
77283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        final AtomicReference<Map<String, List<String>>> requestHeadersRef
77383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                = new AtomicReference<Map<String, List<String>>>();
77483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        ResponseCache.setDefault(new ResponseCache() {
77583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            @Override public CacheResponse get(URI uri, String requestMethod,
77683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                    Map<String, List<String>> requestHeaders) throws IOException {
77783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                requestHeadersRef.set(requestHeaders);
77883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                return null;
77983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            }
78083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            @Override public CacheRequest put(URI uri, URLConnection conn) throws IOException {
78183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                return null;
78283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            }
78383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        });
78483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
78583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        URL url = server.getUrl("/");
78683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        URLConnection urlConnection = url.openConnection();
78783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("A", "android");
78883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        readAscii(urlConnection.getInputStream(), Integer.MAX_VALUE);
78983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(Arrays.asList("android"), requestHeadersRef.get().get("A"));
79083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
79183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
79251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void reliableSkip(InputStream in, int length) throws IOException {
79351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        while (length > 0) {
79451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            length -= in.skip(length);
79551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
79651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
79751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
79851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
79951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * Reads {@code count} characters from the stream. If the stream is
80051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * exhausted before {@code count} characters can be read, the remaining
80151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * characters are returned and the stream is closed.
80251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
80351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private String readAscii(InputStream in, int count) throws IOException {
80451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        StringBuilder result = new StringBuilder();
80551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        for (int i = 0; i < count; i++) {
80651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            int value = in.read();
80751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            if (value == -1) {
80851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                in.close();
80951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                break;
81051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
81151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            result.append((char) value);
81251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
81351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        return result.toString();
81451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
81551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
81651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testServerDisconnectsPrematurelyWithContentLengthHeader() throws IOException {
81751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testServerPrematureDisconnect(TransferKind.FIXED_LENGTH);
81851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
81951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
82051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testServerDisconnectsPrematurelyWithChunkedEncoding() throws IOException {
82151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testServerPrematureDisconnect(TransferKind.CHUNKED);
82251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
82351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
82451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testServerDisconnectsPrematurelyWithNoLengthHeaders() throws IOException {
82551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        /*
82651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson         * Intentionally empty. This case doesn't make sense because there's no
82751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson         * such thing as a premature disconnect when the disconnect itself
82851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson         * indicates the end of the data stream.
82951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson         */
83051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
83151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
83251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void testServerPrematureDisconnect(TransferKind transferKind) throws IOException {
83351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse();
83451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        transferKind.setBody(response, "ABCDE\nFGHIJKLMNOPQRSTUVWXYZ", 16);
83551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(truncateViolently(response, 16));
83651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(new MockResponse().setBody("Request #2"));
83751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
83851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
83951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
84051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(cache);
84151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
84251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        BufferedReader reader = new BufferedReader(new InputStreamReader(
84351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                server.getUrl("/").openConnection().getInputStream()));
84451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("ABCDE", reader.readLine());
84551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        try {
84651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            reader.readLine();
84751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            fail("This implementation silently ignored a truncated HTTP body.");
84851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        } catch (IOException expected) {
84951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
85051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
85151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getAbortCount());
85251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(0, cache.getSuccessCount());
85351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertContent("Request #2", server.getUrl("/").openConnection());
85451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getAbortCount());
85551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getSuccessCount());
85651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
85751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
85851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testClientPrematureDisconnectWithContentLengthHeader() throws IOException {
85951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testClientPrematureDisconnect(TransferKind.FIXED_LENGTH);
86051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
86151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
86251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testClientPrematureDisconnectWithChunkedEncoding() throws IOException {
86351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testClientPrematureDisconnect(TransferKind.CHUNKED);
86451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
86551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
86651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testClientPrematureDisconnectWithNoLengthHeaders() throws IOException {
86751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testClientPrematureDisconnect(TransferKind.END_OF_STREAM);
86851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
86951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
87051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void testClientPrematureDisconnect(TransferKind transferKind) throws IOException {
87151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse();
87251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        transferKind.setBody(response, "ABCDE\nFGHIJKLMNOPQRSTUVWXYZ", 1024);
87351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
87451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(new MockResponse().setBody("Request #2"));
87551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
87651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
87751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
87851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(cache);
87951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
88051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        InputStream in = server.getUrl("/").openConnection().getInputStream();
88151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("ABCDE", readAscii(in, 5));
88251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        in.close();
88351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        try {
88451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            in.read();
88551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            fail("Expected an IOException because the stream is closed.");
88651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        } catch (IOException expected) {
88751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
88851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
88951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getAbortCount());
89051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(0, cache.getSuccessCount());
89151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertContent("Request #2", server.getUrl("/").openConnection());
89251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getAbortCount());
89351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getSuccessCount());
89451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
89551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
89651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
89751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * Shortens the body of {@code response} but not the corresponding headers.
89851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * Only useful to test how clients respond to the premature conclusion of
89951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * the HTTP body.
90051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
90151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private MockResponse truncateViolently(MockResponse response, int numBytesToKeep) {
90251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        response.setDisconnectAtEnd(true);
90351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        List<String> headers = new ArrayList<String>(response.getHeaders());
90451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        response.setBody(Arrays.copyOfRange(response.getBody(), 0, numBytesToKeep));
90551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        response.getHeaders().clear();
90651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        response.getHeaders().addAll(headers);
90751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        return response;
90851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
90951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
91051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithContentLengthHeader() throws IOException {
91151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.FIXED_LENGTH);
91251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
91351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
91451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithChunkedEncoding() throws IOException {
91551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.CHUNKED);
91651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
91751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
91851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithNoLengthHeaders() throws IOException {
91951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.END_OF_STREAM);
92051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
92151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
92251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndReset(TransferKind transferKind) throws IOException {
92351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse();
92451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        transferKind.setBody(response, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1024);
92551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
92651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
92751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
92851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        DefaultResponseCache cache = new DefaultResponseCache();
92951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(cache);
93051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
93151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        InputStream in = server.getUrl("/").openConnection().getInputStream();
93251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertFalse("This implementation claims to support mark().", in.markSupported());
93351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        in.mark(5);
93451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("ABCDE", readAscii(in, 5));
93551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        try {
93651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            in.reset();
93751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            fail();
93851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        } catch (IOException expected) {
93951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
94051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("FGHIJKLMNOPQRSTUVWXYZ", readAscii(in, Integer.MAX_VALUE));
94151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
94251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ", server.getUrl("/").openConnection());
94351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getSuccessCount());
94451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, cache.getHitCount());
94551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
94651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
94751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
94851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * We've had a bug where we forget the HTTP response when we see response
94951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * code 401. This causes a new HTTP request to be issued for every call into
95051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * the URLConnection.
95151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
95251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testUnauthorizedResponseHandling() throws IOException {
95351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse()
95451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .addHeader("WWW-Authenticate: challenge")
95551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .setResponseCode(401) // UNAUTHORIZED
95651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .setBody("Unauthorized");
95751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
95851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
95951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
96051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
96151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
96251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        URL url = server.getUrl("/");
96351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
96451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
96551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
96651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
96751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
96851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, server.getRequestCount());
96951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
97051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
9716906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    public void testNonHexChunkSize() throws IOException {
9726906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.enqueue(new MockResponse()
9736906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .setBody("5\r\nABCDE\r\nG\r\nFGHIJKLMNOPQRSTU\r\n0\r\n\r\n")
9746906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .clearHeaders()
9756906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .addHeader("Transfer-encoding: chunked"));
9766906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.play();
9776906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
9786906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
9796906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        try {
9806906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
9816906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            fail();
9826906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        } catch (IOException e) {
9836906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        }
9846906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    }
9856906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
9866906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    public void testMissingChunkBody() throws IOException {
9876906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.enqueue(new MockResponse()
9886906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .setBody("5")
9896906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .clearHeaders()
9906906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .addHeader("Transfer-encoding: chunked")
9916906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .setDisconnectAtEnd(true));
9926906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.play();
9936906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
9946906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
9956906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        try {
9966906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
9976906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            fail();
9986906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        } catch (IOException e) {
9996906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        }
10006906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    }
10016906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
100250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    /**
100350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * This test checks whether connections are gzipped by default. This
100450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * behavior in not required by the API, so a failure of this test does not
100550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * imply a bug in the implementation.
100650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     */
100750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException {
100850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.enqueue(new MockResponse()
100950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .setBody(gzip("ABCABCABC".getBytes("UTF-8")))
101050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .addHeader("Content-Encoding: gzip"));
101150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.play();
101250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
101350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
101450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
10158116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson        assertNull(connection.getContentEncoding());
101650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
101750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        RecordedRequest request = server.takeRequest();
101850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: gzip");
101950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    }
102050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
1021deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testClientConfiguredGzipContentEncoding() throws Exception {
1022deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(new MockResponse()
1023deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson                .setBody(gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8")))
1024deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson                .addHeader("Content-Encoding: gzip"));
1025deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.play();
1026deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1027deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1028deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "gzip");
1029deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
1030deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
1031deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1032deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        RecordedRequest request = server.takeRequest();
1033deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: gzip");
1034deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
1035deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1036deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testGzipAndConnectionReuseWithFixedLength() throws Exception {
1037deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH);
1038deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
1039deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1040deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception {
1041deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED);
1042deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
1043deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
104450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    public void testClientConfiguredCustomContentEncoding() throws Exception {
104550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.enqueue(new MockResponse()
104650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .setBody("ABCDE")
104750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .addHeader("Content-Encoding: custom"));
104850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.play();
104950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
105050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
105150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "custom");
105250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
105350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
105450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        RecordedRequest request = server.takeRequest();
105550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: custom");
105650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    }
105750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
1058deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    /**
1059deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * Test a bug where gzip input streams weren't exhausting the input stream,
1060deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * which corrupted the request that followed.
1061deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=7059
1062deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     */
1063deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    private void testClientConfiguredGzipContentEncodingAndConnectionReuse(
1064deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            TransferKind transferKind) throws Exception {
1065deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        MockResponse responseOne = new MockResponse();
1066deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        responseOne.addHeader("Content-Encoding: gzip");
1067deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        transferKind.setBody(responseOne, gzip("one (gzipped)".getBytes("UTF-8")), 5);
1068deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(responseOne);
1069deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        MockResponse responseTwo = new MockResponse();
1070deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        transferKind.setBody(responseTwo, "two (identity)", 5);
1071deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(responseTwo);
1072deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.play();
1073deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1074deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1075deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "gzip");
1076deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
1077deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
1078deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1079deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1080deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection = server.getUrl("/").openConnection();
1081deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("two (identity)", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1082deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
1083deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
1084deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1085deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    /**
1086ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * Obnoxiously test that the chunk sizes transmitted exactly equal the
1087ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * requested data+chunk header size. Although setChunkedStreamingMode()
1088ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * isn't specific about whether the size applies to the data or the
1089ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * complete chunk, the RI interprets it as a complete chunk.
1090ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     */
1091ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testSetChunkedStreamingMode() throws IOException, InterruptedException {
1092ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse());
1093ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1094ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1095ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
1096ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        urlConnection.setChunkedStreamingMode(8);
1097ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        urlConnection.setDoOutput(true);
1098ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = urlConnection.getOutputStream();
1099ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII"));
1100ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(200, urlConnection.getResponseCode());
1101ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1102ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1103ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII"));
1104ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes());
1105ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1106ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1107ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithFixedLengthStreaming() throws Exception {
1108ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        testAuthenticateWithStreamingPost(StreamingMode.FIXED_LENGTH);
1109ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1110ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1111ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithChunkedStreaming() throws Exception {
1112ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        testAuthenticateWithStreamingPost(StreamingMode.CHUNKED);
1113ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1114ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1115ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
1116ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
1117ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
1118ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
1119ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
1120ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1121ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1122ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1123ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1124ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1125ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        connection.setDoOutput(true);
1126ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
1127ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        if (streamingMode == StreamingMode.FIXED_LENGTH) {
1128ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.setFixedLengthStreamingMode(requestBody.length);
1129ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        } else if (streamingMode == StreamingMode.CHUNKED) {
1130ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.setChunkedStreamingMode(0);
1131ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1132ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = connection.getOutputStream();
1133ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write(requestBody);
1134ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.close();
1135ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        try {
1136ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.getInputStream();
1137ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            fail();
1138ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        } catch (HttpRetryException expected) {
1139ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1140ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1141ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the request...
1142ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1143ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1144ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
1145ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1146ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1147ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    enum StreamingMode {
1148ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        FIXED_LENGTH, CHUNKED
1149ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1150ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1151ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithPost() throws Exception {
1152ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
1153ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
1154ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
1155ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
1156ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // fail auth three times...
1157ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1158ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1159ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1160ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...then succeed the fourth time
1161ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse().setBody("Successful auth!"));
1162ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1163ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1164ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1165ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1166ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        connection.setDoOutput(true);
1167ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
1168ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = connection.getOutputStream();
1169ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write(requestBody);
1170ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.close();
1171ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1172ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1173ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the first request...
1174ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1175ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1176ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1177da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson        // ...but the three requests that follow include an authorization header
1178ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (int i = 0; i < 3; i++) {
1179ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            request = server.takeRequest();
1180ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals("POST / HTTP/1.1", request.getRequestLine());
1181ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertContains(request.getHeaders(), "Authorization: Basic "
1182ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                    + "dXNlcm5hbWU6cGFzc3dvcmQ="); // "dXNl..." == base64("username:password")
1183ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
1184ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1185ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1186ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1187ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithGet() throws Exception {
1188ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
1189ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
1190ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
1191ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
1192ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // fail auth three times...
1193ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1194ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1195ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1196ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...then succeed the fourth time
1197ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse().setBody("Successful auth!"));
1198ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1199ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1200ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1201ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1202ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1203ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1204ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the first request...
1205ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1206ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1207ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1208ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...but the three requests that follow requests include an authorization header
1209ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (int i = 0; i < 3; i++) {
1210ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            request = server.takeRequest();
1211ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals("GET / HTTP/1.1", request.getRequestLine());
1212ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertContains(request.getHeaders(), "Authorization: Basic "
1213ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                    + "dXNlcm5hbWU6cGFzc3dvcmQ="); // "dXNl..." == base64("username:password")
1214ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1215ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1216ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1217c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithChunkedEncoding() throws Exception {
1218c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.CHUNKED, true);
1219c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1220c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1221c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithContentLengthHeader() throws Exception {
1222c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.FIXED_LENGTH, true);
1223c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1224c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1225c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithNoLengthHeaders() throws Exception {
1226c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.END_OF_STREAM, false);
1227c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1228c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1229c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception {
1230c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockResponse response = new MockResponse()
1231c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1232c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: /foo");
1233c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        transferKind.setBody(response, "This page has moved!", 10);
1234c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(response);
1235c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1236c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1237c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1238c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1239c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the new location!",
1240c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1241c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1242c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
1243c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET / HTTP/1.1", first.getRequestLine());
1244c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest retry = server.takeRequest();
1245c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
1246c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        if (reuse) {
1247c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
1248c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1249c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1250c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1251c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedOnHttps() throws IOException, InterruptedException {
1252c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
1253059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1254c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1255c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1256c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: /foo")
1257c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1258c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1259c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1260c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1261c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
1262059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
1263c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the new location!",
1264c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1265c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1266c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
1267c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET / HTTP/1.1", first.getRequestLine());
1268c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest retry = server.takeRequest();
1269c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
1270c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
1271c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1272c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1273c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testNotRedirectedFromHttpsToHttp() throws IOException, InterruptedException {
1274c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
1275059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1276c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1277c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1278c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: http://anyhost/foo")
1279c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1280c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1281c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1282c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
1283059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
1284c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This page has moved!",
1285c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1286c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1287c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1288c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testNotRedirectedFromHttpToHttps() throws IOException, InterruptedException {
1289c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1290c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1291c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: https://anyhost/foo")
1292c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1293c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1294c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1295c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1296c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This page has moved!",
1297c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1298c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1299c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1300c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectToAnotherOriginServer() throws Exception {
1301c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockWebServer server2 = new MockWebServer();
1302c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
1303c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.play();
1304c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1305c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1306c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1307c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: " + server2.getUrl("/").toString())
1308c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1309c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the first server again!"));
1310c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1311c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1312c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1313c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the 2nd server!",
1314c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1315c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals(server2.getUrl("/"), connection.getURL());
1316c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1317c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        // make sure the first server was careful to recycle the connection
1318c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the first server again!",
1319c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
1320c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1321c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
132200feece22909b7dc79fc96d666d157390b93858eJesse Wilson        assertContains(first.getHeaders(), "Host: " + hostname + ":" + server.getPort());
1323c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest second = server2.takeRequest();
132400feece22909b7dc79fc96d666d157390b93858eJesse Wilson        assertContains(second.getHeaders(), "Host: " + hostname + ":" + server2.getPort());
1325c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest third = server.takeRequest();
1326c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
1327c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1328c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.shutdown();
1329c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1330c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1331c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testHttpsWithCustomTrustManager() throws Exception {
1332c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
1333c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordingTrustManager trustManager = new RecordingTrustManager();
1334c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        SSLContext sc = SSLContext.getInstance("TLS");
1335c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
1336c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1337c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
1338c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
1339c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
1340c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
1341c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        try {
1342c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            TestSSLContext testSSLContext = TestSSLContext.create();
1343059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1344c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("ABC"));
1345c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("DEF"));
1346c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("GHI"));
1347c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.play();
1348c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1349c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            URL url = server.getUrl("/");
1350c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
1351c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
1352c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
1353c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
135400feece22909b7dc79fc96d666d157390b93858eJesse Wilson            assertEquals(Arrays.asList("verify " + hostname), hostnameVerifier.calls);
13554559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom            assertEquals(Arrays.asList("checkServerTrusted ["
135600feece22909b7dc79fc96d666d157390b93858eJesse Wilson                                       + "CN=" + hostname + " 1, "
13574559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom                                       + "CN=Test Intermediate Certificate Authority 1, "
13584559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom                                       + "CN=Test Root Certificate Authority 1"
13594559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom                                       + "] RSA"),
1360c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                    trustManager.calls);
1361c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        } finally {
1362c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
1363c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
1364c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1365c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1366c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1367eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    public void testConnectTimeouts() throws IOException {
1368eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        // 10.0.0.0 is non-routable and will time out on every network
1369eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        URLConnection urlConnection = new URL("http://10.0.0.0/").openConnection();
1370eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        urlConnection.setConnectTimeout(1000);
1371eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        try {
1372eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            urlConnection.getInputStream();
1373eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            fail();
1374eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        } catch (SocketTimeoutException expected) {
1375eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        }
1376eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    }
1377eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1378eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    public void testReadTimeouts() throws IOException {
1379eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        /*
1380eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * This relies on the fact that MockWebServer doesn't close the
1381eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * connection after a response has been sent. This causes the client to
1382eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * try to read more bytes than are sent, which results in a timeout.
1383eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         */
1384eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        MockResponse timeout = new MockResponse()
1385eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .setBody("ABC")
1386eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .clearHeaders()
1387eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .addHeader("Content-Length: 4");
1388eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        server.enqueue(timeout);
1389eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        server.play();
1390eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1391eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        URLConnection urlConnection = server.getUrl("/").openConnection();
1392eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        urlConnection.setReadTimeout(1000);
1393eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        InputStream in = urlConnection.getInputStream();
1394eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('A', in.read());
1395eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('B', in.read());
1396eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('C', in.read());
1397eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        try {
1398eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            in.read(); // if Content-Length was accurate, this would return -1 immediately
1399eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            fail();
1400eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        } catch (SocketTimeoutException expected) {
1401eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        }
1402eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    }
1403eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1404125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson    public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException {
1405125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        server.enqueue(new MockResponse());
1406125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        server.play();
1407125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1408125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
1409125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.setRequestProperty("Transfer-encoding", "chunked");
1410125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.setDoOutput(true);
1411125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.getOutputStream().write("ABC".getBytes("UTF-8"));
1412125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        assertEquals(200, urlConnection.getResponseCode());
1413125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1414125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        RecordedRequest request = server.takeRequest();
1415125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        assertEquals("ABC", new String(request.getBody(), "UTF-8"));
1416125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson    }
1417125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1418f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseInRequest() throws IOException, InterruptedException {
1419f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse()); // server doesn't honor the connection: close header!
1420f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse());
1421f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1422f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1423f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
1424f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        a.setRequestProperty("Connection", "close");
1425f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, a.getResponseCode());
1426f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1427f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
1428f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, b.getResponseCode());
1429f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1430f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1431f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1432f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1433f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1434f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1435f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseInResponse() throws IOException, InterruptedException {
1436f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse().addHeader("Connection: close"));
1437f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse());
1438f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1439f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1440f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
1441f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, a.getResponseCode());
1442f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1443f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
1444f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, b.getResponseCode());
1445f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1446f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1447f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1448f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1449f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1450f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1451f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseWithRedirect() throws IOException, InterruptedException {
1452f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        MockResponse response = new MockResponse()
1453f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1454f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .addHeader("Location: /foo")
1455f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .addHeader("Connection: close");
1456f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(response);
1457f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1458f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1459f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1460f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1461f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("This is the new location!",
1462f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1463f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1464f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1465f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1466f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1467f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1468f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
146965d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson    public void testResponseCodeDisagreesWithHeaders() throws IOException, InterruptedException {
147065d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        server.enqueue(new MockResponse()
147165d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)
147265d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                .setBody("This body is not allowed!"));
147365d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        server.play();
147465d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson
147565d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
147665d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        assertEquals("This body is not allowed!",
147765d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
147865d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson    }
147965d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson
1480ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson    public void testSingleByteReadIsSigned() throws IOException {
1481ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        server.enqueue(new MockResponse().setBody(new byte[] { -2, -1 }));
1482ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        server.play();
1483ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson
1484ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1485ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        InputStream in = connection.getInputStream();
1486ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(254, in.read());
1487ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(255, in.read());
1488ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(-1, in.read());
1489ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson    }
1490ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson
1491f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithChunkedEncoding() throws IOException {
1492f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.CHUNKED);
1493f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1494f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1495f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithFixedLength() throws IOException {
1496f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.FIXED_LENGTH);
1497f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1498f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1499f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithNoLengthHeaders() throws IOException {
1500f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.END_OF_STREAM);
1501f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1502f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1503f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    /**
1504f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * We explicitly permit apps to close the upload stream even after it has
1505f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * been transmitted.  We also permit flush so that buffered streams can
1506f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * do a no-op flush when they are closed. http://b/3038470
1507f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     */
1508f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    private void testFlushAfterStreamTransmitted(TransferKind transferKind) throws IOException {
1509f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        server.enqueue(new MockResponse().setBody("abc"));
1510f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        server.play();
1511f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1512f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1513f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        connection.setDoOutput(true);
1514f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        byte[] upload = "def".getBytes("UTF-8");
1515f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1516f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        if (transferKind == TransferKind.CHUNKED) {
1517f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            connection.setChunkedStreamingMode(0);
1518f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        } else if (transferKind == TransferKind.FIXED_LENGTH) {
1519f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            connection.setFixedLengthStreamingMode(upload.length);
1520f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        }
1521f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1522f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        OutputStream out = connection.getOutputStream();
1523f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        out.write(upload);
1524f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        assertEquals("abc", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1525f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1526f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        out.flush(); // dubious but permitted
1527f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        try {
1528f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            out.write("ghi".getBytes("UTF-8"));
1529f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            fail();
1530f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        } catch (IOException expected) {
1531f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        }
1532f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1533f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1534ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    /**
1535deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * Encodes the response body using GZIP and adds the corresponding header.
1536deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     */
1537deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public byte[] gzip(byte[] bytes) throws IOException {
1538deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
1539deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        OutputStream gzippedOut = new GZIPOutputStream(bytesOut);
1540deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        gzippedOut.write(bytes);
1541deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        gzippedOut.close();
1542deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        return bytesOut.toByteArray();
1543deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
1544deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
154537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    private <T> List<T> toListOrNull(T[] arrayOrNull) {
154637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        return arrayOrNull != null ? Arrays.asList(arrayOrNull) : null;
154737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    }
154837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
1549c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    /**
1550c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     * Reads at most {@code limit} characters from {@code in} and asserts that
1551c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     * content equals {@code expected}.
1552c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     */
1553c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    private void assertContent(String expected, URLConnection connection, int limit)
1554c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson            throws IOException {
155551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(expected, readAscii(connection.getInputStream(), limit));
1556c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        ((HttpURLConnection) connection).disconnect();
1557c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
1558c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
1559c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    private void assertContent(String expected, URLConnection connection) throws IOException {
1560c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent(expected, connection, Integer.MAX_VALUE);
1561c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
1562c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
156360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    private void assertContains(List<String> headers, String header) {
156460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertTrue(headers.toString(), headers.contains(header));
156560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
156651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
1567ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private void assertContainsNoneMatching(List<String> headers, String pattern) {
1568ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (String header : headers) {
1569ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            if (header.matches(pattern)) {
1570ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                fail("Header " + header + " matches " + pattern);
1571ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            }
1572ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1573ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1574ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1575eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    private Set<String> newSet(String... elements) {
157683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        return new HashSet<String>(Arrays.asList(elements));
157783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
157883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
157951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    enum TransferKind {
158051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        CHUNKED() {
1581deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize)
158251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    throws IOException {
158351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setChunkedBody(content, chunkSize);
158451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
158551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        },
158651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        FIXED_LENGTH() {
1587deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize) {
158851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setBody(content);
158951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
159051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        },
159151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        END_OF_STREAM() {
1592deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize) {
159351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setBody(content);
159451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setDisconnectAtEnd(true);
159551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                for (Iterator<String> h = response.getHeaders().iterator(); h.hasNext(); ) {
159651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    if (h.next().startsWith("Content-Length:")) {
159751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                        h.remove();
159851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                        break;
159951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    }
160051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                }
160151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
160251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        };
160351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
1604deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        abstract void setBody(MockResponse response, byte[] content, int chunkSize)
160551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                throws IOException;
1606deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
1607deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        void setBody(MockResponse response, String content, int chunkSize) throws IOException {
1608deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            setBody(response, content.getBytes("UTF-8"), chunkSize);
1609deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        }
161051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
1611c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1612984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    enum ProxyConfig {
1613984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        CREATE_ARG() {
1614984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
1615984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
1616984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection(server.toProxyAddress());
1617984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
1618984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
1619984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
1620984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        PROXY_SYSTEM_PROPERTY() {
1621984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
1622984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
1623984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("proxyHost", "localhost");
1624984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("proxyPort", Integer.toString(server.getPort()));
1625984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
1626984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
1627984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
1628984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
1629984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HTTP_PROXY_SYSTEM_PROPERTY() {
1630984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
1631984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
1632984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("http.proxyHost", "localhost");
1633984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("http.proxyPort", Integer.toString(server.getPort()));
1634984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
1635984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
1636984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
1637984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
1638984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HTTPS_PROXY_SYSTEM_PROPERTY() {
1639984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
1640984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
1641984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("https.proxyHost", "localhost");
1642984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("https.proxyPort", Integer.toString(server.getPort()));
1643984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
1644984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
1645984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        };
1646984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
1647984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        public abstract HttpURLConnection connect(MockWebServer server, URL url) throws IOException;
1648984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
1649984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
1650c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private static class RecordingTrustManager implements X509TrustManager {
1651c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private final List<String> calls = new ArrayList<String>();
1652c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1653c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public X509Certificate[] getAcceptedIssuers() {
1654c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("getAcceptedIssuers");
1655c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return new X509Certificate[] {};
1656c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1657c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1658c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public void checkClientTrusted(X509Certificate[] chain, String authType)
1659c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                throws CertificateException {
1660c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("checkClientTrusted " + certificatesToString(chain) + " " + authType);
1661c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1662c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1663c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public void checkServerTrusted(X509Certificate[] chain, String authType)
1664c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                throws CertificateException {
1665c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("checkServerTrusted " + certificatesToString(chain) + " " + authType);
1666c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1667c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1668c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private String certificatesToString(X509Certificate[] certificates) {
1669c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            List<String> result = new ArrayList<String>();
1670c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            for (X509Certificate certificate : certificates) {
1671c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                result.add(certificate.getSubjectDN() + " " + certificate.getSerialNumber());
1672c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            }
1673c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return result.toString();
1674c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1675c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1676c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1677c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private static class RecordingHostnameVerifier implements HostnameVerifier {
1678c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private final List<String> calls = new ArrayList<String>();
1679c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1680c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public boolean verify(String hostname, SSLSession session) {
1681c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("verify " + hostname);
1682c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return true;
1683c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1684c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
168537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
168637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    private static class InsecureResponseCache extends ResponseCache {
168737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        private final DefaultResponseCache delegate = new DefaultResponseCache();
168837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
168937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException {
169037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson            return delegate.put(uri, connection);
169137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        }
169237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson
169337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        @Override public CacheResponse get(URI uri, String requestMethod,
169437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                Map<String, List<String>> requestHeaders) throws IOException {
169537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson            final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
169637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson            if (response instanceof SecureCacheResponse) {
169737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                return new CacheResponse() {
169837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                    @Override public InputStream getBody() throws IOException {
169937dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                        return response.getBody();
170037dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                    }
170137dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                    @Override public Map<String, List<String>> getHeaders() throws IOException {
170237dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                        return response.getHeaders();
170337dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                    }
170437dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson                };
170537dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson            }
170637dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson            return response;
170737dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson        }
170837dcf5581f177229ca6c8e7d0d640361640bfb00Jesse Wilson    }
1709e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes}
1710