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 861 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 String headerName) { | 996 String headerName) { |
950 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); | 997 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); |
951 Matcher matcher = pattern.matcher(allHeaders); | 998 Matcher matcher = pattern.matcher(allHeaders); |
952 List<String> headerValues = new ArrayList<String>(); | 999 List<String> headerValues = new ArrayList<String>(); |
953 while (matcher.find()) { | 1000 while (matcher.find()) { |
954 headerValues.add(matcher.group(1)); | 1001 headerValues.add(matcher.group(1)); |
955 } | 1002 } |
956 return headerValues; | 1003 return headerValues; |
957 } | 1004 } |
958 } | 1005 } |
OLD | NEW |