From 29b208bdccc6812eadaa3f0bf3b1b038fa146f5f Mon Sep 17 00:00:00 2001 From: Chidera Olibie Date: Wed, 7 Dec 2022 13:30:21 +0000 Subject: [PATCH] Cronet: use testServer for requests Also shutdown the cronetEngine and testServer after each run. Test: atest CronetApiTest Bug: 251012627 Bug: 260358904 Change-Id: I516be64c9f1a9d4cfb06e40d3eab5c7178d6a44e Former-commit-id: a5f1a270692e99902f8912e6f9cf567933de27c8 --- .../net/http/cts/CronetUrlRequestTest.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) 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); }