Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net.http; |
| 18 | |
Jesse Wilson | 7ad00e6 | 2011-06-07 08:06:50 -0700 | [diff] [blame] | 19 | import com.google.mockwebserver.MockResponse; |
| 20 | import com.google.mockwebserver.MockWebServer; |
| 21 | import com.google.mockwebserver.SocketPolicy; |
| 22 | import static com.google.mockwebserver.SocketPolicy.DISCONNECT_AT_END; |
| 23 | import static com.google.mockwebserver.SocketPolicy.SHUTDOWN_INPUT_AT_END; |
| 24 | import static com.google.mockwebserver.SocketPolicy.SHUTDOWN_OUTPUT_AT_END; |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.io.InputStreamReader; |
| 27 | import java.io.Reader; |
| 28 | import java.io.StringWriter; |
| 29 | import junit.framework.TestCase; |
| 30 | import org.apache.http.HttpResponse; |
Jesse Wilson | df867f8 | 2011-06-24 13:50:37 -0700 | [diff] [blame] | 31 | import org.apache.http.auth.AuthenticationException; |
| 32 | import org.apache.http.auth.UsernamePasswordCredentials; |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 33 | import org.apache.http.client.methods.HttpGet; |
Jesse Wilson | df867f8 | 2011-06-24 13:50:37 -0700 | [diff] [blame] | 34 | import org.apache.http.impl.auth.DigestScheme; |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 35 | import org.apache.http.impl.client.DefaultHttpClient; |
Jesse Wilson | df867f8 | 2011-06-24 13:50:37 -0700 | [diff] [blame] | 36 | import org.apache.http.message.BasicHeader; |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 37 | |
Jesse Wilson | df867f8 | 2011-06-24 13:50:37 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Tests for various regressions and problems with DefaultHttpClient. This is |
| 40 | * not a comprehensive test! |
| 41 | */ |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 42 | public final class DefaultHttpClientTest extends TestCase { |
| 43 | |
Neil Fuller | 6f04b2e | 2016-06-28 18:29:03 +0100 | [diff] [blame] | 44 | private MockWebServer server; |
| 45 | |
| 46 | @Override |
| 47 | public void setUp() throws Exception { |
| 48 | super.setUp(); |
| 49 | server = new MockWebServer(); |
| 50 | } |
Jesse Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 51 | |
| 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 Wilson | df867f8 | 2011-06-24 13:50:37 -0700 | [diff] [blame] | 104 | |
| 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 Wilson | 0cb0d7a | 2010-12-16 19:37:36 -0800 | [diff] [blame] | 140 | } |