• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.urlconnection;
6 
7 import static com.google.common.truth.Truth.assertThat;
8 
9 import java.io.ByteArrayOutputStream;
10 import java.io.InputStream;
11 import java.net.HttpURLConnection;
12 
13 /** Helper functions and fields used in Cronet's HttpURLConnection tests. */
14 public class TestUtil {
15     static final String UPLOAD_DATA_STRING = "Nifty upload data!";
16     static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes();
17     static final int REPEAT_COUNT = 100000;
18 
19     /** Helper method to extract response body as a string for testing. */
getResponseAsString(HttpURLConnection connection)20     static String getResponseAsString(HttpURLConnection connection) throws Exception {
21         InputStream in = connection.getInputStream();
22         ByteArrayOutputStream out = new ByteArrayOutputStream();
23         int b;
24         while ((b = in.read()) != -1) {
25             out.write(b);
26         }
27         return out.toString();
28     }
29 
30     /**
31      * Produces a byte array that contains {@code REPEAT_COUNT} of
32      * {@code UPLOAD_DATA_STRING}.
33      */
getLargeData()34     static byte[] getLargeData() {
35         byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length];
36         for (int i = 0; i < REPEAT_COUNT; i++) {
37             System.arraycopy(UPLOAD_DATA, 0, largeData, i * UPLOAD_DATA.length, UPLOAD_DATA.length);
38         }
39         return largeData;
40     }
41 
42     /**
43      * Helper function to check whether {@code data} is a concatenation of
44      * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings.
45      */
checkLargeData(String data)46     static void checkLargeData(String data) {
47         for (int i = 0; i < REPEAT_COUNT; i++) {
48             assertThat(
49                             data.substring(
50                                     UPLOAD_DATA_STRING.length() * i,
51                                     UPLOAD_DATA_STRING.length() * (i + 1)))
52                     .isEqualTo(UPLOAD_DATA_STRING);
53         }
54     }
55 }
56