Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(686)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java

Issue 966743003: [Cronet] Implement getOutputStream in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
Patch Set: Remove unused Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
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 the CronetBufferedOutputStream implementation.
23 */
24 public class CronetBufferedOutputStreamTest extends CronetTestBase {
25 private static final String UPLOAD_DATA_STRING = "Nifty upload data!";
26 private static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes();
27 private static final int REPEAT_COUNT = 100000;
28
29 @Override
30 protected void setUp() throws Exception {
31 super.setUp();
32 launchCronetTestApp();
33 assertTrue(NativeTestServer.startNativeTestServer(
34 getInstrumentation().getTargetContext()));
35 }
36
37 @Override
38 protected void tearDown() throws Exception {
39 NativeTestServer.shutdownNativeTestServer();
40 super.tearDown();
41 }
42
43 @SmallTest
44 @Feature({"Cronet"})
45 @CompareDefaultWithCronet
46 public void testGetOutputStreamAfterConnectionMade() throws Exception {
47 URL url = new URL(NativeTestServer.getEchoBodyURL());
48 HttpURLConnection connection =
49 (HttpURLConnection) url.openConnection();
50 connection.setDoOutput(true);
51 connection.setRequestMethod("POST");
52 assertEquals(200, connection.getResponseCode());
53 try {
54 connection.getOutputStream();
55 fail();
56 } catch (java.net.ProtocolException e) {
57 // Expected.
58 }
59 }
60
61 /**
62 * Tests write after connect. Strangely, the default implementation allows
63 * writing after being connected, so this test only runs against Cronet's
64 * implementation.
65 */
66 @SmallTest
67 @Feature({"Cronet"})
68 @OnlyRunCronetHttpURLConnection
69 public void testWriteAfterConnect() throws Exception {
70 URL url = new URL(NativeTestServer.getEchoBodyURL());
71 HttpURLConnection connection =
72 (HttpURLConnection) url.openConnection();
73 connection.setDoOutput(true);
74 connection.setRequestMethod("POST");
75 OutputStream out = connection.getOutputStream();
76 out.write(UPLOAD_DATA);
77 connection.connect();
78 try {
79 // Attemp to write some more.
80 out.write(UPLOAD_DATA);
81 fail();
82 } catch (IllegalStateException e) {
83 assertEquals("Cannot write after being connected.", e.getMessage());
84 }
85 }
86
87 @SmallTest
88 @Feature({"Cronet"})
89 @CompareDefaultWithCronet
90 public void testWriteAfterReadingResponse() throws Exception {
91 URL url = new URL(NativeTestServer.getEchoBodyURL());
92 HttpURLConnection connection =
93 (HttpURLConnection) url.openConnection();
94 connection.setDoOutput(true);
95 connection.setRequestMethod("POST");
96 OutputStream out = connection.getOutputStream();
97 assertEquals(200, connection.getResponseCode());
98 try {
99 out.write(UPLOAD_DATA);
100 fail();
101 } catch (Exception e) {
102 // Default implementation gives an IOException and says that the
103 // stream is closed. Cronet gives an IllegalStateException and
104 // complains about write after connected.
105 }
106 }
107
108 @SmallTest
109 @Feature({"Cronet"})
110 @CompareDefaultWithCronet
111 public void testPostWithContentLength() throws Exception {
112 URL url = new URL(NativeTestServer.getEchoBodyURL());
113 HttpURLConnection connection =
114 (HttpURLConnection) url.openConnection();
115 connection.setDoOutput(true);
116 connection.setRequestMethod("POST");
117 byte[] largeData = getLargeData();
118 connection.setRequestProperty("Content-Length",
119 Integer.toString(largeData.length));
120 OutputStream out = connection.getOutputStream();
121 int totalBytesWritten = 0;
122 // Number of bytes to write each time. It is doubled each time
123 // to make sure that the buffer grows.
124 int bytesToWrite = 683;
125 while (totalBytesWritten < largeData.length) {
126 if (bytesToWrite > largeData.length - totalBytesWritten) {
127 // Do not write out of bound.
128 bytesToWrite = largeData.length - totalBytesWritten;
129 }
130 out.write(largeData, totalBytesWritten, bytesToWrite);
131 totalBytesWritten += bytesToWrite;
132 bytesToWrite *= 2;
133 }
134 assertEquals(200, connection.getResponseCode());
135 assertEquals("OK", connection.getResponseMessage());
136 checkLargeData(getResponseAsString(connection));
137 connection.disconnect();
138 }
139
140 @SmallTest
141 @Feature({"Cronet"})
142 @CompareDefaultWithCronet
143 public void testPostWithContentLengthOneMassiveWrite() throws Exception {
144 URL url = new URL(NativeTestServer.getEchoBodyURL());
145 HttpURLConnection connection =
146 (HttpURLConnection) url.openConnection();
147 connection.setDoOutput(true);
148 connection.setRequestMethod("POST");
149 byte[] largeData = getLargeData();
150 connection.setRequestProperty("Content-Length",
151 Integer.toString(largeData.length));
152 OutputStream out = connection.getOutputStream();
153 out.write(largeData);
154 assertEquals(200, connection.getResponseCode());
155 assertEquals("OK", connection.getResponseMessage());
156 checkLargeData(getResponseAsString(connection));
157 connection.disconnect();
158 }
159
160 @SmallTest
161 @Feature({"Cronet"})
162 @CompareDefaultWithCronet
163 public void testPostWithContentLengthWriteOneByte() throws Exception {
164 URL url = new URL(NativeTestServer.getEchoBodyURL());
165 HttpURLConnection connection =
166 (HttpURLConnection) url.openConnection();
167 connection.setDoOutput(true);
168 connection.setRequestMethod("POST");
169 byte[] largeData = getLargeData();
170 connection.setRequestProperty("Content-Length",
171 Integer.toString(largeData.length));
172 OutputStream out = connection.getOutputStream();
173 for (int i = 0; i < largeData.length; i++) {
174 out.write(largeData[i]);
175 }
176 assertEquals(200, connection.getResponseCode());
177 assertEquals("OK", connection.getResponseMessage());
178 checkLargeData(getResponseAsString(connection));
179 connection.disconnect();
180 }
181
182 @SmallTest
183 @Feature({"Cronet"})
184 @CompareDefaultWithCronet
185 public void testPostWithZeroContentLength() throws Exception {
186 URL url = new URL(NativeTestServer.getEchoBodyURL());
187 HttpURLConnection connection =
188 (HttpURLConnection) url.openConnection();
189 connection.setDoOutput(true);
190 connection.setRequestMethod("POST");
191 connection.setRequestProperty("Content-Length", "0");
192 assertEquals(200, connection.getResponseCode());
193 assertEquals("OK", connection.getResponseMessage());
194 assertEquals("", getResponseAsString(connection));
195 connection.disconnect();
196 }
197
198 @SmallTest
199 @Feature({"Cronet"})
200 @CompareDefaultWithCronet
201 public void testPostZeroByteWithoutContentLength() throws Exception {
202 // Make sure both implementation sets the Content-Length header to 0.
203 URL url = new URL(NativeTestServer.getEchoHeaderURL("Content-Length"));
204 HttpURLConnection connection =
205 (HttpURLConnection) url.openConnection();
206 connection.setDoOutput(true);
207 connection.setRequestMethod("POST");
208 assertEquals(200, connection.getResponseCode());
209 assertEquals("OK", connection.getResponseMessage());
210 assertEquals("0", getResponseAsString(connection));
211 connection.disconnect();
212
213 // Make sure the server echoes back empty body for both implementation.
214 URL echoBody = new URL(NativeTestServer.getEchoBodyURL());
215 HttpURLConnection connection2 =
216 (HttpURLConnection) echoBody.openConnection();
217 connection2.setDoOutput(true);
218 connection2.setRequestMethod("POST");
219 assertEquals(200, connection2.getResponseCode());
220 assertEquals("OK", connection2.getResponseMessage());
221 assertEquals("", getResponseAsString(connection2));
222 connection2.disconnect();
223 }
224
225 @SmallTest
226 @Feature({"Cronet"})
227 @CompareDefaultWithCronet
228 public void testPostWithoutContentLengthSmall() throws Exception {
229 URL url = new URL(NativeTestServer.getEchoBodyURL());
230 HttpURLConnection connection =
231 (HttpURLConnection) url.openConnection();
232 connection.setDoOutput(true);
233 connection.setRequestMethod("POST");
234 OutputStream out = connection.getOutputStream();
235 out.write(UPLOAD_DATA);
236 assertEquals(200, connection.getResponseCode());
237 assertEquals("OK", connection.getResponseMessage());
238 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
239 connection.disconnect();
240 }
241
242 @SmallTest
243 @Feature({"Cronet"})
244 @CompareDefaultWithCronet
245 public void testPostWithoutContentLength() throws Exception {
246 URL url = new URL(NativeTestServer.getEchoBodyURL());
247 HttpURLConnection connection =
248 (HttpURLConnection) url.openConnection();
249 connection.setDoOutput(true);
250 connection.setRequestMethod("POST");
251 byte[] largeData = getLargeData();
252 OutputStream out = connection.getOutputStream();
253 int totalBytesWritten = 0;
254 // Number of bytes to write each time. It is doubled each time
255 // to make sure that the buffer grows.
256 int bytesToWrite = 683;
257 while (totalBytesWritten < largeData.length) {
258 if (bytesToWrite > largeData.length - totalBytesWritten) {
259 // Do not write out of bound.
260 bytesToWrite = largeData.length - totalBytesWritten;
261 }
262 out.write(largeData, totalBytesWritten, bytesToWrite);
263 totalBytesWritten += bytesToWrite;
264 bytesToWrite *= 2;
265 }
266 assertEquals(200, connection.getResponseCode());
267 assertEquals("OK", connection.getResponseMessage());
268 checkLargeData(getResponseAsString(connection));
269 connection.disconnect();
270 }
271
272 @SmallTest
273 @Feature({"Cronet"})
274 @CompareDefaultWithCronet
275 public void testPostWithoutContentLengthOneMassiveWrite() throws Exception {
276 URL url = new URL(NativeTestServer.getEchoBodyURL());
277 HttpURLConnection connection =
278 (HttpURLConnection) url.openConnection();
279 connection.setDoOutput(true);
280 connection.setRequestMethod("POST");
281 OutputStream out = connection.getOutputStream();
282 byte[] largeData = getLargeData();
283 out.write(largeData);
284 assertEquals(200, connection.getResponseCode());
285 assertEquals("OK", connection.getResponseMessage());
286 checkLargeData(getResponseAsString(connection));
287 connection.disconnect();
288 }
289
290 @SmallTest
291 @Feature({"Cronet"})
292 @CompareDefaultWithCronet
293 public void testPostWithoutContentLengthWriteOneByte() throws Exception {
294 URL url = new URL(NativeTestServer.getEchoBodyURL());
295 HttpURLConnection connection =
296 (HttpURLConnection) url.openConnection();
297 connection.setDoOutput(true);
298 connection.setRequestMethod("POST");
299 OutputStream out = connection.getOutputStream();
300 byte[] largeData = getLargeData();
301 for (int i = 0; i < largeData.length; i++) {
302 out.write(largeData[i]);
303 }
304 assertEquals(200, connection.getResponseCode());
305 assertEquals("OK", connection.getResponseMessage());
306 checkLargeData(getResponseAsString(connection));
307 connection.disconnect();
308 }
309
310 @SmallTest
311 @Feature({"Cronet"})
312 @CompareDefaultWithCronet
313 public void testWriteLessThanContentLength()
314 throws Exception {
315 URL url = new URL(NativeTestServer.getEchoBodyURL());
316 HttpURLConnection connection =
317 (HttpURLConnection) url.openConnection();
318 connection.setDoOutput(true);
319 connection.setRequestMethod("POST");
320 // Set a content length that's 1 byte more.
321 connection.setRequestProperty("Content-Length",
322 Integer.toString(UPLOAD_DATA.length + 1));
323 OutputStream out = connection.getOutputStream();
324 out.write(UPLOAD_DATA);
325 try {
326 connection.getResponseCode();
327 fail();
328 } catch (ProtocolException e) {
329 // Expected.
330 }
331 connection.disconnect();
332 }
333
334 /**
335 * Tests that if caller writes more than the content length provided,
336 * an exception should occur.
337 */
338 @SmallTest
339 @Feature({"Cronet"})
340 @CompareDefaultWithCronet
341 public void testWriteMoreThanContentLength() throws Exception {
342 URL url = new URL(NativeTestServer.getEchoBodyURL());
343 HttpURLConnection connection =
344 (HttpURLConnection) url.openConnection();
345 connection.setDoOutput(true);
346 connection.setRequestMethod("POST");
347 // Use a content length that is 1 byte shorter than actual data.
348 connection.setRequestProperty("Content-Length",
349 Integer.toString(UPLOAD_DATA.length - 1));
350 OutputStream out = connection.getOutputStream();
351 // Write a few bytes first.
352 out.write(UPLOAD_DATA, 0, 3);
353 try {
354 // Write remaining bytes.
355 out.write(UPLOAD_DATA, 3, UPLOAD_DATA.length - 3);
356 fail();
357 } catch (ProtocolException e) {
358 assertEquals("exceeded content-length limit of "
359 + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
360 }
361 }
362
363 /**
364 * Same as {@code testWriteMoreThanContentLength()}, but it only writes one byte
365 * at a time.
366 */
367 @SmallTest
368 @Feature({"Cronet"})
369 @CompareDefaultWithCronet
370 public void testWriteMoreThanContentLengthWriteOneByte() throws Exception {
371 URL url = new URL(NativeTestServer.getEchoBodyURL());
372 HttpURLConnection connection =
373 (HttpURLConnection) url.openConnection();
374 connection.setDoOutput(true);
375 connection.setRequestMethod("POST");
376 // Use a content length that is 1 byte shorter than actual data.
377 connection.setRequestProperty("Content-Length",
378 Integer.toString(UPLOAD_DATA.length - 1));
379 OutputStream out = connection.getOutputStream();
380 try {
381 for (int i = 0; i < UPLOAD_DATA.length; i++) {
382 out.write(UPLOAD_DATA[i]);
383 }
384 fail();
385 } catch (java.net.ProtocolException e) {
386 assertEquals("exceeded content-length limit of "
387 + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
388 }
389 }
390
391 /**
392 * Tests that {@link CronetBufferedOutputStream} supports rewind in a
393 * POST preserving redirect.
394 * Use {@code OnlyRunCronetHttpURLConnection} as the default implementation
395 * does not pass this test.
396 */
397 @SmallTest
398 @Feature({"Cronet"})
399 @OnlyRunCronetHttpURLConnection
400 public void testRewind() throws Exception {
401 URL url = new URL(NativeTestServer.getRedirectToEchoBody());
402 HttpURLConnection connection =
403 (HttpURLConnection) url.openConnection();
404 connection.setDoOutput(true);
405 connection.setRequestMethod("POST");
406 connection.setRequestProperty("Content-Length",
407 Integer.toString(UPLOAD_DATA.length));
408 OutputStream out = connection.getOutputStream();
409 out.write(UPLOAD_DATA);
410 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
411 connection.disconnect();
412 }
413
414 /**
415 * Like {@link #testRewind} but does not set Content-Length header.
416 */
417 @SmallTest
418 @Feature({"Cronet"})
419 @OnlyRunCronetHttpURLConnection
420 public void testRewindWithoutContentLength() throws Exception {
421 URL url = new URL(NativeTestServer.getRedirectToEchoBody());
422 HttpURLConnection connection =
423 (HttpURLConnection) url.openConnection();
424 connection.setDoOutput(true);
425 connection.setRequestMethod("POST");
426 OutputStream out = connection.getOutputStream();
427 out.write(UPLOAD_DATA);
428 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
429 connection.disconnect();
430 }
431
432 /**
433 * Helper method to extract response body as a string for testing.
434 */
435 private String getResponseAsString(HttpURLConnection connection)
436 throws Exception {
437 InputStream in = connection.getInputStream();
438 ByteArrayOutputStream out = new ByteArrayOutputStream();
439 int b;
440 while ((b = in.read()) != -1) {
441 out.write(b);
442 }
443 return out.toString();
444 }
445
446 /**
447 * Produces a byte array that contains {@code REPEAT_COUNT} of
448 * {@code UPLOAD_DATA_STRING}.
449 */
450 private byte[] getLargeData() {
451 byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length];
452 for (int i = 0; i < REPEAT_COUNT; i++) {
453 for (int j = 0; j < UPLOAD_DATA.length; j++) {
454 largeData[i * UPLOAD_DATA.length + j] = UPLOAD_DATA[j];
455 }
456 }
457 return largeData;
458 }
459
460 /**
461 * Helper function to check whether {@code data} is a concatenation of
462 * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings.
463 */
464 private void checkLargeData(String data) {
465 for (int i = 0; i < REPEAT_COUNT; i++) {
466 assertEquals(UPLOAD_DATA_STRING, data.substring(
467 UPLOAD_DATA_STRING.length() * i,
468 UPLOAD_DATA_STRING.length() * (i + 1)));
469 }
470 }
471 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698