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: a5f1a27069
This commit is contained in:
Chidera Olibie 2022-12-07 13:30:21 +00:00
parent 992ed88a0d
commit 29b208bdcc
1 changed files with 15 additions and 14 deletions

View File

@ -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);
}