URLConnectionTest.java revision b2b02ac6cd42a69463fd172531aa1f9b9bb887a8
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
1909336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.MockResponse;
2009336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.MockWebServer;
2109336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.RecordedRequest;
2209336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.SocketPolicy;
2309336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_END;
2409336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_START;
2509336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.SHUTDOWN_INPUT_AT_END;
2609336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.SHUTDOWN_OUTPUT_AT_END;
27c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilsonimport java.io.ByteArrayOutputStream;
286247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.io.IOException;
2951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.io.InputStream;
3002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.io.OutputStream;
314557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.Authenticator;
324557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheRequest;
334557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheResponse;
34b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilsonimport java.net.ConnectException;
354557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpRetryException;
364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpURLConnection;
372d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilsonimport java.net.InetAddress;
384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.PasswordAuthentication;
39f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilsonimport java.net.ProtocolException;
40f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstromimport java.net.Proxy;
414557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.ResponseCache;
424557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.SocketTimeoutException;
434557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URI;
444557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URL;
454557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URLConnection;
46d0d626655f1d452070d3116678037e8759f807f4Jesse Wilsonimport java.net.UnknownHostException;
47c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.CertificateException;
48c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.X509Certificate;
4951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.ArrayList;
5002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.util.Arrays;
5151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Collections;
5283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.HashSet;
5351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Iterator;
546247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.util.List;
5583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.Map;
5651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Set;
57afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilsonimport java.util.concurrent.atomic.AtomicBoolean;
5883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.concurrent.atomic.AtomicReference;
59deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPInputStream;
60deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPOutputStream;
6160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HostnameVerifier;
6260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HttpsURLConnection;
63c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLContext;
64096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilsonimport javax.net.ssl.SSLException;
652915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport javax.net.ssl.SSLHandshakeException;
6660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.SSLSession;
67c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLSocketFactory;
68c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.TrustManager;
69c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.X509TrustManager;
70ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilsonimport junit.framework.TestCase;
712915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport libcore.java.security.TestKeyStore;
7250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilsonimport libcore.javax.net.ssl.TestSSLContext;
735fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughesimport tests.net.StuckServer;
74e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes
75ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilsonpublic final class URLConnectionTest extends TestCase {
76b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
77ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private static final Authenticator SIMPLE_AUTHENTICATOR = new Authenticator() {
78ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        protected PasswordAuthentication getPasswordAuthentication() {
79ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            return new PasswordAuthentication("username", "password".toCharArray());
80ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
81ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    };
82ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
83c996149b500fc4825156106554457fe2394ae087Jesse Wilson    /** base64("username:password") */
84c996149b500fc4825156106554457fe2394ae087Jesse Wilson    private static final String BASE_64_CREDENTIALS = "dXNlcm5hbWU6cGFzc3dvcmQ=";
85c996149b500fc4825156106554457fe2394ae087Jesse Wilson
8651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private MockWebServer server = new MockWebServer();
870c2fd828abec671333b8b88281825fd27a783723Jesse Wilson    private String hostName;
8800feece22909b7dc79fc96d666d157390b93858eJesse Wilson
8900feece22909b7dc79fc96d666d157390b93858eJesse Wilson    @Override protected void setUp() throws Exception {
9000feece22909b7dc79fc96d666d157390b93858eJesse Wilson        super.setUp();
910c2fd828abec671333b8b88281825fd27a783723Jesse Wilson        hostName = server.getHostName();
9200feece22909b7dc79fc96d666d157390b93858eJesse Wilson    }
9351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
9451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    @Override protected void tearDown() throws Exception {
9551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        ResponseCache.setDefault(null);
96ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(null);
97984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("proxyHost");
98984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("proxyPort");
99984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("http.proxyHost");
100984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("http.proxyPort");
101984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("https.proxyHost");
102984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        System.clearProperty("https.proxyPort");
10351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.shutdown();
10400feece22909b7dc79fc96d666d157390b93858eJesse Wilson        super.tearDown();
10551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
10651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
10783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    public void testRequestHeaders() throws IOException, InterruptedException {
10883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.enqueue(new MockResponse());
10983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.play();
11083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
11183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
11283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("D", "e");
11383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("D", "f");
114ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("f", urlConnection.getRequestProperty("D"));
115ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("f", urlConnection.getRequestProperty("d"));
11683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties();
11783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D")));
118ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("d")));
11983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
12083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            requestHeaders.put("G", Arrays.asList("h"));
12183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
12283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
12383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
12483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
12583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            requestHeaders.get("D").add("i");
12683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
12783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
12883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
12983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
13083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.setRequestProperty(null, "j");
13183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail();
13283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (NullPointerException expected) {
13383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
13483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
13583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.addRequestProperty(null, "k");
13683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail();
13783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (NullPointerException expected) {
13883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
13983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.setRequestProperty("NullValue", null); // should fail silently!
140ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertNull(urlConnection.getRequestProperty("NullValue"));
14183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.addRequestProperty("AnotherNullValue", null);  // should fail silently!
142ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertNull(urlConnection.getRequestProperty("AnotherNullValue"));
14383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
14483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        urlConnection.getResponseCode();
14583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        RecordedRequest request = server.takeRequest();
14683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContains(request.getHeaders(), "D: e");
14783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContains(request.getHeaders(), "D: f");
14883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "NullValue.*");
14983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*");
15083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "G:.*");
15183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertContainsNoneMatching(request.getHeaders(), "null:.*");
15283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
15383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
15483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.addRequestProperty("N", "o");
15583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Set header after connect");
15683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (IllegalStateException expected) {
15783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
15883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
15983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            urlConnection.setRequestProperty("P", "q");
16083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Set header after connect");
16183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (IllegalStateException expected) {
16283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
163ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
164ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            urlConnection.getRequestProperties();
165ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
166ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
167ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
168ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
169ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
170ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetRequestPropertyReturnsLastValue() throws Exception {
171ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
172ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
173ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        urlConnection.addRequestProperty("A", "value1");
174ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        urlConnection.addRequestProperty("A", "value2");
175ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("value2", urlConnection.getRequestProperty("A"));
17683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
17783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
17883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    public void testResponseHeaders() throws IOException, InterruptedException {
17983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.enqueue(new MockResponse()
18083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .setStatus("HTTP/1.0 200 Fantastic")
18183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .addHeader("A: c")
182ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .addHeader("B: d")
183ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .addHeader("A: e")
18483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson                .setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8));
18583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        server.play();
18683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
18783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
18883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals(200, urlConnection.getResponseCode());
18983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        assertEquals("Fantastic", urlConnection.getResponseMessage());
190c1a675c80c69decadb736b245f0366f93a94a462Jesse Wilson        assertEquals("HTTP/1.0 200 Fantastic", urlConnection.getHeaderField(null));
19183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        Map<String, List<String>> responseHeaders = urlConnection.getHeaderFields();
1928ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson        assertEquals(Arrays.asList("HTTP/1.0 200 Fantastic"), responseHeaders.get(null));
193ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals(newSet("c", "e"), new HashSet<String>(responseHeaders.get("A")));
194ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals(newSet("c", "e"), new HashSet<String>(responseHeaders.get("a")));
19583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
19683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            responseHeaders.put("N", Arrays.asList("o"));
19783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
19883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
19983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
20083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        try {
201ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            responseHeaders.get("A").add("f");
20283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson            fail("Modified an unmodifiable view.");
20383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        } catch (UnsupportedOperationException expected) {
20483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        }
205ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", urlConnection.getHeaderFieldKey(0));
206ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("c", urlConnection.getHeaderField(0));
207ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("B", urlConnection.getHeaderFieldKey(1));
208ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("d", urlConnection.getHeaderField(1));
209ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", urlConnection.getHeaderFieldKey(2));
210ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("e", urlConnection.getHeaderField(2));
211ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
212ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
213ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetErrorStreamOnSuccessfulRequest() throws Exception {
214ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
215ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
216ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
217ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertNull(connection.getErrorStream());
218ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
219ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
220ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetErrorStreamOnUnsuccessfulRequest() throws Exception {
221ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setResponseCode(404).setBody("A"));
222ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
223ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
224ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", readAscii(connection.getErrorStream(), Integer.MAX_VALUE));
22583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
22683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
227e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // Check that if we don't read to the end of a response, the next request on the
228e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // recycled connection doesn't get the unread tail of the first request's response.
229e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    // http://code.google.com/p/android/issues/detail?id=2939
230e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    public void test_2939() throws Exception {
231b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        MockResponse response = new MockResponse().setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8);
232b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
233b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
234b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
235b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
236b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
237c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDE", server.getUrl("/").openConnection(), 5);
238c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("ABCDE", server.getUrl("/").openConnection(), 5);
2398baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson    }
2408baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson
241977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    // Check that we recognize a few basic mime types by extension.
242977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    // http://code.google.com/p/android/issues/detail?id=10100
243977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    public void test_10100() throws Exception {
244977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes        assertEquals("image/jpeg", URLConnection.guessContentTypeFromName("someFile.jpg"));
245977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes        assertEquals("application/pdf", URLConnection.guessContentTypeFromName("stuff.pdf"));
246977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes    }
247977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes
2488baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson    public void testConnectionsArePooled() throws Exception {
249b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        MockResponse response = new MockResponse().setBody("ABCDEFGHIJKLMNOPQR");
250b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
251b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
252b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
253b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(response);
254b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
255b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
25606e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection());
257c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
25806e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection());
259c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
26006e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection());
261c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertEquals(2, server.takeRequest().getSequenceNumber());
262c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
263c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
264c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testChunkedConnectionsArePooled() throws Exception {
265c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        MockResponse response = new MockResponse().setChunkedBody("ABCDEFGHIJKLMNOPQR", 5);
266c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
267c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
268c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
269c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(response);
270c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
271c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
27206e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection());
273b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
27406e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection());
275b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
27606e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection());
277b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(2, server.takeRequest().getSequenceNumber());
278e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes    }
27902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
2800613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson    /**
2810613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson     * Test that connections are added to the pool as soon as the response has
2820613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson     * been consumed.
2830613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson     */
2840613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson    public void testConnectionsArePooledWithoutExplicitDisconnect() throws Exception {
2850613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
2860613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        server.enqueue(new MockResponse().setBody("DEF"));
2870613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        server.play();
2880613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson
2890613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        URLConnection connection1 = server.getUrl("/").openConnection();
2900613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        assertEquals("ABC", readAscii(connection1.getInputStream(), Integer.MAX_VALUE));
2910613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
2920613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        URLConnection connection2 = server.getUrl("/").openConnection();
2930613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        assertEquals("DEF", readAscii(connection2.getInputStream(), Integer.MAX_VALUE));
2940613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
2950613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson    }
2960613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson
297e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson    public void testServerClosesSocket() throws Exception {
298b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        testServerClosesSocket(DISCONNECT_AT_END);
299e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson    }
300e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson
301e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson    public void testServerShutdownInput() throws Exception {
302b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        testServerClosesSocket(SHUTDOWN_INPUT_AT_END);
303e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson    }
304e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson
305b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    private void testServerClosesSocket(SocketPolicy socketPolicy) throws Exception {
306e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        server.enqueue(new MockResponse()
307e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson                .setBody("This connection won't pool properly")
308e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson                .setSocketPolicy(socketPolicy));
309b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("This comes after a busted connection"));
310e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        server.play();
311e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson
312e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        assertContent("This connection won't pool properly", server.getUrl("/a").openConnection());
313e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
314e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        assertContent("This comes after a busted connection", server.getUrl("/b").openConnection());
315e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        // sequence number 0 means the HTTP socket connection was not reused
316e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
317e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson    }
318e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson
319b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    public void testServerShutdownOutput() throws Exception {
320b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        // This test causes MockWebServer to log a "connection failed" stack trace
321b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse()
322b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson                .setBody("Output shutdown after this response")
323b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson                .setSocketPolicy(SHUTDOWN_OUTPUT_AT_END));
324b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("This response will fail to write"));
325b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("This comes after a busted connection"));
326b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.play();
327b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
328b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertContent("Output shutdown after this response", server.getUrl("/a").openConnection());
329b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
330b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertContent("This comes after a busted connection", server.getUrl("/b").openConnection());
331b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
332b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
333b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    }
334b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
335b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    public void testRetryableRequestBodyAfterBrokenConnection() throws Exception {
336b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AT_END));
337b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("def"));
338b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.play();
339b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
340b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertContent("abc", server.getUrl("/a").openConnection());
341b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/b").openConnection();
342b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        connection.setDoOutput(true);
343b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        OutputStream out = connection.getOutputStream();
344b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        out.write(new byte[] {1, 2, 3});
345b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        out.close();
346b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertContent("def", connection);
347b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    }
348b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
349b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    public void testNonRetryableRequestBodyAfterBrokenConnection() throws Exception {
350b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AT_END));
351b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.enqueue(new MockResponse().setBody("def"));
352b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        server.play();
353b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
354b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        assertContent("abc", server.getUrl("/a").openConnection());
355b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/b").openConnection();
356b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        connection.setDoOutput(true);
357b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        connection.setFixedLengthStreamingMode(3);
358b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        OutputStream out = connection.getOutputStream();
359b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        out.write(new byte[] {1, 2, 3});
360b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        out.close();
361b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        try {
362b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson            connection.getInputStream();
363b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson            fail();
364b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        } catch (IOException expected) {
365b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson        }
366b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson    }
367b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson
368b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson    enum WriteKind { BYTE_BY_BYTE, SMALL_BUFFERS, LARGE_BUFFERS }
36902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
37002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_byteByByte() throws Exception {
37151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.BYTE_BY_BYTE);
37202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
37302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
37402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_smallBuffers() throws Exception {
37551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.SMALL_BUFFERS);
37602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
37702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
37802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_chunkedUpload_largeBuffers() throws Exception {
37951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.CHUNKED, WriteKind.LARGE_BUFFERS);
38002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
38102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
38202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_byteByByte() throws Exception {
38351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.BYTE_BY_BYTE);
38402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
38502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
38602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_smallBuffers() throws Exception {
38751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.SMALL_BUFFERS);
38802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
38902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
39002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    public void test_fixedLengthUpload_largeBuffers() throws Exception {
39151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        doUpload(TransferKind.FIXED_LENGTH, WriteKind.LARGE_BUFFERS);
39202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
39302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes
39451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception {
39502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        int n = 512*1024;
396b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.setBodyLimit(0);
397b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.enqueue(new MockResponse());
398b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        server.play();
399b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson
400b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) server.getUrl("/").openConnection();
40102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        conn.setDoOutput(true);
40202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        conn.setRequestMethod("POST");
40351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        if (uploadKind == TransferKind.CHUNKED) {
40402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            conn.setChunkedStreamingMode(-1);
40502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        } else {
40602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            conn.setFixedLengthStreamingMode(n);
40702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        }
40802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        OutputStream out = conn.getOutputStream();
40902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        if (writeKind == WriteKind.BYTE_BY_BYTE) {
41002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            for (int i = 0; i < n; ++i) {
41102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes                out.write('x');
41202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            }
41302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        } else {
41402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64*1024];
41502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            Arrays.fill(buf, (byte) 'x');
41602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            for (int i = 0; i < n; i += buf.length) {
41702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes                out.write(buf, 0, Math.min(buf.length, n - i));
41802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes            }
41902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        }
42002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes        out.close();
4214cb7f05dc68abb23ae54a5891c369062185f2210Elliott Hughes        assertEquals(200, conn.getResponseCode());
422b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        RecordedRequest request = server.takeRequest();
423b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        assertEquals(n, request.getBodySize());
42451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        if (uploadKind == TransferKind.CHUNKED) {
425b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson            assertTrue(request.getChunkSizes().size() > 0);
426b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        } else {
427b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson            assertTrue(request.getChunkSizes().isEmpty());
428b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson        }
42902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes    }
4306247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes
431f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson    public void testGetResponseCodeNoResponseBody() throws Exception {
432f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        server.enqueue(new MockResponse()
433f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson                .addHeader("abc: def"));
434f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        server.play();
435f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson
436f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        URL url = server.getUrl("/");
437f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
438f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        conn.setDoInput(false);
439f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        assertEquals("def", conn.getHeaderField("abc"));
440f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        assertEquals(200, conn.getResponseCode());
441f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        try {
442f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson            conn.getInputStream();
443f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson            fail();
444f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        } catch (ProtocolException expected) {
445f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson        }
446f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson    }
447f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson
44860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    public void testConnectViaHttps() throws IOException, InterruptedException {
44960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
45060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
451059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
452c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
45360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        server.play();
45460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
455096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
4564559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
45760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
458c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via HTTPS", connection);
45960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
46060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        RecordedRequest request = server.takeRequest();
46160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
46260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
46360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
464096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson    public void testConnectViaHttpsReusingConnections() throws IOException, InterruptedException {
465096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
466096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
467059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
468096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
469096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.enqueue(new MockResponse().setBody("another response via HTTPS"));
470096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        server.play();
471096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
472b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
473b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
474b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertContent("this response comes via HTTPS", connection);
475b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
476b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection();
477b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
478b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertContent("another response via HTTPS", connection);
479b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
480b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
481b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
482b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson    }
483b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
4848116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson    public void testConnectViaHttpsReusingConnectionsDifferentFactories()
485b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson            throws IOException, InterruptedException {
486b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
487b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
488b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
489b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
490b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.enqueue(new MockResponse().setBody("another response via HTTPS"));
491b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson        server.play();
492b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson
493096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        // install a custom SSL socket factory so the server can be authorized
494096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
495059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
496096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        assertContent("this response comes via HTTPS", connection);
497096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
498096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        connection = (HttpsURLConnection) server.getUrl("/").openConnection();
499096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        try {
500096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
501b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson            fail("without an SSL socket factory, the connection should fail");
502096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        } catch (SSLException expected) {
503096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson        }
504096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson    }
505096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson
5064559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom    public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
5074559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        TestSSLContext testSSLContext = TestSSLContext.create();
5084559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
5094559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
510e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
5114559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.enqueue(new MockResponse().setBody("this response comes via SSL"));
5124559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        server.play();
5134559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
5144559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
5154559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
5164559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
5174559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        assertContent("this response comes via SSL", connection);
5184559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
5194559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        RecordedRequest request = server.takeRequest();
5204559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
5214559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom    }
5224559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom
5232915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom    /**
5242915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom     * Verify that we don't retry connections on certificate verification errors.
5252915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom     *
5262915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom     * http://code.google.com/p/android/issues/detail?id=13178
5272915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom     */
5282915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom    public void testConnectViaHttpsToUntrustedServer() throws IOException, InterruptedException {
5292915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        TestSSLContext testSSLContext = TestSSLContext.create(TestKeyStore.getClientCA2(),
5302915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom                                                              TestKeyStore.getServer());
5312915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom
5322915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
5332915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        server.enqueue(new MockResponse()); // unused
5342915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        server.play();
5352915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom
5362915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
5372915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
5382915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        try {
5392915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom            connection.getInputStream();
5402915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom            fail();
5412915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        } catch (SSLHandshakeException expected) {
5422915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom            assertTrue(expected.getCause() instanceof CertificateException);
5432915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        }
5442915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom        assertEquals(0, server.getRequestCount());
5452915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom    }
5462915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom
547984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingProxyArg() throws Exception {
548984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.CREATE_ARG);
549984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
550984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
551984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingProxySystemProperty() throws Exception {
552984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY);
553984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
554984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
555984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaProxyUsingHttpProxySystemProperty() throws Exception {
556984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaProxy(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY);
557984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
558984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
559984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception {
560c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockResponse mockResponse = new MockResponse().setBody("this response comes via a proxy");
56151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(mockResponse);
56251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
56360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
564984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        URL url = new URL("http://android.com/foo");
565984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HttpURLConnection connection = proxyConfig.connect(server, url);
566c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via a proxy", connection);
56760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
56851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest request = server.takeRequest();
56960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET http://android.com/foo HTTP/1.1", request.getRequestLine());
57060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(request.getHeaders(), "Host: android.com");
57160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
57260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
573c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testContentDisagreesWithContentLengthHeader() throws IOException {
574c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(new MockResponse()
575c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .setBody("abc\r\nYOU SHOULD NOT SEE THIS")
576c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .clearHeaders()
577c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson                .addHeader("Content-Length: 3"));
578c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
579c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
580c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("abc", server.getUrl("/").openConnection());
581c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
582c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
583c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    public void testContentDisagreesWithChunkedHeader() throws IOException {
584c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        MockResponse mockResponse = new MockResponse();
585c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.setChunkedBody("abc", 3);
586c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
587c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        bytesOut.write(mockResponse.getBody());
588c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        bytesOut.write("\r\nYOU SHOULD NOT SEE THIS".getBytes());
589c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.setBody(bytesOut.toByteArray());
590c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.clearHeaders();
591c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        mockResponse.addHeader("Transfer-encoding: chunked");
592c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
593c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.enqueue(mockResponse);
594c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        server.play();
595c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
596c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("abc", server.getUrl("/").openConnection());
597c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
598c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
599f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    public void testConnectViaHttpProxyToHttpsUsingProxyArgWithNoProxy() throws Exception {
600f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        testConnectViaDirectProxyToHttps(ProxyConfig.NO_PROXY);
601f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    }
602f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
603f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception {
604f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        // https should not use http proxy
605f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        testConnectViaDirectProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY);
606f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    }
607f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
608f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception {
609f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        TestSSLContext testSSLContext = TestSSLContext.create();
610f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
611f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
612f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
613f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        server.play();
614f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
615f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        URL url = server.getUrl("/foo");
616f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
617f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
618f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
619f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        assertContent("this response comes via HTTPS", connection);
620f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
621f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        RecordedRequest request = server.takeRequest();
622f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
623f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom    }
624f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
625f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
626984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingProxyArg() throws Exception {
627984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.CREATE_ARG);
628984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
629984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
630984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    /**
631984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * We weren't honoring all of the appropriate proxy system properties when
632984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * connecting via HTTPS. http://b/3097518
633984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     */
634984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingProxySystemProperty() throws Exception {
635984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY);
636984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
637984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
638984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    public void testConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty() throws Exception {
639984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY);
640984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
641984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
642984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    /**
643984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * We were verifying the wrong hostname when connecting to an HTTPS site
644984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     * through a proxy. http://b/3097277
645984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson     */
646984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
64760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
648984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
64960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
650059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
651c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.enqueue(new MockResponse()
652c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
653c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .clearHeaders());
654c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
65551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
65660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
65760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        URL url = new URL("https://android.com/foo");
658984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
659059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
660984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        connection.setHostnameVerifier(hostnameVerifier);
66160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
662c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent("this response comes via a secure proxy", connection);
66360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
66451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest connect = server.takeRequest();
66560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("Connect line failure on proxy",
66660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson                "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
66760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(connect.getHeaders(), "Host: android.com");
66860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
66951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        RecordedRequest get = server.takeRequest();
67060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
67160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertContains(get.getHeaders(), "Host: android.com");
672984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
67360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
67460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson
675d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    /**
676d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson     * Test which headers are sent unencrypted to the HTTP proxy.
677d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson     */
678d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    public void testProxyConnectIncludesProxyHeadersOnly()
679d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            throws IOException, InterruptedException {
680984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
681d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
682d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
683d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
684c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.enqueue(new MockResponse()
685c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
686c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .clearHeaders());
687d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
688d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.play();
689d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
690d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        URL url = new URL("https://android.com/foo");
691d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(
692d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson                server.toProxyAddress());
693d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("Private", "Secret");
694d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("Proxy-Authorization", "bar");
695d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.addRequestProperty("User-Agent", "baz");
696d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
697984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        connection.setHostnameVerifier(hostnameVerifier);
698d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContent("encrypted response from the origin server", connection);
699d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
700d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        RecordedRequest connect = server.takeRequest();
701d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContainsNoneMatching(connect.getHeaders(), "Private.*");
702d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Proxy-Authorization: bar");
703d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "User-Agent: baz");
704d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Host: android.com");
705d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive");
706d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
707d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        RecordedRequest get = server.takeRequest();
708d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertContains(get.getHeaders(), "Private: Secret");
709984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
710d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    }
711d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
712c996149b500fc4825156106554457fe2394ae087Jesse Wilson    public void testProxyAuthenticateOnConnect() throws Exception {
713c996149b500fc4825156106554457fe2394ae087Jesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
714c996149b500fc4825156106554457fe2394ae087Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
715c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
716c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.enqueue(new MockResponse()
717c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .setResponseCode(407)
718c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
719c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.enqueue(new MockResponse()
720c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
721c996149b500fc4825156106554457fe2394ae087Jesse Wilson                .clearHeaders());
722c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.enqueue(new MockResponse().setBody("A"));
723c996149b500fc4825156106554457fe2394ae087Jesse Wilson        server.play();
724c996149b500fc4825156106554457fe2394ae087Jesse Wilson
725c996149b500fc4825156106554457fe2394ae087Jesse Wilson        URL url = new URL("https://android.com/foo");
726c996149b500fc4825156106554457fe2394ae087Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(
727c996149b500fc4825156106554457fe2394ae087Jesse Wilson                server.toProxyAddress());
728c996149b500fc4825156106554457fe2394ae087Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
729c996149b500fc4825156106554457fe2394ae087Jesse Wilson        connection.setHostnameVerifier(new RecordingHostnameVerifier());
730c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertContent("A", connection);
731c996149b500fc4825156106554457fe2394ae087Jesse Wilson
732c996149b500fc4825156106554457fe2394ae087Jesse Wilson        RecordedRequest connect1 = server.takeRequest();
733c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
734c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*");
735c996149b500fc4825156106554457fe2394ae087Jesse Wilson
736c996149b500fc4825156106554457fe2394ae087Jesse Wilson        RecordedRequest connect2 = server.takeRequest();
737c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
738c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " + BASE_64_CREDENTIALS);
739c996149b500fc4825156106554457fe2394ae087Jesse Wilson
740c996149b500fc4825156106554457fe2394ae087Jesse Wilson        RecordedRequest get = server.takeRequest();
741c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
742c996149b500fc4825156106554457fe2394ae087Jesse Wilson        assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*");
743c996149b500fc4825156106554457fe2394ae087Jesse Wilson    }
744c996149b500fc4825156106554457fe2394ae087Jesse Wilson
745d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    public void testDisconnectedConnection() throws IOException {
746d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.enqueue(new MockResponse().setBody("ABCDEFGHIJKLMNOPQR"));
747d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        server.play();
748d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
749d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
750d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        InputStream in = connection.getInputStream();
751d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        assertEquals('A', (char) in.read());
752d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        connection.disconnect();
753d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        try {
754d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            in.read();
755d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson            fail("Expected a connection closed exception");
756d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        } catch (IOException expected) {
757d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson        }
758d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson    }
759d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson
760d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testDisconnectBeforeConnect() throws IOException {
761d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.enqueue(new MockResponse().setBody("A"));
762d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.play();
763d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
764d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
765d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        connection.disconnect();
766d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
767d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertContent("A", connection);
768d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals(200, connection.getResponseCode());
769d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
770d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
771d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testDefaultRequestProperty() throws Exception {
772d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        URLConnection.setDefaultRequestProperty("X-testSetDefaultRequestProperty", "A");
773d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertNull(URLConnection.getDefaultRequestProperty("X-setDefaultRequestProperty"));
774d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
775d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
77651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
77751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * Reads {@code count} characters from the stream. If the stream is
77851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * exhausted before {@code count} characters can be read, the remaining
77951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * characters are returned and the stream is closed.
78051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
78151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    private String readAscii(InputStream in, int count) throws IOException {
78251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        StringBuilder result = new StringBuilder();
78351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        for (int i = 0; i < count; i++) {
78451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            int value = in.read();
78551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            if (value == -1) {
78651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                in.close();
78751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                break;
78851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
78951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            result.append((char) value);
79051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
79151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        return result.toString();
79251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
79351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
79451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithContentLengthHeader() throws IOException {
79551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.FIXED_LENGTH);
79651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
79751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
79851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithChunkedEncoding() throws IOException {
79951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.CHUNKED);
80051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
80151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
80251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testMarkAndResetWithNoLengthHeaders() throws IOException {
80351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        testMarkAndReset(TransferKind.END_OF_STREAM);
80451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
80551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
8060c2fd828abec671333b8b88281825fd27a783723Jesse Wilson    private void testMarkAndReset(TransferKind transferKind) throws IOException {
80751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse();
80851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        transferKind.setBody(response, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1024);
80951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
810953df613522e12a418cb7cb73248594d6c9f53d4Jesse Wilson        server.enqueue(response);
81151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
81251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
81351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        InputStream in = server.getUrl("/").openConnection().getInputStream();
81451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertFalse("This implementation claims to support mark().", in.markSupported());
81551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        in.mark(5);
81651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("ABCDE", readAscii(in, 5));
81751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        try {
81851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            in.reset();
81951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            fail();
82051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        } catch (IOException expected) {
82151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        }
82251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals("FGHIJKLMNOPQRSTUVWXYZ", readAscii(in, Integer.MAX_VALUE));
82351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ", server.getUrl("/").openConnection());
82451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
82551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
82651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    /**
82751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * We've had a bug where we forget the HTTP response when we see response
82851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * code 401. This causes a new HTTP request to be issued for every call into
82951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     * the URLConnection.
83051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson     */
83151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    public void testUnauthorizedResponseHandling() throws IOException {
83251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        MockResponse response = new MockResponse()
83351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .addHeader("WWW-Authenticate: challenge")
83451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .setResponseCode(401) // UNAUTHORIZED
83551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                .setBody("Unauthorized");
83651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
83751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
83851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.enqueue(response);
83951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        server.play();
84051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
84151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        URL url = server.getUrl("/");
84251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
84351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
84451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
84551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
84651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(401, conn.getResponseCode());
84751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(1, server.getRequestCount());
84851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
84951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
8506906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    public void testNonHexChunkSize() throws IOException {
8516906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.enqueue(new MockResponse()
8526906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .setBody("5\r\nABCDE\r\nG\r\nFGHIJKLMNOPQRSTU\r\n0\r\n\r\n")
8536906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .clearHeaders()
8546906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .addHeader("Transfer-encoding: chunked"));
8556906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.play();
8566906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
8576906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
8586906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        try {
8596906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
8606906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            fail();
8616906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        } catch (IOException e) {
8626906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        }
8636906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    }
8646906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
8656906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    public void testMissingChunkBody() throws IOException {
8666906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.enqueue(new MockResponse()
8676906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .setBody("5")
8686906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .clearHeaders()
8696906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson                .addHeader("Transfer-encoding: chunked")
870e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson                .setSocketPolicy(DISCONNECT_AT_END));
8716906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        server.play();
8726906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
8736906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
8746906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        try {
8756906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            readAscii(connection.getInputStream(), Integer.MAX_VALUE);
8766906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson            fail();
8776906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        } catch (IOException e) {
8786906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson        }
8796906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson    }
8806906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson
88150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    /**
88250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * This test checks whether connections are gzipped by default. This
88350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * behavior in not required by the API, so a failure of this test does not
88450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     * imply a bug in the implementation.
88550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson     */
88650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException {
88750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.enqueue(new MockResponse()
88850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .setBody(gzip("ABCABCABC".getBytes("UTF-8")))
88950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .addHeader("Content-Encoding: gzip"));
89050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.play();
89150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
89250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
89350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
8948116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson        assertNull(connection.getContentEncoding());
89550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
89650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        RecordedRequest request = server.takeRequest();
89750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: gzip");
89850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    }
89950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
900deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testClientConfiguredGzipContentEncoding() throws Exception {
901deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(new MockResponse()
902deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson                .setBody(gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8")))
903deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson                .addHeader("Content-Encoding: gzip"));
904deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.play();
905deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
906deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
907deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "gzip");
908deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
909deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
910deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
911deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        RecordedRequest request = server.takeRequest();
912deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: gzip");
913deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
914deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
915deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testGzipAndConnectionReuseWithFixedLength() throws Exception {
916deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH);
917deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
918deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
919deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception {
920deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED);
921deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
922deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
92350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    public void testClientConfiguredCustomContentEncoding() throws Exception {
92450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.enqueue(new MockResponse()
92550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .setBody("ABCDE")
92650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson                .addHeader("Content-Encoding: custom"));
92750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        server.play();
92850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
92950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
93050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "custom");
93150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
93250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
93350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        RecordedRequest request = server.takeRequest();
93450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson        assertContains(request.getHeaders(), "Accept-Encoding: custom");
93550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson    }
93650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson
937deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    /**
938deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * Test a bug where gzip input streams weren't exhausting the input stream,
939deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * which corrupted the request that followed.
940deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=7059
941deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     */
942deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    private void testClientConfiguredGzipContentEncodingAndConnectionReuse(
943deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            TransferKind transferKind) throws Exception {
944deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        MockResponse responseOne = new MockResponse();
945deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        responseOne.addHeader("Content-Encoding: gzip");
946deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        transferKind.setBody(responseOne, gzip("one (gzipped)".getBytes("UTF-8")), 5);
947deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(responseOne);
948deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        MockResponse responseTwo = new MockResponse();
949deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        transferKind.setBody(responseTwo, "two (identity)", 5);
950deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.enqueue(responseTwo);
951deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        server.play();
952deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
953deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
954deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection.addRequestProperty("Accept-Encoding", "gzip");
955deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
956deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
957deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
958deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
959deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        connection = server.getUrl("/").openConnection();
960deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals("two (identity)", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
961deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        assertEquals(1, server.takeRequest().getSequenceNumber());
962deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
963deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
964deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    /**
965ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * Obnoxiously test that the chunk sizes transmitted exactly equal the
966ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * requested data+chunk header size. Although setChunkedStreamingMode()
967ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * isn't specific about whether the size applies to the data or the
968ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     * complete chunk, the RI interprets it as a complete chunk.
969ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson     */
970ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testSetChunkedStreamingMode() throws IOException, InterruptedException {
971ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse());
972ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
973ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
974ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
975ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        urlConnection.setChunkedStreamingMode(8);
976ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        urlConnection.setDoOutput(true);
977ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = urlConnection.getOutputStream();
978ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII"));
979ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(200, urlConnection.getResponseCode());
980ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
981ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
982ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII"));
983ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes());
984ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
985ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
986ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithFixedLengthStreaming() throws Exception {
987ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        testAuthenticateWithStreamingPost(StreamingMode.FIXED_LENGTH);
988ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
989ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
990ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithChunkedStreaming() throws Exception {
991ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        testAuthenticateWithStreamingPost(StreamingMode.CHUNKED);
992ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
993ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
994ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception {
995ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
996ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
997ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
998ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
999ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1000ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1001ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1002ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1003ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1004ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        connection.setDoOutput(true);
1005ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
1006ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        if (streamingMode == StreamingMode.FIXED_LENGTH) {
1007ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.setFixedLengthStreamingMode(requestBody.length);
1008ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        } else if (streamingMode == StreamingMode.CHUNKED) {
1009ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.setChunkedStreamingMode(0);
1010ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1011ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = connection.getOutputStream();
1012ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write(requestBody);
1013ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.close();
1014ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        try {
1015ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            connection.getInputStream();
1016ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            fail();
1017ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        } catch (HttpRetryException expected) {
1018ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1019ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1020ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the request...
1021ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1022ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1023ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
1024ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1025ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1026ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testSetValidRequestMethod() throws Exception {
1027ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1028ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("GET");
1029ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("DELETE");
1030ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("HEAD");
1031ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("OPTIONS");
1032ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("POST");
1033ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("PUT");
1034ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertValidRequestMethod("TRACE");
1035ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1036ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1037ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    private void assertValidRequestMethod(String requestMethod) throws Exception {
1038ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1039ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setRequestMethod(requestMethod);
1040ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals(requestMethod, connection.getRequestMethod());
1041ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1042ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1043ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testSetInvalidRequestMethodLowercase() throws Exception {
1044ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1045ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertInvalidRequestMethod("get");
1046ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1047ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1048ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testSetInvalidRequestMethodConnect() throws Exception {
1049ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1050ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertInvalidRequestMethod("CONNECT");
1051ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1052ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1053ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    private void assertInvalidRequestMethod(String requestMethod) throws Exception {
1054ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1055ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1056ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setRequestMethod(requestMethod);
1057ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1058ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (ProtocolException expected) {
1059ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1060ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1061ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1062ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCannotSetNegativeFixedLengthStreamingMode() throws Exception {
1063ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1064ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1065ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1066ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setFixedLengthStreamingMode(-2);
1067ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1068ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalArgumentException expected) {
1069ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1070ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1071ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1072ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCanSetNegativeChunkedStreamingMode() throws Exception {
1073ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1074ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1075ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setChunkedStreamingMode(-2);
1076ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1077ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1078ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCannotSetFixedLengthStreamingModeAfterConnect() throws Exception {
1079ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
1080ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1081ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1082ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1083ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1084ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setFixedLengthStreamingMode(1);
1085ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1086ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1087ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1088ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1089ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1090ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCannotSetChunkedStreamingModeAfterConnect() throws Exception {
1091ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
1092ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1093ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1094ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1095ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1096ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setChunkedStreamingMode(1);
1097ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1098ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1099ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1100ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1101ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1102ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCannotSetFixedLengthStreamingModeAfterChunkedStreamingMode() throws Exception {
1103ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1104ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1105ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setChunkedStreamingMode(1);
1106ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1107ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setFixedLengthStreamingMode(1);
1108ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1109ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1110ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1111ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1112ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1113ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testCannotSetChunkedStreamingModeAfterFixedLengthStreamingMode() throws Exception {
1114ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1115ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1116ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setFixedLengthStreamingMode(1);
1117ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1118ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setChunkedStreamingMode(1);
1119ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1120ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1121ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1122ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1123ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
112435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    public void testSecureFixedLengthStreaming() throws Exception {
112535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        testSecureStreamingPost(StreamingMode.FIXED_LENGTH);
112635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    }
112735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson
112835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    public void testSecureChunkedStreaming() throws Exception {
112935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        testSecureStreamingPost(StreamingMode.CHUNKED);
113035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    }
113135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson
113235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    /**
113335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson     * Users have reported problems using HTTPS with streaming request bodies.
113435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=12860
113535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson     */
113635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception {
113735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
113835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
113935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        server.enqueue(new MockResponse().setBody("Success!"));
114035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        server.play();
114135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson
114235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
114335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
114435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        connection.setDoOutput(true);
114535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
114635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        if (streamingMode == StreamingMode.FIXED_LENGTH) {
114735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson            connection.setFixedLengthStreamingMode(requestBody.length);
114835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        } else if (streamingMode == StreamingMode.CHUNKED) {
114935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson            connection.setChunkedStreamingMode(0);
115035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        }
115135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        OutputStream outputStream = connection.getOutputStream();
115235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        outputStream.write(requestBody);
115335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        outputStream.close();
115435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
115535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson
115635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        RecordedRequest request = server.takeRequest();
115735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        assertEquals("POST / HTTP/1.1", request.getRequestLine());
115835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        if (streamingMode == StreamingMode.FIXED_LENGTH) {
115935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson            assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes());
116035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        } else if (streamingMode == StreamingMode.CHUNKED) {
116135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson            assertEquals(Arrays.asList(4), request.getChunkSizes());
116235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        }
116335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson        assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
116435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson    }
116535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson
1166ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    enum StreamingMode {
1167ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        FIXED_LENGTH, CHUNKED
1168ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1169ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1170ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithPost() throws Exception {
1171ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
1172ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
1173ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
1174ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
1175ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // fail auth three times...
1176ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1177ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1178ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1179ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...then succeed the fourth time
1180ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse().setBody("Successful auth!"));
1181ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1182ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1183ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1184ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1185ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        connection.setDoOutput(true);
1186ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
1187ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        OutputStream outputStream = connection.getOutputStream();
1188ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.write(requestBody);
1189ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        outputStream.close();
1190ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1191ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1192ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the first request...
1193ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1194ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1195ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1196da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson        // ...but the three requests that follow include an authorization header
1197ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (int i = 0; i < 3; i++) {
1198ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            request = server.takeRequest();
1199ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals("POST / HTTP/1.1", request.getRequestLine());
1200c996149b500fc4825156106554457fe2394ae087Jesse Wilson            assertContains(request.getHeaders(), "Authorization: Basic " + BASE_64_CREDENTIALS);
1201ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
1202ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1203ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1204ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1205ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    public void testAuthenticateWithGet() throws Exception {
1206ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        MockResponse pleaseAuthenticate = new MockResponse()
1207ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setResponseCode(401)
1208ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
1209ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                .setBody("Please authenticate.");
1210ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // fail auth three times...
1211ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1212ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1213ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(pleaseAuthenticate);
1214ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...then succeed the fourth time
1215ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.enqueue(new MockResponse().setBody("Successful auth!"));
1216ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        server.play();
1217ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1218ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        Authenticator.setDefault(SIMPLE_AUTHENTICATOR);
1219ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1220ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1221ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1222ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // no authorization header for the first request...
1223ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        RecordedRequest request = server.takeRequest();
1224ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*");
1225ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1226ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        // ...but the three requests that follow requests include an authorization header
1227ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (int i = 0; i < 3; i++) {
1228ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            request = server.takeRequest();
1229ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            assertEquals("GET / HTTP/1.1", request.getRequestLine());
1230c996149b500fc4825156106554457fe2394ae087Jesse Wilson            assertContains(request.getHeaders(), "Authorization: Basic " + BASE_64_CREDENTIALS);
1231ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
1232ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
1233ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
1234c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithChunkedEncoding() throws Exception {
1235c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.CHUNKED, true);
1236c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1237c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1238c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithContentLengthHeader() throws Exception {
1239c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.FIXED_LENGTH, true);
1240c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1241c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1242c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedWithNoLengthHeaders() throws Exception {
1243c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        testRedirected(TransferKind.END_OF_STREAM, false);
1244c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1245c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1246c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception {
1247c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockResponse response = new MockResponse()
1248c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1249c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: /foo");
1250c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        transferKind.setBody(response, "This page has moved!", 10);
1251c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(response);
1252c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1253c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1254c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1255c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1256c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the new location!",
1257c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1258c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1259c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
1260c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET / HTTP/1.1", first.getRequestLine());
1261c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest retry = server.takeRequest();
1262c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
1263c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        if (reuse) {
1264c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
1265c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1266c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1267c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1268c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectedOnHttps() throws IOException, InterruptedException {
1269c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
1270059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1271c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1272c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1273c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: /foo")
1274c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1275c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1276c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1277c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1278c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
1279059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
1280c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the new location!",
1281c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1282c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1283c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
1284c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET / HTTP/1.1", first.getRequestLine());
1285c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest retry = server.takeRequest();
1286c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
1287c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
1288c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1289c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1290c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testNotRedirectedFromHttpsToHttp() throws IOException, InterruptedException {
1291c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        TestSSLContext testSSLContext = TestSSLContext.create();
1292059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1293c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1294c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1295c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: http://anyhost/foo")
1296c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1297c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1298c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1299c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
1300059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom        connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
1301c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This page has moved!",
1302c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1303c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1304c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1305c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testNotRedirectedFromHttpToHttps() throws IOException, InterruptedException {
1306c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1307c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1308c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: https://anyhost/foo")
1309c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1310c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1311c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1312c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1313c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This page has moved!",
1314c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1315c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1316c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1317c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testRedirectToAnotherOriginServer() throws Exception {
1318c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        MockWebServer server2 = new MockWebServer();
1319c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
1320c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.play();
1321c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1322c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse()
1323c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1324c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .addHeader("Location: " + server2.getUrl("/").toString())
1325c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                .setBody("This page has moved!"));
1326c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the first server again!"));
1327c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server.play();
1328c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1329c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1330c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the 2nd server!",
1331c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1332c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals(server2.getUrl("/"), connection.getURL());
1333c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1334c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        // make sure the first server was careful to recycle the connection
1335c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("This is the first server again!",
1336c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
1337c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1338c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest first = server.takeRequest();
13390c2fd828abec671333b8b88281825fd27a783723Jesse Wilson        assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort());
1340c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest second = server2.takeRequest();
13410c2fd828abec671333b8b88281825fd27a783723Jesse Wilson        assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort());
1342c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordedRequest third = server.takeRequest();
1343c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        assertEquals("Expected connection reuse", 1, third.getSequenceNumber());
1344c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1345c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        server2.shutdown();
1346c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1347c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1348d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testResponse300MultipleChoiceWithPost() throws Exception {
1349d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        // Chrome doesn't follow the redirect, but Firefox and the RI both do
1350d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        testResponseRedirectedWithPost(HttpURLConnection.HTTP_MULT_CHOICE);
1351d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1352d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1353d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testResponse301MovedPermanentlyWithPost() throws Exception {
1354d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_PERM);
1355d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1356d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1357d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testResponse302MovedTemporarilyWithPost() throws Exception {
1358d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_TEMP);
1359d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1360d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1361d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testResponse303SeeOtherWithPost() throws Exception {
1362d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        testResponseRedirectedWithPost(HttpURLConnection.HTTP_SEE_OTHER);
1363d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1364d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1365d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    private void testResponseRedirectedWithPost(int redirectCode) throws Exception {
1366d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.enqueue(new MockResponse()
1367d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .setResponseCode(redirectCode)
1368d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .addHeader("Location: /page2")
1369d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .setBody("This page has moved!"));
1370d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.enqueue(new MockResponse().setBody("Page 2"));
1371d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.play();
1372d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1373d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/page1").openConnection();
1374d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        connection.setDoOutput(true);
1375d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        byte[] requestBody = { 'A', 'B', 'C', 'D' };
1376d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        OutputStream outputStream = connection.getOutputStream();
1377d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        outputStream.write(requestBody);
1378d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        outputStream.close();
1379d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1380d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertTrue(connection.getDoOutput());
1381d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1382d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        RecordedRequest page1 = server.takeRequest();
1383d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
1384d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals(Arrays.toString(requestBody), Arrays.toString(page1.getBody()));
1385d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1386d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        RecordedRequest page2 = server.takeRequest();
1387d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
1388d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1389d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1390d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    public void testResponse305UseProxy() throws Exception {
1391d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.play();
1392d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.enqueue(new MockResponse()
1393d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_USE_PROXY)
1394d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .addHeader("Location: " + server.getUrl("/"))
1395d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                .setBody("This page has moved!"));
1396d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        server.enqueue(new MockResponse().setBody("Proxy Response"));
1397d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1398d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/foo").openConnection();
1399d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        // Fails on the RI, which gets "Proxy Response"
1400d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals("This page has moved!",
1401d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1402d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1403d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        RecordedRequest page1 = server.takeRequest();
1404d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals("GET /foo HTTP/1.1", page1.getRequestLine());
1405d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson        assertEquals(1, server.getRequestCount());
1406d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson    }
1407d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson
1408c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    public void testHttpsWithCustomTrustManager() throws Exception {
1409c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
1410c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        RecordingTrustManager trustManager = new RecordingTrustManager();
1411c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        SSLContext sc = SSLContext.getInstance("TLS");
1412c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
1413c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1414c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
1415c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
1416c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
1417c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
1418c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        try {
1419c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            TestSSLContext testSSLContext = TestSSLContext.create();
1420059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom            server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
1421c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("ABC"));
1422c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("DEF"));
1423c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.enqueue(new MockResponse().setBody("GHI"));
1424c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            server.play();
1425c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
1426c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            URL url = server.getUrl("/");
1427c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
1428c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
1429c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
1430c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
14310c2fd828abec671333b8b88281825fd27a783723Jesse Wilson            assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls);
14324559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom            assertEquals(Arrays.asList("checkServerTrusted ["
14330c2fd828abec671333b8b88281825fd27a783723Jesse Wilson                    + "CN=" + hostName + " 1, "
1434b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson                    + "CN=Test Intermediate Certificate Authority 1, "
1435b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson                    + "CN=Test Root Certificate Authority 1"
1436b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson                    + "] RSA"),
1437c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                    trustManager.calls);
1438c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        } finally {
1439c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
1440c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
1441c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
1442c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
1443c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
14442d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson    /**
14452d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson     * Test that the timeout period is honored. The timeout may be doubled!
14462d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson     * HttpURLConnection will wait the full timeout for each of the server's IP
14472d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson     * addresses. This is typically one IPv4 address and one IPv6 address.
14482d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson     */
1449eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    public void testConnectTimeouts() throws IOException {
14505fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes        StuckServer ss = new StuckServer();
14515fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes        int serverPort = ss.getLocalPort();
1452f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        URLConnection urlConnection = new URL("http://localhost:" + serverPort).openConnection();
1453b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        int timeout = 1000;
1454b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        urlConnection.setConnectTimeout(timeout);
1455b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        long start = System.currentTimeMillis();
1456eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        try {
1457eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            urlConnection.getInputStream();
1458eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            fail();
1459eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        } catch (SocketTimeoutException expected) {
14602d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson            long elapsed = System.currentTimeMillis() - start;
14612d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson            int attempts = InetAddress.getAllByName("localhost").length; // one per IP address
14622d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson            assertTrue("timeout=" +timeout + ", elapsed=" + elapsed + ", attempts=" + attempts,
14632d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson                    Math.abs((attempts * timeout) - elapsed) < 500);
14645fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes        } finally {
14655fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes            ss.close();
1466f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        }
1467eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    }
1468eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1469eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    public void testReadTimeouts() throws IOException {
1470eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        /*
1471eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * This relies on the fact that MockWebServer doesn't close the
1472eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * connection after a response has been sent. This causes the client to
1473eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         * try to read more bytes than are sent, which results in a timeout.
1474eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson         */
1475eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        MockResponse timeout = new MockResponse()
1476eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .setBody("ABC")
1477eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .clearHeaders()
1478eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson                .addHeader("Content-Length: 4");
1479eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        server.enqueue(timeout);
1480b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        server.enqueue(new MockResponse().setBody("unused")); // to keep the server alive
1481eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        server.play();
1482eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1483eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        URLConnection urlConnection = server.getUrl("/").openConnection();
1484eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        urlConnection.setReadTimeout(1000);
1485eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        InputStream in = urlConnection.getInputStream();
1486eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('A', in.read());
1487eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('B', in.read());
1488eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        assertEquals('C', in.read());
1489eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        try {
1490eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            in.read(); // if Content-Length was accurate, this would return -1 immediately
1491eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson            fail();
1492eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        } catch (SocketTimeoutException expected) {
1493eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson        }
1494eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    }
1495eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson
1496125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson    public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException {
1497125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        server.enqueue(new MockResponse());
1498125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        server.play();
1499125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1500125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection();
1501125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.setRequestProperty("Transfer-encoding", "chunked");
1502125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.setDoOutput(true);
1503125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        urlConnection.getOutputStream().write("ABC".getBytes("UTF-8"));
1504125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        assertEquals(200, urlConnection.getResponseCode());
1505125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1506125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        RecordedRequest request = server.takeRequest();
1507125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson        assertEquals("ABC", new String(request.getBody(), "UTF-8"));
1508125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson    }
1509125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson
1510f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseInRequest() throws IOException, InterruptedException {
1511f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse()); // server doesn't honor the connection: close header!
1512f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse());
1513f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1514f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1515f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
1516f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        a.setRequestProperty("Connection", "close");
1517f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, a.getResponseCode());
1518f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1519f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
1520f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, b.getResponseCode());
1521f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1522f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1523f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1524f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1525f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1526f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1527f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseInResponse() throws IOException, InterruptedException {
1528f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse().addHeader("Connection: close"));
1529f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse());
1530f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1531f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1532f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection();
1533f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, a.getResponseCode());
1534f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1535f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection();
1536f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(200, b.getResponseCode());
1537f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1538f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1539f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1540f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1541f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1542f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1543f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    public void testConnectionCloseWithRedirect() throws IOException, InterruptedException {
1544f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        MockResponse response = new MockResponse()
1545f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
1546f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .addHeader("Location: /foo")
1547f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                .addHeader("Connection: close");
1548f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(response);
1549f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.enqueue(new MockResponse().setBody("This is the new location!"));
1550f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        server.play();
1551f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1552f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1553f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("This is the new location!",
1554f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1555f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
1556f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals(0, server.takeRequest().getSequenceNumber());
1557f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson        assertEquals("When connection: close is used, each request should get its own connection",
1558f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson                0, server.takeRequest().getSequenceNumber());
1559f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson    }
1560f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson
156165d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson    public void testResponseCodeDisagreesWithHeaders() throws IOException, InterruptedException {
156265d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        server.enqueue(new MockResponse()
156365d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)
156465d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                .setBody("This body is not allowed!"));
156565d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        server.play();
156665d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson
156765d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
156865d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson        assertEquals("This body is not allowed!",
156965d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson                readAscii(connection.getInputStream(), Integer.MAX_VALUE));
157065d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson    }
157165d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson
1572ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson    public void testSingleByteReadIsSigned() throws IOException {
1573ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        server.enqueue(new MockResponse().setBody(new byte[] { -2, -1 }));
1574ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        server.play();
1575ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson
1576ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
1577ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        InputStream in = connection.getInputStream();
1578ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(254, in.read());
1579ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(255, in.read());
1580ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson        assertEquals(-1, in.read());
1581ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson    }
1582ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson
1583f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithChunkedEncoding() throws IOException {
1584f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.CHUNKED);
1585f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1586f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1587f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithFixedLength() throws IOException {
1588f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.FIXED_LENGTH);
1589f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1590f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1591f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    public void testFlushAfterStreamTransmittedWithNoLengthHeaders() throws IOException {
1592f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        testFlushAfterStreamTransmitted(TransferKind.END_OF_STREAM);
1593f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1594f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1595f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    /**
1596f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * We explicitly permit apps to close the upload stream even after it has
1597f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * been transmitted.  We also permit flush so that buffered streams can
1598f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     * do a no-op flush when they are closed. http://b/3038470
1599f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson     */
1600f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    private void testFlushAfterStreamTransmitted(TransferKind transferKind) throws IOException {
1601f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        server.enqueue(new MockResponse().setBody("abc"));
1602f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        server.play();
1603f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1604f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1605f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        connection.setDoOutput(true);
1606f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        byte[] upload = "def".getBytes("UTF-8");
1607f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1608f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        if (transferKind == TransferKind.CHUNKED) {
1609f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            connection.setChunkedStreamingMode(0);
1610f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        } else if (transferKind == TransferKind.FIXED_LENGTH) {
1611f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            connection.setFixedLengthStreamingMode(upload.length);
1612f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        }
1613f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1614f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        OutputStream out = connection.getOutputStream();
1615f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        out.write(upload);
1616f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        assertEquals("abc", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1617f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1618f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        out.flush(); // dubious but permitted
1619f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        try {
1620f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            out.write("ghi".getBytes("UTF-8"));
1621f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson            fail();
1622f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        } catch (IOException expected) {
1623f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        }
1624f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1625f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1626c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson    public void testGetHeadersThrows() throws IOException {
1627e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson        server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
1628c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        server.play();
1629c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson
1630c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1631c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        try {
1632c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson            connection.getInputStream();
1633c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson            fail();
1634c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        } catch (IOException expected) {
1635c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        }
1636c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson
1637c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson        try {
1638c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson            connection.getInputStream();
1639c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson            fail();
1640f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        } catch (IOException expected) {
1641f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson        }
1642f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson    }
1643f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson
1644b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson    public void testGetKeepAlive() throws Exception {
1645b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        MockWebServer server = new MockWebServer();
1646b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
1647b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        server.play();
1648b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson
1649b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        // The request should work once and then fail
1650b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        URLConnection connection = server.getUrl("").openConnection();
1651b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        InputStream input = connection.getInputStream();
1652b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
1653b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        input.close();
1654b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        try {
1655b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson            server.getUrl("").openConnection().getInputStream();
1656b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson            fail();
1657b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        } catch (ConnectException expected) {
1658b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson        }
1659b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson    }
1660b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson
1661b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson    /**
1662d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * This test goes through the exhaustive set of interesting ASCII characters
1663d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * because most of those characters are interesting in some way according to
1664d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * RFC 2396 and RFC 2732. http://b/1158780
1665b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson     */
1666d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    public void testLenientUrlToUri() throws Exception {
1667d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        // alphanum
1668d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09");
1669d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1670d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        // control characters
1671d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u0001", "%01", "%01", "%01", "%01");
1672d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u001f", "%1F", "%1F", "%1F", "%1F");
1673d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1674d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        // ascii characters
1675d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("%20", "%20", "%20", "%20", "%20");
1676d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("%20", "%20", "%20", "%20", "%20");
1677d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(" ", "%20", "%20", "%20", "%20");
1678d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("!", "!", "!", "!", "!");
1679d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\"", "%22", "%22", "%22", "%22");
1680d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("#", null, null, null, "%23");
1681d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("$", "$", "$", "$", "$");
1682d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("&", "&", "&", "&", "&");
1683d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("'", "'", "'", "'", "'");
1684d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("(", "(", "(", "(", "(");
1685d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(")", ")", ")", ")", ")");
1686d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("*", "*", "*", "*", "*");
1687d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("+", "+", "+", "+", "+");
1688d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(",", ",", ",", ",", ",");
1689d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("-", "-", "-", "-", "-");
1690d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(".", ".", ".", ".", ".");
1691d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("/", null, "/", "/", "/");
1692d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(":", null, ":", ":", ":");
1693d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(";", ";", ";", ";", ";");
1694d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("<", "%3C", "%3C", "%3C", "%3C");
1695d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("=", "=", "=", "=", "=");
1696d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping(">", "%3E", "%3E", "%3E", "%3E");
1697d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("?", null, null, "?", "?");
1698d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("@", "@", "@", "@", "@");
1699d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("[", null, "%5B", null, "%5B");
1700d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\\", "%5C", "%5C", "%5C", "%5C");
1701d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("]", null, "%5D", null, "%5D");
1702d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("^", "%5E", "%5E", "%5E", "%5E");
1703d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("_", "_", "_", "_", "_");
1704d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("`", "%60", "%60", "%60", "%60");
1705d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("{", "%7B", "%7B", "%7B", "%7B");
1706d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("|", "%7C", "%7C", "%7C", "%7C");
1707d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("}", "%7D", "%7D", "%7D", "%7D");
1708d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("~", "~", "~", "~", "~");
1709d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("~", "~", "~", "~", "~");
1710d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u007f", "%7F", "%7F", "%7F", "%7F");
1711d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1712d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        // beyond ascii
1713d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u0080", "%C2%80", "%C2%80", "%C2%80", "%C2%80");
1714d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u20ac", "\u20ac", "\u20ac", "\u20ac", "\u20ac");
171532559028b14b9b321b10eede050afd554a376569Jesse Wilson        testUrlToUriMapping("\ud842\udf9f",
171632559028b14b9b321b10eede050afd554a376569Jesse Wilson                "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f");
1717d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    }
1718d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1719d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    public void testLenientUrlToUriNul() throws Exception {
1720d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        testUrlToUriMapping("\u0000", "%00", "%00", "%00", "%00"); // RI fails this
1721d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    }
1722d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1723d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    private void testUrlToUriMapping(String string, String asAuthority, String asFile,
1724d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            String asQuery, String asFragment) throws Exception {
1725d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        if (asAuthority != null) {
1726d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            assertEquals("http://host" + asAuthority + ".tld/",
1727d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                    backdoorUrlToUri(new URL("http://host" + string + ".tld/")).toString());
1728d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        }
1729d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        if (asFile != null) {
1730d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            assertEquals("http://host.tld/file" + asFile + "/",
1731d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                    backdoorUrlToUri(new URL("http://host.tld/file" + string + "/")).toString());
1732d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        }
1733d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        if (asQuery != null) {
1734d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            assertEquals("http://host.tld/file?q" + asQuery + "=x",
1735d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                    backdoorUrlToUri(new URL("http://host.tld/file?q" + string + "=x")).toString());
1736d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        }
1737d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        assertEquals("http://host.tld/file#" + asFragment + "-x",
1738d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                backdoorUrlToUri(new URL("http://host.tld/file#" + asFragment + "-x")).toString());
1739d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    }
1740b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson
1741d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    /**
1742d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI,
1743d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * HttpURLConnection recovers from URLs with unescaped but unsupported URI
1744d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     * characters like '{' and '|' by escaping these characters.
1745d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson     */
1746d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson    private URI backdoorUrlToUri(URL url) throws Exception {
1747d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        final AtomicReference<URI> uriReference = new AtomicReference<URI>();
1748d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1749d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        ResponseCache.setDefault(new ResponseCache() {
1750d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException {
1751d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                return null;
1752d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            }
1753d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            @Override public CacheResponse get(URI uri, String requestMethod,
1754d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                    Map<String, List<String>> requestHeaders) throws IOException {
1755d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                uriReference.set(uri);
1756d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson                throw new UnsupportedOperationException();
1757d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            }
1758d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        });
1759d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1760d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        try {
1761d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
1762d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson            connection.getResponseCode();
1763d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        } catch (Exception expected) {
1764d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        }
1765d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
1766d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson        return uriReference.get();
1767b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson    }
1768b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson
1769afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson    /**
1770afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson     * Don't explode if the cache returns a null body. http://b/3373699
1771afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson     */
1772afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson    public void testResponseCacheReturnsNullOutputStream() throws Exception {
1773afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        final AtomicBoolean aborted = new AtomicBoolean();
1774afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        ResponseCache.setDefault(new ResponseCache() {
1775afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson            @Override public CacheResponse get(URI uri, String requestMethod,
1776afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                    Map<String, List<String>> requestHeaders) throws IOException {
1777afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                return null;
1778afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson            }
1779afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson            @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException {
1780afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                return new CacheRequest() {
1781afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                    @Override public void abort() {
1782afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                        aborted.set(true);
1783afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                    }
1784afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                    @Override public OutputStream getBody() throws IOException {
1785afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                        return null;
1786afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                    }
1787afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson                };
1788afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson            }
1789afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        });
1790afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson
1791afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        server.enqueue(new MockResponse().setBody("abcdef"));
1792afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        server.play();
1793afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson
1794afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1795afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        InputStream in = connection.getInputStream();
1796afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        assertEquals("abc", readAscii(in, 3));
1797afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        in.close();
1798afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson        assertFalse(aborted.get()); // The best behavior is ambiguous, but RI 6 doesn't abort here
1799afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson    }
1800d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson
18015e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson
18025e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson    /**
18035e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=14562
18045e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson     */
18055e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson    public void testReadAfterLastByte() throws Exception {
18065e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        server.enqueue(new MockResponse()
18075e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson                .setBody("ABC")
18085e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson                .clearHeaders()
18095e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson                .addHeader("Connection: close")
18105e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson                .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
18115e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        server.play();
18125e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson
18135e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
18145e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        InputStream in = connection.getInputStream();
18155e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        assertEquals("ABC", readAscii(in, 3));
18165e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson        assertEquals(-1, in.read());
1817bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson        assertEquals(-1, in.read()); // throws IOException in Gingerbread
1818bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson    }
1819bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson
1820ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetContent() throws Exception {
1821ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
1822ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1823ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1824ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        InputStream in = (InputStream) connection.getContent();
1825ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", readAscii(in, Integer.MAX_VALUE));
1826ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1827ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1828ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetContentOfType() throws Exception {
1829ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
1830ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1831ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1832ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1833ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.getContent(null);
1834ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1835ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (NullPointerException expected) {
1836ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1837ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1838ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.getContent(new Class[] { null });
1839ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1840ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (NullPointerException expected) {
1841ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1842ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertNull(connection.getContent(new Class[] { getClass() }));
1843ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.disconnect();
1844ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1845ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1846ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetOutputStreamOnGetFails() throws Exception {
1847ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse());
1848ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1849ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1850ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1851ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.getOutputStream();
1852ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1853ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (ProtocolException expected) {
1854ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1855ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1856ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1857ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetOutputAfterGetInputStreamFails() throws Exception {
1858ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse());
1859ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1860ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1861ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setDoOutput(true);
1862ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1863ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.getInputStream();
1864ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.getOutputStream();
1865ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1866ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (ProtocolException expected) {
1867ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1868ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1869ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1870ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testSetDoOutputOrDoInputAfterConnectFails() throws Exception {
1871ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse());
1872ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1873ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1874ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.connect();
1875ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1876ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setDoOutput(true);
1877ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1878ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1879ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1880ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        try {
1881ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            connection.setDoInput(true);
1882ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson            fail();
1883ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        } catch (IllegalStateException expected) {
1884ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        }
1885ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.disconnect();
1886ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1887ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1888ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testClientSendsContentLength() throws Exception {
1889ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("A"));
1890ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1891ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1892ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.setDoOutput(true);
1893ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        OutputStream out = connection.getOutputStream();
1894ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        out.write(new byte[] { 'A', 'B', 'C' });
1895ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        out.close();
1896ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
1897ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        RecordedRequest request = server.takeRequest();
1898ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertContains(request.getHeaders(), "Content-Length: 3");
1899ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1900ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1901ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetContentLengthConnects() throws Exception {
1902ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse().setBody("ABC"));
1903ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1904ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1905ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals(3, connection.getContentLength());
1906ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.disconnect();
1907ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1908ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1909ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetContentTypeConnects() throws Exception {
1910ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse()
1911ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .addHeader("Content-Type: text/plain")
1912ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .setBody("ABC"));
1913ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1914ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1915ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("text/plain", connection.getContentType());
1916ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.disconnect();
1917ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1918ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
1919ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    public void testGetContentEncodingConnects() throws Exception {
1920ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.enqueue(new MockResponse()
1921ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .addHeader("Content-Encoding: identity")
1922ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson                .setBody("ABC"));
1923ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        server.play();
1924ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection();
1925ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        assertEquals("identity", connection.getContentEncoding());
1926ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson        connection.disconnect();
1927ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson    }
1928ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson
19295292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson    // http://b/4361656
19305292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson    public void testUrlContainsQueryButNoPath() throws Exception {
19315292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        server.enqueue(new MockResponse().setBody("A"));
19325292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        server.play();
19335292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        URL url = new URL("http", server.getHostName(), server.getPort(), "?query");
19345292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        assertEquals("A", readAscii(url.openConnection().getInputStream(), Integer.MAX_VALUE));
19355292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        RecordedRequest request = server.takeRequest();
19365292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson        assertEquals("GET /?query HTTP/1.1", request.getRequestLine());
19375292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson    }
19385292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson
193925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    // http://code.google.com/p/android/issues/detail?id=20442
194025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    public void testInputStreamAvailableWithChunkedEncoding() throws Exception {
194125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        testInputStreamAvailable(TransferKind.CHUNKED);
194225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    }
194325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson
194425a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    public void testInputStreamAvailableWithContentLengthHeader() throws Exception {
194525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        testInputStreamAvailable(TransferKind.FIXED_LENGTH);
194625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    }
194725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson
194825a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    public void testInputStreamAvailableWithNoLengthHeaders() throws Exception {
194925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        testInputStreamAvailable(TransferKind.END_OF_STREAM);
195025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    }
195125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson
195225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    private void testInputStreamAvailable(TransferKind transferKind) throws IOException {
195325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        String body = "ABCDEFGH";
195425a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        MockResponse response = new MockResponse();
195525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        transferKind.setBody(response, body, 4);
195625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        server.enqueue(response);
195725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        server.play();
195825a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        URLConnection connection = server.getUrl("/").openConnection();
195925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        InputStream in = connection.getInputStream();
196025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        for (int i = 0; i < body.length(); i++) {
196125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson            assertTrue(in.available() >= 0);
196225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson            assertEquals(body.charAt(i), in.read());
196325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        }
196425a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        assertEquals(0, in.available());
196525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson        assertEquals(-1, in.read());
196625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson    }
196725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson
1968d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson    // http://code.google.com/p/android/issues/detail?id=16895
1969d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson    public void testUrlWithSpaceInHost() throws Exception {
1970d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        URLConnection urlConnection = new URL("http://and roid.com/").openConnection();
1971d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        try {
1972d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson            urlConnection.getInputStream();
1973d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson            fail();
1974d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        } catch (UnknownHostException expected) {
1975d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        }
1976d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson    }
1977d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson
1978d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson    public void testUrlWithSpaceInHostViaHttpProxy() throws Exception {
1979d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        server.enqueue(new MockResponse());
1980d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        server.play();
1981d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        URLConnection urlConnection = new URL("http://and roid.com/")
1982d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson                .openConnection(server.toProxyAddress());
1983d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        try {
1984d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson            urlConnection.getInputStream();
1985d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson            fail(); // the RI makes a bogus proxy request for "GET http://and roid.com/ HTTP/1.1"
1986d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        } catch (UnknownHostException expected) {
1987d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson        }
1988d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson    }
1989d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson
1990bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson    /**
19910c59055dd24e1659f85d9ff7e2148883f663bd62Jesse Wilson     * Returns a gzipped copy of {@code bytes}.
1992deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson     */
1993deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    public byte[] gzip(byte[] bytes) throws IOException {
1994deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
1995deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        OutputStream gzippedOut = new GZIPOutputStream(bytesOut);
1996deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        gzippedOut.write(bytes);
1997deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        gzippedOut.close();
1998deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        return bytesOut.toByteArray();
1999deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson    }
2000deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
2001c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    /**
2002c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     * Reads at most {@code limit} characters from {@code in} and asserts that
2003c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     * content equals {@code expected}.
2004c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson     */
2005c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    private void assertContent(String expected, URLConnection connection, int limit)
2006c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson            throws IOException {
2007f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        connection.connect();
200851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        assertEquals(expected, readAscii(connection.getInputStream(), limit));
2009c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        ((HttpURLConnection) connection).disconnect();
2010c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
2011c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
2012c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    private void assertContent(String expected, URLConnection connection) throws IOException {
2013c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson        assertContent(expected, connection, Integer.MAX_VALUE);
2014c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson    }
2015c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson
201660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    private void assertContains(List<String> headers, String header) {
201760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson        assertTrue(headers.toString(), headers.contains(header));
201860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson    }
201951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
2020ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    private void assertContainsNoneMatching(List<String> headers, String pattern) {
2021ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        for (String header : headers) {
2022ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            if (header.matches(pattern)) {
2023ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson                fail("Header " + header + " matches " + pattern);
2024ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson            }
2025ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson        }
2026ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson    }
2027ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson
2028eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson    private Set<String> newSet(String... elements) {
202983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson        return new HashSet<String>(Arrays.asList(elements));
203083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson    }
203183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson
203251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    enum TransferKind {
203351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        CHUNKED() {
2034deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize)
203551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    throws IOException {
203651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setChunkedBody(content, chunkSize);
203751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
203851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        },
203951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        FIXED_LENGTH() {
2040deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize) {
204151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setBody(content);
204251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
204351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        },
204451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        END_OF_STREAM() {
2045deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            @Override void setBody(MockResponse response, byte[] content, int chunkSize) {
204651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                response.setBody(content);
2047e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson                response.setSocketPolicy(DISCONNECT_AT_END);
204851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                for (Iterator<String> h = response.getHeaders().iterator(); h.hasNext(); ) {
204951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    if (h.next().startsWith("Content-Length:")) {
205051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                        h.remove();
205151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                        break;
205251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                    }
205351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                }
205451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson            }
205551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson        };
205651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson
2057deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        abstract void setBody(MockResponse response, byte[] content, int chunkSize)
205851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson                throws IOException;
2059deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson
2060deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        void setBody(MockResponse response, String content, int chunkSize) throws IOException {
2061deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson            setBody(response, content.getBytes("UTF-8"), chunkSize);
2062deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson        }
206351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson    }
2064c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2065984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    enum ProxyConfig {
2066f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        NO_PROXY() {
2067f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom            @Override public HttpURLConnection connect(MockWebServer server, URL url)
2068f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom                    throws IOException {
2069f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom                return (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
2070f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom            }
2071f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom        },
2072f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom
2073984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        CREATE_ARG() {
2074984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
2075984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
2076984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection(server.toProxyAddress());
2077984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
2078984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
2079984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
2080984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        PROXY_SYSTEM_PROPERTY() {
2081984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
2082984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
2083984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("proxyHost", "localhost");
2084984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("proxyPort", Integer.toString(server.getPort()));
2085984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
2086984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
2087984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
2088984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
2089984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HTTP_PROXY_SYSTEM_PROPERTY() {
2090984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
2091984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
2092984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("http.proxyHost", "localhost");
2093984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("http.proxyPort", Integer.toString(server.getPort()));
2094984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
2095984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
2096984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        },
2097984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
2098984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        HTTPS_PROXY_SYSTEM_PROPERTY() {
2099984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            @Override public HttpURLConnection connect(MockWebServer server, URL url)
2100984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                    throws IOException {
2101984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("https.proxyHost", "localhost");
2102984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                System.setProperty("https.proxyPort", Integer.toString(server.getPort()));
2103984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson                return (HttpURLConnection) url.openConnection();
2104984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson            }
2105984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        };
2106984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
2107984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson        public abstract HttpURLConnection connect(MockWebServer server, URL url) throws IOException;
2108984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson    }
2109984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson
2110c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private static class RecordingTrustManager implements X509TrustManager {
2111c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private final List<String> calls = new ArrayList<String>();
2112c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2113c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public X509Certificate[] getAcceptedIssuers() {
2114c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("getAcceptedIssuers");
2115c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return new X509Certificate[] {};
2116c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
2117c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2118c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public void checkClientTrusted(X509Certificate[] chain, String authType)
2119c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                throws CertificateException {
2120c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("checkClientTrusted " + certificatesToString(chain) + " " + authType);
2121c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
2122c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2123c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public void checkServerTrusted(X509Certificate[] chain, String authType)
2124c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                throws CertificateException {
2125c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("checkServerTrusted " + certificatesToString(chain) + " " + authType);
2126c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
2127c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2128c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private String certificatesToString(X509Certificate[] certificates) {
2129c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            List<String> result = new ArrayList<String>();
2130c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            for (X509Certificate certificate : certificates) {
2131c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson                result.add(certificate.getSubjectDN() + " " + certificate.getSerialNumber());
2132c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            }
2133c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return result.toString();
2134c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
2135c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
2136c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2137c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    private static class RecordingHostnameVerifier implements HostnameVerifier {
2138c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        private final List<String> calls = new ArrayList<String>();
2139c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson
2140c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        public boolean verify(String hostname, SSLSession session) {
2141c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            calls.add("verify " + hostname);
2142c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson            return true;
2143c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson        }
2144c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson    }
2145e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes}
2146