URLConnectionTest.java revision c996149b500fc4825156106554457fe2394ae087
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 19c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilsonimport java.io.ByteArrayOutputStream; 206247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.io.IOException; 2151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.io.InputStream; 2202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.io.OutputStream; 234557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.Authenticator; 244557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheRequest; 254557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheResponse; 26b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilsonimport java.net.ConnectException; 274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpRetryException; 284557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpURLConnection; 294557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.PasswordAuthentication; 30f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilsonimport java.net.ProtocolException; 31f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstromimport java.net.Proxy; 324557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.ResponseCache; 334557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.SocketTimeoutException; 344557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URI; 354557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URL; 364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URLConnection; 37c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.CertificateException; 38c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.X509Certificate; 3951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.ArrayList; 4002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.util.Arrays; 4151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Collections; 4283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.HashSet; 4351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Iterator; 446247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.util.List; 4583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.Map; 4651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Set; 47afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilsonimport java.util.concurrent.atomic.AtomicBoolean; 4883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.concurrent.atomic.AtomicReference; 49deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPInputStream; 50deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPOutputStream; 5160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HostnameVerifier; 5260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HttpsURLConnection; 53c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLContext; 54096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilsonimport javax.net.ssl.SSLException; 552915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport javax.net.ssl.SSLHandshakeException; 5660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.SSLSession; 57c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLSocketFactory; 58c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.TrustManager; 59c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.X509TrustManager; 602915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport libcore.java.security.TestKeyStore; 6150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilsonimport libcore.javax.net.ssl.TestSSLContext; 6260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.MockResponse; 6360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.MockWebServer; 6460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport tests.http.RecordedRequest; 65e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilsonimport tests.http.SocketPolicy; 66e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilsonimport static tests.http.SocketPolicy.DISCONNECT_AT_END; 67e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilsonimport static tests.http.SocketPolicy.DISCONNECT_AT_START; 68e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilsonimport static tests.http.SocketPolicy.SHUTDOWN_INPUT_AT_END; 69e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilsonimport static tests.http.SocketPolicy.SHUTDOWN_OUTPUT_AT_END; 705fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughesimport tests.net.StuckServer; 71e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes 72e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughespublic class URLConnectionTest extends junit.framework.TestCase { 73b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 74ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson private static final Authenticator SIMPLE_AUTHENTICATOR = new Authenticator() { 75ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson protected PasswordAuthentication getPasswordAuthentication() { 76ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson return new PasswordAuthentication("username", "password".toCharArray()); 77ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 78ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson }; 79ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 80c996149b500fc4825156106554457fe2394ae087Jesse Wilson /** base64("username:password") */ 81c996149b500fc4825156106554457fe2394ae087Jesse Wilson private static final String BASE_64_CREDENTIALS = "dXNlcm5hbWU6cGFzc3dvcmQ="; 82c996149b500fc4825156106554457fe2394ae087Jesse Wilson 8351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private MockWebServer server = new MockWebServer(); 840c2fd828abec671333b8b88281825fd27a783723Jesse Wilson private String hostName; 8500feece22909b7dc79fc96d666d157390b93858eJesse Wilson 8600feece22909b7dc79fc96d666d157390b93858eJesse Wilson @Override protected void setUp() throws Exception { 8700feece22909b7dc79fc96d666d157390b93858eJesse Wilson super.setUp(); 880c2fd828abec671333b8b88281825fd27a783723Jesse Wilson hostName = server.getHostName(); 8900feece22909b7dc79fc96d666d157390b93858eJesse Wilson } 9051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 9151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson @Override protected void tearDown() throws Exception { 9251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson ResponseCache.setDefault(null); 93ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson Authenticator.setDefault(null); 94984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("proxyHost"); 95984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("proxyPort"); 96984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("http.proxyHost"); 97984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("http.proxyPort"); 98984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("https.proxyHost"); 99984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("https.proxyPort"); 10051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.shutdown(); 10100feece22909b7dc79fc96d666d157390b93858eJesse Wilson super.tearDown(); 10251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 10351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 10483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson public void testRequestHeaders() throws IOException, InterruptedException { 10583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.enqueue(new MockResponse()); 10683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.play(); 10783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 10883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 10983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("D", "e"); 11083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("D", "f"); 11183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties(); 11283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D"))); 11383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 11483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson requestHeaders.put("G", Arrays.asList("h")); 11583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 11683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 11783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 11883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 11983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson requestHeaders.get("D").add("i"); 12083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 12183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 12283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 12383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 12483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty(null, "j"); 12583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail(); 12683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (NullPointerException expected) { 12783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 12883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 12983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty(null, "k"); 13083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail(); 13183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (NullPointerException expected) { 13283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 13383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty("NullValue", null); // should fail silently! 13483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("AnotherNullValue", null); // should fail silently! 13583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 13683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.getResponseCode(); 13783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson RecordedRequest request = server.takeRequest(); 13883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContains(request.getHeaders(), "D: e"); 13983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContains(request.getHeaders(), "D: f"); 14083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "NullValue.*"); 14183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*"); 14283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "G:.*"); 14383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "null:.*"); 14483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 14583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 14683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("N", "o"); 14783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Set header after connect"); 14883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (IllegalStateException expected) { 14983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 15083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 15183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty("P", "q"); 15283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Set header after connect"); 15383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (IllegalStateException expected) { 15483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 15583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 15683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 15783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson public void testResponseHeaders() throws IOException, InterruptedException { 15883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.enqueue(new MockResponse() 15983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .setStatus("HTTP/1.0 200 Fantastic") 16083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .addHeader("A: b") 16183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .addHeader("A: c") 16283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8)); 16383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.play(); 16483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 16583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 16683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals(200, urlConnection.getResponseCode()); 16783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals("Fantastic", urlConnection.getResponseMessage()); 168c1a675c80c69decadb736b245f0366f93a94a462Jesse Wilson assertEquals("HTTP/1.0 200 Fantastic", urlConnection.getHeaderField(null)); 16983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson Map<String, List<String>> responseHeaders = urlConnection.getHeaderFields(); 1708ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson assertEquals(Arrays.asList("HTTP/1.0 200 Fantastic"), responseHeaders.get(null)); 17183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals(newSet("b", "c"), new HashSet<String>(responseHeaders.get("A"))); 17283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 17383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson responseHeaders.put("N", Arrays.asList("o")); 17483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 17583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 17683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 17783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 17883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson responseHeaders.get("A").add("d"); 17983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 18083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 18183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 18283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 18383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 184e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // Check that if we don't read to the end of a response, the next request on the 185e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // recycled connection doesn't get the unread tail of the first request's response. 186e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // http://code.google.com/p/android/issues/detail?id=2939 187e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes public void test_2939() throws Exception { 188b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson MockResponse response = new MockResponse().setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8); 189b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 190b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 191b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 192b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 193b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 194c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("ABCDE", server.getUrl("/").openConnection(), 5); 195c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("ABCDE", server.getUrl("/").openConnection(), 5); 1968baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson } 1978baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson 198977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes // Check that we recognize a few basic mime types by extension. 199977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes // http://code.google.com/p/android/issues/detail?id=10100 200977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes public void test_10100() throws Exception { 201977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes assertEquals("image/jpeg", URLConnection.guessContentTypeFromName("someFile.jpg")); 202977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes assertEquals("application/pdf", URLConnection.guessContentTypeFromName("stuff.pdf")); 203977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes } 204977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes 2058baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson public void testConnectionsArePooled() throws Exception { 206b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson MockResponse response = new MockResponse().setBody("ABCDEFGHIJKLMNOPQR"); 207b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 208b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 209b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 210b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 211b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 212b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 21306e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection()); 214c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 21506e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection()); 216c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 21706e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection()); 218c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(2, server.takeRequest().getSequenceNumber()); 219c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 220c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 221c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testChunkedConnectionsArePooled() throws Exception { 222c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson MockResponse response = new MockResponse().setChunkedBody("ABCDEFGHIJKLMNOPQR", 5); 223c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 224c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 225c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 226c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 227c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 228c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 22906e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection()); 230b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 23106e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection()); 232b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 23306e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection()); 234b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(2, server.takeRequest().getSequenceNumber()); 235e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 23602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 237e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson public void testServerClosesSocket() throws Exception { 238e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson testServerClosesOutput(DISCONNECT_AT_END); 239e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 240e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 241e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson public void testServerShutdownInput() throws Exception { 242e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson testServerClosesOutput(SHUTDOWN_INPUT_AT_END); 243e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 244e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 245e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson public void testServerShutdownOutput() throws Exception { 246e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson testServerClosesOutput(SHUTDOWN_OUTPUT_AT_END); 247e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 248e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 249e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception { 250e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse() 251e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setBody("This connection won't pool properly") 252e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setSocketPolicy(socketPolicy)); 253e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse() 254e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setBody("This comes after a busted connection")); 255e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.play(); 256e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 257e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertContent("This connection won't pool properly", server.getUrl("/a").openConnection()); 258e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 259e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertContent("This comes after a busted connection", server.getUrl("/b").openConnection()); 260e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson // sequence number 0 means the HTTP socket connection was not reused 261e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 262e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 263e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 264b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson enum WriteKind { BYTE_BY_BYTE, SMALL_BUFFERS, LARGE_BUFFERS } 26502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 26602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_byteByByte() throws Exception { 26751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.BYTE_BY_BYTE); 26802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 26902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 27002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_smallBuffers() throws Exception { 27151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.SMALL_BUFFERS); 27202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 27302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 27402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_largeBuffers() throws Exception { 27551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.LARGE_BUFFERS); 27602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 27702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 27802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_byteByByte() throws Exception { 27951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.BYTE_BY_BYTE); 28002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 28102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 28202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_smallBuffers() throws Exception { 28351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.SMALL_BUFFERS); 28402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 28502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 28602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_largeBuffers() throws Exception { 28751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.LARGE_BUFFERS); 28802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 28902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 29051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception { 29102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes int n = 512*1024; 292b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.setBodyLimit(0); 293b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(new MockResponse()); 294b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 295b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 296b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson HttpURLConnection conn = (HttpURLConnection) server.getUrl("/").openConnection(); 29702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setDoOutput(true); 29802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setRequestMethod("POST"); 29951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (uploadKind == TransferKind.CHUNKED) { 30002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setChunkedStreamingMode(-1); 30102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } else { 30202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setFixedLengthStreamingMode(n); 30302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 30402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes OutputStream out = conn.getOutputStream(); 30502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes if (writeKind == WriteKind.BYTE_BY_BYTE) { 30602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes for (int i = 0; i < n; ++i) { 30702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.write('x'); 30802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 30902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } else { 31002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64*1024]; 31102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes Arrays.fill(buf, (byte) 'x'); 31202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes for (int i = 0; i < n; i += buf.length) { 31302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.write(buf, 0, Math.min(buf.length, n - i)); 31402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 31502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 31602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.close(); 3174cb7f05dc68abb23ae54a5891c369062185f2210Elliott Hughes assertEquals(200, conn.getResponseCode()); 318b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson RecordedRequest request = server.takeRequest(); 319b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(n, request.getBodySize()); 32051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (uploadKind == TransferKind.CHUNKED) { 321b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertTrue(request.getChunkSizes().size() > 0); 322b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson } else { 323b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertTrue(request.getChunkSizes().isEmpty()); 324b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson } 32502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 3266247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes 327f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson public void testGetResponseCodeNoResponseBody() throws Exception { 328f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson server.enqueue(new MockResponse() 329f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson .addHeader("abc: def")); 330f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson server.play(); 331f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson 332f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson URL url = server.getUrl("/"); 333f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 334f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson conn.setDoInput(false); 335f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson assertEquals("def", conn.getHeaderField("abc")); 336f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson assertEquals(200, conn.getResponseCode()); 337f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson try { 338f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson conn.getInputStream(); 339f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson fail(); 340f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } catch (ProtocolException expected) { 341f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } 342f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } 343f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson 34460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson public void testConnectViaHttps() throws IOException, InterruptedException { 34560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 34660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 347059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 348c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 34960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson server.play(); 35060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 351096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 3524559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 35360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 354c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via HTTPS", connection); 35560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 35660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson RecordedRequest request = server.takeRequest(); 35760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 35860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 35960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 360096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson public void testConnectViaHttpsReusingConnections() throws IOException, InterruptedException { 361096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 362096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 363059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 364096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 365096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.enqueue(new MockResponse().setBody("another response via HTTPS")); 366096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.play(); 367096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 368b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 369b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 370b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertContent("this response comes via HTTPS", connection); 371b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 372b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 373b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 374b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertContent("another response via HTTPS", connection); 375b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 376b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 377b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 378b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson } 379b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 3808116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson public void testConnectViaHttpsReusingConnectionsDifferentFactories() 381b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson throws IOException, InterruptedException { 382b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 383b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 384b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 385b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 386b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.enqueue(new MockResponse().setBody("another response via HTTPS")); 387b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.play(); 388b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 389096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson // install a custom SSL socket factory so the server can be authorized 390096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 391059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 392096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson assertContent("this response comes via HTTPS", connection); 393096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 394096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 395096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson try { 396096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 397b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson fail("without an SSL socket factory, the connection should fail"); 398096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } catch (SSLException expected) { 399096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } 400096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } 401096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 4024559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException { 4034559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(); 4044559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 4054559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 406e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START)); 4074559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.enqueue(new MockResponse().setBody("this response comes via SSL")); 4084559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.play(); 4094559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 4104559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 4114559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 4124559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 4134559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertContent("this response comes via SSL", connection); 4144559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 4154559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom RecordedRequest request = server.takeRequest(); 4164559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 4174559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom } 4184559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 4192915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom /** 4202915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * Verify that we don't retry connections on certificate verification errors. 4212915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * 4222915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * http://code.google.com/p/android/issues/detail?id=13178 4232915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom */ 4242915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom public void testConnectViaHttpsToUntrustedServer() throws IOException, InterruptedException { 4252915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(TestKeyStore.getClientCA2(), 4262915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom TestKeyStore.getServer()); 4272915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 4282915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 4292915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.enqueue(new MockResponse()); // unused 4302915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.play(); 4312915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 4322915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 4332915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 4342915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom try { 4352915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom connection.getInputStream(); 4362915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom fail(); 4372915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } catch (SSLHandshakeException expected) { 4382915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom assertTrue(expected.getCause() instanceof CertificateException); 4392915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } 4402915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom assertEquals(0, server.getRequestCount()); 4412915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } 4422915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 443984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingProxyArg() throws Exception { 444984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.CREATE_ARG); 445984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 446984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 447984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingProxySystemProperty() throws Exception { 448984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY); 449984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 450984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 451984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingHttpProxySystemProperty() throws Exception { 452984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); 453984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 454984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 455984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception { 456c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockResponse mockResponse = new MockResponse().setBody("this response comes via a proxy"); 45751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(mockResponse); 45851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 45960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 460984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson URL url = new URL("http://android.com/foo"); 461984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HttpURLConnection connection = proxyConfig.connect(server, url); 462c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via a proxy", connection); 46360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 46451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest request = server.takeRequest(); 46560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET http://android.com/foo HTTP/1.1", request.getRequestLine()); 46660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(request.getHeaders(), "Host: android.com"); 46760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 46860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 469c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testContentDisagreesWithContentLengthHeader() throws IOException { 470c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(new MockResponse() 471c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .setBody("abc\r\nYOU SHOULD NOT SEE THIS") 472c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .clearHeaders() 473c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .addHeader("Content-Length: 3")); 474c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 475c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 476c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("abc", server.getUrl("/").openConnection()); 477c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 478c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 479c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testContentDisagreesWithChunkedHeader() throws IOException { 480c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson MockResponse mockResponse = new MockResponse(); 481c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.setChunkedBody("abc", 3); 482c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 483c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson bytesOut.write(mockResponse.getBody()); 484c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson bytesOut.write("\r\nYOU SHOULD NOT SEE THIS".getBytes()); 485c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.setBody(bytesOut.toByteArray()); 486c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.clearHeaders(); 487c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.addHeader("Transfer-encoding: chunked"); 488c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 489c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(mockResponse); 490c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 491c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 492c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("abc", server.getUrl("/").openConnection()); 493c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 494c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 495f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom public void testConnectViaHttpProxyToHttpsUsingProxyArgWithNoProxy() throws Exception { 496f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom testConnectViaDirectProxyToHttps(ProxyConfig.NO_PROXY); 497f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 498f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 499f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception { 500f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom // https should not use http proxy 501f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom testConnectViaDirectProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); 502f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 503f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 504f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception { 505f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(); 506f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 507f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 508f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 509f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.play(); 510f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 511f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom URL url = server.getUrl("/foo"); 512f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); 513f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 514f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 515f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom assertContent("this response comes via HTTPS", connection); 516f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 517f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom RecordedRequest request = server.takeRequest(); 518f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 519f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 520f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 521f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 522984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingProxyArg() throws Exception { 523984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.CREATE_ARG); 524984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 525984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 526984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson /** 527984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * We weren't honoring all of the appropriate proxy system properties when 528984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * connecting via HTTPS. http://b/3097518 529984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson */ 530984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingProxySystemProperty() throws Exception { 531984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY); 532984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 533984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 534984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty() throws Exception { 535984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY); 536984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 537984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 538984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson /** 539984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * We were verifying the wrong hostname when connecting to an HTTPS site 540984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * through a proxy. http://b/3097277 541984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson */ 542984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception { 54360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 544984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 54560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 546059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 547c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 548c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 549c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 550c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via a secure proxy")); 55151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 55260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 55360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson URL url = new URL("https://android.com/foo"); 554984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); 555059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 556984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson connection.setHostnameVerifier(hostnameVerifier); 55760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 558c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via a secure proxy", connection); 55960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 56051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest connect = server.takeRequest(); 56160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("Connect line failure on proxy", 56260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine()); 56360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(connect.getHeaders(), "Host: android.com"); 56460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 56551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest get = server.takeRequest(); 56660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET /foo HTTP/1.1", get.getRequestLine()); 56760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(get.getHeaders(), "Host: android.com"); 568984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls); 56960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 57060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 571d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson /** 572d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson * Test which headers are sent unencrypted to the HTTP proxy. 573d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson */ 574d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson public void testProxyConnectIncludesProxyHeadersOnly() 575d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson throws IOException, InterruptedException { 576984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 577d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 578d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 579d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 580c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 581c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 582c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 583d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.enqueue(new MockResponse().setBody("encrypted response from the origin server")); 584d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.play(); 585d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 586d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson URL url = new URL("https://android.com/foo"); 587d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) url.openConnection( 588d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.toProxyAddress()); 589d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("Private", "Secret"); 590d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("Proxy-Authorization", "bar"); 591d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("User-Agent", "baz"); 592d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 593984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson connection.setHostnameVerifier(hostnameVerifier); 594d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContent("encrypted response from the origin server", connection); 595d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 596d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson RecordedRequest connect = server.takeRequest(); 597d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContainsNoneMatching(connect.getHeaders(), "Private.*"); 598d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Proxy-Authorization: bar"); 599d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "User-Agent: baz"); 600d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Host: android.com"); 601d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive"); 602d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 603d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson RecordedRequest get = server.takeRequest(); 604d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(get.getHeaders(), "Private: Secret"); 605984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls); 606d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 607d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 608c996149b500fc4825156106554457fe2394ae087Jesse Wilson public void testProxyAuthenticateOnConnect() throws Exception { 609c996149b500fc4825156106554457fe2394ae087Jesse Wilson Authenticator.setDefault(SIMPLE_AUTHENTICATOR); 610c996149b500fc4825156106554457fe2394ae087Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 611c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 612c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 613c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setResponseCode(407) 614c996149b500fc4825156106554457fe2394ae087Jesse Wilson .addHeader("Proxy-Authenticate: Basic realm=\"localhost\"")); 615c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 616c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 617c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 618c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse().setBody("A")); 619c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.play(); 620c996149b500fc4825156106554457fe2394ae087Jesse Wilson 621c996149b500fc4825156106554457fe2394ae087Jesse Wilson URL url = new URL("https://android.com/foo"); 622c996149b500fc4825156106554457fe2394ae087Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) url.openConnection( 623c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.toProxyAddress()); 624c996149b500fc4825156106554457fe2394ae087Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 625c996149b500fc4825156106554457fe2394ae087Jesse Wilson connection.setHostnameVerifier(new RecordingHostnameVerifier()); 626c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContent("A", connection); 627c996149b500fc4825156106554457fe2394ae087Jesse Wilson 628c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest connect1 = server.takeRequest(); 629c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine()); 630c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*"); 631c996149b500fc4825156106554457fe2394ae087Jesse Wilson 632c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest connect2 = server.takeRequest(); 633c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine()); 634c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " + BASE_64_CREDENTIALS); 635c996149b500fc4825156106554457fe2394ae087Jesse Wilson 636c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest get = server.takeRequest(); 637c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("GET /foo HTTP/1.1", get.getRequestLine()); 638c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*"); 639c996149b500fc4825156106554457fe2394ae087Jesse Wilson } 640c996149b500fc4825156106554457fe2394ae087Jesse Wilson 641d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson public void testDisconnectedConnection() throws IOException { 642d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.enqueue(new MockResponse().setBody("ABCDEFGHIJKLMNOPQR")); 643d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.play(); 644d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 645d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 646d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson InputStream in = connection.getInputStream(); 647d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertEquals('A', (char) in.read()); 648d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.disconnect(); 649d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson try { 650d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson in.read(); 651d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson fail("Expected a connection closed exception"); 652d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } catch (IOException expected) { 653d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 654d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 655d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 656d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testDisconnectBeforeConnect() throws IOException { 657d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("A")); 658d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 659d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 660d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 661d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson connection.disconnect(); 662d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 663d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertContent("A", connection); 664d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(200, connection.getResponseCode()); 665d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 666d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 667d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testDefaultRequestProperty() throws Exception { 668d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson URLConnection.setDefaultRequestProperty("X-testSetDefaultRequestProperty", "A"); 669d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertNull(URLConnection.getDefaultRequestProperty("X-setDefaultRequestProperty")); 670d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 671d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 67251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson /** 67351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * Reads {@code count} characters from the stream. If the stream is 67451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * exhausted before {@code count} characters can be read, the remaining 67551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * characters are returned and the stream is closed. 67651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson */ 67751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private String readAscii(InputStream in, int count) throws IOException { 67851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson StringBuilder result = new StringBuilder(); 67951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson for (int i = 0; i < count; i++) { 68051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson int value = in.read(); 68151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (value == -1) { 68251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.close(); 68351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson break; 68451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 68551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson result.append((char) value); 68651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 68751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson return result.toString(); 68851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 68951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 69051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithContentLengthHeader() throws IOException { 69151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.FIXED_LENGTH); 69251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 69351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 69451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithChunkedEncoding() throws IOException { 69551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.CHUNKED); 69651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 69751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 69851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithNoLengthHeaders() throws IOException { 69951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.END_OF_STREAM); 70051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 70151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 7020c2fd828abec671333b8b88281825fd27a783723Jesse Wilson private void testMarkAndReset(TransferKind transferKind) throws IOException { 70351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson MockResponse response = new MockResponse(); 70451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson transferKind.setBody(response, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1024); 70551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 706953df613522e12a418cb7cb73248594d6c9f53d4Jesse Wilson server.enqueue(response); 70751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 70851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 70951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson InputStream in = server.getUrl("/").openConnection().getInputStream(); 71051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertFalse("This implementation claims to support mark().", in.markSupported()); 71151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.mark(5); 71251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals("ABCDE", readAscii(in, 5)); 71351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson try { 71451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.reset(); 71551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson fail(); 71651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } catch (IOException expected) { 71751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 71851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals("FGHIJKLMNOPQRSTUVWXYZ", readAscii(in, Integer.MAX_VALUE)); 71951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ", server.getUrl("/").openConnection()); 72051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 72151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 72251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson /** 72351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * We've had a bug where we forget the HTTP response when we see response 72451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * code 401. This causes a new HTTP request to be issued for every call into 72551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * the URLConnection. 72651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson */ 72751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testUnauthorizedResponseHandling() throws IOException { 72851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson MockResponse response = new MockResponse() 72951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson .addHeader("WWW-Authenticate: challenge") 73051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson .setResponseCode(401) // UNAUTHORIZED 73151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson .setBody("Unauthorized"); 73251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 73351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 73451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 73551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 73651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 73751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson URL url = server.getUrl("/"); 73851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 73951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 74051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 74151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 74251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 74351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(1, server.getRequestCount()); 74451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 74551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 7466906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson public void testNonHexChunkSize() throws IOException { 7476906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.enqueue(new MockResponse() 7486906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .setBody("5\r\nABCDE\r\nG\r\nFGHIJKLMNOPQRSTU\r\n0\r\n\r\n") 7496906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .clearHeaders() 7506906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .addHeader("Transfer-encoding: chunked")); 7516906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.play(); 7526906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 7536906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 7546906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson try { 7556906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 7566906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson fail(); 7576906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } catch (IOException e) { 7586906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 7596906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 7606906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 7616906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson public void testMissingChunkBody() throws IOException { 7626906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.enqueue(new MockResponse() 7636906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .setBody("5") 7646906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .clearHeaders() 7656906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .addHeader("Transfer-encoding: chunked") 766e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setSocketPolicy(DISCONNECT_AT_END)); 7676906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.play(); 7686906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 7696906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 7706906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson try { 7716906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 7726906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson fail(); 7736906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } catch (IOException e) { 7746906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 7756906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 7766906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 77750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson /** 77850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * This test checks whether connections are gzipped by default. This 77950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * behavior in not required by the API, so a failure of this test does not 78050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * imply a bug in the implementation. 78150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson */ 78250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException { 78350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.enqueue(new MockResponse() 78450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .setBody(gzip("ABCABCABC".getBytes("UTF-8"))) 78550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .addHeader("Content-Encoding: gzip")); 78650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.play(); 78750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 78850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 78950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 7908116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson assertNull(connection.getContentEncoding()); 79150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 79250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson RecordedRequest request = server.takeRequest(); 79350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: gzip"); 79450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson } 79550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 796deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testClientConfiguredGzipContentEncoding() throws Exception { 797deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(new MockResponse() 798deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson .setBody(gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"))) 799deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson .addHeader("Content-Encoding: gzip")); 800deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.play(); 801deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 802deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 803deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection.addRequestProperty("Accept-Encoding", "gzip"); 804deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); 805deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE)); 806deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 807deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson RecordedRequest request = server.takeRequest(); 808deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: gzip"); 809deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 810deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 811deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testGzipAndConnectionReuseWithFixedLength() throws Exception { 812deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH); 813deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 814deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 815deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception { 816deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED); 817deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 818deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 81950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson public void testClientConfiguredCustomContentEncoding() throws Exception { 82050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.enqueue(new MockResponse() 82150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .setBody("ABCDE") 82250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .addHeader("Content-Encoding: custom")); 82350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.play(); 82450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 82550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 82650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson connection.addRequestProperty("Accept-Encoding", "custom"); 82750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 82850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 82950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson RecordedRequest request = server.takeRequest(); 83050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: custom"); 83150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson } 83250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 833deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson /** 834deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * Test a bug where gzip input streams weren't exhausting the input stream, 835deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * which corrupted the request that followed. 836deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * http://code.google.com/p/android/issues/detail?id=7059 837deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson */ 838deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson private void testClientConfiguredGzipContentEncodingAndConnectionReuse( 839deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson TransferKind transferKind) throws Exception { 840deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson MockResponse responseOne = new MockResponse(); 841deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson responseOne.addHeader("Content-Encoding: gzip"); 842deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson transferKind.setBody(responseOne, gzip("one (gzipped)".getBytes("UTF-8")), 5); 843deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(responseOne); 844deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson MockResponse responseTwo = new MockResponse(); 845deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson transferKind.setBody(responseTwo, "two (identity)", 5); 846deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(responseTwo); 847deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.play(); 848deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 849deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 850deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection.addRequestProperty("Accept-Encoding", "gzip"); 851deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); 852deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE)); 853deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 854deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 855deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection = server.getUrl("/").openConnection(); 856deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("two (identity)", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 857deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 858deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 859deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 860deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson /** 861ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * Obnoxiously test that the chunk sizes transmitted exactly equal the 862ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * requested data+chunk header size. Although setChunkedStreamingMode() 863ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * isn't specific about whether the size applies to the data or the 864ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * complete chunk, the RI interprets it as a complete chunk. 865ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson */ 866ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testSetChunkedStreamingMode() throws IOException, InterruptedException { 867ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse()); 868ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 869ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 870ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 871ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson urlConnection.setChunkedStreamingMode(8); 872ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson urlConnection.setDoOutput(true); 873ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = urlConnection.getOutputStream(); 874ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII")); 875ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(200, urlConnection.getResponseCode()); 876ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 877ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 878ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII")); 879ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes()); 880ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 881ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 882ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithFixedLengthStreaming() throws Exception { 883ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson testAuthenticateWithStreamingPost(StreamingMode.FIXED_LENGTH); 884ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 885ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 886ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithChunkedStreaming() throws Exception { 887ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson testAuthenticateWithStreamingPost(StreamingMode.CHUNKED); 888ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 889ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 890ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception { 891ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 892ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 893ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 894ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 895ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 896ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 897ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 898ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson Authenticator.setDefault(SIMPLE_AUTHENTICATOR); 899ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 900ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setDoOutput(true); 901ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 902ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 903ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setFixedLengthStreamingMode(requestBody.length); 904ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 905ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setChunkedStreamingMode(0); 906ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 907ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = connection.getOutputStream(); 908ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write(requestBody); 909ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.close(); 910ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson try { 911ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.getInputStream(); 912ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson fail(); 913ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } catch (HttpRetryException expected) { 914ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 915ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 916ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the request... 917ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 918ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*"); 919ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 920ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 921ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 92235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson public void testSecureFixedLengthStreaming() throws Exception { 92335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson testSecureStreamingPost(StreamingMode.FIXED_LENGTH); 92435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 92535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 92635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson public void testSecureChunkedStreaming() throws Exception { 92735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson testSecureStreamingPost(StreamingMode.CHUNKED); 92835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 92935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 93035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson /** 93135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson * Users have reported problems using HTTPS with streaming request bodies. 93235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson * http://code.google.com/p/android/issues/detail?id=12860 93335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson */ 93435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception { 93535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 93635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 93735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.enqueue(new MockResponse().setBody("Success!")); 93835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.play(); 93935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 94035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 94135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 94235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setDoOutput(true); 94335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 94435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 94535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setFixedLengthStreamingMode(requestBody.length); 94635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 94735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setChunkedStreamingMode(0); 94835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 94935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson OutputStream outputStream = connection.getOutputStream(); 95035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson outputStream.write(requestBody); 95135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson outputStream.close(); 95235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 95335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 95435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson RecordedRequest request = server.takeRequest(); 95535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals("POST / HTTP/1.1", request.getRequestLine()); 95635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 95735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes()); 95835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 95935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Arrays.asList(4), request.getChunkSizes()); 96035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 96135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 96235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 96335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 964ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson enum StreamingMode { 965ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson FIXED_LENGTH, CHUNKED 966ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 967ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 968ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithPost() throws Exception { 969ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 970ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 971ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 972ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 973ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // fail auth three times... 974ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 975ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 976ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 977ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...then succeed the fourth time 978ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 979ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 980ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 981ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson Authenticator.setDefault(SIMPLE_AUTHENTICATOR); 982ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 983ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setDoOutput(true); 984ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 985ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = connection.getOutputStream(); 986ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write(requestBody); 987ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.close(); 988ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 989ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 990ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the first request... 991ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 992ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*"); 993ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 994da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson // ...but the three requests that follow include an authorization header 995ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (int i = 0; i < 3; i++) { 996ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson request = server.takeRequest(); 997ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("POST / HTTP/1.1", request.getRequestLine()); 998c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContains(request.getHeaders(), "Authorization: Basic " + BASE_64_CREDENTIALS); 999ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 1000ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1001ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1002ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1003ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithGet() throws Exception { 1004ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 1005ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 1006ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 1007ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 1008ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // fail auth three times... 1009ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1010ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1011ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1012ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...then succeed the fourth time 1013ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 1014ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 1015ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1016ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson Authenticator.setDefault(SIMPLE_AUTHENTICATOR); 1017ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1018ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1019ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1020ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the first request... 1021ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 1022ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*"); 1023ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1024ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...but the three requests that follow requests include an authorization header 1025ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (int i = 0; i < 3; i++) { 1026ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson request = server.takeRequest(); 1027ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("GET / HTTP/1.1", request.getRequestLine()); 1028c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContains(request.getHeaders(), "Authorization: Basic " + BASE_64_CREDENTIALS); 1029ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1030ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1031ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1032c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithChunkedEncoding() throws Exception { 1033c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.CHUNKED, true); 1034c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1035c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1036c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithContentLengthHeader() throws Exception { 1037c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.FIXED_LENGTH, true); 1038c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1039c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1040c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithNoLengthHeaders() throws Exception { 1041c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.END_OF_STREAM, false); 1042c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1043c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1044c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception { 1045c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockResponse response = new MockResponse() 1046c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1047c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: /foo"); 1048c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson transferKind.setBody(response, "This page has moved!", 10); 1049c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(response); 1050c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1051c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1052c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1053c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1054c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the new location!", 1055c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1056c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1057c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 1058c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET / HTTP/1.1", first.getRequestLine()); 1059c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest retry = server.takeRequest(); 1060c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET /foo HTTP/1.1", retry.getRequestLine()); 1061c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson if (reuse) { 1062c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, retry.getSequenceNumber()); 1063c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1064c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1065c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1066c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedOnHttps() throws IOException, InterruptedException { 1067c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1068059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1069c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1070c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1071c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: /foo") 1072c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1073c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1074c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1075c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1076c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 1077059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 1078c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the new location!", 1079c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1080c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1081c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 1082c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET / HTTP/1.1", first.getRequestLine()); 1083c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest retry = server.takeRequest(); 1084c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET /foo HTTP/1.1", retry.getRequestLine()); 1085c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, retry.getSequenceNumber()); 1086c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1087c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1088c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testNotRedirectedFromHttpsToHttp() throws IOException, InterruptedException { 1089c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1090059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1091c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1092c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1093c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: http://anyhost/foo") 1094c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1095c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1096c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1097c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 1098059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 1099c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This page has moved!", 1100c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1101c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1102c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1103c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testNotRedirectedFromHttpToHttps() throws IOException, InterruptedException { 1104c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1105c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1106c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: https://anyhost/foo") 1107c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1108c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1109c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1110c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1111c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This page has moved!", 1112c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1113c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1114c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1115c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectToAnotherOriginServer() throws Exception { 1116c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockWebServer server2 = new MockWebServer(); 1117c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.enqueue(new MockResponse().setBody("This is the 2nd server!")); 1118c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.play(); 1119c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1120c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1121c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1122c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: " + server2.getUrl("/").toString()) 1123c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1124c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the first server again!")); 1125c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1126c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1127c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1128c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the 2nd server!", 1129c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1130c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals(server2.getUrl("/"), connection.getURL()); 1131c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1132c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson // make sure the first server was careful to recycle the connection 1133c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the first server again!", 1134c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE)); 1135c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1136c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 11370c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort()); 1138c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest second = server2.takeRequest(); 11390c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort()); 1140c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest third = server.takeRequest(); 1141c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, third.getSequenceNumber()); 1142c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1143c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.shutdown(); 1144c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1145c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1146d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse300MultipleChoiceWithPost() throws Exception { 1147d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson // Chrome doesn't follow the redirect, but Firefox and the RI both do 1148d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MULT_CHOICE); 1149d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1150d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1151d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse301MovedPermanentlyWithPost() throws Exception { 1152d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_PERM); 1153d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1154d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1155d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse302MovedTemporarilyWithPost() throws Exception { 1156d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_TEMP); 1157d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1158d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1159d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse303SeeOtherWithPost() throws Exception { 1160d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_SEE_OTHER); 1161d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1162d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1163d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson private void testResponseRedirectedWithPost(int redirectCode) throws Exception { 1164d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse() 1165d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setResponseCode(redirectCode) 1166d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .addHeader("Location: /page2") 1167d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setBody("This page has moved!")); 1168d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("Page 2")); 1169d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 1170d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1171d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/page1").openConnection(); 1172d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson connection.setDoOutput(true); 1173d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 1174d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson OutputStream outputStream = connection.getOutputStream(); 1175d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson outputStream.write(requestBody); 1176d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson outputStream.close(); 1177d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1178d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertTrue(connection.getDoOutput()); 1179d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1180d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page1 = server.takeRequest(); 1181d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine()); 1182d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(page1.getBody())); 1183d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1184d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page2 = server.takeRequest(); 1185d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine()); 1186d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1187d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1188d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse305UseProxy() throws Exception { 1189d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 1190d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse() 1191d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_USE_PROXY) 1192d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .addHeader("Location: " + server.getUrl("/")) 1193d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setBody("This page has moved!")); 1194d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("Proxy Response")); 1195d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1196d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/foo").openConnection(); 1197d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson // Fails on the RI, which gets "Proxy Response" 1198d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("This page has moved!", 1199d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1200d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1201d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page1 = server.takeRequest(); 1202d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("GET /foo HTTP/1.1", page1.getRequestLine()); 1203d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(1, server.getRequestCount()); 1204d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1205d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1206c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testHttpsWithCustomTrustManager() throws Exception { 1207c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 1208c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordingTrustManager trustManager = new RecordingTrustManager(); 1209c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson SSLContext sc = SSLContext.getInstance("TLS"); 1210c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom()); 1211c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1212c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); 1213c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 1214c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); 1215c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 1216c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson try { 1217c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1218059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1219c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 1220c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("DEF")); 1221c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("GHI")); 1222c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1223c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1224c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URL url = server.getUrl("/"); 1225c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE)); 1226c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE)); 1227c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE)); 1228c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 12290c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls); 12304559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertEquals(Arrays.asList("checkServerTrusted [" 12310c2fd828abec671333b8b88281825fd27a783723Jesse Wilson + "CN=" + hostName + " 1, " 1232b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "CN=Test Intermediate Certificate Authority 1, " 1233b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "CN=Test Root Certificate Authority 1" 1234b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "] RSA"), 1235c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson trustManager.calls); 1236c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } finally { 1237c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier); 1238c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory); 1239c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1240c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1241c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1242eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson public void testConnectTimeouts() throws IOException { 12435fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes StuckServer ss = new StuckServer(); 12445fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes int serverPort = ss.getLocalPort(); 1245f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom URLConnection urlConnection = new URL("http://localhost:" + serverPort).openConnection(); 1246b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson int timeout = 1000; 1247b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson urlConnection.setConnectTimeout(timeout); 1248b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson long start = System.currentTimeMillis(); 1249eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson try { 1250eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson urlConnection.getInputStream(); 1251eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson fail(); 1252eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } catch (SocketTimeoutException expected) { 1253b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson long actual = System.currentTimeMillis() - start; 1254b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson assertTrue(Math.abs(timeout - actual) < 500); 12555fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes } finally { 12565fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes ss.close(); 1257f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 1258eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1259eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1260eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson public void testReadTimeouts() throws IOException { 1261eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson /* 1262eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * This relies on the fact that MockWebServer doesn't close the 1263eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * connection after a response has been sent. This causes the client to 1264eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * try to read more bytes than are sent, which results in a timeout. 1265eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson */ 1266eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson MockResponse timeout = new MockResponse() 1267eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .setBody("ABC") 1268eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .clearHeaders() 1269eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .addHeader("Content-Length: 4"); 1270eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson server.enqueue(timeout); 1271b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.enqueue(new MockResponse().setBody("unused")); // to keep the server alive 1272eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson server.play(); 1273eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1274eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson URLConnection urlConnection = server.getUrl("/").openConnection(); 1275eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson urlConnection.setReadTimeout(1000); 1276eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson InputStream in = urlConnection.getInputStream(); 1277eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('A', in.read()); 1278eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('B', in.read()); 1279eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('C', in.read()); 1280eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson try { 1281eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson in.read(); // if Content-Length was accurate, this would return -1 immediately 1282eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson fail(); 1283eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } catch (SocketTimeoutException expected) { 1284eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1285eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1286eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1287125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException { 1288125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson server.enqueue(new MockResponse()); 1289125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson server.play(); 1290125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1291125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 1292125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.setRequestProperty("Transfer-encoding", "chunked"); 1293125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.setDoOutput(true); 1294125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.getOutputStream().write("ABC".getBytes("UTF-8")); 1295125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson assertEquals(200, urlConnection.getResponseCode()); 1296125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1297125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson RecordedRequest request = server.takeRequest(); 1298125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson assertEquals("ABC", new String(request.getBody(), "UTF-8")); 1299125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson } 1300125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1301f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseInRequest() throws IOException, InterruptedException { 1302f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); // server doesn't honor the connection: close header! 1303f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); 1304f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1305f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1306f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection(); 1307f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson a.setRequestProperty("Connection", "close"); 1308f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, a.getResponseCode()); 1309f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1310f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection(); 1311f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, b.getResponseCode()); 1312f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1313f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1314f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1315f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1316f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1317f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1318f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseInResponse() throws IOException, InterruptedException { 1319f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse().addHeader("Connection: close")); 1320f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); 1321f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1322f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1323f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection(); 1324f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, a.getResponseCode()); 1325f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1326f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection(); 1327f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, b.getResponseCode()); 1328f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1329f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1330f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1331f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1332f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1333f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1334f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseWithRedirect() throws IOException, InterruptedException { 1335f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson MockResponse response = new MockResponse() 1336f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1337f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .addHeader("Location: /foo") 1338f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .addHeader("Connection: close"); 1339f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(response); 1340f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1341f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1342f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1343f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1344f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("This is the new location!", 1345f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1346f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1347f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1348f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1349f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1350f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1351f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 135265d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson public void testResponseCodeDisagreesWithHeaders() throws IOException, InterruptedException { 135365d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson server.enqueue(new MockResponse() 135465d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT) 135565d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson .setBody("This body is not allowed!")); 135665d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson server.play(); 135765d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson 135865d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 135965d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson assertEquals("This body is not allowed!", 136065d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 136165d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson } 136265d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson 1363ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson public void testSingleByteReadIsSigned() throws IOException { 1364ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson server.enqueue(new MockResponse().setBody(new byte[] { -2, -1 })); 1365ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson server.play(); 1366ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson 1367ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1368ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson InputStream in = connection.getInputStream(); 1369ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(254, in.read()); 1370ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(255, in.read()); 1371ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(-1, in.read()); 1372ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson } 1373ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson 1374f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithChunkedEncoding() throws IOException { 1375f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.CHUNKED); 1376f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1377f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1378f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithFixedLength() throws IOException { 1379f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.FIXED_LENGTH); 1380f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1381f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1382f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithNoLengthHeaders() throws IOException { 1383f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.END_OF_STREAM); 1384f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1385f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1386f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson /** 1387f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * We explicitly permit apps to close the upload stream even after it has 1388f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * been transmitted. We also permit flush so that buffered streams can 1389f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * do a no-op flush when they are closed. http://b/3038470 1390f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson */ 1391f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson private void testFlushAfterStreamTransmitted(TransferKind transferKind) throws IOException { 1392f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson server.enqueue(new MockResponse().setBody("abc")); 1393f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson server.play(); 1394f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1395f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1396f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setDoOutput(true); 1397f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson byte[] upload = "def".getBytes("UTF-8"); 1398f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1399f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson if (transferKind == TransferKind.CHUNKED) { 1400f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setChunkedStreamingMode(0); 1401f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } else if (transferKind == TransferKind.FIXED_LENGTH) { 1402f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setFixedLengthStreamingMode(upload.length); 1403f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1404f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1405f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson OutputStream out = connection.getOutputStream(); 1406f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.write(upload); 1407f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson assertEquals("abc", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1408f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1409f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.flush(); // dubious but permitted 1410f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson try { 1411f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.write("ghi".getBytes("UTF-8")); 1412f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson fail(); 1413f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } catch (IOException expected) { 1414f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1415f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1416f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1417c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson public void testGetHeadersThrows() throws IOException { 1418e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START)); 1419c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson server.play(); 1420c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson 1421c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1422c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson try { 1423c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson connection.getInputStream(); 1424c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson fail(); 1425c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson } catch (IOException expected) { 1426c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson } 1427c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson 1428c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson try { 1429c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson connection.getInputStream(); 1430c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson fail(); 1431f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } catch (IOException expected) { 1432f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1433f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1434f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1435b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson public void testGetKeepAlive() throws Exception { 1436b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson MockWebServer server = new MockWebServer(); 1437b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 1438b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.play(); 1439b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1440b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson // The request should work once and then fail 1441b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson URLConnection connection = server.getUrl("").openConnection(); 1442b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson InputStream input = connection.getInputStream(); 1443b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson assertEquals("ABC", readAscii(input, Integer.MAX_VALUE)); 1444b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson input.close(); 1445b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson try { 1446b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.getUrl("").openConnection().getInputStream(); 1447b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson fail(); 1448b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } catch (ConnectException expected) { 1449b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1450b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1451b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1452b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson /** 1453d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * This test goes through the exhaustive set of interesting ASCII characters 1454d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * because most of those characters are interesting in some way according to 1455d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * RFC 2396 and RFC 2732. http://b/1158780 1456b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson */ 1457d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson public void testLenientUrlToUri() throws Exception { 1458d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // alphanum 1459d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09"); 1460d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1461d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // control characters 1462d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0001", "%01", "%01", "%01", "%01"); 1463d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u001f", "%1F", "%1F", "%1F", "%1F"); 1464d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1465d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // ascii characters 1466d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("%20", "%20", "%20", "%20", "%20"); 1467d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("%20", "%20", "%20", "%20", "%20"); 1468d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(" ", "%20", "%20", "%20", "%20"); 1469d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("!", "!", "!", "!", "!"); 1470d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\"", "%22", "%22", "%22", "%22"); 1471d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("#", null, null, null, "%23"); 1472d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("$", "$", "$", "$", "$"); 1473d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("&", "&", "&", "&", "&"); 1474d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("'", "'", "'", "'", "'"); 1475d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("(", "(", "(", "(", "("); 1476d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(")", ")", ")", ")", ")"); 1477d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("*", "*", "*", "*", "*"); 1478d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("+", "+", "+", "+", "+"); 1479d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(",", ",", ",", ",", ","); 1480d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("-", "-", "-", "-", "-"); 1481d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(".", ".", ".", ".", "."); 1482d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("/", null, "/", "/", "/"); 1483d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(":", null, ":", ":", ":"); 1484d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(";", ";", ";", ";", ";"); 1485d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("<", "%3C", "%3C", "%3C", "%3C"); 1486d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("=", "=", "=", "=", "="); 1487d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(">", "%3E", "%3E", "%3E", "%3E"); 1488d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("?", null, null, "?", "?"); 1489d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("@", "@", "@", "@", "@"); 1490d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("[", null, "%5B", null, "%5B"); 1491d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\\", "%5C", "%5C", "%5C", "%5C"); 1492d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("]", null, "%5D", null, "%5D"); 1493d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("^", "%5E", "%5E", "%5E", "%5E"); 1494d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("_", "_", "_", "_", "_"); 1495d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("`", "%60", "%60", "%60", "%60"); 1496d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("{", "%7B", "%7B", "%7B", "%7B"); 1497d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("|", "%7C", "%7C", "%7C", "%7C"); 1498d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("}", "%7D", "%7D", "%7D", "%7D"); 1499d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("~", "~", "~", "~", "~"); 1500d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("~", "~", "~", "~", "~"); 1501d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u007f", "%7F", "%7F", "%7F", "%7F"); 1502d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1503d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // beyond ascii 1504d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0080", "%C2%80", "%C2%80", "%C2%80", "%C2%80"); 1505d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u20ac", "\u20ac", "\u20ac", "\u20ac", "\u20ac"); 150632559028b14b9b321b10eede050afd554a376569Jesse Wilson testUrlToUriMapping("\ud842\udf9f", 150732559028b14b9b321b10eede050afd554a376569Jesse Wilson "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f"); 1508d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1509d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1510d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson public void testLenientUrlToUriNul() throws Exception { 1511d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0000", "%00", "%00", "%00", "%00"); // RI fails this 1512d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1513d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1514d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson private void testUrlToUriMapping(String string, String asAuthority, String asFile, 1515d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson String asQuery, String asFragment) throws Exception { 1516d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asAuthority != null) { 1517d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host" + asAuthority + ".tld/", 1518d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host" + string + ".tld/")).toString()); 1519d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1520d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asFile != null) { 1521d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file" + asFile + "/", 1522d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file" + string + "/")).toString()); 1523d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1524d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asQuery != null) { 1525d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file?q" + asQuery + "=x", 1526d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file?q" + string + "=x")).toString()); 1527d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1528d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file#" + asFragment + "-x", 1529d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file#" + asFragment + "-x")).toString()); 1530d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1531b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1532d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson /** 1533d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI, 1534d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * HttpURLConnection recovers from URLs with unescaped but unsupported URI 1535d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * characters like '{' and '|' by escaping these characters. 1536d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson */ 1537d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson private URI backdoorUrlToUri(URL url) throws Exception { 1538d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson final AtomicReference<URI> uriReference = new AtomicReference<URI>(); 1539d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1540d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson ResponseCache.setDefault(new ResponseCache() { 1541d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { 1542d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson return null; 1543d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1544d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson @Override public CacheResponse get(URI uri, String requestMethod, 1545d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson Map<String, List<String>> requestHeaders) throws IOException { 1546d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson uriReference.set(uri); 1547d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson throw new UnsupportedOperationException(); 1548d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1549d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson }); 1550d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1551d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson try { 1552d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 1553d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson connection.getResponseCode(); 1554d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } catch (Exception expected) { 1555d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1556d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1557d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson return uriReference.get(); 1558b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1559b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1560afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson /** 1561afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson * Don't explode if the cache returns a null body. http://b/3373699 1562afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson */ 1563afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson public void testResponseCacheReturnsNullOutputStream() throws Exception { 1564afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson final AtomicBoolean aborted = new AtomicBoolean(); 1565afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson ResponseCache.setDefault(new ResponseCache() { 1566afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public CacheResponse get(URI uri, String requestMethod, 1567afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson Map<String, List<String>> requestHeaders) throws IOException { 1568afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return null; 1569afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1570afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { 1571afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return new CacheRequest() { 1572afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public void abort() { 1573afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson aborted.set(true); 1574afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1575afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public OutputStream getBody() throws IOException { 1576afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return null; 1577afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1578afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson }; 1579afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1580afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson }); 1581afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson 1582afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson server.enqueue(new MockResponse().setBody("abcdef")); 1583afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson server.play(); 1584afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson 1585afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1586afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson InputStream in = connection.getInputStream(); 1587afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson assertEquals("abc", readAscii(in, 3)); 1588afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson in.close(); 1589afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson assertFalse(aborted.get()); // The best behavior is ambiguous, but RI 6 doesn't abort here 1590afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1591d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1592ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson /** 1593bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson * http://code.google.com/p/android/issues/detail?id=14562 1594bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson */ 1595bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson public void testReadAfterLastByte() throws Exception { 1596bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson server.enqueue(new MockResponse() 1597bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson .setBody("ABC") 1598bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson .clearHeaders() 1599bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson .addHeader("Connection: close") 1600bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END)); 1601bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson server.play(); 1602bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson 1603bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1604bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson InputStream in = connection.getInputStream(); 1605bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson assertEquals("ABC", readAscii(in, 3)); 1606bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson assertEquals(-1, in.read()); 1607bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson assertEquals(-1, in.read()); // throws IOException in Gingerbread 1608bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson } 1609bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson 1610bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson /** 16110c59055dd24e1659f85d9ff7e2148883f663bd62Jesse Wilson * Returns a gzipped copy of {@code bytes}. 1612deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson */ 1613deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public byte[] gzip(byte[] bytes) throws IOException { 1614deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 1615deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson OutputStream gzippedOut = new GZIPOutputStream(bytesOut); 1616deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson gzippedOut.write(bytes); 1617deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson gzippedOut.close(); 1618deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson return bytesOut.toByteArray(); 1619deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 1620deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 1621c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson /** 1622c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson * Reads at most {@code limit} characters from {@code in} and asserts that 1623c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson * content equals {@code expected}. 1624c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson */ 1625c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson private void assertContent(String expected, URLConnection connection, int limit) 1626c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson throws IOException { 1627f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom connection.connect(); 162851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(expected, readAscii(connection.getInputStream(), limit)); 1629c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson ((HttpURLConnection) connection).disconnect(); 1630c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 1631c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 1632c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson private void assertContent(String expected, URLConnection connection) throws IOException { 1633c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent(expected, connection, Integer.MAX_VALUE); 1634c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 1635c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 163660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson private void assertContains(List<String> headers, String header) { 163760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertTrue(headers.toString(), headers.contains(header)); 163860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 163951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 1640ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson private void assertContainsNoneMatching(List<String> headers, String pattern) { 1641ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (String header : headers) { 1642ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson if (header.matches(pattern)) { 1643ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson fail("Header " + header + " matches " + pattern); 1644ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1645ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1646ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1647ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1648eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson private Set<String> newSet(String... elements) { 164983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson return new HashSet<String>(Arrays.asList(elements)); 165083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 165183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 165251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson enum TransferKind { 165351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson CHUNKED() { 1654deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) 165551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson throws IOException { 165651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setChunkedBody(content, chunkSize); 165751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 165851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }, 165951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson FIXED_LENGTH() { 1660deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) { 166151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setBody(content); 166251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 166351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }, 166451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson END_OF_STREAM() { 1665deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) { 166651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setBody(content); 1667e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson response.setSocketPolicy(DISCONNECT_AT_END); 166851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson for (Iterator<String> h = response.getHeaders().iterator(); h.hasNext(); ) { 166951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (h.next().startsWith("Content-Length:")) { 167051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson h.remove(); 167151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson break; 167251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 167351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 167451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 167551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }; 167651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 1677deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson abstract void setBody(MockResponse response, byte[] content, int chunkSize) 167851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson throws IOException; 1679deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 1680deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson void setBody(MockResponse response, String content, int chunkSize) throws IOException { 1681deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson setBody(response, content.getBytes("UTF-8"), chunkSize); 1682deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 168351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 1684c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1685984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson enum ProxyConfig { 1686f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom NO_PROXY() { 1687f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom @Override public HttpURLConnection connect(MockWebServer server, URL url) 1688f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom throws IOException { 1689f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom return (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); 1690f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 1691f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom }, 1692f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 1693984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson CREATE_ARG() { 1694984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 1695984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 1696984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(server.toProxyAddress()); 1697984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 1698984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 1699984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 1700984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson PROXY_SYSTEM_PROPERTY() { 1701984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 1702984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 1703984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("proxyHost", "localhost"); 1704984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("proxyPort", Integer.toString(server.getPort())); 1705984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 1706984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 1707984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 1708984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 1709984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HTTP_PROXY_SYSTEM_PROPERTY() { 1710984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 1711984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 1712984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("http.proxyHost", "localhost"); 1713984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("http.proxyPort", Integer.toString(server.getPort())); 1714984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 1715984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 1716984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 1717984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 1718984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HTTPS_PROXY_SYSTEM_PROPERTY() { 1719984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 1720984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 1721984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("https.proxyHost", "localhost"); 1722984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("https.proxyPort", Integer.toString(server.getPort())); 1723984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 1724984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 1725984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }; 1726984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 1727984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public abstract HttpURLConnection connect(MockWebServer server, URL url) throws IOException; 1728984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 1729984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 1730c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private static class RecordingTrustManager implements X509TrustManager { 1731c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private final List<String> calls = new ArrayList<String>(); 1732c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1733c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public X509Certificate[] getAcceptedIssuers() { 1734c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("getAcceptedIssuers"); 1735c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return new X509Certificate[] {}; 1736c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1737c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1738c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void checkClientTrusted(X509Certificate[] chain, String authType) 1739c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson throws CertificateException { 1740c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("checkClientTrusted " + certificatesToString(chain) + " " + authType); 1741c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1742c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1743c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void checkServerTrusted(X509Certificate[] chain, String authType) 1744c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson throws CertificateException { 1745c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("checkServerTrusted " + certificatesToString(chain) + " " + authType); 1746c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1747c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1748c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private String certificatesToString(X509Certificate[] certificates) { 1749c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson List<String> result = new ArrayList<String>(); 1750c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson for (X509Certificate certificate : certificates) { 1751c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson result.add(certificate.getSubjectDN() + " " + certificate.getSerialNumber()); 1752c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1753c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return result.toString(); 1754c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1755c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1756c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1757c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private static class RecordingHostnameVerifier implements HostnameVerifier { 1758c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private final List<String> calls = new ArrayList<String>(); 1759c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1760c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public boolean verify(String hostname, SSLSession session) { 1761c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("verify " + hostname); 1762c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return true; 1763c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1764c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1765e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes} 1766