OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
6 | 6 |
7 import android.os.Build; | 7 import android.os.Build; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.annotations.SuppressFBWarnings; | 10 import org.chromium.base.annotations.SuppressFBWarnings; |
11 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
12 import org.chromium.net.CronetTestActivity; | 12 import org.chromium.net.CronetTestActivity; |
13 import org.chromium.net.CronetTestBase; | 13 import org.chromium.net.CronetTestBase; |
14 import org.chromium.net.MockUrlRequestJobFactory; | 14 import org.chromium.net.MockUrlRequestJobFactory; |
15 import org.chromium.net.NativeTestServer; | 15 import org.chromium.net.NativeTestServer; |
16 import org.chromium.net.UrlRequestContextConfig; | 16 import org.chromium.net.UrlRequestContextConfig; |
17 import org.chromium.net.UrlRequestException; | 17 import org.chromium.net.UrlRequestException; |
18 | 18 |
19 import java.io.ByteArrayOutputStream; | 19 import java.io.ByteArrayOutputStream; |
20 import java.io.FileNotFoundException; | 20 import java.io.FileNotFoundException; |
21 import java.io.IOException; | 21 import java.io.IOException; |
22 import java.io.InputStream; | 22 import java.io.InputStream; |
| 23 import java.io.OutputStream; |
| 24 import java.lang.reflect.Method; |
23 import java.net.HttpURLConnection; | 25 import java.net.HttpURLConnection; |
24 import java.net.MalformedURLException; | 26 import java.net.MalformedURLException; |
25 import java.net.URL; | 27 import java.net.URL; |
26 import java.util.ArrayList; | 28 import java.util.ArrayList; |
27 import java.util.Arrays; | 29 import java.util.Arrays; |
28 import java.util.List; | 30 import java.util.List; |
29 import java.util.Map; | 31 import java.util.Map; |
30 import java.util.regex.Matcher; | 32 import java.util.regex.Matcher; |
31 import java.util.regex.Pattern; | 33 import java.util.regex.Pattern; |
32 | 34 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 public void testBasicGet() throws Exception { | 70 public void testBasicGet() throws Exception { |
69 URL url = new URL(NativeTestServer.getEchoMethodURL()); | 71 URL url = new URL(NativeTestServer.getEchoMethodURL()); |
70 HttpURLConnection urlConnection = | 72 HttpURLConnection urlConnection = |
71 (HttpURLConnection) url.openConnection(); | 73 (HttpURLConnection) url.openConnection(); |
72 assertEquals(200, urlConnection.getResponseCode()); | 74 assertEquals(200, urlConnection.getResponseCode()); |
73 assertEquals("OK", urlConnection.getResponseMessage()); | 75 assertEquals("OK", urlConnection.getResponseMessage()); |
74 assertEquals("GET", getResponseAsString(urlConnection)); | 76 assertEquals("GET", getResponseAsString(urlConnection)); |
75 urlConnection.disconnect(); | 77 urlConnection.disconnect(); |
76 } | 78 } |
77 | 79 |
| 80 /** |
| 81 * Tests that using reflection to find {@code fixedContentLengthLong} works. |
| 82 */ |
| 83 @SmallTest |
| 84 @Feature({"Cronet"}) |
| 85 @OnlyRunCronetHttpURLConnection |
| 86 public void testSetFixedLengthStreamingModeLong() throws Exception { |
| 87 URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| 88 HttpURLConnection connection = |
| 89 (HttpURLConnection) url.openConnection(); |
| 90 connection.setDoOutput(true); |
| 91 connection.setRequestMethod("POST"); |
| 92 if (Build.VERSION.SDK_INT >= 19) { |
| 93 String dataString = "some very important data"; |
| 94 byte[] data = dataString.getBytes(); |
| 95 Class<?> c = connection.getClass(); |
| 96 Method method = c.getMethod("setFixedLengthStreamingMode", |
| 97 new Class[] {long.class}); |
| 98 method.invoke(connection, (long) data.length); |
| 99 OutputStream out = connection.getOutputStream(); |
| 100 out.write(data); |
| 101 assertEquals(200, connection.getResponseCode()); |
| 102 assertEquals("OK", connection.getResponseMessage()); |
| 103 assertEquals(dataString, getResponseAsString(connection)); |
| 104 connection.disconnect(); |
| 105 } |
| 106 } |
| 107 |
| 108 @SmallTest |
| 109 @Feature({"Cronet"}) |
| 110 @OnlyRunCronetHttpURLConnection |
| 111 // TODO(xunjieli): Change the test after chunked support is added. |
| 112 public void testPostChunked() throws Exception { |
| 113 URL url = new URL(NativeTestServer.getEchoBodyURL()); |
| 114 HttpURLConnection connection = |
| 115 (HttpURLConnection) url.openConnection(); |
| 116 connection.setDoOutput(true); |
| 117 connection.setRequestMethod("POST"); |
| 118 try { |
| 119 connection.setChunkedStreamingMode(0); |
| 120 } catch (UnsupportedOperationException e) { |
| 121 assertEquals("Chunked mode not supported yet", e.getMessage()); |
| 122 } |
| 123 } |
| 124 |
78 @SmallTest | 125 @SmallTest |
79 @Feature({"Cronet"}) | 126 @Feature({"Cronet"}) |
80 @CompareDefaultWithCronet | 127 @CompareDefaultWithCronet |
81 public void testNotFoundURLRequest() throws Exception { | 128 public void testNotFoundURLRequest() throws Exception { |
82 URL url = new URL(NativeTestServer.getFileURL("/notfound.html")); | 129 URL url = new URL(NativeTestServer.getFileURL("/notfound.html")); |
83 HttpURLConnection urlConnection = | 130 HttpURLConnection urlConnection = |
84 (HttpURLConnection) url.openConnection(); | 131 (HttpURLConnection) url.openConnection(); |
85 assertEquals(404, urlConnection.getResponseCode()); | 132 assertEquals(404, urlConnection.getResponseCode()); |
86 assertEquals("Not Found", urlConnection.getResponseMessage()); | 133 assertEquals("Not Found", urlConnection.getResponseMessage()); |
87 try { | 134 try { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 InputStream in = urlConnection.getInputStream(); | 266 InputStream in = urlConnection.getInputStream(); |
220 fail(); | 267 fail(); |
221 } catch (Exception e) { | 268 } catch (Exception e) { |
222 // Ignored. | 269 // Ignored. |
223 } | 270 } |
224 } | 271 } |
225 | 272 |
226 @SmallTest | 273 @SmallTest |
227 @Feature({"Cronet"}) | 274 @Feature({"Cronet"}) |
228 @CompareDefaultWithCronet | 275 @CompareDefaultWithCronet |
| 276 public void testMultipleDisconnect() throws Exception { |
| 277 URL url = new URL(NativeTestServer.getEchoMethodURL()); |
| 278 HttpURLConnection urlConnection = |
| 279 (HttpURLConnection) url.openConnection(); |
| 280 assertEquals(200, urlConnection.getResponseCode()); |
| 281 assertEquals("OK", urlConnection.getResponseMessage()); |
| 282 assertEquals("GET", getResponseAsString(urlConnection)); |
| 283 // Disconnect multiple times should be fine. |
| 284 for (int i = 0; i < 10; i++) { |
| 285 urlConnection.disconnect(); |
| 286 } |
| 287 } |
| 288 |
| 289 @SmallTest |
| 290 @Feature({"Cronet"}) |
| 291 @CompareDefaultWithCronet |
229 public void testAddRequestProperty() throws Exception { | 292 public void testAddRequestProperty() throws Exception { |
230 URL url = new URL(NativeTestServer.getEchoAllHeadersURL()); | 293 URL url = new URL(NativeTestServer.getEchoAllHeadersURL()); |
231 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | 294 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
232 connection.addRequestProperty("foo-header", "foo"); | 295 connection.addRequestProperty("foo-header", "foo"); |
233 connection.addRequestProperty("bar-header", "bar"); | 296 connection.addRequestProperty("bar-header", "bar"); |
234 | 297 |
235 // Before connection is made, check request headers are set. | 298 // Before connection is made, check request headers are set. |
236 Map<String, List<String>> requestHeadersMap = | 299 Map<String, List<String>> requestHeadersMap = |
237 connection.getRequestProperties(); | 300 connection.getRequestProperties(); |
238 List<String> fooValues = requestHeadersMap.get("foo-header"); | 301 List<String> fooValues = requestHeadersMap.get("foo-header"); |
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
947 String headerName) { | 1010 String headerName) { |
948 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); | 1011 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); |
949 Matcher matcher = pattern.matcher(allHeaders); | 1012 Matcher matcher = pattern.matcher(allHeaders); |
950 List<String> headerValues = new ArrayList<String>(); | 1013 List<String> headerValues = new ArrayList<String>(); |
951 while (matcher.find()) { | 1014 while (matcher.find()) { |
952 headerValues.add(matcher.group(1)); | 1015 headerValues.add(matcher.group(1)); |
953 } | 1016 } |
954 return headerValues; | 1017 return headerValues; |
955 } | 1018 } |
956 } | 1019 } |
OLD | NEW |