• 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 org.junit.Assert;
8 
9 import java.io.ByteArrayOutputStream;
10 import java.io.InputStream;
11 import java.net.HttpURLConnection;
12 
13 /**
14  * Helper functions and fields used in Cronet's HttpURLConnection tests.
15  */
16 public class TestUtil {
17     static final String UPLOAD_DATA_STRING = "Nifty upload data!";
18     static final byte[] UPLOAD_DATA = UPLOAD_DATA_STRING.getBytes();
19     static final int REPEAT_COUNT = 100000;
20 
21     /**
22      * Helper method to extract response body as a string for testing.
23      */
getResponseAsString(HttpURLConnection connection)24     static String getResponseAsString(HttpURLConnection connection) throws Exception {
25         InputStream in = connection.getInputStream();
26         ByteArrayOutputStream out = new ByteArrayOutputStream();
27         int b;
28         while ((b = in.read()) != -1) {
29             out.write(b);
30         }
31         return out.toString();
32     }
33 
34     /**
35      * Produces a byte array that contains {@code REPEAT_COUNT} of
36      * {@code UPLOAD_DATA_STRING}.
37      */
getLargeData()38     static byte[] getLargeData() {
39         byte[] largeData = new byte[REPEAT_COUNT * UPLOAD_DATA.length];
40         for (int i = 0; i < REPEAT_COUNT; i++) {
41             System.arraycopy(UPLOAD_DATA, 0, largeData, i * UPLOAD_DATA.length, UPLOAD_DATA.length);
42         }
43         return largeData;
44     }
45 
46     /**
47      * Helper function to check whether {@code data} is a concatenation of
48      * {@code REPEAT_COUNT} {@code UPLOAD_DATA_STRING} strings.
49      */
checkLargeData(String data)50     static void checkLargeData(String data) {
51         for (int i = 0; i < REPEAT_COUNT; i++) {
52             Assert.assertEquals(UPLOAD_DATA_STRING, data.substring(UPLOAD_DATA_STRING.length() * i,
53                                                             UPLOAD_DATA_STRING.length() * (i + 1)));
54         }
55     }
56 }
57