URLConnectionTest.java revision 4ec2d0dea800397fb32227e0631246f4d2a73191
1e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes/* 2e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Copyright (C) 2009 The Android Open Source Project 3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 4e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * you may not use this file except in compliance with the License. 6e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 8e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 10e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * See the License for the specific language governing permissions and 14e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes * limitations under the License. 15e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes */ 16e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.net; 18e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes 1909336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.MockResponse; 2009336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.MockWebServer; 2109336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.RecordedRequest; 2209336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport com.google.mockwebserver.SocketPolicy; 2309336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_END; 2409336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_START; 2509336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.SHUTDOWN_INPUT_AT_END; 2609336c914b4fc813e493acc82469b9ad89fd8694Jesse Wilsonimport static com.google.mockwebserver.SocketPolicy.SHUTDOWN_OUTPUT_AT_END; 27c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilsonimport java.io.ByteArrayOutputStream; 284ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.io.File; 296247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.io.IOException; 3051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.io.InputStream; 3102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.io.OutputStream; 324557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.Authenticator; 334557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheRequest; 344557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.CacheResponse; 35b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilsonimport java.net.ConnectException; 364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpRetryException; 374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.HttpURLConnection; 382d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilsonimport java.net.InetAddress; 394557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.PasswordAuthentication; 40f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilsonimport java.net.ProtocolException; 41f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstromimport java.net.Proxy; 424557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.ResponseCache; 434557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.SocketTimeoutException; 444557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URI; 454557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URL; 464557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.net.URLConnection; 47d0d626655f1d452070d3116678037e8759f807f4Jesse Wilsonimport java.net.UnknownHostException; 48c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.CertificateException; 49c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport java.security.cert.X509Certificate; 504ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.text.DateFormat; 514ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.text.SimpleDateFormat; 5251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.ArrayList; 5302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughesimport java.util.Arrays; 5451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Collections; 554ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.util.Date; 5683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.HashSet; 5751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Iterator; 586247987eb505a482a67f5f19678260d9e7240a5fElliott Hughesimport java.util.List; 594ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.util.Locale; 6083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.Map; 6151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilsonimport java.util.Set; 624ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.util.TimeZone; 634ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.util.UUID; 644ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport java.util.concurrent.TimeUnit; 65afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilsonimport java.util.concurrent.atomic.AtomicBoolean; 6683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilsonimport java.util.concurrent.atomic.AtomicReference; 67deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPInputStream; 68deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilsonimport java.util.zip.GZIPOutputStream; 6960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HostnameVerifier; 7060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.HttpsURLConnection; 71c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLContext; 72096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilsonimport javax.net.ssl.SSLException; 732915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport javax.net.ssl.SSLHandshakeException; 7460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilsonimport javax.net.ssl.SSLSession; 75c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.SSLSocketFactory; 76c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.TrustManager; 77c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilsonimport javax.net.ssl.X509TrustManager; 78ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilsonimport junit.framework.TestCase; 792915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstromimport libcore.java.security.TestKeyStore; 8050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilsonimport libcore.javax.net.ssl.TestSSLContext; 814ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstromimport libcore.net.http.HttpResponseCache; 825fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughesimport tests.net.StuckServer; 83e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes 84ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilsonpublic final class URLConnectionTest extends TestCase { 8551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private MockWebServer server = new MockWebServer(); 864ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom private HttpResponseCache cache; 870c2fd828abec671333b8b88281825fd27a783723Jesse Wilson private String hostName; 8800feece22909b7dc79fc96d666d157390b93858eJesse Wilson 8900feece22909b7dc79fc96d666d157390b93858eJesse Wilson @Override protected void setUp() throws Exception { 9000feece22909b7dc79fc96d666d157390b93858eJesse Wilson super.setUp(); 910c2fd828abec671333b8b88281825fd27a783723Jesse Wilson hostName = server.getHostName(); 9200feece22909b7dc79fc96d666d157390b93858eJesse Wilson } 9351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 9451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson @Override protected void tearDown() throws Exception { 9551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson ResponseCache.setDefault(null); 96ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson Authenticator.setDefault(null); 97984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("proxyHost"); 98984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("proxyPort"); 99984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("http.proxyHost"); 100984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("http.proxyPort"); 101984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("https.proxyHost"); 102984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.clearProperty("https.proxyPort"); 10351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.shutdown(); 1044ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom if (cache != null) { 1054ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom cache.getCache().delete(); 1064ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom } 10700feece22909b7dc79fc96d666d157390b93858eJesse Wilson super.tearDown(); 10851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 10951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 11083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson public void testRequestHeaders() throws IOException, InterruptedException { 11183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.enqueue(new MockResponse()); 11283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.play(); 11383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 11483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 11583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("D", "e"); 11683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("D", "f"); 117ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("f", urlConnection.getRequestProperty("D")); 118ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("f", urlConnection.getRequestProperty("d")); 11983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson Map<String, List<String>> requestHeaders = urlConnection.getRequestProperties(); 12083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("D"))); 121ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals(newSet("e", "f"), new HashSet<String>(requestHeaders.get("d"))); 12283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 12383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson requestHeaders.put("G", Arrays.asList("h")); 12483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 12583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 12683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 12783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 12883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson requestHeaders.get("D").add("i"); 12983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 13083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 13183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 13283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 13383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty(null, "j"); 13483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail(); 13583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (NullPointerException expected) { 13683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 13783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 13883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty(null, "k"); 13983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail(); 14083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (NullPointerException expected) { 14183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 14283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty("NullValue", null); // should fail silently! 143ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertNull(urlConnection.getRequestProperty("NullValue")); 14483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("AnotherNullValue", null); // should fail silently! 145ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertNull(urlConnection.getRequestProperty("AnotherNullValue")); 14683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 14783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.getResponseCode(); 14883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson RecordedRequest request = server.takeRequest(); 14983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContains(request.getHeaders(), "D: e"); 15083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContains(request.getHeaders(), "D: f"); 15183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "NullValue.*"); 15283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "AnotherNullValue.*"); 15383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "G:.*"); 15483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertContainsNoneMatching(request.getHeaders(), "null:.*"); 15583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 15683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 15783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.addRequestProperty("N", "o"); 15883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Set header after connect"); 15983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (IllegalStateException expected) { 16083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 16183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 16283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson urlConnection.setRequestProperty("P", "q"); 16383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Set header after connect"); 16483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (IllegalStateException expected) { 16583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 166ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 167ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson urlConnection.getRequestProperties(); 168ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 169ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 170ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 171ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 172ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 173ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetRequestPropertyReturnsLastValue() throws Exception { 174ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 175ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 176ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson urlConnection.addRequestProperty("A", "value1"); 177ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson urlConnection.addRequestProperty("A", "value2"); 178ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("value2", urlConnection.getRequestProperty("A")); 17983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 18083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 18183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson public void testResponseHeaders() throws IOException, InterruptedException { 18283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.enqueue(new MockResponse() 18383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .setStatus("HTTP/1.0 200 Fantastic") 18483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .addHeader("A: c") 185ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .addHeader("B: d") 186ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .addHeader("A: e") 18783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson .setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8)); 18883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson server.play(); 18983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 19083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 19183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals(200, urlConnection.getResponseCode()); 19283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson assertEquals("Fantastic", urlConnection.getResponseMessage()); 193c1a675c80c69decadb736b245f0366f93a94a462Jesse Wilson assertEquals("HTTP/1.0 200 Fantastic", urlConnection.getHeaderField(null)); 19483a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson Map<String, List<String>> responseHeaders = urlConnection.getHeaderFields(); 1958ac847a52e72f0cefbb20a6850ae04468d433a9eJesse Wilson assertEquals(Arrays.asList("HTTP/1.0 200 Fantastic"), responseHeaders.get(null)); 196ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals(newSet("c", "e"), new HashSet<String>(responseHeaders.get("A"))); 197ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals(newSet("c", "e"), new HashSet<String>(responseHeaders.get("a"))); 19883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 19983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson responseHeaders.put("N", Arrays.asList("o")); 20083a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 20183a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 20283a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 20383a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson try { 204ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson responseHeaders.get("A").add("f"); 20583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson fail("Modified an unmodifiable view."); 20683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } catch (UnsupportedOperationException expected) { 20783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 208ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", urlConnection.getHeaderFieldKey(0)); 209ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("c", urlConnection.getHeaderField(0)); 210ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("B", urlConnection.getHeaderFieldKey(1)); 211ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("d", urlConnection.getHeaderField(1)); 212ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", urlConnection.getHeaderFieldKey(2)); 213ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("e", urlConnection.getHeaderField(2)); 214ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 215ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 216ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetErrorStreamOnSuccessfulRequest() throws Exception { 217ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 218ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 219ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 220ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertNull(connection.getErrorStream()); 221ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 222ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 223ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetErrorStreamOnUnsuccessfulRequest() throws Exception { 224ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setResponseCode(404).setBody("A")); 225ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 226ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 227ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", readAscii(connection.getErrorStream(), Integer.MAX_VALUE)); 22883a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 22983a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 230e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // Check that if we don't read to the end of a response, the next request on the 231e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // recycled connection doesn't get the unread tail of the first request's response. 232e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes // http://code.google.com/p/android/issues/detail?id=2939 233e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes public void test_2939() throws Exception { 234b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson MockResponse response = new MockResponse().setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8); 235b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 236b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 237b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 238b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 239b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 240c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("ABCDE", server.getUrl("/").openConnection(), 5); 241c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("ABCDE", server.getUrl("/").openConnection(), 5); 2428baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson } 2438baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson 244977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes // Check that we recognize a few basic mime types by extension. 245977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes // http://code.google.com/p/android/issues/detail?id=10100 246977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes public void test_10100() throws Exception { 247977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes assertEquals("image/jpeg", URLConnection.guessContentTypeFromName("someFile.jpg")); 248977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes assertEquals("application/pdf", URLConnection.guessContentTypeFromName("stuff.pdf")); 249977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes } 250977a9954414ec41256b218e6278a8544ea135d45Elliott Hughes 2518baf143a7c8921d07b54adbc66ac1e5b42de5fe6Jesse Wilson public void testConnectionsArePooled() throws Exception { 252b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson MockResponse response = new MockResponse().setBody("ABCDEFGHIJKLMNOPQR"); 253b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 254b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 255b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 256b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(response); 257b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 258b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 25906e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection()); 260c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 26106e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection()); 262c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 26306e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection()); 264c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertEquals(2, server.takeRequest().getSequenceNumber()); 265c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 266c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 267c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testChunkedConnectionsArePooled() throws Exception { 268c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson MockResponse response = new MockResponse().setChunkedBody("ABCDEFGHIJKLMNOPQR", 5); 269c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 270c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 271c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 272c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(response); 273c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 274c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 27506e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/foo").openConnection()); 276b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 27706e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/bar?baz=quux").openConnection()); 278b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 27906e15e6c528fcb773bedb43e34b0577312570927Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQR", server.getUrl("/z").openConnection()); 280b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(2, server.takeRequest().getSequenceNumber()); 281e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes } 28202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 2830613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson /** 2840613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson * Test that connections are added to the pool as soon as the response has 2850613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson * been consumed. 2860613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson */ 2870613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson public void testConnectionsArePooledWithoutExplicitDisconnect() throws Exception { 2880613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 2890613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson server.enqueue(new MockResponse().setBody("DEF")); 2900613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson server.play(); 2910613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson 2920613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson URLConnection connection1 = server.getUrl("/").openConnection(); 2930613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson assertEquals("ABC", readAscii(connection1.getInputStream(), Integer.MAX_VALUE)); 2940613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 2950613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson URLConnection connection2 = server.getUrl("/").openConnection(); 2960613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson assertEquals("DEF", readAscii(connection2.getInputStream(), Integer.MAX_VALUE)); 2970613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 2980613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson } 2990613de89655e481fa610bfd4f1bcaeeae3272205Jesse Wilson 300e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson public void testServerClosesSocket() throws Exception { 301b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson testServerClosesSocket(DISCONNECT_AT_END); 302e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 303e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 304e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson public void testServerShutdownInput() throws Exception { 305b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson testServerClosesSocket(SHUTDOWN_INPUT_AT_END); 306e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 307e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 308b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson private void testServerClosesSocket(SocketPolicy socketPolicy) throws Exception { 309e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse() 310e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setBody("This connection won't pool properly") 311e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setSocketPolicy(socketPolicy)); 312b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("This comes after a busted connection")); 313e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.play(); 314e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 315e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertContent("This connection won't pool properly", server.getUrl("/a").openConnection()); 316e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 317e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertContent("This comes after a busted connection", server.getUrl("/b").openConnection()); 318e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson // sequence number 0 means the HTTP socket connection was not reused 319e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 320e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson } 321e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson 322b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson public void testServerShutdownOutput() throws Exception { 323b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson // This test causes MockWebServer to log a "connection failed" stack trace 324b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse() 325b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson .setBody("Output shutdown after this response") 326b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson .setSocketPolicy(SHUTDOWN_OUTPUT_AT_END)); 327b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("This response will fail to write")); 328b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("This comes after a busted connection")); 329b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.play(); 330b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 331b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertContent("Output shutdown after this response", server.getUrl("/a").openConnection()); 332b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 333b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertContent("This comes after a busted connection", server.getUrl("/b").openConnection()); 334b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 335b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 336b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson } 337b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 338b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson public void testRetryableRequestBodyAfterBrokenConnection() throws Exception { 339b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AT_END)); 340b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("def")); 341b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.play(); 342b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 343b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertContent("abc", server.getUrl("/a").openConnection()); 344b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/b").openConnection(); 345b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson connection.setDoOutput(true); 346b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson OutputStream out = connection.getOutputStream(); 347b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson out.write(new byte[] {1, 2, 3}); 348b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson out.close(); 349b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertContent("def", connection); 350b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson } 351b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 352b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson public void testNonRetryableRequestBodyAfterBrokenConnection() throws Exception { 353b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AT_END)); 354b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.enqueue(new MockResponse().setBody("def")); 355b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson server.play(); 356b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 357b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson assertContent("abc", server.getUrl("/a").openConnection()); 358b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/b").openConnection(); 359b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson connection.setDoOutput(true); 360b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson connection.setFixedLengthStreamingMode(3); 361b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson OutputStream out = connection.getOutputStream(); 362b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson out.write(new byte[] {1, 2, 3}); 363b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson out.close(); 364b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson try { 365b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson connection.getInputStream(); 366b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson fail(); 367b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson } catch (IOException expected) { 368b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson } 369b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson } 370b2b02ac6cd42a69463fd172531aa1f9b9bb887a8Jesse Wilson 371b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson enum WriteKind { BYTE_BY_BYTE, SMALL_BUFFERS, LARGE_BUFFERS } 37202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 37302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_byteByByte() throws Exception { 37451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.BYTE_BY_BYTE); 37502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 37602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 37702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_smallBuffers() throws Exception { 37851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.SMALL_BUFFERS); 37902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 38002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 38102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_chunkedUpload_largeBuffers() throws Exception { 38251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.CHUNKED, WriteKind.LARGE_BUFFERS); 38302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 38402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 38502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_byteByByte() throws Exception { 38651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.BYTE_BY_BYTE); 38702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 38802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 38902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_smallBuffers() throws Exception { 39051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.SMALL_BUFFERS); 39102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 39202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 39302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes public void test_fixedLengthUpload_largeBuffers() throws Exception { 39451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson doUpload(TransferKind.FIXED_LENGTH, WriteKind.LARGE_BUFFERS); 39502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 39602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes 39751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception { 39802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes int n = 512*1024; 399b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.setBodyLimit(0); 400b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.enqueue(new MockResponse()); 401b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson server.play(); 402b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson 403b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson HttpURLConnection conn = (HttpURLConnection) server.getUrl("/").openConnection(); 40402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setDoOutput(true); 40502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setRequestMethod("POST"); 40651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (uploadKind == TransferKind.CHUNKED) { 40702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setChunkedStreamingMode(-1); 40802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } else { 40902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes conn.setFixedLengthStreamingMode(n); 41002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 41102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes OutputStream out = conn.getOutputStream(); 41202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes if (writeKind == WriteKind.BYTE_BY_BYTE) { 41302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes for (int i = 0; i < n; ++i) { 41402f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.write('x'); 41502f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 41602f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } else { 41702f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64*1024]; 41802f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes Arrays.fill(buf, (byte) 'x'); 41902f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes for (int i = 0; i < n; i += buf.length) { 42002f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.write(buf, 0, Math.min(buf.length, n - i)); 42102f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 42202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 42302f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes out.close(); 4244cb7f05dc68abb23ae54a5891c369062185f2210Elliott Hughes assertEquals(200, conn.getResponseCode()); 425b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson RecordedRequest request = server.takeRequest(); 426b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertEquals(n, request.getBodySize()); 42751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (uploadKind == TransferKind.CHUNKED) { 428b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertTrue(request.getChunkSizes().size() > 0); 429b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson } else { 430b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson assertTrue(request.getChunkSizes().isEmpty()); 431b1b5baac449d2725002338735f4db34bec8fd001Jesse Wilson } 43202f0cb2eb84a112fcf644d7d1fd0b5f94ea2f03bElliott Hughes } 4336247987eb505a482a67f5f19678260d9e7240a5fElliott Hughes 434f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson public void testGetResponseCodeNoResponseBody() throws Exception { 435f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson server.enqueue(new MockResponse() 436f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson .addHeader("abc: def")); 437f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson server.play(); 438f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson 439f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson URL url = server.getUrl("/"); 440f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 441f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson conn.setDoInput(false); 442f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson assertEquals("def", conn.getHeaderField("abc")); 443f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson assertEquals(200, conn.getResponseCode()); 444f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson try { 445f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson conn.getInputStream(); 446f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson fail(); 447f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } catch (ProtocolException expected) { 448f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } 449f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson } 450f14bee4dbf9f724a177a85a7b73f7b0bc6df1ec0Jesse Wilson 45160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson public void testConnectViaHttps() throws IOException, InterruptedException { 45260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 45360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 454059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 455c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 45660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson server.play(); 45760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 458096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 4594559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 46060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 461c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via HTTPS", connection); 46260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 46360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson RecordedRequest request = server.takeRequest(); 46460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 4653d74b4bec8543e6e3f89eafe3afe0925f3a69f01Brian Carlstrom assertEquals("TLSv1", request.getSslProtocol()); 46660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 46760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 468096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson public void testConnectViaHttpsReusingConnections() throws IOException, InterruptedException { 469096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 470fa5e8dfe3c7ed144b0fbe69091628dedd6a3b961Jesse Wilson SSLSocketFactory clientSocketFactory = testSSLContext.clientContext.getSocketFactory(); 471096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 472059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 473096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 474096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.enqueue(new MockResponse().setBody("another response via HTTPS")); 475096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson server.play(); 476096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 477b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 478fa5e8dfe3c7ed144b0fbe69091628dedd6a3b961Jesse Wilson connection.setSSLSocketFactory(clientSocketFactory); 479b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertContent("this response comes via HTTPS", connection); 480b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 481b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 482fa5e8dfe3c7ed144b0fbe69091628dedd6a3b961Jesse Wilson connection.setSSLSocketFactory(clientSocketFactory); 483b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertContent("another response via HTTPS", connection); 484b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 485b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 486b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 487b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson } 488b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 4898116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson public void testConnectViaHttpsReusingConnectionsDifferentFactories() 490b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson throws IOException, InterruptedException { 491b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 492b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 493b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 494b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 495b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.enqueue(new MockResponse().setBody("another response via HTTPS")); 496b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson server.play(); 497b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson 498096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson // install a custom SSL socket factory so the server can be authorized 499096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 500059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 501096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson assertContent("this response comes via HTTPS", connection); 502096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 503096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 504096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson try { 505096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 506b7155fd57239e986bbaba254a91aeb9600d60305Jesse Wilson fail("without an SSL socket factory, the connection should fail"); 507096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } catch (SSLException expected) { 508096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } 509096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson } 510096aac7b8a607d3da237900f52cab1c5066bf992Jesse Wilson 5114559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException { 5124559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(); 5134559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 5144559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 515e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START)); 5164559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.enqueue(new MockResponse().setBody("this response comes via SSL")); 5174559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom server.play(); 5184559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 5194559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 5204559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 5214559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 5224559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertContent("this response comes via SSL", connection); 5234559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 5244559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom RecordedRequest request = server.takeRequest(); 5254559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 5264559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom } 5274559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom 5282915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom /** 5292915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * Verify that we don't retry connections on certificate verification errors. 5302915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * 5312915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom * http://code.google.com/p/android/issues/detail?id=13178 5322915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom */ 5332915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom public void testConnectViaHttpsToUntrustedServer() throws IOException, InterruptedException { 5342915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(TestKeyStore.getClientCA2(), 5352915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom TestKeyStore.getServer()); 5362915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 5372915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 5382915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.enqueue(new MockResponse()); // unused 5392915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom server.play(); 5402915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 5412915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection(); 5422915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 5432915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom try { 5442915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom connection.getInputStream(); 5452915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom fail(); 5462915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } catch (SSLHandshakeException expected) { 5472915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom assertTrue(expected.getCause() instanceof CertificateException); 5482915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } 5492915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom assertEquals(0, server.getRequestCount()); 5502915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom } 5512915378e253f08e47fe5a9bfd026cd1ca7c6c351Brian Carlstrom 552984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingProxyArg() throws Exception { 553984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.CREATE_ARG); 554984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 555984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 556984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingProxySystemProperty() throws Exception { 557984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.PROXY_SYSTEM_PROPERTY); 558984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 559984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 560984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaProxyUsingHttpProxySystemProperty() throws Exception { 561984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaProxy(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); 562984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 563984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 564984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson private void testConnectViaProxy(ProxyConfig proxyConfig) throws Exception { 565c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockResponse mockResponse = new MockResponse().setBody("this response comes via a proxy"); 56651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(mockResponse); 56751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 56860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 569984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson URL url = new URL("http://android.com/foo"); 570984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HttpURLConnection connection = proxyConfig.connect(server, url); 571c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via a proxy", connection); 57260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 57351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest request = server.takeRequest(); 57460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET http://android.com/foo HTTP/1.1", request.getRequestLine()); 57560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(request.getHeaders(), "Host: android.com"); 57660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 57760476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 578c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testContentDisagreesWithContentLengthHeader() throws IOException { 579c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(new MockResponse() 580c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .setBody("abc\r\nYOU SHOULD NOT SEE THIS") 581c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .clearHeaders() 582c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson .addHeader("Content-Length: 3")); 583c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 584c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 585c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("abc", server.getUrl("/").openConnection()); 586c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 587c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 588c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson public void testContentDisagreesWithChunkedHeader() throws IOException { 589c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson MockResponse mockResponse = new MockResponse(); 590c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.setChunkedBody("abc", 3); 591c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 592c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson bytesOut.write(mockResponse.getBody()); 593c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson bytesOut.write("\r\nYOU SHOULD NOT SEE THIS".getBytes()); 594c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.setBody(bytesOut.toByteArray()); 595c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.clearHeaders(); 596c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson mockResponse.addHeader("Transfer-encoding: chunked"); 597c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 598c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.enqueue(mockResponse); 599c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson server.play(); 600c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 601c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("abc", server.getUrl("/").openConnection()); 602c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 603c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 604f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom public void testConnectViaHttpProxyToHttpsUsingProxyArgWithNoProxy() throws Exception { 605f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom testConnectViaDirectProxyToHttps(ProxyConfig.NO_PROXY); 606f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 607f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 608f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception { 609f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom // https should not use http proxy 610f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom testConnectViaDirectProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); 611f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 612f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 613f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception { 614f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(); 615f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 616f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 617f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); 618f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom server.play(); 619f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 620f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom URL url = server.getUrl("/foo"); 621f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); 622f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 623f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 624f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom assertContent("this response comes via HTTPS", connection); 625f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 626f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom RecordedRequest request = server.takeRequest(); 627f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); 628f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 629f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 630f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 631984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingProxyArg() throws Exception { 632984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.CREATE_ARG); 633984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 634984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 635984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson /** 636984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * We weren't honoring all of the appropriate proxy system properties when 637984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * connecting via HTTPS. http://b/3097518 638984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson */ 639984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingProxySystemProperty() throws Exception { 640984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY); 641984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 642984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 643984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public void testConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty() throws Exception { 644984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY); 645984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 646984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 647984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson /** 648984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * We were verifying the wrong hostname when connecting to an HTTPS site 649984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson * through a proxy. http://b/3097277 650984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson */ 651984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception { 65260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 653984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 65460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 655059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 656c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 657c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 658c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 659c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("this response comes via a secure proxy")); 66051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 66160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 66260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson URL url = new URL("https://android.com/foo"); 663984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); 664059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 665984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson connection.setHostnameVerifier(hostnameVerifier); 66660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 667c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent("this response comes via a secure proxy", connection); 66860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 66951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest connect = server.takeRequest(); 67060476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("Connect line failure on proxy", 67160476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine()); 67260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(connect.getHeaders(), "Host: android.com"); 67360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 67451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson RecordedRequest get = server.takeRequest(); 67560476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertEquals("GET /foo HTTP/1.1", get.getRequestLine()); 67660476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertContains(get.getHeaders(), "Host: android.com"); 677984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls); 67860476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 67960476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson 6804ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 6814ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom /** 6824ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom * Tolerate bad https proxy response when using HttpResponseCache. http://b/6754912 6834ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom */ 6844ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom public void testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception { 6854ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom ProxyConfig proxyConfig = ProxyConfig.PROXY_SYSTEM_PROPERTY; 6864ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 6874ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom TestSSLContext testSSLContext = TestSSLContext.create(); 6884ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 6894ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom initResponseCache(); 6904ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 6914ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 6924ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom server.enqueue(new MockResponse() 6934ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 6944ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom .clearHeaders() 6954ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom .setBody("bogus proxy connect response content")); // Key to reproducing b/6754912 6964ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom server.play(); 6974ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 6984ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom URL url = new URL("https://android.com/foo"); 6994ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); 7004ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 7014ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 7024ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom try { 7034ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom connection.connect(); 7044ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom fail(); 7054ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom } catch (IOException expected) { 7064ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom // Thrown when the connect causes SSLSocket.startHandshake() to throw 7074ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom // when it sees the "bogus proxy connect response content" 7084ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom // instead of a ServerHello handshake message. 7094ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom } 7104ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 7114ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom RecordedRequest connect = server.takeRequest(); 7124ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom assertEquals("Connect line failure on proxy", 7134ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine()); 7144ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom assertContains(connect.getHeaders(), "Host: android.com"); 7154ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom } 7164ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 7174ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom private void initResponseCache() throws IOException { 7184ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom String tmp = System.getProperty("java.io.tmpdir"); 7194ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom File cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); 7204ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom cache = new HttpResponseCache(cacheDir, Integer.MAX_VALUE); 7214ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom ResponseCache.setDefault(cache); 7224ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom } 7234ec2d0dea800397fb32227e0631246f4d2a73191Brian Carlstrom 724d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson /** 725d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson * Test which headers are sent unencrypted to the HTTP proxy. 726d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson */ 727d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson public void testProxyConnectIncludesProxyHeadersOnly() 728d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson throws IOException, InterruptedException { 729984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 730d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 731d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 732d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 733c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 734c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 735c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 736d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.enqueue(new MockResponse().setBody("encrypted response from the origin server")); 737d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.play(); 738d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 739d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson URL url = new URL("https://android.com/foo"); 740d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) url.openConnection( 741d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.toProxyAddress()); 742d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("Private", "Secret"); 743d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("Proxy-Authorization", "bar"); 744d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.addRequestProperty("User-Agent", "baz"); 745d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 746984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson connection.setHostnameVerifier(hostnameVerifier); 747d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContent("encrypted response from the origin server", connection); 748d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 749d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson RecordedRequest connect = server.takeRequest(); 750d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContainsNoneMatching(connect.getHeaders(), "Private.*"); 751d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Proxy-Authorization: bar"); 752d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "User-Agent: baz"); 753d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Host: android.com"); 754d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(connect.getHeaders(), "Proxy-Connection: Keep-Alive"); 755d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 756d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson RecordedRequest get = server.takeRequest(); 757d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertContains(get.getHeaders(), "Private: Secret"); 758984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls); 759d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 760d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 761c996149b500fc4825156106554457fe2394ae087Jesse Wilson public void testProxyAuthenticateOnConnect() throws Exception { 7625757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(new SimpleAuthenticator()); 763c996149b500fc4825156106554457fe2394ae087Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 764c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), true); 765c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 766c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setResponseCode(407) 767c996149b500fc4825156106554457fe2394ae087Jesse Wilson .addHeader("Proxy-Authenticate: Basic realm=\"localhost\"")); 768c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse() 769c996149b500fc4825156106554457fe2394ae087Jesse Wilson .setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END) 770c996149b500fc4825156106554457fe2394ae087Jesse Wilson .clearHeaders()); 771c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.enqueue(new MockResponse().setBody("A")); 772c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.play(); 773c996149b500fc4825156106554457fe2394ae087Jesse Wilson 774c996149b500fc4825156106554457fe2394ae087Jesse Wilson URL url = new URL("https://android.com/foo"); 775c996149b500fc4825156106554457fe2394ae087Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) url.openConnection( 776c996149b500fc4825156106554457fe2394ae087Jesse Wilson server.toProxyAddress()); 777c996149b500fc4825156106554457fe2394ae087Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 778c996149b500fc4825156106554457fe2394ae087Jesse Wilson connection.setHostnameVerifier(new RecordingHostnameVerifier()); 779c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContent("A", connection); 780c996149b500fc4825156106554457fe2394ae087Jesse Wilson 781c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest connect1 = server.takeRequest(); 782c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine()); 783c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*"); 784c996149b500fc4825156106554457fe2394ae087Jesse Wilson 785c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest connect2 = server.takeRequest(); 786c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine()); 7875757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " 7885757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson + SimpleAuthenticator.BASE_64_CREDENTIALS); 789c996149b500fc4825156106554457fe2394ae087Jesse Wilson 790c996149b500fc4825156106554457fe2394ae087Jesse Wilson RecordedRequest get = server.takeRequest(); 791c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertEquals("GET /foo HTTP/1.1", get.getRequestLine()); 792c996149b500fc4825156106554457fe2394ae087Jesse Wilson assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*"); 793c996149b500fc4825156106554457fe2394ae087Jesse Wilson } 794c996149b500fc4825156106554457fe2394ae087Jesse Wilson 795d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson public void testDisconnectedConnection() throws IOException { 796d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.enqueue(new MockResponse().setBody("ABCDEFGHIJKLMNOPQR")); 797d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson server.play(); 798d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 799d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 800d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson InputStream in = connection.getInputStream(); 801d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson assertEquals('A', (char) in.read()); 802d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson connection.disconnect(); 803d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson try { 804d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson in.read(); 805d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson fail("Expected a connection closed exception"); 806d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } catch (IOException expected) { 807d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 808d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson } 809d4bddd7d1fb7b1b7f0836648228235c6e4b56a18Jesse Wilson 810d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testDisconnectBeforeConnect() throws IOException { 811d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("A")); 812d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 813d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 814d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 815d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson connection.disconnect(); 816d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 817d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertContent("A", connection); 818d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(200, connection.getResponseCode()); 819d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 820d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 821d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testDefaultRequestProperty() throws Exception { 822d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson URLConnection.setDefaultRequestProperty("X-testSetDefaultRequestProperty", "A"); 823d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertNull(URLConnection.getDefaultRequestProperty("X-setDefaultRequestProperty")); 824d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 825d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 82651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson /** 82751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * Reads {@code count} characters from the stream. If the stream is 82851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * exhausted before {@code count} characters can be read, the remaining 82951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * characters are returned and the stream is closed. 83051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson */ 83151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson private String readAscii(InputStream in, int count) throws IOException { 83251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson StringBuilder result = new StringBuilder(); 83351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson for (int i = 0; i < count; i++) { 83451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson int value = in.read(); 83551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (value == -1) { 83651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.close(); 83751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson break; 83851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 83951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson result.append((char) value); 84051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 84151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson return result.toString(); 84251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 84351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 84451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithContentLengthHeader() throws IOException { 84551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.FIXED_LENGTH); 84651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 84751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 84851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithChunkedEncoding() throws IOException { 84951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.CHUNKED); 85051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 85151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 85251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testMarkAndResetWithNoLengthHeaders() throws IOException { 85351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson testMarkAndReset(TransferKind.END_OF_STREAM); 85451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 85551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 8560c2fd828abec671333b8b88281825fd27a783723Jesse Wilson private void testMarkAndReset(TransferKind transferKind) throws IOException { 85751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson MockResponse response = new MockResponse(); 85851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson transferKind.setBody(response, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1024); 85951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 860953df613522e12a418cb7cb73248594d6c9f53d4Jesse Wilson server.enqueue(response); 86151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 86251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 86351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson InputStream in = server.getUrl("/").openConnection().getInputStream(); 86451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertFalse("This implementation claims to support mark().", in.markSupported()); 86551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.mark(5); 86651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals("ABCDE", readAscii(in, 5)); 86751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson try { 86851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson in.reset(); 86951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson fail(); 87051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } catch (IOException expected) { 87151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 87251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals("FGHIJKLMNOPQRSTUVWXYZ", readAscii(in, Integer.MAX_VALUE)); 87351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ", server.getUrl("/").openConnection()); 87451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 87551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 87651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson /** 87751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * We've had a bug where we forget the HTTP response when we see response 87851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * code 401. This causes a new HTTP request to be issued for every call into 87951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson * the URLConnection. 88051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson */ 88151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson public void testUnauthorizedResponseHandling() throws IOException { 88251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson MockResponse response = new MockResponse() 8835757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 88451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson .setResponseCode(401) // UNAUTHORIZED 88551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson .setBody("Unauthorized"); 88651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 88751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 88851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.enqueue(response); 88951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson server.play(); 89051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 89151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson URL url = server.getUrl("/"); 89251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 89351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 89451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 89551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 89651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(401, conn.getResponseCode()); 89751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(1, server.getRequestCount()); 89851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 89951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 9006906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson public void testNonHexChunkSize() throws IOException { 9016906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.enqueue(new MockResponse() 9026906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .setBody("5\r\nABCDE\r\nG\r\nFGHIJKLMNOPQRSTU\r\n0\r\n\r\n") 9036906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .clearHeaders() 9046906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .addHeader("Transfer-encoding: chunked")); 9056906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.play(); 9066906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 9076906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 9086906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson try { 9096906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 9106906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson fail(); 9116906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } catch (IOException e) { 9126906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 9136906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 9146906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 9156906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson public void testMissingChunkBody() throws IOException { 9166906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.enqueue(new MockResponse() 9176906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .setBody("5") 9186906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .clearHeaders() 9196906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson .addHeader("Transfer-encoding: chunked") 920e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson .setSocketPolicy(DISCONNECT_AT_END)); 9216906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson server.play(); 9226906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 9236906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 9246906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson try { 9256906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE); 9266906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson fail(); 9276906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } catch (IOException e) { 9286906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 9296906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson } 9306906b0c12dcf3216883d0373973a252812a20d32Jesse Wilson 93150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson /** 93250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * This test checks whether connections are gzipped by default. This 93350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * behavior in not required by the API, so a failure of this test does not 93450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson * imply a bug in the implementation. 93550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson */ 93650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException { 93750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.enqueue(new MockResponse() 93850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .setBody(gzip("ABCABCABC".getBytes("UTF-8"))) 93950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .addHeader("Content-Encoding: gzip")); 94050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.play(); 94150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 94250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 94350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 9448116f7e97e00d223e7fbe5c950c9a5e3277de124Jesse Wilson assertNull(connection.getContentEncoding()); 94550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 94650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson RecordedRequest request = server.takeRequest(); 94750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: gzip"); 94850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson } 94950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 950deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testClientConfiguredGzipContentEncoding() throws Exception { 951deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(new MockResponse() 952deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson .setBody(gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"))) 953deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson .addHeader("Content-Encoding: gzip")); 954deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.play(); 955deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 956deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 957deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection.addRequestProperty("Accept-Encoding", "gzip"); 958deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); 959deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE)); 960deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 961deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson RecordedRequest request = server.takeRequest(); 962deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: gzip"); 963deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 964deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 965deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testGzipAndConnectionReuseWithFixedLength() throws Exception { 966deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH); 967deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 968deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 969deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception { 970deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED); 971deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 972deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 97350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson public void testClientConfiguredCustomContentEncoding() throws Exception { 97450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.enqueue(new MockResponse() 97550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .setBody("ABCDE") 97650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson .addHeader("Content-Encoding: custom")); 97750ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson server.play(); 97850ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 97950ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 98050ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson connection.addRequestProperty("Accept-Encoding", "custom"); 98150ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 98250ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 98350ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson RecordedRequest request = server.takeRequest(); 98450ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson assertContains(request.getHeaders(), "Accept-Encoding: custom"); 98550ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson } 98650ae32218918eae80298bd1ab8e4f588bbbabdb2Jesse Wilson 987deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson /** 988deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * Test a bug where gzip input streams weren't exhausting the input stream, 989deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * which corrupted the request that followed. 990deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson * http://code.google.com/p/android/issues/detail?id=7059 991deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson */ 992deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson private void testClientConfiguredGzipContentEncodingAndConnectionReuse( 993deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson TransferKind transferKind) throws Exception { 994deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson MockResponse responseOne = new MockResponse(); 995deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson responseOne.addHeader("Content-Encoding: gzip"); 996deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson transferKind.setBody(responseOne, gzip("one (gzipped)".getBytes("UTF-8")), 5); 997deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(responseOne); 998deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson MockResponse responseTwo = new MockResponse(); 999deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson transferKind.setBody(responseTwo, "two (identity)", 5); 1000deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.enqueue(responseTwo); 1001deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson server.play(); 1002deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 1003deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1004deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection.addRequestProperty("Accept-Encoding", "gzip"); 1005deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); 1006deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE)); 1007deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1008deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 1009deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson connection = server.getUrl("/").openConnection(); 1010deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals("two (identity)", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1011deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 1012deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 1013deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 1014deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson /** 1015df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson * Test that HEAD requests don't have a body regardless of the response 1016df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson * headers. http://code.google.com/p/android/issues/detail?id=24672 1017df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson */ 1018df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson public void testHeadAndContentLength() throws Exception { 1019df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson server.enqueue(new MockResponse() 1020df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson .clearHeaders() 1021df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson .addHeader("Content-Length: 100")); 1022df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson server.enqueue(new MockResponse().setBody("A")); 1023df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson server.play(); 1024df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson 1025df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson HttpURLConnection connection1 = (HttpURLConnection) server.getUrl("/").openConnection(); 1026df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson connection1.setRequestMethod("HEAD"); 1027df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson assertEquals("100", connection1.getHeaderField("Content-Length")); 1028df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson assertContent("", connection1); 1029df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson 1030df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson HttpURLConnection connection2 = (HttpURLConnection) server.getUrl("/").openConnection(); 1031df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson assertEquals("A", readAscii(connection2.getInputStream(), Integer.MAX_VALUE)); 1032df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson 1033df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1034df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson assertEquals(1, server.takeRequest().getSequenceNumber()); 1035df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson } 1036df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson 1037df29c7dc69965462cd19de8910b04c8cb463e57fJesse Wilson /** 1038ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * Obnoxiously test that the chunk sizes transmitted exactly equal the 1039ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * requested data+chunk header size. Although setChunkedStreamingMode() 1040ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * isn't specific about whether the size applies to the data or the 1041ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson * complete chunk, the RI interprets it as a complete chunk. 1042ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson */ 1043ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testSetChunkedStreamingMode() throws IOException, InterruptedException { 1044ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse()); 1045ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 1046ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1047ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 1048ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson urlConnection.setChunkedStreamingMode(8); 1049ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson urlConnection.setDoOutput(true); 1050ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = urlConnection.getOutputStream(); 1051ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII")); 1052ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(200, urlConnection.getResponseCode()); 1053ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1054ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 1055ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII")); 1056ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes()); 1057ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1058ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1059ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithFixedLengthStreaming() throws Exception { 1060ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson testAuthenticateWithStreamingPost(StreamingMode.FIXED_LENGTH); 1061ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1062ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1063ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithChunkedStreaming() throws Exception { 1064ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson testAuthenticateWithStreamingPost(StreamingMode.CHUNKED); 1065ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1066ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1067ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson private void testAuthenticateWithStreamingPost(StreamingMode streamingMode) throws Exception { 1068ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 1069ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 1070ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 1071ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 1072ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1073ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 1074ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 10755757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(new SimpleAuthenticator()); 1076ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1077ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setDoOutput(true); 1078ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 1079ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 1080ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setFixedLengthStreamingMode(requestBody.length); 1081ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 1082ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setChunkedStreamingMode(0); 1083ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1084ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = connection.getOutputStream(); 1085ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write(requestBody); 1086ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.close(); 1087ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson try { 1088ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.getInputStream(); 1089ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson fail(); 1090ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } catch (HttpRetryException expected) { 1091ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1092ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1093ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the request... 1094ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 1095ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: Basic .*"); 1096ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 1097ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1098ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1099ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testSetValidRequestMethod() throws Exception { 1100ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1101ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("GET"); 1102ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("DELETE"); 1103ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("HEAD"); 1104ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("OPTIONS"); 1105ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("POST"); 1106ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("PUT"); 1107ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertValidRequestMethod("TRACE"); 1108ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1109ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1110ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson private void assertValidRequestMethod(String requestMethod) throws Exception { 1111ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1112ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setRequestMethod(requestMethod); 1113ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals(requestMethod, connection.getRequestMethod()); 1114ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1115ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1116ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testSetInvalidRequestMethodLowercase() throws Exception { 1117ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1118ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertInvalidRequestMethod("get"); 1119ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1120ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1121ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testSetInvalidRequestMethodConnect() throws Exception { 1122ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1123ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertInvalidRequestMethod("CONNECT"); 1124ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1125ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1126ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson private void assertInvalidRequestMethod(String requestMethod) throws Exception { 1127ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1128ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1129ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setRequestMethod(requestMethod); 1130ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1131ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (ProtocolException expected) { 1132ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1133ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1134ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1135ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCannotSetNegativeFixedLengthStreamingMode() throws Exception { 1136ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1137ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1138ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1139ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setFixedLengthStreamingMode(-2); 1140ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1141ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalArgumentException expected) { 1142ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1143ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1144ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1145ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCanSetNegativeChunkedStreamingMode() throws Exception { 1146ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1147ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1148ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setChunkedStreamingMode(-2); 1149ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1150ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1151ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCannotSetFixedLengthStreamingModeAfterConnect() throws Exception { 1152ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 1153ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1154ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1155ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1156ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1157ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setFixedLengthStreamingMode(1); 1158ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1159ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 1160ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1161ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1162ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1163ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCannotSetChunkedStreamingModeAfterConnect() throws Exception { 1164ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 1165ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1166ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1167ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1168ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1169ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setChunkedStreamingMode(1); 1170ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1171ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 1172ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1173ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1174ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1175ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCannotSetFixedLengthStreamingModeAfterChunkedStreamingMode() throws Exception { 1176ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1177ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1178ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setChunkedStreamingMode(1); 1179ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1180ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setFixedLengthStreamingMode(1); 1181ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1182ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 1183ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1184ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1185ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1186ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testCannotSetChunkedStreamingModeAfterFixedLengthStreamingMode() throws Exception { 1187ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1188ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1189ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setFixedLengthStreamingMode(1); 1190ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1191ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setChunkedStreamingMode(1); 1192ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1193ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 1194ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1195ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1196ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 119735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson public void testSecureFixedLengthStreaming() throws Exception { 119835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson testSecureStreamingPost(StreamingMode.FIXED_LENGTH); 119935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 120035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 120135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson public void testSecureChunkedStreaming() throws Exception { 120235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson testSecureStreamingPost(StreamingMode.CHUNKED); 120335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 120435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 120535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson /** 120635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson * Users have reported problems using HTTPS with streaming request bodies. 120735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson * http://code.google.com/p/android/issues/detail?id=12860 120835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson */ 120935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception { 121035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 121135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 121235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.enqueue(new MockResponse().setBody("Success!")); 121335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson server.play(); 121435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 121535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 121635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 121735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setDoOutput(true); 121835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 121935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 122035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setFixedLengthStreamingMode(requestBody.length); 122135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 122235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson connection.setChunkedStreamingMode(0); 122335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 122435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson OutputStream outputStream = connection.getOutputStream(); 122535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson outputStream.write(requestBody); 122635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson outputStream.close(); 122735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 122835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 122935eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson RecordedRequest request = server.takeRequest(); 123035eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals("POST / HTTP/1.1", request.getRequestLine()); 123135eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson if (streamingMode == StreamingMode.FIXED_LENGTH) { 123235eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes()); 123335eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } else if (streamingMode == StreamingMode.CHUNKED) { 123435eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Arrays.asList(4), request.getChunkSizes()); 123535eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 123635eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 123735eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson } 123835eef71e8ce721c4199c525890ecc1a263054596Jesse Wilson 1239ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson enum StreamingMode { 1240ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson FIXED_LENGTH, CHUNKED 1241ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1242ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1243ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithPost() throws Exception { 1244ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 1245ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 1246ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 1247ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 1248ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // fail auth three times... 1249ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1250ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1251ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1252ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...then succeed the fourth time 1253ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 1254ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 1255ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 12565757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(new SimpleAuthenticator()); 1257ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1258ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson connection.setDoOutput(true); 1259ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 1260ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson OutputStream outputStream = connection.getOutputStream(); 1261ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.write(requestBody); 1262ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson outputStream.close(); 1263ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1264ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1265ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the first request... 1266ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 12675757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: .*"); 1268ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1269da289bcd0a9e207cc03c752f7c21c9004056e179Jesse Wilson // ...but the three requests that follow include an authorization header 1270ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (int i = 0; i < 3; i++) { 1271ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson request = server.takeRequest(); 1272ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("POST / HTTP/1.1", request.getRequestLine()); 12735757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContains(request.getHeaders(), "Authorization: Basic " 12745757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson + SimpleAuthenticator.BASE_64_CREDENTIALS); 1275ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody())); 1276ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1277ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1278ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1279ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson public void testAuthenticateWithGet() throws Exception { 1280ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson MockResponse pleaseAuthenticate = new MockResponse() 1281ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setResponseCode(401) 1282ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .addHeader("WWW-Authenticate: Basic realm=\"protected area\"") 1283ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson .setBody("Please authenticate."); 1284ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // fail auth three times... 1285ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1286ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1287ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(pleaseAuthenticate); 1288ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...then succeed the fourth time 1289ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 1290ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson server.play(); 1291ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 12925757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson SimpleAuthenticator authenticator = new SimpleAuthenticator(); 12935757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(authenticator); 1294ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1295ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 12965757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals(Authenticator.RequestorType.SERVER, authenticator.requestorType); 12975757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals(server.getPort(), authenticator.requestingPort); 12985757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals(InetAddress.getByName(server.getHostName()), authenticator.requestingSite); 12995757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("protected area", authenticator.requestingPrompt); 13005757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("http", authenticator.requestingProtocol); 13015757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("Basic", authenticator.requestingScheme); 1302ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1303ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // no authorization header for the first request... 1304ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson RecordedRequest request = server.takeRequest(); 13055757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContainsNoneMatching(request.getHeaders(), "Authorization: .*"); 1306ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 1307ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson // ...but the three requests that follow requests include an authorization header 1308ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (int i = 0; i < 3; i++) { 1309ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson request = server.takeRequest(); 1310ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson assertEquals("GET / HTTP/1.1", request.getRequestLine()); 13115757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContains(request.getHeaders(), "Authorization: Basic " 13125757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson + SimpleAuthenticator.BASE_64_CREDENTIALS); 1313ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1314ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 1315ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 13165757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson // http://code.google.com/p/android/issues/detail?id=19081 13175757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson public void testAuthenticateWithCommaSeparatedAuthenticationMethods() throws Exception { 13185757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.enqueue(new MockResponse() 13195757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .setResponseCode(401) 13205757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .addHeader("WWW-Authenticate: Scheme1 realm=\"a\", Scheme2 realm=\"b\", " 13215757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson + "Scheme3 realm=\"c\"") 13225757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .setBody("Please authenticate.")); 13235757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 13245757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.play(); 13255757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 13265757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson SimpleAuthenticator authenticator = new SimpleAuthenticator(); 13275757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson authenticator.expectedPrompt = "b"; 13285757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(authenticator); 13295757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 13305757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 13315757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 13325757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContainsNoneMatching(server.takeRequest().getHeaders(), "Authorization: .*"); 13335757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContains(server.takeRequest().getHeaders(), 13345757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson "Authorization: Scheme2 " + SimpleAuthenticator.BASE_64_CREDENTIALS); 13355757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("Scheme2", authenticator.requestingScheme); 13365757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson } 13375757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 13385757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson public void testAuthenticateWithMultipleAuthenticationHeaders() throws Exception { 13395757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.enqueue(new MockResponse() 13405757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .setResponseCode(401) 13415757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .addHeader("WWW-Authenticate: Scheme1 realm=\"a\"") 13425757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .addHeader("WWW-Authenticate: Scheme2 realm=\"b\"") 13435757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .addHeader("WWW-Authenticate: Scheme3 realm=\"c\"") 13445757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson .setBody("Please authenticate.")); 13455757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.enqueue(new MockResponse().setBody("Successful auth!")); 13465757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson server.play(); 13475757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 13485757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson SimpleAuthenticator authenticator = new SimpleAuthenticator(); 13495757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson authenticator.expectedPrompt = "b"; 13505757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson Authenticator.setDefault(authenticator); 13515757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 13525757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 13535757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 13545757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContainsNoneMatching(server.takeRequest().getHeaders(), "Authorization: .*"); 13555757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertContains(server.takeRequest().getHeaders(), 13565757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson "Authorization: Scheme2 " + SimpleAuthenticator.BASE_64_CREDENTIALS); 13575757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson assertEquals("Scheme2", authenticator.requestingScheme); 13585757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson } 13595757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 1360c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithChunkedEncoding() throws Exception { 1361c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.CHUNKED, true); 1362c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1363c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1364c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithContentLengthHeader() throws Exception { 1365c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.FIXED_LENGTH, true); 1366c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1367c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1368c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedWithNoLengthHeaders() throws Exception { 1369c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson testRedirected(TransferKind.END_OF_STREAM, false); 1370c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1371c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1372c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private void testRedirected(TransferKind transferKind, boolean reuse) throws Exception { 1373c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockResponse response = new MockResponse() 1374c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1375c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: /foo"); 1376c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson transferKind.setBody(response, "This page has moved!", 10); 1377c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(response); 1378c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1379c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1380c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1381c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1382c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the new location!", 1383c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1384c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1385c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 1386c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET / HTTP/1.1", first.getRequestLine()); 1387c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest retry = server.takeRequest(); 1388c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET /foo HTTP/1.1", retry.getRequestLine()); 1389c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson if (reuse) { 1390c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, retry.getSequenceNumber()); 1391c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1392c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1393c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1394c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectedOnHttps() throws IOException, InterruptedException { 1395c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1396059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1397c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1398c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1399c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: /foo") 1400c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1401c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1402c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1403c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1404c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 1405059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 1406c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the new location!", 1407c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1408c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1409c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 1410c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET / HTTP/1.1", first.getRequestLine()); 1411c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest retry = server.takeRequest(); 1412c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GET /foo HTTP/1.1", retry.getRequestLine()); 1413c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, retry.getSequenceNumber()); 1414c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1415c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1416c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testNotRedirectedFromHttpsToHttp() throws IOException, InterruptedException { 1417c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1418059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1419c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1420c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1421c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: http://anyhost/foo") 1422c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1423c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1424c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1425c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 1426059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 1427c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This page has moved!", 1428c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1429c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1430c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1431c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testNotRedirectedFromHttpToHttps() throws IOException, InterruptedException { 1432c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1433c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1434c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: https://anyhost/foo") 1435c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1436c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1437c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1438c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1439c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This page has moved!", 1440c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1441c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1442c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1443c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testRedirectToAnotherOriginServer() throws Exception { 1444c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson MockWebServer server2 = new MockWebServer(); 1445c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.enqueue(new MockResponse().setBody("This is the 2nd server!")); 1446c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.play(); 1447c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1448c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse() 1449c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1450c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .addHeader("Location: " + server2.getUrl("/").toString()) 1451c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson .setBody("This page has moved!")); 1452c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("This is the first server again!")); 1453c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1454c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1455c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1456c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the 2nd server!", 1457c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1458c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals(server2.getUrl("/"), connection.getURL()); 1459c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1460c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson // make sure the first server was careful to recycle the connection 1461c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("This is the first server again!", 1462c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE)); 1463c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1464c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest first = server.takeRequest(); 14650c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort()); 1466c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest second = server2.takeRequest(); 14670c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort()); 1468c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordedRequest third = server.takeRequest(); 1469c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("Expected connection reuse", 1, third.getSequenceNumber()); 1470c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1471c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server2.shutdown(); 1472c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1473c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1474d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse300MultipleChoiceWithPost() throws Exception { 1475d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson // Chrome doesn't follow the redirect, but Firefox and the RI both do 1476d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MULT_CHOICE); 1477d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1478d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1479d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse301MovedPermanentlyWithPost() throws Exception { 1480d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_PERM); 1481d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1482d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1483d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse302MovedTemporarilyWithPost() throws Exception { 1484d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_MOVED_TEMP); 1485d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1486d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1487d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse303SeeOtherWithPost() throws Exception { 1488d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson testResponseRedirectedWithPost(HttpURLConnection.HTTP_SEE_OTHER); 1489d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1490d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1491d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson private void testResponseRedirectedWithPost(int redirectCode) throws Exception { 1492d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse() 1493d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setResponseCode(redirectCode) 1494d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .addHeader("Location: /page2") 1495d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setBody("This page has moved!")); 1496d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("Page 2")); 1497d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 1498d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1499d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/page1").openConnection(); 1500d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson connection.setDoOutput(true); 1501d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson byte[] requestBody = { 'A', 'B', 'C', 'D' }; 1502d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson OutputStream outputStream = connection.getOutputStream(); 1503d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson outputStream.write(requestBody); 1504d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson outputStream.close(); 1505d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1506d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertTrue(connection.getDoOutput()); 1507d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1508d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page1 = server.takeRequest(); 1509d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine()); 1510d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(Arrays.toString(requestBody), Arrays.toString(page1.getBody())); 1511d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1512d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page2 = server.takeRequest(); 1513d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine()); 1514d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1515d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1516d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson public void testResponse305UseProxy() throws Exception { 1517d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.play(); 1518d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse() 1519d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_USE_PROXY) 1520d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .addHeader("Location: " + server.getUrl("/")) 1521d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson .setBody("This page has moved!")); 1522d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson server.enqueue(new MockResponse().setBody("Proxy Response")); 1523d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1524d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/foo").openConnection(); 1525d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson // Fails on the RI, which gets "Proxy Response" 1526d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("This page has moved!", 1527d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1528d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1529d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson RecordedRequest page1 = server.takeRequest(); 1530d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals("GET /foo HTTP/1.1", page1.getRequestLine()); 1531d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson assertEquals(1, server.getRequestCount()); 1532d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson } 1533d742d7fc7f0593eb8c6e4ac9dd4c0f6a80374e46Jesse Wilson 1534c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void testHttpsWithCustomTrustManager() throws Exception { 1535c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier(); 1536c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson RecordingTrustManager trustManager = new RecordingTrustManager(); 1537c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson SSLContext sc = SSLContext.getInstance("TLS"); 1538c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom()); 1539c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1540c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); 1541c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 1542c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); 1543c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 1544c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson try { 1545c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 1546059dbc04218144f985b20a228bbe98139d400d0cBrian Carlstrom server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 1547c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 1548c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("DEF")); 1549c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.enqueue(new MockResponse().setBody("GHI")); 1550c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson server.play(); 1551c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 1552c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson URL url = server.getUrl("/"); 1553c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE)); 1554c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE)); 1555c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE)); 1556c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 15570c2fd828abec671333b8b88281825fd27a783723Jesse Wilson assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls); 15584559b1d37edcb5d7f1da086cf2e3290388d74f46Brian Carlstrom assertEquals(Arrays.asList("checkServerTrusted [" 15590c2fd828abec671333b8b88281825fd27a783723Jesse Wilson + "CN=" + hostName + " 1, " 1560b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "CN=Test Intermediate Certificate Authority 1, " 1561b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "CN=Test Root Certificate Authority 1" 1562b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson + "] RSA"), 1563c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson trustManager.calls); 1564c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } finally { 1565c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier); 1566c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory); 1567c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1568c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 1569c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 15702d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson /** 15712d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson * Test that the timeout period is honored. The timeout may be doubled! 15722d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson * HttpURLConnection will wait the full timeout for each of the server's IP 15732d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson * addresses. This is typically one IPv4 address and one IPv6 address. 15742d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson */ 1575eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson public void testConnectTimeouts() throws IOException { 15765fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes StuckServer ss = new StuckServer(); 15775fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes int serverPort = ss.getLocalPort(); 1578f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom URLConnection urlConnection = new URL("http://localhost:" + serverPort).openConnection(); 1579b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson int timeout = 1000; 1580b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson urlConnection.setConnectTimeout(timeout); 1581b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson long start = System.currentTimeMillis(); 1582eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson try { 1583eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson urlConnection.getInputStream(); 1584eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson fail(); 1585eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } catch (SocketTimeoutException expected) { 15862d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson long elapsed = System.currentTimeMillis() - start; 15872d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson int attempts = InetAddress.getAllByName("localhost").length; // one per IP address 15882d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson assertTrue("timeout=" +timeout + ", elapsed=" + elapsed + ", attempts=" + attempts, 15892d9fa917aae6a6da38e9d1eda05841ffdf8855bbJesse Wilson Math.abs((attempts * timeout) - elapsed) < 500); 15905fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes } finally { 15915fc5dde4c719c1dfdac46b67d5d2e4884d07721eElliott Hughes ss.close(); 1592f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 1593eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1594eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1595eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson public void testReadTimeouts() throws IOException { 1596eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson /* 1597eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * This relies on the fact that MockWebServer doesn't close the 1598eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * connection after a response has been sent. This causes the client to 1599eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson * try to read more bytes than are sent, which results in a timeout. 1600eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson */ 1601eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson MockResponse timeout = new MockResponse() 1602eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .setBody("ABC") 1603eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .clearHeaders() 1604eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson .addHeader("Content-Length: 4"); 1605eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson server.enqueue(timeout); 1606b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.enqueue(new MockResponse().setBody("unused")); // to keep the server alive 1607eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson server.play(); 1608eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1609eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson URLConnection urlConnection = server.getUrl("/").openConnection(); 1610eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson urlConnection.setReadTimeout(1000); 1611eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson InputStream in = urlConnection.getInputStream(); 1612eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('A', in.read()); 1613eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('B', in.read()); 1614eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson assertEquals('C', in.read()); 1615eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson try { 1616eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson in.read(); // if Content-Length was accurate, this would return -1 immediately 1617eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson fail(); 1618eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } catch (SocketTimeoutException expected) { 1619eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1620eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson } 1621eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson 1622125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson public void testSetChunkedEncodingAsRequestProperty() throws IOException, InterruptedException { 1623125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson server.enqueue(new MockResponse()); 1624125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson server.play(); 1625125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1626125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson HttpURLConnection urlConnection = (HttpURLConnection) server.getUrl("/").openConnection(); 1627125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.setRequestProperty("Transfer-encoding", "chunked"); 1628125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.setDoOutput(true); 1629125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson urlConnection.getOutputStream().write("ABC".getBytes("UTF-8")); 1630125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson assertEquals(200, urlConnection.getResponseCode()); 1631125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1632125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson RecordedRequest request = server.takeRequest(); 1633125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson assertEquals("ABC", new String(request.getBody(), "UTF-8")); 1634125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson } 1635125f068f0a6cd739beac97821c9421cf8317cc87Jesse Wilson 1636f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseInRequest() throws IOException, InterruptedException { 1637f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); // server doesn't honor the connection: close header! 1638f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); 1639f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1640f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1641f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection(); 1642f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson a.setRequestProperty("Connection", "close"); 1643f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, a.getResponseCode()); 1644f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1645f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection(); 1646f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, b.getResponseCode()); 1647f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1648f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1649f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1650f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1651f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1652f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1653f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseInResponse() throws IOException, InterruptedException { 1654f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse().addHeader("Connection: close")); 1655f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse()); 1656f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1657f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1658f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection a = (HttpURLConnection) server.getUrl("/").openConnection(); 1659f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, a.getResponseCode()); 1660f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1661f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson HttpURLConnection b = (HttpURLConnection) server.getUrl("/").openConnection(); 1662f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(200, b.getResponseCode()); 1663f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1664f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1665f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1666f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1667f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1668f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1669f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson public void testConnectionCloseWithRedirect() throws IOException, InterruptedException { 1670f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson MockResponse response = new MockResponse() 1671f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP) 1672f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .addHeader("Location: /foo") 1673f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson .addHeader("Connection: close"); 1674f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(response); 1675f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.enqueue(new MockResponse().setBody("This is the new location!")); 1676f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson server.play(); 1677f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1678f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1679f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("This is the new location!", 1680f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1681f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 1682f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals(0, server.takeRequest().getSequenceNumber()); 1683f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson assertEquals("When connection: close is used, each request should get its own connection", 1684f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 0, server.takeRequest().getSequenceNumber()); 1685f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson } 1686f418bf447fd007cd2ec2d45b4b0399a11904e9b4Jesse Wilson 168765d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson public void testResponseCodeDisagreesWithHeaders() throws IOException, InterruptedException { 168865d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson server.enqueue(new MockResponse() 168965d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT) 169065d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson .setBody("This body is not allowed!")); 169165d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson server.play(); 169265d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson 169365d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 169465d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson assertEquals("This body is not allowed!", 169565d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 169665d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson } 169765d890eb22aeba9b009ee642ffd4fff48a6f98aeJesse Wilson 1698ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson public void testSingleByteReadIsSigned() throws IOException { 1699ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson server.enqueue(new MockResponse().setBody(new byte[] { -2, -1 })); 1700ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson server.play(); 1701ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson 1702ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 1703ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson InputStream in = connection.getInputStream(); 1704ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(254, in.read()); 1705ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(255, in.read()); 1706ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson assertEquals(-1, in.read()); 1707ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson } 1708ef66494dce45a0b7ec22ec3fb20c60096517a4e3Jesse Wilson 1709f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithChunkedEncoding() throws IOException { 1710f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.CHUNKED); 1711f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1712f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1713f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithFixedLength() throws IOException { 1714f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.FIXED_LENGTH); 1715f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1716f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1717f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson public void testFlushAfterStreamTransmittedWithNoLengthHeaders() throws IOException { 1718f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson testFlushAfterStreamTransmitted(TransferKind.END_OF_STREAM); 1719f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1720f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1721f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson /** 1722f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * We explicitly permit apps to close the upload stream even after it has 1723f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * been transmitted. We also permit flush so that buffered streams can 1724f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson * do a no-op flush when they are closed. http://b/3038470 1725f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson */ 1726f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson private void testFlushAfterStreamTransmitted(TransferKind transferKind) throws IOException { 1727f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson server.enqueue(new MockResponse().setBody("abc")); 1728f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson server.play(); 1729f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1730f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1731f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setDoOutput(true); 1732f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson byte[] upload = "def".getBytes("UTF-8"); 1733f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1734f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson if (transferKind == TransferKind.CHUNKED) { 1735f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setChunkedStreamingMode(0); 1736f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } else if (transferKind == TransferKind.FIXED_LENGTH) { 1737f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson connection.setFixedLengthStreamingMode(upload.length); 1738f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1739f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1740f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson OutputStream out = connection.getOutputStream(); 1741f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.write(upload); 1742f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson assertEquals("abc", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 1743f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1744f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.flush(); // dubious but permitted 1745f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson try { 1746f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson out.write("ghi".getBytes("UTF-8")); 1747f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson fail(); 1748f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } catch (IOException expected) { 1749f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1750f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1751f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1752c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson public void testGetHeadersThrows() throws IOException { 1753e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START)); 1754c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson server.play(); 1755c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson 1756c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1757c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson try { 1758c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson connection.getInputStream(); 1759c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson fail(); 1760c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson } catch (IOException expected) { 1761c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson } 1762c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson 1763c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson try { 1764c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson connection.getInputStream(); 1765c6dae581716b9362a5c7f166c80a7f2b46ed1124Jesse Wilson fail(); 1766f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } catch (IOException expected) { 1767f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1768f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson } 1769f29ad8a60254345d1943d1b3836482395a7c916fJesse Wilson 1770b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson public void testGetKeepAlive() throws Exception { 1771b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson MockWebServer server = new MockWebServer(); 1772b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 1773b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.play(); 1774b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1775b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson // The request should work once and then fail 1776b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson URLConnection connection = server.getUrl("").openConnection(); 1777b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson InputStream input = connection.getInputStream(); 1778b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson assertEquals("ABC", readAscii(input, Integer.MAX_VALUE)); 1779b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson input.close(); 1780b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson try { 1781b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson server.getUrl("").openConnection().getInputStream(); 1782b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson fail(); 1783b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } catch (ConnectException expected) { 1784b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1785b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1786b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1787b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson /** 1788d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * This test goes through the exhaustive set of interesting ASCII characters 1789d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * because most of those characters are interesting in some way according to 1790d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * RFC 2396 and RFC 2732. http://b/1158780 1791b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson */ 1792d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson public void testLenientUrlToUri() throws Exception { 1793d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // alphanum 1794d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09", "abzABZ09"); 1795d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1796d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // control characters 1797d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0001", "%01", "%01", "%01", "%01"); 1798d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u001f", "%1F", "%1F", "%1F", "%1F"); 1799d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1800d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // ascii characters 1801d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("%20", "%20", "%20", "%20", "%20"); 1802d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("%20", "%20", "%20", "%20", "%20"); 1803d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(" ", "%20", "%20", "%20", "%20"); 1804d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("!", "!", "!", "!", "!"); 1805d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\"", "%22", "%22", "%22", "%22"); 1806d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("#", null, null, null, "%23"); 1807d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("$", "$", "$", "$", "$"); 1808d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("&", "&", "&", "&", "&"); 1809d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("'", "'", "'", "'", "'"); 1810d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("(", "(", "(", "(", "("); 1811d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(")", ")", ")", ")", ")"); 1812d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("*", "*", "*", "*", "*"); 1813d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("+", "+", "+", "+", "+"); 1814d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(",", ",", ",", ",", ","); 1815d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("-", "-", "-", "-", "-"); 1816d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(".", ".", ".", ".", "."); 1817d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("/", null, "/", "/", "/"); 1818d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(":", null, ":", ":", ":"); 1819d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(";", ";", ";", ";", ";"); 1820d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("<", "%3C", "%3C", "%3C", "%3C"); 1821d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("=", "=", "=", "=", "="); 1822d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping(">", "%3E", "%3E", "%3E", "%3E"); 1823d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("?", null, null, "?", "?"); 1824d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("@", "@", "@", "@", "@"); 1825d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("[", null, "%5B", null, "%5B"); 1826d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\\", "%5C", "%5C", "%5C", "%5C"); 1827d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("]", null, "%5D", null, "%5D"); 1828d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("^", "%5E", "%5E", "%5E", "%5E"); 1829d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("_", "_", "_", "_", "_"); 1830d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("`", "%60", "%60", "%60", "%60"); 1831d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("{", "%7B", "%7B", "%7B", "%7B"); 1832d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("|", "%7C", "%7C", "%7C", "%7C"); 1833d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("}", "%7D", "%7D", "%7D", "%7D"); 1834d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("~", "~", "~", "~", "~"); 1835d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("~", "~", "~", "~", "~"); 1836d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u007f", "%7F", "%7F", "%7F", "%7F"); 1837d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1838d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson // beyond ascii 1839d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0080", "%C2%80", "%C2%80", "%C2%80", "%C2%80"); 1840d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u20ac", "\u20ac", "\u20ac", "\u20ac", "\u20ac"); 184132559028b14b9b321b10eede050afd554a376569Jesse Wilson testUrlToUriMapping("\ud842\udf9f", 184232559028b14b9b321b10eede050afd554a376569Jesse Wilson "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f", "\ud842\udf9f"); 1843d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1844d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1845d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson public void testLenientUrlToUriNul() throws Exception { 1846d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson testUrlToUriMapping("\u0000", "%00", "%00", "%00", "%00"); // RI fails this 1847d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1848d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1849d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson private void testUrlToUriMapping(String string, String asAuthority, String asFile, 1850d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson String asQuery, String asFragment) throws Exception { 1851d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asAuthority != null) { 1852d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host" + asAuthority + ".tld/", 1853d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host" + string + ".tld/")).toString()); 1854d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1855d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asFile != null) { 1856d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file" + asFile + "/", 1857d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file" + string + "/")).toString()); 1858d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1859d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson if (asQuery != null) { 1860d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file?q" + asQuery + "=x", 1861d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file?q" + string + "=x")).toString()); 1862d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1863d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson assertEquals("http://host.tld/file#" + asFragment + "-x", 1864d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson backdoorUrlToUri(new URL("http://host.tld/file#" + asFragment + "-x")).toString()); 1865d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1866b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1867d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson /** 1868d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * Exercises HttpURLConnection to convert URL to a URI. Unlike URL#toURI, 1869d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * HttpURLConnection recovers from URLs with unescaped but unsupported URI 1870d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson * characters like '{' and '|' by escaping these characters. 1871d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson */ 1872d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson private URI backdoorUrlToUri(URL url) throws Exception { 1873d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson final AtomicReference<URI> uriReference = new AtomicReference<URI>(); 1874d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1875d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson ResponseCache.setDefault(new ResponseCache() { 1876d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { 1877d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson return null; 1878d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1879d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson @Override public CacheResponse get(URI uri, String requestMethod, 1880d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson Map<String, List<String>> requestHeaders) throws IOException { 1881d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson uriReference.set(uri); 1882d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson throw new UnsupportedOperationException(); 1883d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1884d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson }); 1885d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1886d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson try { 1887d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 1888d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson connection.getResponseCode(); 1889d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } catch (Exception expected) { 1890d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson } 1891d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 1892d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson return uriReference.get(); 1893b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson } 1894b7f4d6c3968c372767b2510f38a3d506067aced6Jesse Wilson 1895afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson /** 1896afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson * Don't explode if the cache returns a null body. http://b/3373699 1897afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson */ 1898afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson public void testResponseCacheReturnsNullOutputStream() throws Exception { 1899afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson final AtomicBoolean aborted = new AtomicBoolean(); 1900afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson ResponseCache.setDefault(new ResponseCache() { 1901afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public CacheResponse get(URI uri, String requestMethod, 1902afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson Map<String, List<String>> requestHeaders) throws IOException { 1903afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return null; 1904afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1905afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public CacheRequest put(URI uri, URLConnection connection) throws IOException { 1906afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return new CacheRequest() { 1907afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public void abort() { 1908afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson aborted.set(true); 1909afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1910afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson @Override public OutputStream getBody() throws IOException { 1911afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson return null; 1912afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1913afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson }; 1914afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1915afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson }); 1916afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson 1917afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson server.enqueue(new MockResponse().setBody("abcdef")); 1918afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson server.play(); 1919afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson 1920afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1921afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson InputStream in = connection.getInputStream(); 1922afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson assertEquals("abc", readAscii(in, 3)); 1923afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson in.close(); 1924afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson assertFalse(aborted.get()); // The best behavior is ambiguous, but RI 6 doesn't abort here 1925afd9b157f467b7c4f2f0b5592dca72f18d844602Jesse Wilson } 1926d1b5e5da828434388e486a388710d21e4306dae0Jesse Wilson 19275e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson 19285e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson /** 19295e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson * http://code.google.com/p/android/issues/detail?id=14562 19305e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson */ 19315e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson public void testReadAfterLastByte() throws Exception { 19325e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson server.enqueue(new MockResponse() 19335e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson .setBody("ABC") 19345e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson .clearHeaders() 19355e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson .addHeader("Connection: close") 19365e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson .setSocketPolicy(SocketPolicy.DISCONNECT_AT_END)); 19375e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson server.play(); 19385e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson 19395e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 19405e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson InputStream in = connection.getInputStream(); 19415e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson assertEquals("ABC", readAscii(in, 3)); 19425e8b5a55eda914b67c62b4e77152922d5c77eb68Jesse Wilson assertEquals(-1, in.read()); 1943bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson assertEquals(-1, in.read()); // throws IOException in Gingerbread 1944bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson } 1945bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson 1946ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetContent() throws Exception { 1947ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 1948ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1949ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1950ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson InputStream in = (InputStream) connection.getContent(); 1951ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", readAscii(in, Integer.MAX_VALUE)); 1952ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1953ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1954ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetContentOfType() throws Exception { 1955ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 1956ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1957ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1958ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1959ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.getContent(null); 1960ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1961ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (NullPointerException expected) { 1962ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1963ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1964ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.getContent(new Class[] { null }); 1965ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1966ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (NullPointerException expected) { 1967ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1968ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertNull(connection.getContent(new Class[] { getClass() })); 1969ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.disconnect(); 1970ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1971ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1972ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetOutputStreamOnGetFails() throws Exception { 1973ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse()); 1974ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1975ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1976ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1977ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.getOutputStream(); 1978ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1979ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (ProtocolException expected) { 1980ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1981ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1982ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1983ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetOutputAfterGetInputStreamFails() throws Exception { 1984ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse()); 1985ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1986ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 1987ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setDoOutput(true); 1988ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 1989ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.getInputStream(); 1990ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.getOutputStream(); 1991ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 1992ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (ProtocolException expected) { 1993ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1994ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 1995ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 1996ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testSetDoOutputOrDoInputAfterConnectFails() throws Exception { 1997ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse()); 1998ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 1999ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 2000ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.connect(); 2001ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 2002ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setDoOutput(true); 2003ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 2004ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 2005ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2006ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson try { 2007ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setDoInput(true); 2008ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson fail(); 2009ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } catch (IllegalStateException expected) { 2010ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2011ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.disconnect(); 2012ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2013ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 2014ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testClientSendsContentLength() throws Exception { 2015ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("A")); 2016ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 2017ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 2018ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.setDoOutput(true); 2019ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson OutputStream out = connection.getOutputStream(); 2020ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson out.write(new byte[] { 'A', 'B', 'C' }); 2021ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson out.close(); 2022ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("A", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 2023ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson RecordedRequest request = server.takeRequest(); 2024ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertContains(request.getHeaders(), "Content-Length: 3"); 2025ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2026ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 2027ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetContentLengthConnects() throws Exception { 2028ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse().setBody("ABC")); 2029ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 2030ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 2031ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals(3, connection.getContentLength()); 2032ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.disconnect(); 2033ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2034ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 2035ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetContentTypeConnects() throws Exception { 2036ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse() 2037ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .addHeader("Content-Type: text/plain") 2038ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .setBody("ABC")); 2039ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 2040ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 2041ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("text/plain", connection.getContentType()); 2042ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.disconnect(); 2043ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2044ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 2045ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson public void testGetContentEncodingConnects() throws Exception { 2046ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.enqueue(new MockResponse() 2047ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .addHeader("Content-Encoding: identity") 2048ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson .setBody("ABC")); 2049ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson server.play(); 2050ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson HttpURLConnection connection = (HttpURLConnection) server.getUrl("/").openConnection(); 2051ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson assertEquals("identity", connection.getContentEncoding()); 2052ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson connection.disconnect(); 2053ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson } 2054ec6163ceac64902ccde3259e8a426f0f9ad6a88cJesse Wilson 20555292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson // http://b/4361656 20565292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson public void testUrlContainsQueryButNoPath() throws Exception { 20575292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson server.enqueue(new MockResponse().setBody("A")); 20585292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson server.play(); 20595292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson URL url = new URL("http", server.getHostName(), server.getPort(), "?query"); 20605292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson assertEquals("A", readAscii(url.openConnection().getInputStream(), Integer.MAX_VALUE)); 20615292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson RecordedRequest request = server.takeRequest(); 20625292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson assertEquals("GET /?query HTTP/1.1", request.getRequestLine()); 20635292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson } 20645292410e4ebf7fb5149eefd2f52fcb94c46690a6Jesse Wilson 206525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson // http://code.google.com/p/android/issues/detail?id=20442 206625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson public void testInputStreamAvailableWithChunkedEncoding() throws Exception { 206725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson testInputStreamAvailable(TransferKind.CHUNKED); 206825a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson } 206925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson 207025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson public void testInputStreamAvailableWithContentLengthHeader() throws Exception { 207125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson testInputStreamAvailable(TransferKind.FIXED_LENGTH); 207225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson } 207325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson 207425a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson public void testInputStreamAvailableWithNoLengthHeaders() throws Exception { 207525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson testInputStreamAvailable(TransferKind.END_OF_STREAM); 207625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson } 207725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson 207825a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson private void testInputStreamAvailable(TransferKind transferKind) throws IOException { 207925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson String body = "ABCDEFGH"; 208025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson MockResponse response = new MockResponse(); 208125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson transferKind.setBody(response, body, 4); 208225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson server.enqueue(response); 208325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson server.play(); 208425a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson URLConnection connection = server.getUrl("/").openConnection(); 208525a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson InputStream in = connection.getInputStream(); 208625a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson for (int i = 0; i < body.length(); i++) { 208725a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson assertTrue(in.available() >= 0); 208825a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson assertEquals(body.charAt(i), in.read()); 208925a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson } 209025a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson assertEquals(0, in.available()); 209125a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson assertEquals(-1, in.read()); 209225a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson } 209325a753691a80186cd4d7086b12c0e52225d95897Jesse Wilson 2094deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes // http://code.google.com/p/android/issues/detail?id=28095 2095deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes public void testInvalidIpv4Address() throws Exception { 2096deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes try { 2097deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes URI uri = new URI("http://1111.111.111.111/index.html"); 2098deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes uri.toURL().openConnection().connect(); 2099deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes fail(); 2100deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes } catch (UnknownHostException expected) { 2101deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes } 2102deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes } 2103deb26711801f9bfafcd5cad20963b3c3913404fdElliott Hughes 2104d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson // http://code.google.com/p/android/issues/detail?id=16895 2105d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson public void testUrlWithSpaceInHost() throws Exception { 2106d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson URLConnection urlConnection = new URL("http://and roid.com/").openConnection(); 2107d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson try { 2108d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson urlConnection.getInputStream(); 2109d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson fail(); 2110d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } catch (UnknownHostException expected) { 2111d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } 2112d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } 2113d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson 2114d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson public void testUrlWithSpaceInHostViaHttpProxy() throws Exception { 2115d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson server.enqueue(new MockResponse()); 2116d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson server.play(); 2117d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson URLConnection urlConnection = new URL("http://and roid.com/") 2118d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson .openConnection(server.toProxyAddress()); 2119d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson try { 2120d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson urlConnection.getInputStream(); 2121d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson fail(); // the RI makes a bogus proxy request for "GET http://and roid.com/ HTTP/1.1" 2122d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } catch (UnknownHostException expected) { 2123d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } 2124d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson } 2125d0d626655f1d452070d3116678037e8759f807f4Jesse Wilson 2126abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson public void testSslFallback() throws Exception { 2127abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 2128abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 2129abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE)); 2130abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson server.enqueue(new MockResponse().setBody("This required a 2nd handshake")); 2131abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson server.play(); 2132abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson 2133abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 2134abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 2135abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson assertEquals("This required a 2nd handshake", 2136abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson readAscii(connection.getInputStream(), Integer.MAX_VALUE)); 2137abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson 2138abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson RecordedRequest first = server.takeRequest(); 2139abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson assertEquals(0, first.getSequenceNumber()); 2140abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson RecordedRequest retry = server.takeRequest(); 2141abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson assertEquals(0, retry.getSequenceNumber()); 2142abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson assertEquals("SSLv3", retry.getSslProtocol()); 2143abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson } 2144abe4e615f473b387b1c00a738062a7c428f05a33Jesse Wilson 2145a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson public void testInspectSslBeforeConnect() throws Exception { 2146a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 2147a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 2148a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.enqueue(new MockResponse()); 2149a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.play(); 2150a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson 2151a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 2152a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 2153a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNotNull(connection.getHostnameVerifier()); 2154a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson try { 2155a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.getLocalCertificates(); 2156a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson fail(); 2157a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } catch (IllegalStateException expected) { 2158a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2159a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson try { 2160a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.getServerCertificates(); 2161a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson fail(); 2162a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } catch (IllegalStateException expected) { 2163a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2164a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson try { 2165a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.getCipherSuite(); 2166a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson fail(); 2167a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } catch (IllegalStateException expected) { 2168a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2169a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson try { 2170a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.getPeerPrincipal(); 2171a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson fail(); 2172a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } catch (IllegalStateException expected) { 2173a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2174a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2175a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson 2176a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson /** 2177a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson * Test that we can inspect the SSL session after connect(). 2178a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson * http://code.google.com/p/android/issues/detail?id=24431 2179a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson */ 2180a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson public void testInspectSslAfterConnect() throws Exception { 2181a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson TestSSLContext testSSLContext = TestSSLContext.create(); 2182a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); 2183a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.enqueue(new MockResponse()); 2184a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson server.play(); 2185a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson 2186a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection(); 2187a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); 2188a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson connection.connect(); 2189a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNotNull(connection.getHostnameVerifier()); 2190a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNull(connection.getLocalCertificates()); 2191a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNotNull(connection.getServerCertificates()); 2192a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNotNull(connection.getCipherSuite()); 2193a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson assertNotNull(connection.getPeerPrincipal()); 2194a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson } 2195a468471e8ccc2f642d5b2ae3c75873a74267ece2Jesse Wilson 2196bc4c79c6a2059003f695f7ad204de36700e8d701Jesse Wilson /** 21970c59055dd24e1659f85d9ff7e2148883f663bd62Jesse Wilson * Returns a gzipped copy of {@code bytes}. 2198deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson */ 2199deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson public byte[] gzip(byte[] bytes) throws IOException { 2200deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 2201deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson OutputStream gzippedOut = new GZIPOutputStream(bytesOut); 2202deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson gzippedOut.write(bytes); 2203deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson gzippedOut.close(); 2204deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson return bytesOut.toByteArray(); 2205deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 2206deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 2207c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson /** 2208c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson * Reads at most {@code limit} characters from {@code in} and asserts that 2209c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson * content equals {@code expected}. 2210c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson */ 2211c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson private void assertContent(String expected, URLConnection connection, int limit) 2212c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson throws IOException { 2213f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom connection.connect(); 221451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson assertEquals(expected, readAscii(connection.getInputStream(), limit)); 2215c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson ((HttpURLConnection) connection).disconnect(); 2216c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 2217c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 2218c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson private void assertContent(String expected, URLConnection connection) throws IOException { 2219c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson assertContent(expected, connection, Integer.MAX_VALUE); 2220c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson } 2221c8977f474b30c5f3807398859a6b16687af6fc7bJesse Wilson 222260476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson private void assertContains(List<String> headers, String header) { 222360476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson assertTrue(headers.toString(), headers.contains(header)); 222460476787f0e0f052366d8031c74e507ffd3d16a3Jesse Wilson } 222551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 2226ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson private void assertContainsNoneMatching(List<String> headers, String pattern) { 2227ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson for (String header : headers) { 2228ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson if (header.matches(pattern)) { 2229ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson fail("Header " + header + " matches " + pattern); 2230ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 2231ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 2232ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson } 2233ffd579b668428272b78f5c6c64f9c89766f37c1aJesse Wilson 2234eafede536f2059bb6c869e7a5f07fd7ad9758e28Jesse Wilson private Set<String> newSet(String... elements) { 223583a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson return new HashSet<String>(Arrays.asList(elements)); 223683a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson } 223783a47d4d0c536e06fc53eda9d5a1a5d93f9accc6Jesse Wilson 223851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson enum TransferKind { 223951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson CHUNKED() { 2240deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) 224151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson throws IOException { 224251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setChunkedBody(content, chunkSize); 224351e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 224451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }, 224551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson FIXED_LENGTH() { 2246deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) { 224751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setBody(content); 224851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 224951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }, 225051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson END_OF_STREAM() { 2251deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson @Override void setBody(MockResponse response, byte[] content, int chunkSize) { 225251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson response.setBody(content); 2253e942f46f10bb9384a1b186b3d7b74f9704c57090Jesse Wilson response.setSocketPolicy(DISCONNECT_AT_END); 225451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson for (Iterator<String> h = response.getHeaders().iterator(); h.hasNext(); ) { 225551e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson if (h.next().startsWith("Content-Length:")) { 225651e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson h.remove(); 225751e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson break; 225851e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 225951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 226051e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 226151e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson }; 226251e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson 2263deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson abstract void setBody(MockResponse response, byte[] content, int chunkSize) 226451e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson throws IOException; 2265deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson 2266deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson void setBody(MockResponse response, String content, int chunkSize) throws IOException { 2267deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson setBody(response, content.getBytes("UTF-8"), chunkSize); 2268deb236fb06f2a14861e7d40dea959f181cd5cf28Jesse Wilson } 226951e468abf2628ce964d3657042f3ac8f2c947504Jesse Wilson } 2270c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2271984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson enum ProxyConfig { 2272f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom NO_PROXY() { 2273f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom @Override public HttpURLConnection connect(MockWebServer server, URL url) 2274f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom throws IOException { 2275f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom return (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); 2276f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom } 2277f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom }, 2278f02c695ed03e708623d9365dec26d533356ef2d0Brian Carlstrom 2279984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson CREATE_ARG() { 2280984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 2281984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 2282984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(server.toProxyAddress()); 2283984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 2284984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 2285984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 2286984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson PROXY_SYSTEM_PROPERTY() { 2287984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 2288984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 2289984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("proxyHost", "localhost"); 2290984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("proxyPort", Integer.toString(server.getPort())); 2291984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 2292984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 2293984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 2294984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 2295984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HTTP_PROXY_SYSTEM_PROPERTY() { 2296984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 2297984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 2298984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("http.proxyHost", "localhost"); 2299984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("http.proxyPort", Integer.toString(server.getPort())); 2300984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 2301984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 2302984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }, 2303984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 2304984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson HTTPS_PROXY_SYSTEM_PROPERTY() { 2305984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson @Override public HttpURLConnection connect(MockWebServer server, URL url) 2306984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson throws IOException { 2307984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("https.proxyHost", "localhost"); 2308984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson System.setProperty("https.proxyPort", Integer.toString(server.getPort())); 2309984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson return (HttpURLConnection) url.openConnection(); 2310984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 2311984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson }; 2312984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 2313984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson public abstract HttpURLConnection connect(MockWebServer server, URL url) throws IOException; 2314984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson } 2315984fcff696380abd6ea14e80030f9fd2d09dbad0Jesse Wilson 2316c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private static class RecordingTrustManager implements X509TrustManager { 2317c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private final List<String> calls = new ArrayList<String>(); 2318c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2319c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public X509Certificate[] getAcceptedIssuers() { 2320c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("getAcceptedIssuers"); 2321c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return new X509Certificate[] {}; 2322c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2323c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2324c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void checkClientTrusted(X509Certificate[] chain, String authType) 2325c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson throws CertificateException { 2326c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("checkClientTrusted " + certificatesToString(chain) + " " + authType); 2327c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2328c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2329c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public void checkServerTrusted(X509Certificate[] chain, String authType) 2330c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson throws CertificateException { 2331c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("checkServerTrusted " + certificatesToString(chain) + " " + authType); 2332c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2333c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2334c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private String certificatesToString(X509Certificate[] certificates) { 2335c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson List<String> result = new ArrayList<String>(); 2336c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson for (X509Certificate certificate : certificates) { 2337c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson result.add(certificate.getSubjectDN() + " " + certificate.getSerialNumber()); 2338c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2339c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return result.toString(); 2340c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2341c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2342c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2343c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private static class RecordingHostnameVerifier implements HostnameVerifier { 2344c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson private final List<String> calls = new ArrayList<String>(); 2345c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson 2346c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson public boolean verify(String hostname, SSLSession session) { 2347c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson calls.add("verify " + hostname); 2348c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson return true; 2349c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 2350c0372d90016d241ac979faa6fa1731f30b6f2a03Jesse Wilson } 23515757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 23525757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private static class SimpleAuthenticator extends Authenticator { 23535757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson /** base64("username:password") */ 23545757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private static final String BASE_64_CREDENTIALS = "dXNlcm5hbWU6cGFzc3dvcmQ="; 23555757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 23565757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private String expectedPrompt; 23575757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private RequestorType requestorType; 23585757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private int requestingPort; 23595757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private InetAddress requestingSite; 23605757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private String requestingPrompt; 23615757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private String requestingProtocol; 23625757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson private String requestingScheme; 23635757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson 23645757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson protected PasswordAuthentication getPasswordAuthentication() { 23655757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestorType = getRequestorType(); 23665757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestingPort = getRequestingPort(); 23675757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestingSite = getRequestingSite(); 23685757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestingPrompt = getRequestingPrompt(); 23695757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestingProtocol = getRequestingProtocol(); 23705757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson requestingScheme = getRequestingScheme(); 23715757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson return (expectedPrompt == null || expectedPrompt.equals(requestingPrompt)) 23725757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson ? new PasswordAuthentication("username", "password".toCharArray()) 23735757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson : null; 23745757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson } 23755757cb35ad2e58d7accb7f5d3db6f2d5e72f097dJesse Wilson } 2376e40c9e3935a5024c0f3ebfb3f1441fcd5c48ed86Elliott Hughes} 2377