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

Unified 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: Addressed Matt's comments Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..f95045a4cf0f79e2493b411b2f9449c430620af6
--- /dev/null
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java
@@ -0,0 +1,289 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net.urlconnection;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.chromium.base.test.util.Feature;
+
+import org.chromium.net.CronetTestActivity;
+import org.chromium.net.CronetTestBase;
+import org.chromium.net.NativeTestServer;
+import org.chromium.net.UploadDataSink;
+import org.chromium.net.UrlRequestContext;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+/**
+ * Tests the CronetBufferedOutputStream implementation.
+ */
+public class CronetBufferedOutputStreamTest extends CronetTestBase {
+ private static final String UPLOAD_DATA_STRING = "Nifty upload data!";
+ private static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes();
+ private static final int REPEAT_COUNT = 100000;
+ private CronetTestActivity mActivity;
+ private UrlRequestContext mUrlRequestContext;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mActivity = launchCronetTestApp();
+ mUrlRequestContext = mActivity.mUrlRequestContext;
+ assertTrue(NativeTestServer.startNativeTestServer(
+ getInstrumentation().getTargetContext()));
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ NativeTestServer.shutdownNativeTestServer();
+ super.tearDown();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostAfterConnectionMade() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ assertEquals(200, connection.getResponseCode());
+ try {
+ connection.getOutputStream();
+ fail();
+ } catch (java.net.ProtocolException e) {
+ // Expected.
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostWithContentLength() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Length",
+ Integer.toString(UPLOAD_DATA.length * REPEAT_COUNT));
+ byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length];
+ for (int i = 0; i < REPEAT_COUNT; i++) {
+ for (int j = 0; j < UPLOAD_DATA.length; j++) {
+ largeData[i * UPLOAD_DATA.length + j] = UPLOAD_DATA[j];
+ }
+ }
+ OutputStream out = connection.getOutputStream();
+ int totalBytesWritten = 0;
+ // Number of bytes to write each time. It is incremented by one from 0.
+ int bytesToWrite = 0;
+ while (totalBytesWritten < largeData.length) {
+ if (bytesToWrite > largeData.length - totalBytesWritten) {
+ // Do not write out of bound.
+ bytesToWrite = largeData.length - totalBytesWritten;
+ }
+ out.write(largeData, totalBytesWritten, bytesToWrite);
+ totalBytesWritten += bytesToWrite;
+ bytesToWrite++;
+ }
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ checkLargeData(getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostWithContentLengthWriteOneByte() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Length",
+ Integer.toString(UPLOAD_DATA.length * REPEAT_COUNT));
+ OutputStream out = connection.getOutputStream();
+ for (int i = 0; i < REPEAT_COUNT; i++) {
+ for (int j = 0; j < UPLOAD_DATA.length; j++) {
+ // Write one byte at a time.
+ out.write(UPLOAD_DATA[j]);
+ }
+ }
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ checkLargeData(getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostWithZeroContentLength() throws Exception {
mmenke 2015/03/25 18:34:57 Maybe a zero length upload without a content lengt
xunjieli 2015/03/27 17:46:45 Done.
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Length", "0");
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ assertEquals("", getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostWithoutContentLength() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ OutputStream out = connection.getOutputStream();
+ for (int i = 0; i < REPEAT_COUNT; i++) {
+ out.write(UPLOAD_DATA);
+ }
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ checkLargeData(getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ /**
+ * Tests that if caller writes more than the content length provided,
+ * an exception should occur.
+ */
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testWriteBufferOutOfBound() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ // Use a content length that is 1 byte shorter than actual data.
+ connection.setRequestProperty("Content-Length",
+ Integer.toString(UPLOAD_DATA.length - 1));
+ OutputStream out = connection.getOutputStream();
+ try {
+ out.write(UPLOAD_DATA);
mmenke 2015/03/25 18:34:57 Suggest a test like this that does two writes.
xunjieli 2015/03/27 17:46:45 Done.
+ fail();
+ } catch (java.net.ProtocolException e) {
+ assertEquals("exceeded content-length limit of "
+ + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
+ }
+ }
+
+ /**
+ * Same as {@code testWriteBufferOutOfBound()}, but it only writes one byte
+ * at a time.
+ */
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testWriteBufferOutOfBoundWriteOneByte() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ // Use a content length that is 1 byte shorter than actual data.
+ connection.setRequestProperty("Content-Length",
+ Integer.toString(UPLOAD_DATA.length - 1));
+ OutputStream out = connection.getOutputStream();
+ try {
+ for (int i = 0; i < UPLOAD_DATA.length; i++) {
+ out.write(UPLOAD_DATA[i]);
+ }
+ fail();
+ } catch (java.net.ProtocolException e) {
+ assertEquals("exceeded content-length limit of "
+ + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
+ }
+ }
+
+ /**
+ * Tests that {@link CronetBufferedOutputStream} supports rewind.
+ */
+ @SmallTest
+ @Feature({"Cronet"})
+ @OnlyRunCronetHttpURLConnection
+ public void testRewind() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ CronetHttpURLConnection conn = new CronetHttpURLConnection(url,
+ mUrlRequestContext);
+ CronetBufferedOutputStream out =
+ new CronetBufferedOutputStream(conn, UPLOAD_DATA.length);
+ out.write(UPLOAD_DATA);
+ TestUploadDataSink sink = new TestUploadDataSink();
+ out.rewind(sink);
+ assertTrue(sink.mRewindSucceededCalled);
+ ByteBuffer byteBuffer = ByteBuffer.allocate(UPLOAD_DATA.length);
+ out.read(sink, byteBuffer);
+ assertTrue(sink.mReadSucceededCalled);
+ assertTrue(Arrays.equals(UPLOAD_DATA, byteBuffer.array()));
mmenke 2015/03/25 18:34:57 Rather than doing all this manually, can we just u
xunjieli 2015/03/27 17:46:45 Done. Good idea! But the default implementation re
+ }
+
+ private static class TestUploadDataSink implements UploadDataSink {
+ boolean mReadSucceededCalled = false;
+ boolean mRewindSucceededCalled = false;
+
+ @Override
+ public void onReadSucceeded(boolean finalChunk) {
+ mReadSucceededCalled = true;
+ }
+
+ @Override
+ public void onReadError(Exception exception) {
+ // Left blank.
+ }
+
+ @Override
+ public void onRewindSucceeded() {
+ mRewindSucceededCalled = true;
+ }
+
+ @Override
+ public void onRewindError(Exception e) {
+ // Left blank.
+ }
+ }
+
+ /**
+ * Helper method to extract response body as a string for testing.
+ */
+ private String getResponseAsString(HttpURLConnection connection)
+ throws Exception {
+ InputStream in = connection.getInputStream();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ int b;
+ while ((b = in.read()) != -1) {
+ out.write(b);
+ }
+ return out.toString();
+ }
+
+ /**
+ * Helper function to check whether {@code data} is a concatenation of
+ * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings.
+ */
+ private void checkLargeData(String data) {
+ for (int i = 0; i < REPEAT_COUNT; i++) {
+ assertEquals(UPLOAD_DATA_STRING, data.substring(
+ UPLOAD_DATA_STRING.length() * i,
+ UPLOAD_DATA_STRING.length() * (i + 1)));
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698