Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/TestUtil.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/TestUtil.java b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/TestUtil.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8dbec6d281b9ef3ed83d582f1d99f4feb7d1d1f |
| --- /dev/null |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/net/urlconnection/TestUtil.java |
| @@ -0,0 +1,58 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net.urlconnection; |
| + |
| +import junit.framework.Assert; |
| + |
| +import java.io.ByteArrayOutputStream; |
| +import java.io.InputStream; |
| +import java.net.HttpURLConnection; |
| + |
| +/** |
| + * Helper functions and fields used in Cronet's HttpURLConnection tests. |
| + */ |
| +public class TestUtil { |
| + static final String UPLOAD_DATA_STRING = "Nifty upload data!"; |
| + static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes(); |
| + static final int REPEAT_COUNT = 100000; |
| + |
| + /** |
| + * Helper method to extract response body as a string for testing. |
| + */ |
| + static String getResponseAsString(HttpURLConnection connection) throws Exception { |
| + InputStream in = connection.getInputStream(); |
| + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| + int b; |
| + while ((b = in.read()) != -1) { |
|
pauljensen
2015/12/07 15:20:08
nit: I've heard you can do this without looping wi
xunjieli
2015/12/07 15:53:12
I tried using a wrapper before (to read line by li
pauljensen
2015/12/07 16:07:52
sgtm
|
| + out.write(b); |
| + } |
| + return out.toString(); |
| + } |
| + |
| + /** |
| + * Produces a byte array that contains {@code REPEAT_COUNT} of |
| + * {@code UPLOAD_DATA_STRING}. |
| + */ |
| + static 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++) { |
|
pauljensen
2015/12/07 15:20:08
nit: might be good to replace this inner loop with
xunjieli
2015/12/07 15:53:12
Done.
|
| + 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. |
| + */ |
| + static void checkLargeData(String data) { |
| + for (int i = 0; i < REPEAT_COUNT; i++) { |
| + Assert.assertEquals(UPLOAD_DATA_STRING, data.substring(UPLOAD_DATA_STRING.length() * i, |
| + UPLOAD_DATA_STRING.length() * (i + 1))); |
| + } |
| + } |
| +} |