summaryrefslogtreecommitdiffstats
path: root/core/tests
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2010-12-16 19:37:36 -0800
committerJesse Wilson <jessewilson@google.com>2010-12-16 19:38:32 -0800
commit0cb0d7a2d43874895e764fabed6a6692818dc89d (patch)
treef6d21e3f168679807284b4756c90e87ab459525b /core/tests
parentbf1df887d4b87f7da69cd4fe9306eb0d19166d52 (diff)
downloadframeworks_base-0cb0d7a2d43874895e764fabed6a6692818dc89d.zip
frameworks_base-0cb0d7a2d43874895e764fabed6a6692818dc89d.tar.gz
frameworks_base-0cb0d7a2d43874895e764fabed6a6692818dc89d.tar.bz2
Test that DefaultHttpClient responds gracefully when the server closes a socket.
http://b/2612240 Change-Id: Iafde060572f7865b72fc568b3b55578ed5dd9c94
Diffstat (limited to 'core/tests')
-rw-r--r--core/tests/coretests/src/android/net/http/DefaultHttpClientTest.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/core/tests/coretests/src/android/net/http/DefaultHttpClientTest.java b/core/tests/coretests/src/android/net/http/DefaultHttpClientTest.java
new file mode 100644
index 0000000..ad3ec3d
--- /dev/null
+++ b/core/tests/coretests/src/android/net/http/DefaultHttpClientTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.http;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringWriter;
+import junit.framework.TestCase;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import tests.http.MockResponse;
+import tests.http.MockWebServer;
+import tests.http.SocketPolicy;
+import static tests.http.SocketPolicy.DISCONNECT_AT_END;
+import static tests.http.SocketPolicy.SHUTDOWN_INPUT_AT_END;
+import static tests.http.SocketPolicy.SHUTDOWN_OUTPUT_AT_END;
+
+public final class DefaultHttpClientTest extends TestCase {
+
+ private MockWebServer server = new MockWebServer();
+
+ @Override protected void tearDown() throws Exception {
+ server.shutdown();
+ super.tearDown();
+ }
+
+ public void testServerClosesSocket() throws Exception {
+ testServerClosesOutput(DISCONNECT_AT_END);
+ }
+
+ public void testServerShutdownInput() throws Exception {
+ testServerClosesOutput(SHUTDOWN_INPUT_AT_END);
+ }
+
+ /**
+ * DefaultHttpClient fails if the server shutdown the output after the
+ * response was sent. http://b/2612240
+ */
+ public void testServerShutdownOutput() throws Exception {
+ testServerClosesOutput(SHUTDOWN_OUTPUT_AT_END);
+ }
+
+ private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
+ server.enqueue(new MockResponse()
+ .setBody("This connection won't pool properly")
+ .setSocketPolicy(socketPolicy));
+ server.enqueue(new MockResponse()
+ .setBody("This comes after a busted connection"));
+ server.play();
+
+ DefaultHttpClient client = new DefaultHttpClient();
+
+ HttpResponse a = client.execute(new HttpGet(server.getUrl("/a").toURI()));
+ assertEquals("This connection won't pool properly", contentToString(a));
+ assertEquals(0, server.takeRequest().getSequenceNumber());
+
+ HttpResponse b = client.execute(new HttpGet(server.getUrl("/b").toURI()));
+ assertEquals("This comes after a busted connection", contentToString(b));
+ // sequence number 0 means the HTTP socket connection was not reused
+ assertEquals(0, server.takeRequest().getSequenceNumber());
+ }
+
+ private String contentToString(HttpResponse response) throws IOException {
+ StringWriter writer = new StringWriter();
+ char[] buffer = new char[1024];
+ Reader reader = new InputStreamReader(response.getEntity().getContent());
+ int length;
+ while ((length = reader.read(buffer)) != -1) {
+ writer.write(buffer, 0, length);
+ }
+ reader.close();
+ return writer.toString();
+ }
+}