OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.net.urlconnection; | |
6 | |
7 import android.test.suitebuilder.annotation.SmallTest; | |
8 | |
9 import org.chromium.base.test.util.Feature; | |
10 import org.chromium.net.CronetTestActivity; | |
11 import org.chromium.net.CronetTestBase; | |
12 import org.chromium.net.NativeTestServer; | |
13 | |
14 import java.io.ByteArrayOutputStream; | |
15 import java.io.InputStream; | |
16 import java.io.OutputStream; | |
17 import java.net.HttpURLConnection; | |
18 import java.net.ProtocolException; | |
19 import java.net.URL; | |
20 | |
21 /** | |
22 * Tests {@code getOutputStream} when {@code setFixedLengthStreamingMode} is | |
23 * enabled. | |
24 * Tests annotated with {@code CompareDefaultWithCronet} will run once with the | |
25 * default HttpURLConnection implementation and then with Cronet's | |
26 * HttpURLConnection implementation. Tests annotated with | |
27 * {@code OnlyRunCronetHttpURLConnection} only run Cronet's implementation. | |
28 * See {@link CronetTestBase#runTest()} for details. | |
29 */ | |
30 public class CronetFixedModeOutputStreamTest extends CronetTestBase { | |
31 private static final String UPLOAD_DATA_STRING = "Nifty upload data!"; | |
32 private static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes(); | |
33 private static final int REPEAT_COUNT = 100000; | |
34 private CronetTestActivity mActivity; | |
35 | |
36 @Override | |
37 protected void setUp() throws Exception { | |
38 super.setUp(); | |
39 mActivity = launchCronetTestApp(); | |
40 assertTrue(NativeTestServer.startNativeTestServer( | |
41 getInstrumentation().getTargetContext())); | |
42 } | |
43 | |
44 @Override | |
45 protected void tearDown() throws Exception { | |
46 NativeTestServer.shutdownNativeTestServer(); | |
47 super.tearDown(); | |
48 } | |
49 | |
50 @SmallTest | |
51 @Feature({"Cronet"}) | |
52 @CompareDefaultWithCronet | |
53 public void testFixedLengthStreamingModeZeroContentLength() | |
mmenke
2015/04/01 17:41:05
Didn't you say that with fix length streaming mode
xunjieli
2015/04/01 19:04:55
Done.
| |
54 throws Exception { | |
55 // Check content length is set. | |
56 URL echoLength = new URL(NativeTestServer.getEchoHeaderURL("Content-Leng th")); | |
57 HttpURLConnection connection1 = | |
58 (HttpURLConnection) echoLength.openConnection(); | |
59 connection1.setDoOutput(true); | |
60 connection1.setRequestMethod("POST"); | |
61 connection1.setFixedLengthStreamingMode(0); | |
62 assertEquals(200, connection1.getResponseCode()); | |
63 assertEquals("OK", connection1.getResponseMessage()); | |
64 assertEquals("0", getResponseAsString(connection1)); | |
65 connection1.disconnect(); | |
66 | |
67 // Check body is empty. | |
68 URL echoBody = new URL(NativeTestServer.getEchoBodyURL()); | |
69 HttpURLConnection connection2 = | |
70 (HttpURLConnection) echoBody.openConnection(); | |
71 connection2.setDoOutput(true); | |
72 connection2.setRequestMethod("POST"); | |
73 connection2.setFixedLengthStreamingMode(0); | |
74 assertEquals(200, connection2.getResponseCode()); | |
75 assertEquals("OK", connection2.getResponseMessage()); | |
76 assertEquals("", getResponseAsString(connection2)); | |
77 connection2.disconnect(); | |
78 } | |
79 | |
80 @SmallTest | |
81 @Feature({"Cronet"}) | |
82 @CompareDefaultWithCronet | |
83 public void testWriteLessThanContentLength() | |
84 throws Exception { | |
85 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
86 HttpURLConnection connection = | |
87 (HttpURLConnection) url.openConnection(); | |
88 connection.setDoOutput(true); | |
89 connection.setRequestMethod("POST"); | |
90 // Set a content length that's 1 byte more. | |
91 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length + 1); | |
92 OutputStream out = connection.getOutputStream(); | |
93 out.write(UPLOAD_DATA); | |
94 try { | |
95 connection.getResponseCode(); | |
96 fail(); | |
97 } catch (ProtocolException e) { | |
98 // Expected. | |
99 } | |
100 connection.disconnect(); | |
101 } | |
102 | |
103 @SmallTest | |
104 @Feature({"Cronet"}) | |
105 @CompareDefaultWithCronet | |
106 public void testWriteMoreThanContentLength() | |
107 throws Exception { | |
108 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
109 HttpURLConnection connection = | |
110 (HttpURLConnection) url.openConnection(); | |
111 connection.setDoOutput(true); | |
112 connection.setRequestMethod("POST"); | |
113 // Set a content length that's 1 byte short. | |
114 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1); | |
115 OutputStream out = connection.getOutputStream(); | |
116 try { | |
117 out.write(UPLOAD_DATA); | |
118 fail(); | |
119 } catch (ProtocolException e) { | |
120 // Expected. | |
121 assertEquals("expected " + (UPLOAD_DATA.length - 1) | |
122 + " bytes but received " | |
123 + UPLOAD_DATA.length, e.getMessage()); | |
124 } | |
125 connection.disconnect(); | |
126 } | |
127 | |
128 @SmallTest | |
129 @Feature({"Cronet"}) | |
130 @CompareDefaultWithCronet | |
131 public void testWriteMoreThanContentLengthWriteOneByte() | |
132 throws Exception { | |
133 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
134 HttpURLConnection connection = | |
135 (HttpURLConnection) url.openConnection(); | |
136 connection.setDoOutput(true); | |
137 connection.setRequestMethod("POST"); | |
138 // Set a content length that's 1 byte short. | |
139 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1); | |
140 OutputStream out = connection.getOutputStream(); | |
141 for (int i = 0; i < UPLOAD_DATA.length - 1; i++) { | |
142 out.write(UPLOAD_DATA[i]); | |
143 } | |
144 try { | |
145 // Try upload an extra byte. | |
146 out.write(UPLOAD_DATA[UPLOAD_DATA.length - 1]); | |
147 fail(); | |
148 } catch (ProtocolException e) { | |
149 // Expected. | |
150 assertEquals("expected 0 bytes but received 1", e.getMessage()); | |
151 } | |
152 connection.disconnect(); | |
153 } | |
154 | |
155 @SmallTest | |
156 @Feature({"Cronet"}) | |
157 @CompareDefaultWithCronet | |
158 public void testFixedLengthStreamingMode() throws Exception { | |
159 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
160 HttpURLConnection connection = | |
161 (HttpURLConnection) url.openConnection(); | |
162 connection.setDoOutput(true); | |
163 connection.setRequestMethod("POST"); | |
164 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); | |
165 OutputStream out = connection.getOutputStream(); | |
166 out.write(UPLOAD_DATA); | |
167 assertEquals(200, connection.getResponseCode()); | |
168 assertEquals("OK", connection.getResponseMessage()); | |
169 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection)); | |
170 connection.disconnect(); | |
171 } | |
172 | |
173 @SmallTest | |
174 @Feature({"Cronet"}) | |
175 @CompareDefaultWithCronet | |
176 public void testFixedLengthStreamingModeWriteOneByte() | |
177 throws Exception { | |
178 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
179 HttpURLConnection connection = | |
180 (HttpURLConnection) url.openConnection(); | |
181 connection.setDoOutput(true); | |
182 connection.setRequestMethod("POST"); | |
183 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); | |
184 OutputStream out = connection.getOutputStream(); | |
185 for (int i = 0; i < UPLOAD_DATA.length; i++) { | |
186 // Write one byte at a time. | |
187 out.write(UPLOAD_DATA[i]); | |
188 } | |
189 assertEquals(200, connection.getResponseCode()); | |
190 assertEquals("OK", connection.getResponseMessage()); | |
191 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection)); | |
192 connection.disconnect(); | |
193 } | |
194 | |
195 @SmallTest | |
196 @Feature({"Cronet"}) | |
197 @CompareDefaultWithCronet | |
198 public void testFixedLengthStreamingModeLargeData() throws Exception { | |
199 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
200 HttpURLConnection connection = | |
201 (HttpURLConnection) url.openConnection(); | |
202 connection.setDoOutput(true); | |
203 connection.setRequestMethod("POST"); | |
204 byte[] largeData = getLargeData(); | |
205 connection.setFixedLengthStreamingMode(largeData.length); | |
206 OutputStream out = connection.getOutputStream(); | |
207 int totalBytesWritten = 0; | |
208 // Number of bytes to write each time. It is doubled each time | |
209 // to make sure that the implementation can handle large writes. | |
210 int bytesToWrite = 683; | |
211 while (totalBytesWritten < largeData.length) { | |
212 if (bytesToWrite > largeData.length - totalBytesWritten) { | |
213 // Do not write out of bound. | |
214 bytesToWrite = largeData.length - totalBytesWritten; | |
215 } | |
216 out.write(largeData, totalBytesWritten, bytesToWrite); | |
217 totalBytesWritten += bytesToWrite; | |
218 bytesToWrite *= 2; | |
219 } | |
220 assertEquals(200, connection.getResponseCode()); | |
221 assertEquals("OK", connection.getResponseMessage()); | |
222 checkLargeData(getResponseAsString(connection)); | |
223 connection.disconnect(); | |
224 } | |
225 | |
226 @SmallTest | |
227 @Feature({"Cronet"}) | |
228 @CompareDefaultWithCronet | |
229 public void testFixedLengthStreamingModeLargeDataWriteOneByte() | |
230 throws Exception { | |
231 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
232 HttpURLConnection connection = | |
233 (HttpURLConnection) url.openConnection(); | |
234 connection.setDoOutput(true); | |
235 connection.setRequestMethod("POST"); | |
236 byte[] largeData = getLargeData(); | |
237 connection.setFixedLengthStreamingMode(largeData.length); | |
238 OutputStream out = connection.getOutputStream(); | |
239 for (int i = 0; i < largeData.length; i++) { | |
240 // Write one byte at a time. | |
241 out.write(largeData[i]); | |
242 } | |
243 assertEquals(200, connection.getResponseCode()); | |
244 assertEquals("OK", connection.getResponseMessage()); | |
245 checkLargeData(getResponseAsString(connection)); | |
246 connection.disconnect(); | |
247 } | |
248 | |
249 @SmallTest | |
250 @Feature({"Cronet"}) | |
251 @OnlyRunCronetHttpURLConnection | |
252 public void testLargeDataMoreThanNativeBufferSize() | |
253 throws Exception { | |
254 // Set an internal buffer of size larger than the buffer size used | |
255 // in network stack internally. | |
256 // Normal stream uses 16384, QUIC uses 14520, and SPDY uses 2852. | |
257 CronetFixedModeOutputStream.setDefaultBufferLengthForTesting(17384); | |
258 testFixedLengthStreamingModeLargeDataWriteOneByte(); | |
259 testFixedLengthStreamingModeLargeData(); | |
260 } | |
261 | |
262 @SmallTest | |
263 @Feature({"Cronet"}) | |
264 @CompareDefaultWithCronet | |
265 public void testOneMassiveWrite() throws Exception { | |
266 URL url = new URL(NativeTestServer.getEchoBodyURL()); | |
267 HttpURLConnection connection = | |
268 (HttpURLConnection) url.openConnection(); | |
269 connection.setDoOutput(true); | |
270 connection.setRequestMethod("POST"); | |
271 byte[] largeData = getLargeData(); | |
272 connection.setFixedLengthStreamingMode(largeData.length); | |
273 OutputStream out = connection.getOutputStream(); | |
274 // Write everything at one go, so the data is larger than the buffer | |
275 // used in CronetFixedModeOutputStream. | |
276 out.write(largeData); | |
277 assertEquals(200, connection.getResponseCode()); | |
278 assertEquals("OK", connection.getResponseMessage()); | |
279 checkLargeData(getResponseAsString(connection)); | |
280 connection.disconnect(); | |
281 } | |
282 | |
283 @SmallTest | |
284 @Feature({"Cronet"}) | |
285 @OnlyRunCronetHttpURLConnection | |
286 public void testRewind() throws Exception { | |
287 // Post preserving redirect should fail. | |
288 URL url = new URL(NativeTestServer.getRedirectToEchoBody()); | |
289 HttpURLConnection connection = | |
290 (HttpURLConnection) url.openConnection(); | |
291 connection.setDoOutput(true); | |
292 connection.setRequestMethod("POST"); | |
293 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length); | |
294 try { | |
295 OutputStream out = connection.getOutputStream(); | |
296 out.write(UPLOAD_DATA); | |
297 } catch (IllegalStateException e) { | |
298 assertEquals("Rewind is not supported by CronetFixedModeOutputStream .", | |
299 e.getMessage()); | |
300 } | |
301 connection.disconnect(); | |
302 } | |
303 | |
304 /** | |
305 * Helper method to extract response body as a string for testing. | |
306 */ | |
307 private String getResponseAsString(HttpURLConnection connection) | |
308 throws Exception { | |
309 InputStream in = connection.getInputStream(); | |
310 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
311 int b; | |
312 while ((b = in.read()) != -1) { | |
313 out.write(b); | |
314 } | |
315 return out.toString(); | |
316 } | |
317 | |
318 /** | |
319 * Produces a byte array that contains {@code REPEAT_COUNT} of | |
320 * {@code UPLOAD_DATA_STRING}. | |
321 */ | |
322 private byte[] getLargeData() { | |
323 byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length]; | |
324 for (int i = 0; i < REPEAT_COUNT; i++) { | |
325 for (int j = 0; j < UPLOAD_DATA.length; j++) { | |
326 largeData[i * UPLOAD_DATA.length + j] = UPLOAD_DATA[j]; | |
327 } | |
328 } | |
329 return largeData; | |
330 } | |
331 | |
332 /** | |
333 * Helper function to check whether {@code data} is a concatenation of | |
334 * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings. | |
335 */ | |
336 private void checkLargeData(String data) { | |
337 for (int i = 0; i < REPEAT_COUNT; i++) { | |
338 assertEquals(UPLOAD_DATA_STRING, data.substring( | |
339 UPLOAD_DATA_STRING.length() * i, | |
340 UPLOAD_DATA_STRING.length() * (i + 1))); | |
341 } | |
342 } | |
343 } | |
OLD | NEW |