| 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..080a89c6ca7c5292650d76bec2329861e9ad20f3 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
|
| @@ -10,6 +10,7 @@ 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.MockUrlRequestJobFactory;
|
| import org.chromium.net.NativeTestServer;
|
| import org.chromium.net.UrlRequestContextConfig;
|
| import org.chromium.net.UrlRequestException;
|
| @@ -433,24 +434,34 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
|
|
|
| @SmallTest
|
| @Feature({"Cronet"})
|
| - @CompareDefaultWithCronet
|
| + @OnlyRunCronetHttpURLConnection
|
| public void testInputStreamReadOneByte() throws Exception {
|
| - String testInputString = "this is a really long header";
|
| - URL url = new URL(NativeTestServer.getEchoHeaderURL("foo"));
|
| - HttpURLConnection urlConnection =
|
| + String data = "MyBigFunkyData";
|
| + int dataLength = data.length();
|
| + int repeatCount = 100000;
|
| + MockUrlRequestJobFactory mockUrlRequestJobFactory = new MockUrlRequestJobFactory(
|
| + getInstrumentation().getTargetContext());
|
| + URL url = new URL(mockUrlRequestJobFactory.getMockUrlForData(data,
|
| + repeatCount));
|
| + HttpURLConnection connection =
|
| (HttpURLConnection) url.openConnection();
|
| - urlConnection.addRequestProperty("foo", testInputString);
|
| - assertEquals(200, urlConnection.getResponseCode());
|
| - assertEquals("OK", urlConnection.getResponseMessage());
|
| - InputStream in = urlConnection.getInputStream();
|
| + InputStream in = connection.getInputStream();
|
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
| int b;
|
| while ((b = in.read()) != -1) {
|
| out.write(b);
|
| }
|
| - urlConnection.disconnect();
|
| - assertTrue(Arrays.equals(
|
| - testInputString.getBytes(), out.toByteArray()));
|
| +
|
| + // All data has been read. Try reading beyond what is available should give -1.
|
| + assertEquals(-1, in.read());
|
| + String responseData = new String(out.toByteArray());
|
| + for (int i = 0; i < repeatCount; ++i) {
|
| + assertEquals(data, responseData.substring(dataLength * i,
|
| + dataLength * (i + 1)));
|
| + }
|
| + // TODO(xunjieli): Enable after crbug.com/463720 is fixed.
|
| + // assertEquals(200, connection.getResponseCode());
|
| + // assertEquals("OK", connection.getResponseMessage());
|
| }
|
|
|
| @SmallTest
|
| @@ -468,14 +479,61 @@ public class CronetHttpURLConnectionTest extends CronetTestBase {
|
| InputStream in = urlConnection.getInputStream();
|
| byte[] actualOutput = new byte[testInputBytes.length + 256];
|
| int bytesRead = in.read(actualOutput, 0, actualOutput.length);
|
| + assertEquals(testInputBytes.length, bytesRead);
|
| byte[] readSomeMore = new byte[10];
|
| int bytesReadBeyondAvailable = in.read(readSomeMore, 0, 10);
|
| - urlConnection.disconnect();
|
| - assertEquals(testInputBytes.length, bytesRead);
|
| assertEquals(-1, bytesReadBeyondAvailable);
|
| for (int i = 0; i < bytesRead; i++) {
|
| assertEquals(testInputBytes[i], actualOutput[i]);
|
| }
|
| + urlConnection.disconnect();
|
| + }
|
| +
|
| + /**
|
| + * Tests batch reading on CronetInputStream when
|
| + * {@link CronetHttpURLConnection#getMoreData} is called multiple times.
|
| + */
|
| + @SmallTest
|
| + @Feature({"Cronet"})
|
| + @OnlyRunCronetHttpURLConnection
|
| + public void testBigDataRead() throws Exception {
|
| + String data = "MyBigFunkyData";
|
| + int dataLength = data.length();
|
| + int repeatCount = 100000;
|
| + MockUrlRequestJobFactory mockUrlRequestJobFactory = new MockUrlRequestJobFactory(
|
| + getInstrumentation().getTargetContext());
|
| + URL url = new URL(mockUrlRequestJobFactory.getMockUrlForData(data,
|
| + repeatCount));
|
| + HttpURLConnection connection =
|
| + (HttpURLConnection) url.openConnection();
|
| + InputStream in = connection.getInputStream();
|
| + byte[] actualOutput = new byte[dataLength * repeatCount];
|
| + int totalBytesRead = 0;
|
| + // Number of bytes to read each time. It is incremented by one from 0.
|
| + int numBytesToRead = 0;
|
| + while (totalBytesRead < actualOutput.length) {
|
| + if (actualOutput.length - totalBytesRead < numBytesToRead) {
|
| + // Do not read out of bound.
|
| + numBytesToRead = actualOutput.length - totalBytesRead;
|
| + }
|
| + int bytesRead = in.read(actualOutput, totalBytesRead, numBytesToRead);
|
| + assertTrue(bytesRead <= numBytesToRead);
|
| + totalBytesRead += bytesRead;
|
| + numBytesToRead++;
|
| + }
|
| +
|
| + // All data has been read. Try reading beyond what is available should give -1.
|
| + assertEquals(0, in.read(actualOutput, 0, 0));
|
| + assertEquals(-1, in.read(actualOutput, 0, 1));
|
| +
|
| + String responseData = new String(actualOutput);
|
| + for (int i = 0; i < repeatCount; ++i) {
|
| + assertEquals(data, responseData.substring(dataLength * i,
|
| + dataLength * (i + 1)));
|
| + }
|
| + // TODO(xunjieli): Enable after crbug.com/463720 is fixed.
|
| + // assertEquals(200, connection.getResponseCode());
|
| + // assertEquals("OK", connection.getResponseMessage());
|
| }
|
|
|
| @SmallTest
|
|
|