blob: 80c7a4c4b09e72cd63863bf18288f1e88e48ca1f [file] [log] [blame]
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.http;
18
Jesse Wilson7ad00e62011-06-07 08:06:50 -070019import com.google.mockwebserver.MockResponse;
20import com.google.mockwebserver.MockWebServer;
21import com.google.mockwebserver.SocketPolicy;
22import static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_END;
23import static com.google.mockwebserver.SocketPolicy.SHUTDOWN_INPUT_AT_END;
24import static com.google.mockwebserver.SocketPolicy.SHUTDOWN_OUTPUT_AT_END;
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080025import java.io.IOException;
26import java.io.InputStreamReader;
27import java.io.Reader;
28import java.io.StringWriter;
29import junit.framework.TestCase;
30import org.apache.http.HttpResponse;
Jesse Wilsondf867f82011-06-24 13:50:37 -070031import org.apache.http.auth.AuthenticationException;
32import org.apache.http.auth.UsernamePasswordCredentials;
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080033import org.apache.http.client.methods.HttpGet;
Jesse Wilsondf867f82011-06-24 13:50:37 -070034import org.apache.http.impl.auth.DigestScheme;
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080035import org.apache.http.impl.client.DefaultHttpClient;
Jesse Wilsondf867f82011-06-24 13:50:37 -070036import org.apache.http.message.BasicHeader;
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080037
Jesse Wilsondf867f82011-06-24 13:50:37 -070038/**
39 * Tests for various regressions and problems with DefaultHttpClient. This is
40 * not a comprehensive test!
41 */
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080042public final class DefaultHttpClientTest extends TestCase {
43
Neil Fuller6f04b2e2016-06-28 18:29:03 +010044 private MockWebServer server;
45
46 @Override
47 public void setUp() throws Exception {
48 super.setUp();
49 server = new MockWebServer();
50 }
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -080051
52 @Override protected void tearDown() throws Exception {
53 server.shutdown();
54 super.tearDown();
55 }
56
57 public void testServerClosesSocket() throws Exception {
58 testServerClosesOutput(DISCONNECT_AT_END);
59 }
60
61 public void testServerShutdownInput() throws Exception {
62 testServerClosesOutput(SHUTDOWN_INPUT_AT_END);
63 }
64
65 /**
66 * DefaultHttpClient fails if the server shutdown the output after the
67 * response was sent. http://b/2612240
68 */
69 public void testServerShutdownOutput() throws Exception {
70 testServerClosesOutput(SHUTDOWN_OUTPUT_AT_END);
71 }
72
73 private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
74 server.enqueue(new MockResponse()
75 .setBody("This connection won't pool properly")
76 .setSocketPolicy(socketPolicy));
77 server.enqueue(new MockResponse()
78 .setBody("This comes after a busted connection"));
79 server.play();
80
81 DefaultHttpClient client = new DefaultHttpClient();
82
83 HttpResponse a = client.execute(new HttpGet(server.getUrl("/a").toURI()));
84 assertEquals("This connection won't pool properly", contentToString(a));
85 assertEquals(0, server.takeRequest().getSequenceNumber());
86
87 HttpResponse b = client.execute(new HttpGet(server.getUrl("/b").toURI()));
88 assertEquals("This comes after a busted connection", contentToString(b));
89 // sequence number 0 means the HTTP socket connection was not reused
90 assertEquals(0, server.takeRequest().getSequenceNumber());
91 }
92
93 private String contentToString(HttpResponse response) throws IOException {
94 StringWriter writer = new StringWriter();
95 char[] buffer = new char[1024];
96 Reader reader = new InputStreamReader(response.getEntity().getContent());
97 int length;
98 while ((length = reader.read(buffer)) != -1) {
99 writer.write(buffer, 0, length);
100 }
101 reader.close();
102 return writer.toString();
103 }
Jesse Wilsondf867f82011-06-24 13:50:37 -0700104
105 // http://code.google.com/p/android/issues/detail?id=16051
106 public void testDigestSchemeAlgorithms() throws Exception {
107 authenticateDigestAlgorithm("MD5");
108 authenticateDigestAlgorithm("MD5-sess");
109 authenticateDigestAlgorithm("md5");
110 authenticateDigestAlgorithm("md5-sess");
111 authenticateDigestAlgorithm("md5-SESS");
112 authenticateDigestAlgorithm("MD5-SESS");
113 try {
114 authenticateDigestAlgorithm("MD5-");
115 } catch (AuthenticationException expected) {
116 }
117 try {
118 authenticateDigestAlgorithm("MD6");
119 } catch (AuthenticationException expected) {
120 }
121 try {
122 authenticateDigestAlgorithm("MD");
123 } catch (AuthenticationException expected) {
124 }
125 try {
126 authenticateDigestAlgorithm("");
127 } catch (AuthenticationException expected) {
128 }
129 }
130
131 private void authenticateDigestAlgorithm(String algorithm) throws Exception {
132 String challenge = "Digest realm=\"protected area\", "
133 + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
134 + "algorithm=" + algorithm;
135 DigestScheme digestScheme = new DigestScheme();
136 digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
137 HttpGet get = new HttpGet();
138 digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
139 }
Jesse Wilson0cb0d7a2010-12-16 19:37:36 -0800140}