@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()); }
public HttpUrlConnectionUrlRequestFactory(Context context, CronetEngine.Builder config) { mContext = context; String userAgent = config.userAgent(); if (userAgent.isEmpty()) { userAgent = UserAgent.from(mContext); } mDefaultUserAgent = userAgent; }
@UsedByReflection("HttpUrlRequestFactory.java") public ChromiumUrlRequestFactory(Context context, CronetEngine.Builder config) { if (isEnabled()) { String userAgent = config.getUserAgent(); if (userAgent.isEmpty()) { // Cannot use config.getDefaultUserAgent() as config.mContext may be null. userAgent = new CronetEngine.Builder(context).getDefaultUserAgent(); } mRequestContext = new ChromiumUrlRequestContext(context, userAgent, config); } }
@UsedByReflection public ChromiumUrlRequestFactory(Context paramContext, CronetEngine.Builder paramBuilder) { if (a()) { String str2 = paramBuilder.b(); String str1 = str2; if (str2 == null) { str1 = new CronetEngine.Builder(paramContext).a(); } a = new ChromiumUrlRequestContext(paramContext, str1, paramBuilder); } }
@SmallTest @Feature({"Cronet"}) public void testConfigUserAgent() throws Exception { String userAgentName = "User-Agent"; String userAgentValue = "User-Agent-Value"; CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(mActivity); cronetEngineBuilder.setUserAgent(userAgentValue); cronetEngineBuilder.setLibraryName("cronet_tests"); String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, cronetEngineBuilder.toString()}; mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, commandLineArgs); assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext())); TestUrlRequestListener listener = new TestUrlRequestListener(); UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( NativeTestServer.getEchoHeaderURL(userAgentName), listener, listener.getExecutor(), mActivity.mCronetEngine); urlRequestBuilder.build().start(); listener.blockForDone(); assertEquals(userAgentValue, listener.mResponseAsString); }