コード例 #1
0
  @SmallTest
  @Feature({"Cronet"})
  public void testDataReductionProxyEnabled() throws Exception {
    mActivity = launchCronetTestAppAndSkipFactoryInit();

    // Ensure native code is loaded before trying to start test server.
    new CronetEngine.Builder(getInstrumentation().getTargetContext())
        .setLibraryName("cronet_tests")
        .build()
        .shutdown();

    assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext()));
    if (!NativeTestServer.isDataReductionProxySupported()) {
      return;
    }
    String serverHostPort = NativeTestServer.getHostPort();

    // Enable the Data Reduction Proxy and configure it to use the test
    // server as its primary proxy, and to check successfully that this
    // proxy is OK to use.
    CronetEngine.Builder cronetEngineBuilder =
        new CronetEngine.Builder(getInstrumentation().getTargetContext());
    cronetEngineBuilder.enableDataReductionProxy("test-key");
    cronetEngineBuilder.setDataReductionProxyOptions(
        serverHostPort,
        "unused.net:9999",
        NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
    cronetEngineBuilder.setLibraryName("cronet_tests");
    mActivity.mCronetEngine = cronetEngineBuilder.build();
    TestUrlRequestListener listener = new TestUrlRequestListener();

    // Construct and start a request that can only be returned by the test
    // server. This request will fail if the configuration logic for the
    // Data Reduction Proxy is not used.
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(
            "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
            listener,
            listener.getExecutor(),
            mActivity.mCronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();

    // Verify that the request is successful and that the Data Reduction
    // Proxy logic configured to use the test server as its proxy.
    assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
    assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer());
    assertEquals(
        "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
        listener.mResponseInfo.getUrl());
  }