diff --git a/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java index 55f8edde9f..2898f54817 100644 --- a/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java +++ b/Cronet/tests/cts/src/android/net/http/cts/CronetUrlRequestTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue; import android.content.Context; import android.net.ConnectivityManager; +import android.net.http.cts.util.CronetCtsTestServer; import android.net.http.cts.util.TestUrlRequestCallback; import android.net.http.cts.util.TestUrlRequestCallback.ResponseStep; @@ -32,20 +33,18 @@ import androidx.test.runner.AndroidJUnit4; import org.chromium.net.CronetEngine; import org.chromium.net.UrlRequest; import org.chromium.net.UrlResponseInfo; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import java.util.Random; - @RunWith(AndroidJUnit4.class) public class CronetUrlRequestTest { private static final String TAG = CronetUrlRequestTest.class.getSimpleName(); - private static final String HTTPS_PREFIX = "https://"; - private final String[] mTestDomains = {"www.google.com", "www.android.com"}; @NonNull private CronetEngine mCronetEngine; @NonNull private ConnectivityManager mCm; + @NonNull private CronetCtsTestServer mTestServer; @Before public void setUp() throws Exception { @@ -57,6 +56,13 @@ public class CronetUrlRequestTest { // .enableBrotli(true) .enableQuic(true); mCronetEngine = builder.build(); + mTestServer = new CronetCtsTestServer(context); + } + + @After + public void tearDown() throws Exception { + mCronetEngine.shutdown(); + mTestServer.shutdown(); } private static void assertGreaterThan(String msg, int first, int second) { @@ -67,25 +73,20 @@ public class CronetUrlRequestTest { assertNotNull("This test requires a working Internet connection", mCm.getActiveNetwork()); } - private String getRandomDomain() { - int index = (new Random()).nextInt(mTestDomains.length); - return mTestDomains[index]; - } - @Test public void testUrlRequestGet_CompletesSuccessfully() throws Exception { assertHasTestableNetworks(); - String url = HTTPS_PREFIX + getRandomDomain(); + String url = mTestServer.getSuccessUrl(); TestUrlRequestCallback callback = new TestUrlRequestCallback(); - UrlRequest.Builder builder = mCronetEngine.newUrlRequestBuilder(url, callback, - callback.getExecutor()); + UrlRequest.Builder builder = + mCronetEngine.newUrlRequestBuilder(url, callback, callback.getExecutor()); builder.build().start(); callback.expectCallback(ResponseStep.ON_SUCCEEDED); UrlResponseInfo info = callback.mResponseInfo; - assertEquals("Unexpected http status code from " + url + ".", 200, - info.getHttpStatusCode()); + assertEquals( + "Unexpected http status code from " + url + ".", 200, info.getHttpStatusCode()); assertGreaterThan( "Received byte from " + url + " is 0.", (int) info.getReceivedByteCount(), 0); }