| 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
|
| index 6ff44cf22f754fd7f1090c97bf5c8ea738454a46..01061b2c0ab788cdb552f6bf653e8dd83b2dfae6 100644
|
| --- 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
|
| @@ -12,8 +12,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.HttpURLConnection;
|
| import java.net.ProtocolException;
|
| @@ -23,10 +21,6 @@ import java.net.URL;
|
| * 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;
|
| -
|
| @Override
|
| protected void setUp() throws Exception {
|
| super.setUp();
|
| @@ -76,11 +70,11 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| OutputStream out = connection.getOutputStream();
|
| - out.write(UPLOAD_DATA);
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| connection.connect();
|
| try {
|
| // Attemp to write some more.
|
| - out.write(UPLOAD_DATA);
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| fail();
|
| } catch (IllegalStateException e) {
|
| assertEquals("Cannot write after being connected.", e.getMessage());
|
| @@ -99,7 +93,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| OutputStream out = connection.getOutputStream();
|
| assertEquals(200, connection.getResponseCode());
|
| try {
|
| - out.write(UPLOAD_DATA);
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| fail();
|
| } catch (Exception e) {
|
| // Default implementation gives an IOException and says that the
|
| @@ -117,7 +111,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| (HttpURLConnection) url.openConnection();
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| connection.setRequestProperty("Content-Length",
|
| Integer.toString(largeData.length));
|
| OutputStream out = connection.getOutputStream();
|
| @@ -136,7 +130,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| }
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -149,14 +143,14 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| (HttpURLConnection) url.openConnection();
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| connection.setRequestProperty("Content-Length",
|
| Integer.toString(largeData.length));
|
| OutputStream out = connection.getOutputStream();
|
| out.write(largeData);
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -169,7 +163,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| (HttpURLConnection) url.openConnection();
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| connection.setRequestProperty("Content-Length",
|
| Integer.toString(largeData.length));
|
| OutputStream out = connection.getOutputStream();
|
| @@ -178,7 +172,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| }
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -194,7 +188,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setRequestProperty("Content-Length", "0");
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - assertEquals("", getResponseAsString(connection));
|
| + assertEquals("", TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -210,7 +204,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setRequestMethod("POST");
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - assertEquals("0", getResponseAsString(connection));
|
| + assertEquals("0", TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
|
|
| // Make sure the server echoes back empty body for both implementation.
|
| @@ -221,7 +215,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection2.setRequestMethod("POST");
|
| assertEquals(200, connection2.getResponseCode());
|
| assertEquals("OK", connection2.getResponseMessage());
|
| - assertEquals("", getResponseAsString(connection2));
|
| + assertEquals("", TestUtil.getResponseAsString(connection2));
|
| connection2.disconnect();
|
| }
|
|
|
| @@ -235,10 +229,10 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| 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();
|
| }
|
|
|
| @@ -251,7 +245,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| (HttpURLConnection) url.openConnection();
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| OutputStream out = connection.getOutputStream();
|
| int totalBytesWritten = 0;
|
| // Number of bytes to write each time. It is doubled each time
|
| @@ -268,7 +262,7 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| }
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -282,11 +276,11 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| OutputStream out = connection.getOutputStream();
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| out.write(largeData);
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -300,13 +294,13 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| OutputStream out = connection.getOutputStream();
|
| - byte[] largeData = getLargeData();
|
| + byte[] largeData = TestUtil.getLargeData();
|
| for (int i = 0; i < largeData.length; i++) {
|
| out.write(largeData[i]);
|
| }
|
| assertEquals(200, connection.getResponseCode());
|
| assertEquals("OK", connection.getResponseMessage());
|
| - checkLargeData(getResponseAsString(connection));
|
| + TestUtil.checkLargeData(TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -321,10 +315,10 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| // Set a content length that's 1 byte more.
|
| - connection.setRequestProperty("Content-Length",
|
| - Integer.toString(UPLOAD_DATA.length + 1));
|
| + connection.setRequestProperty(
|
| + "Content-Length", Integer.toString(TestUtil.UPLOAD_DATA.length + 1));
|
| OutputStream out = connection.getOutputStream();
|
| - out.write(UPLOAD_DATA);
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| try {
|
| connection.getResponseCode();
|
| fail();
|
| @@ -348,20 +342,21 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| 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));
|
| + connection.setRequestProperty(
|
| + "Content-Length", Integer.toString(TestUtil.UPLOAD_DATA.length - 1));
|
| OutputStream out = connection.getOutputStream();
|
| // Write a few bytes first.
|
| - out.write(UPLOAD_DATA, 0, 3);
|
| + out.write(TestUtil.UPLOAD_DATA, 0, 3);
|
| try {
|
| // Write remaining bytes.
|
| - out.write(UPLOAD_DATA, 3, UPLOAD_DATA.length - 3);
|
| + out.write(TestUtil.UPLOAD_DATA, 3, TestUtil.UPLOAD_DATA.length - 3);
|
| // On Lollipop, default implementation only triggers the error when reading response.
|
| connection.getInputStream();
|
| fail();
|
| } catch (ProtocolException e) {
|
| - assertEquals("exceeded content-length limit of "
|
| - + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
|
| + assertEquals("exceeded content-length limit of " + (TestUtil.UPLOAD_DATA.length - 1)
|
| + + " bytes",
|
| + e.getMessage());
|
| }
|
| }
|
|
|
| @@ -379,19 +374,20 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| 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));
|
| + connection.setRequestProperty(
|
| + "Content-Length", Integer.toString(TestUtil.UPLOAD_DATA.length - 1));
|
| OutputStream out = connection.getOutputStream();
|
| try {
|
| - for (int i = 0; i < UPLOAD_DATA.length; i++) {
|
| - out.write(UPLOAD_DATA[i]);
|
| + for (int i = 0; i < TestUtil.UPLOAD_DATA.length; i++) {
|
| + out.write(TestUtil.UPLOAD_DATA[i]);
|
| }
|
| // On Lollipop, default implementation only triggers the error when reading response.
|
| connection.getInputStream();
|
| fail();
|
| } catch (java.net.ProtocolException e) {
|
| - assertEquals("exceeded content-length limit of "
|
| - + (UPLOAD_DATA.length - 1) + " bytes", e.getMessage());
|
| + assertEquals("exceeded content-length limit of " + (TestUtil.UPLOAD_DATA.length - 1)
|
| + + " bytes",
|
| + e.getMessage());
|
| }
|
| }
|
|
|
| @@ -410,11 +406,11 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| (HttpURLConnection) url.openConnection();
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| - connection.setRequestProperty("Content-Length",
|
| - Integer.toString(UPLOAD_DATA.length));
|
| + connection.setRequestProperty(
|
| + "Content-Length", Integer.toString(TestUtil.UPLOAD_DATA.length));
|
| OutputStream out = connection.getOutputStream();
|
| - out.write(UPLOAD_DATA);
|
| - assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| + assertEquals(TestUtil.UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
|
| connection.disconnect();
|
| }
|
|
|
| @@ -431,48 +427,8 @@ public class CronetBufferedOutputStreamTest extends CronetTestBase {
|
| connection.setDoOutput(true);
|
| connection.setRequestMethod("POST");
|
| OutputStream out = connection.getOutputStream();
|
| - out.write(UPLOAD_DATA);
|
| - assertEquals(UPLOAD_DATA_STRING, getResponseAsString(connection));
|
| + out.write(TestUtil.UPLOAD_DATA);
|
| + assertEquals(TestUtil.UPLOAD_DATA_STRING, TestUtil.getResponseAsString(connection));
|
| 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)));
|
| - }
|
| - }
|
| }
|
|
|