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

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: Use ByteBuffer instead of ByteArrayOutputStream in CronetBufferedOutputStream 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.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()
54 throws Exception {
mmenke 2015/03/31 18:19:32 Maybe a test where we try to connect or read a res
xunjieli 2015/04/01 17:04:09 Done. Good idea!
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 testWriteMoreThanContentLength()
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 short.
91 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1);
92 OutputStream out = connection.getOutputStream();
93 try {
94 out.write(UPLOAD_DATA);
95 fail();
96 } catch (ProtocolException e) {
97 // Expected.
98 assertEquals("expected " + (UPLOAD_DATA.length - 1)
99 + " bytes but received "
100 + UPLOAD_DATA.length, e.getMessage());
101 }
102 connection.disconnect();
103 }
104
105 @SmallTest
106 @Feature({"Cronet"})
107 @CompareDefaultWithCronet
108 public void testWriteMoreThanContentLengthWriteOneByte()
109 throws Exception {
110 URL url = new URL(NativeTestServer.getEchoBodyURL());
111 HttpURLConnection connection =
112 (HttpURLConnection) url.openConnection();
113 connection.setDoOutput(true);
114 connection.setRequestMethod("POST");
115 // Set a content length that's 1 byte short.
116 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1);
117 OutputStream out = connection.getOutputStream();
118 for (int i = 0; i < UPLOAD_DATA.length - 1; i++) {
119 out.write(UPLOAD_DATA[i]);
120 }
121 try {
122 // Try upload an extra byte.
123 out.write(UPLOAD_DATA[UPLOAD_DATA.length - 1]);
124 fail();
125 } catch (ProtocolException e) {
126 // Expected.
127 assertEquals("expected 0 bytes but received 1", e.getMessage());
128 }
129 connection.disconnect();
130 }
131
132 @SmallTest
133 @Feature({"Cronet"})
134 @CompareDefaultWithCronet
135 public void testFixedLengthStreamingMode() throws Exception {
136 URL url = new URL(NativeTestServer.getEchoBodyURL());
137 HttpURLConnection connection =
138 (HttpURLConnection) url.openConnection();
139 connection.setDoOutput(true);
140 connection.setRequestMethod("POST");
141 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
142 OutputStream out = connection.getOutputStream();
143 out.write(UPLOAD_DATA);
144 assertEquals(200, connection.getResponseCode());
145 assertEquals("OK", connection.getResponseMessage());
146 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
147 connection.disconnect();
148 }
149
150 @SmallTest
151 @Feature({"Cronet"})
152 @CompareDefaultWithCronet
153 public void testFixedLengthStreamingModeWriteOneByte()
154 throws Exception {
155 URL url = new URL(NativeTestServer.getEchoBodyURL());
156 HttpURLConnection connection =
157 (HttpURLConnection) url.openConnection();
158 connection.setDoOutput(true);
159 connection.setRequestMethod("POST");
160 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
161 OutputStream out = connection.getOutputStream();
162 for (int i = 0; i < UPLOAD_DATA.length; i++) {
163 // Write one byte at a time.
164 out.write(UPLOAD_DATA[i]);
165 }
166 assertEquals(200, connection.getResponseCode());
167 assertEquals("OK", connection.getResponseMessage());
168 assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
169 connection.disconnect();
170 }
171
172 @SmallTest
173 @Feature({"Cronet"})
174 @CompareDefaultWithCronet
175 public void testFixedLengthStreamingModeLargeData() throws Exception {
176 URL url = new URL(NativeTestServer.getEchoBodyURL());
177 HttpURLConnection connection =
178 (HttpURLConnection) url.openConnection();
179 connection.setDoOutput(true);
180 connection.setRequestMethod("POST");
181 byte[] largeData = getLargeData();
182 connection.setFixedLengthStreamingMode(largeData.length);
183 OutputStream out = connection.getOutputStream();
184 int totalBytesWritten = 0;
185 // Number of bytes to write each time. It is incremented by one from 0.
186 int bytesToWrite = 0;
187 while (totalBytesWritten < largeData.length) {
mmenke 2015/03/31 18:19:32 Again, suggest having this match the other two.
xunjieli 2015/04/01 17:04:09 Done.
188 if (bytesToWrite > largeData.length - totalBytesWritten) {
189 // Do not write out of bound.
190 bytesToWrite = largeData.length - totalBytesWritten;
191 }
192 out.write(largeData, totalBytesWritten, bytesToWrite);
193 totalBytesWritten += bytesToWrite;
194 bytesToWrite++;
195 }
196 assertEquals(200, connection.getResponseCode());
197 assertEquals("OK", connection.getResponseMessage());
198 checkLargeData(getResponseAsString(connection));
199 connection.disconnect();
200 }
201
202 @SmallTest
203 @Feature({"Cronet"})
204 @CompareDefaultWithCronet
205 public void testFixedLengthStreamingModeLargeDataWriteOneByte()
206 throws Exception {
207 URL url = new URL(NativeTestServer.getEchoBodyURL());
208 HttpURLConnection connection =
209 (HttpURLConnection) url.openConnection();
210 connection.setDoOutput(true);
211 connection.setRequestMethod("POST");
212 byte[] largeData = getLargeData();
213 connection.setFixedLengthStreamingMode(largeData.length);
214 OutputStream out = connection.getOutputStream();
215 for (int i = 0; i < largeData.length; i++) {
216 // Write one byte at a time.
217 out.write(largeData[i]);
218 }
219 assertEquals(200, connection.getResponseCode());
220 assertEquals("OK", connection.getResponseMessage());
221 checkLargeData(getResponseAsString(connection));
222 connection.disconnect();
223 }
224
225 @SmallTest
226 @Feature({"Cronet"})
227 @OnlyRunCronetHttpURLConnection
228 public void testLargeDataMoreThanNativeBufferSize()
229 throws Exception {
230 // Set an internal buffer of size larger than the buffer size used
231 // in network stack internally.
232 // Normal stream uses 16384, QUIC uses 14520, and SPDY uses 2852.
233 CronetFixedModeOutputStream.setDefaultBufferLengthForTesting(17384);
234 testFixedLengthStreamingModeLargeDataWriteOneByte();
235 testFixedLengthStreamingModeLargeData();
236 }
237
238 @SmallTest
239 @Feature({"Cronet"})
240 @CompareDefaultWithCronet
241 public void testMassiveWrite() throws Exception {
mmenke 2015/03/31 18:19:32 OneMassiveWrite, to match the tests in the other f
xunjieli 2015/04/01 17:04:09 Done.
242 URL url = new URL(NativeTestServer.getEchoBodyURL());
243 HttpURLConnection connection =
244 (HttpURLConnection) url.openConnection();
245 connection.setDoOutput(true);
246 connection.setRequestMethod("POST");
247 byte[] largeData = getLargeData();
248 connection.setFixedLengthStreamingMode(largeData.length);
249 OutputStream out = connection.getOutputStream();
250 // Write everything at one go, so the data is larger than the buffer
251 // used in CronetFixedModeOutputStream.
252 out.write(largeData);
253 assertEquals(200, connection.getResponseCode());
254 assertEquals("OK", connection.getResponseMessage());
255 checkLargeData(getResponseAsString(connection));
256 connection.disconnect();
257 }
258
259 @SmallTest
260 @Feature({"Cronet"})
261 @OnlyRunCronetHttpURLConnection
262 public void testRewind() throws Exception {
263 // Post preserving redirect should fail.
264 URL url = new URL(NativeTestServer.getRedirectToEchoBody());
265 HttpURLConnection connection =
266 (HttpURLConnection) url.openConnection();
267 connection.setDoOutput(true);
268 connection.setRequestMethod("POST");
269 connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
270 try {
271 OutputStream out = connection.getOutputStream();
272 out.write(UPLOAD_DATA);
273 } catch (IllegalStateException e) {
274 assertEquals("Rewind is not supported by CronetFixedModeOutputStream .",
275 e.getMessage());
276 }
277 connection.disconnect();
278 }
279
280 /**
281 * Helper method to extract response body as a string for testing.
282 */
283 private String getResponseAsString(HttpURLConnection connection)
284 throws Exception {
285 InputStream in = connection.getInputStream();
286 ByteArrayOutputStream out = new ByteArrayOutputStream();
287 int b;
288 while ((b = in.read()) != -1) {
289 out.write(b);
290 }
291 return out.toString();
292 }
293
294 /**
295 * Produces a byte array that contains {@code REPEAT_COUNT} of
296 * {@code UPLOAD_DATA_STRING}.
297 */
298 private byte[] getLargeData() {
299 byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length];
300 for (int i = 0; i < REPEAT_COUNT; i++) {
301 for (int j = 0; j < UPLOAD_DATA.length; j++) {
302 largeData[i * UPLOAD_DATA.length + j] = UPLOAD_DATA[j];
303 }
304 }
305 return largeData;
306 }
307
308 /**
309 * Helper function to check whether {@code data} is a concatenation of
310 * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings.
311 */
312 private void checkLargeData(String data) {
313 for (int i = 0; i < REPEAT_COUNT; i++) {
314 assertEquals(UPLOAD_DATA_STRING, data.substring(
315 UPLOAD_DATA_STRING.length() * i,
316 UPLOAD_DATA_STRING.length() * (i + 1)));
317 }
318 }
319 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698