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

Unified Diff: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java

Issue 966743003: [Cronet] Implement getOutputStream in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
Patch Set: Use large data in buffered case too 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/CronetHttpURLConnectionTest.java
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
index f4ff70cdb0c693ecf60c9f532007ec18eb8a7ffc..39165022390c643742b24d602bc7f5d1a3806c51 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java
@@ -18,6 +18,7 @@ import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@@ -37,6 +38,10 @@ import java.util.regex.Pattern;
* See {@link CronetTestBase#runTest()} for details.
*/
public class CronetHttpURLConnectionTest 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;
@Override
@@ -76,6 +81,191 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
@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 testPostFixedLengthStreamingMode() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ OutputStream out = connection.getOutputStream();
+ out.write(UPLOAD_DATA);
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostFixedLengthStreamingModeWriteOneByte() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ OutputStream out = connection.getOutputStream();
+ for (int i = 0; i < UPLOAD_DATA.length; i++) {
+ // Write one byte at a time.
+ out.write(UPLOAD_DATA[i]);
+ }
+ assertEquals(200, connection.getResponseCode());
+ assertEquals("OK", connection.getResponseMessage());
+ assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
+ connection.disconnect();
+ }
+
+ /**
+ * 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)));
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
+ public void testPostFixedLengthStreamingModeLargeData() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ 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];
+ }
+ }
+ connection.setFixedLengthStreamingMode(UPLOAD_DATA.length * REPEAT_COUNT);
+ 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 testPostFixedLengthStreamingModeLargeDataWriteOneByte()
+ throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ connection.setFixedLengthStreamingMode(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 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));
+ 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();
+ }
+
+ @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();
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @OnlyRunCronetHttpURLConnection
+ // TODO(xunjieli): Change the test after chunked support is added.
+ public void testPostChunked() throws Exception {
+ URL url = new URL(NativeTestServer.getEchoBodyURL());
+ HttpURLConnection connection =
+ (HttpURLConnection) url.openConnection();
+ connection.setDoOutput(true);
+ connection.setRequestMethod("POST");
+ try {
+ connection.setChunkedStreamingMode(0);
+ } catch (UnsupportedOperationException e) {
+ assertEquals("Chunked mode not supported yet", e.getMessage());
+ }
+ }
+
+ @SmallTest
+ @Feature({"Cronet"})
+ @CompareDefaultWithCronet
public void testNotFoundURLRequest() throws Exception {
URL url = new URL(NativeTestServer.getFileURL("/notfound.html"));
HttpURLConnection urlConnection =

Powered by Google App Engine
This is Rietveld 408576698