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

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

Issue 1413303006: [Cronet] When connection is disconnected, InputStream#read should give an exception (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use @code Created 5 years, 1 month 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/CronetFixedModeOutputStreamTest.java
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java
index d5451ba0cfb6b043d7b024c3302a1cbe9707d39e..272e5d92d1c391aa2c209a30a34a9db1b88a95b1 100644
--- a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java
@@ -11,8 +11,6 @@ import org.chromium.net.CronetTestBase;
import org.chromium.net.CronetTestFramework;
import org.chromium.net.NativeTestServer;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpRetryException;
import java.net.HttpURLConnection;
@@ -29,10 +27,6 @@ import java.net.URL;
* See {@link CronetTestBase#runTest()} for details.
*/
public class CronetFixedModeOutputStreamTest 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;
-
@Override
protected void setUp() throws Exception {
super.setUp();
@@ -58,13 +52,13 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length);
OutputStream out = connection.getOutputStream();
connection.connect();
- out.write(UPLOAD_DATA);
+ out.write(TestUtil.UPLOAD_DATA);
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
+ assertEquals(TestUtil.UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -82,7 +76,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
connection1.setFixedLengthStreamingMode(0);
assertEquals(200, connection1.getResponseCode());
assertEquals("OK", connection1.getResponseMessage());
- assertEquals("0", getResponseAsString(connection1));
+ assertEquals("0", TestUtil.getResponseAsString(connection1));
connection1.disconnect();
// Check body is empty.
@@ -94,7 +88,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
connection2.setFixedLengthStreamingMode(0);
assertEquals(200, connection2.getResponseCode());
assertEquals("OK", connection2.getResponseMessage());
- assertEquals("", getResponseAsString(connection2));
+ assertEquals("", TestUtil.getResponseAsString(connection2));
connection2.disconnect();
}
@@ -109,9 +103,9 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// Set a content length that's 1 byte more.
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length + 1);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length + 1);
OutputStream out = connection.getOutputStream();
- out.write(UPLOAD_DATA);
+ out.write(TestUtil.UPLOAD_DATA);
try {
connection.getResponseCode();
fail();
@@ -132,18 +126,18 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// Set a content length that's 1 byte short.
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length - 1);
OutputStream out = connection.getOutputStream();
try {
- out.write(UPLOAD_DATA);
+ out.write(TestUtil.UPLOAD_DATA);
// On Lollipop, default implementation only triggers the error when reading response.
connection.getInputStream();
fail();
} catch (ProtocolException e) {
// Expected.
- assertEquals("expected " + (UPLOAD_DATA.length - 1)
- + " bytes but received "
- + UPLOAD_DATA.length, e.getMessage());
+ assertEquals("expected " + (TestUtil.UPLOAD_DATA.length - 1) + " bytes but received "
+ + TestUtil.UPLOAD_DATA.length,
+ e.getMessage());
}
connection.disconnect();
}
@@ -159,22 +153,22 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// Set a content length that's 1 byte short.
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length - 1);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length - 1);
OutputStream out = connection.getOutputStream();
- for (int i = 0; i < UPLOAD_DATA.length - 1; i++) {
- out.write(UPLOAD_DATA[i]);
+ for (int i = 0; i < TestUtil.UPLOAD_DATA.length - 1; i++) {
+ out.write(TestUtil.UPLOAD_DATA[i]);
}
try {
// Try upload an extra byte.
- out.write(UPLOAD_DATA[UPLOAD_DATA.length - 1]);
+ out.write(TestUtil.UPLOAD_DATA[TestUtil.UPLOAD_DATA.length - 1]);
// On Lollipop, default implementation only triggers the error when reading response.
connection.getInputStream();
fail();
} catch (ProtocolException e) {
// Expected.
String expectedVariant = "expected 0 bytes but received 1";
- String expectedVariantOnLollipop = "expected " + (UPLOAD_DATA.length - 1)
- + " bytes but received " + UPLOAD_DATA.length;
+ String expectedVariantOnLollipop = "expected " + (TestUtil.UPLOAD_DATA.length - 1)
+ + " bytes but received " + TestUtil.UPLOAD_DATA.length;
assertTrue(expectedVariant.equals(e.getMessage())
|| expectedVariantOnLollipop.equals(e.getMessage()));
}
@@ -190,12 +184,12 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length);
OutputStream out = connection.getOutputStream();
- out.write(UPLOAD_DATA);
+ out.write(TestUtil.UPLOAD_DATA);
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
+ assertEquals(TestUtil.UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -209,15 +203,15 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length);
OutputStream out = connection.getOutputStream();
- for (int i = 0; i < UPLOAD_DATA.length; i++) {
+ for (int i = 0; i < TestUtil.UPLOAD_DATA.length; i++) {
// Write one byte at a time.
- out.write(UPLOAD_DATA[i]);
+ out.write(TestUtil.UPLOAD_DATA[i]);
}
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
+ assertEquals(TestUtil.UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -230,7 +224,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- byte[] largeData = getLargeData();
+ byte[] largeData = TestUtil.getLargeData();
connection.setFixedLengthStreamingMode(largeData.length);
OutputStream out = connection.getOutputStream();
int totalBytesWritten = 0;
@@ -248,7 +242,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
}
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- checkLargeData(getResponseAsString(connection));
+ TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -262,7 +256,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- byte[] largeData = getLargeData();
+ byte[] largeData = TestUtil.getLargeData();
connection.setFixedLengthStreamingMode(largeData.length);
OutputStream out = connection.getOutputStream();
for (int i = 0; i < largeData.length; i++) {
@@ -271,7 +265,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
}
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- checkLargeData(getResponseAsString(connection));
+ TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -297,7 +291,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- byte[] largeData = getLargeData();
+ byte[] largeData = TestUtil.getLargeData();
connection.setFixedLengthStreamingMode(largeData.length);
OutputStream out = connection.getOutputStream();
// Write everything at one go, so the data is larger than the buffer
@@ -305,7 +299,7 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
out.write(largeData);
assertEquals(200, connection.getResponseCode());
assertEquals("OK", connection.getResponseMessage());
- checkLargeData(getResponseAsString(connection));
+ TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
connection.disconnect();
}
@@ -319,53 +313,13 @@ public class CronetFixedModeOutputStreamTest extends CronetTestBase {
(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
- connection.setFixedLengthStreamingMode(UPLOAD_DATA.length);
+ connection.setFixedLengthStreamingMode(TestUtil.UPLOAD_DATA.length);
try {
OutputStream out = connection.getOutputStream();
- out.write(UPLOAD_DATA);
+ out.write(TestUtil.UPLOAD_DATA);
} catch (HttpRetryException e) {
assertEquals("Cannot retry streamed Http body", e.getMessage());
}
connection.disconnect();
}
-
- /**
- * 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();
- }
-
- /**
- * Produces a byte array that contains {@code REPEAT_COUNT} of
- * {@code UPLOAD_DATA_STRING}.
- */
- private byte[] getLargeData() {
- 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];
- }
- }
- return largeData;
- }
-
- /**
- * 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