OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
6 | 6 |
7 import android.os.Build; | 7 import android.os.Build; |
8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
9 | 9 |
10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
11 import org.chromium.net.CronetTestActivity; | 11 import org.chromium.net.CronetTestActivity; |
12 import org.chromium.net.CronetTestBase; | 12 import org.chromium.net.CronetTestBase; |
13 import org.chromium.net.MockUrlRequestJobFactory; | |
13 import org.chromium.net.NativeTestServer; | 14 import org.chromium.net.NativeTestServer; |
14 import org.chromium.net.UrlRequestContextConfig; | 15 import org.chromium.net.UrlRequestContextConfig; |
15 import org.chromium.net.UrlRequestException; | 16 import org.chromium.net.UrlRequestException; |
16 | 17 |
17 import java.io.ByteArrayOutputStream; | 18 import java.io.ByteArrayOutputStream; |
18 import java.io.FileNotFoundException; | 19 import java.io.FileNotFoundException; |
19 import java.io.IOException; | 20 import java.io.IOException; |
20 import java.io.InputStream; | 21 import java.io.InputStream; |
21 import java.net.HttpURLConnection; | 22 import java.net.HttpURLConnection; |
22 import java.net.MalformedURLException; | 23 import java.net.MalformedURLException; |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 byte[] readSomeMore = new byte[10]; | 472 byte[] readSomeMore = new byte[10]; |
472 int bytesReadBeyondAvailable = in.read(readSomeMore, 0, 10); | 473 int bytesReadBeyondAvailable = in.read(readSomeMore, 0, 10); |
473 urlConnection.disconnect(); | 474 urlConnection.disconnect(); |
474 assertEquals(testInputBytes.length, bytesRead); | 475 assertEquals(testInputBytes.length, bytesRead); |
475 assertEquals(-1, bytesReadBeyondAvailable); | 476 assertEquals(-1, bytesReadBeyondAvailable); |
476 for (int i = 0; i < bytesRead; i++) { | 477 for (int i = 0; i < bytesRead; i++) { |
477 assertEquals(testInputBytes[i], actualOutput[i]); | 478 assertEquals(testInputBytes[i], actualOutput[i]); |
478 } | 479 } |
479 } | 480 } |
480 | 481 |
482 /** | |
483 * Tests batch reading on CronetInputStream when | |
484 * {@link CronetHttpURLConnection#getMoreData} is called multiple times. | |
485 */ | |
486 @SmallTest | |
487 @Feature({"Cronet"}) | |
488 @OnlyRunCronetHttpURLConnection | |
489 public void testBigDataRead() throws Exception { | |
490 String data = "MyBigFunkyData"; | |
491 int dataLength = data.length(); | |
492 int repeatCount = 100000; | |
493 MockUrlRequestJobFactory mMockUrlRequestJobFactory = new MockUrlRequestJ obFactory( | |
494 getInstrumentation().getTargetContext()); | |
495 URL url = new URL(mMockUrlRequestJobFactory.getMockUrlForData(data, | |
496 repeatCount)); | |
497 HttpURLConnection connection = | |
498 (HttpURLConnection) url.openConnection(); | |
499 InputStream in = connection.getInputStream(); | |
500 byte[] actualOutput = new byte[dataLength * repeatCount]; | |
mmenke
2015/03/05 21:42:21
Suggest another test where we only have X body byt
xunjieli
2015/03/06 15:21:23
Is this the same as testInputStreamReadMoreBytesTh
mmenke
2015/03/06 16:42:16
Oops. Yea, it is.
| |
501 int totalBytesRead = 0; | |
502 // Number of bytes to read each time. It is incremented by one from 0. | |
503 int numBytesToRead = 0; | |
504 while (totalBytesRead < actualOutput.length) { | |
505 if (actualOutput.length - totalBytesRead < numBytesToRead) { | |
506 // Do not read out of bound. | |
507 numBytesToRead = actualOutput.length - totalBytesRead; | |
508 } | |
509 int bytesRead = in.read(actualOutput, totalBytesRead, numBytesToRead ); | |
510 assertTrue(bytesRead <= numBytesToRead); | |
511 totalBytesRead += bytesRead; | |
512 numBytesToRead++; | |
513 } | |
514 | |
515 // All data has been read. Try reading beyond what is available should g ive -1. | |
516 assertEquals(0, in.read(actualOutput, 0, 0)); | |
517 assertEquals(-1, in.read(actualOutput, 0, 1)); | |
518 | |
519 String responseData = new String(actualOutput); | |
520 for (int i = 0; i < repeatCount; ++i) { | |
521 assertEquals(data, responseData.substring(dataLength * i, | |
522 dataLength * (i + 1))); | |
523 } | |
524 // TODO(xunjieli): Enable after crbug.com/463720 is fixed. | |
525 // assertEquals(200, connection.getResponseCode()); | |
526 // assertEquals("OK", connection.getResponseMessage()); | |
527 } | |
528 | |
481 @SmallTest | 529 @SmallTest |
482 @Feature({"Cronet"}) | 530 @Feature({"Cronet"}) |
483 @CompareDefaultWithCronet | 531 @CompareDefaultWithCronet |
484 public void testInputStreamReadExactBytesAvailable() throws Exception { | 532 public void testInputStreamReadExactBytesAvailable() throws Exception { |
485 String testInputString = "this is a really long header"; | 533 String testInputString = "this is a really long header"; |
486 byte[] testInputBytes = testInputString.getBytes(); | 534 byte[] testInputBytes = testInputString.getBytes(); |
487 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo")); | 535 URL url = new URL(NativeTestServer.getEchoHeaderURL("foo")); |
488 HttpURLConnection urlConnection = | 536 HttpURLConnection urlConnection = |
489 (HttpURLConnection) url.openConnection(); | 537 (HttpURLConnection) url.openConnection(); |
490 urlConnection.addRequestProperty("foo", testInputString); | 538 urlConnection.addRequestProperty("foo", testInputString); |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
851 String headerName) { | 899 String headerName) { |
852 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); | 900 Pattern pattern = Pattern.compile(headerName + ":\\s(.*)\\r\\n"); |
853 Matcher matcher = pattern.matcher(allHeaders); | 901 Matcher matcher = pattern.matcher(allHeaders); |
854 List<String> headerValues = new ArrayList<String>(); | 902 List<String> headerValues = new ArrayList<String>(); |
855 while (matcher.find()) { | 903 while (matcher.find()) { |
856 headerValues.add(matcher.group(1)); | 904 headerValues.add(matcher.group(1)); |
857 } | 905 } |
858 return headerValues; | 906 return headerValues; |
859 } | 907 } |
860 } | 908 } |
OLD | NEW |