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

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

Powered by Google App Engine
This is Rietveld 408576698