mirror of
https://github.com/square/okhttp.git
synced 2025-11-23 06:42:24 +03:00
Run okhttp-android unit tests (#7865)
This commit is contained in:
@@ -24,7 +24,7 @@ import java.net.UnknownHostException
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import mockwebserver3.MockResponse
|
||||
import mockwebserver3.MockWebServer
|
||||
import mockwebserver3.junit5.internal.MockWebServerExtension
|
||||
import mockwebserver3.junit4.MockWebServerRule
|
||||
import okhttp3.AsyncDns
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
@@ -34,17 +34,17 @@ import okhttp3.tls.HeldCertificate
|
||||
import okio.IOException
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.fail
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.opentest4j.TestAbortedException
|
||||
import org.junit.AssumptionViolatedException
|
||||
import org.junit.Before
|
||||
import org.junit.Ignore
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Run with "./gradlew :android-test:connectedCheck -PandroidBuild=true" and make sure ANDROID_SDK_ROOT is set.
|
||||
*/
|
||||
@ExtendWith(MockWebServerExtension::class)
|
||||
class AndroidAsyncDnsTest {
|
||||
@JvmField @Rule val serverRule = MockWebServerRule()
|
||||
|
||||
private val localhost: HandshakeCertificates by lazy {
|
||||
// Generate a self-signed cert for the server to serve and the client to trust.
|
||||
@@ -63,20 +63,17 @@ class AndroidAsyncDnsTest {
|
||||
.sslSocketFactory(localhost.sslSocketFactory(), localhost.trustManager)
|
||||
.build()
|
||||
|
||||
private lateinit var server: MockWebServer
|
||||
|
||||
@BeforeEach
|
||||
fun init(server: MockWebServer) {
|
||||
this.server = server
|
||||
server.useHttps(localhost.sslSocketFactory())
|
||||
@Before
|
||||
fun init() {
|
||||
serverRule.server.useHttps(localhost.sslSocketFactory())
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("java.net.UnknownHostException: No results for localhost, in CI.")
|
||||
@Ignore("java.net.UnknownHostException: No results for localhost, in CI.")
|
||||
fun testRequest() {
|
||||
server.enqueue(MockResponse())
|
||||
serverRule.server.enqueue(MockResponse())
|
||||
|
||||
val call = client.newCall(Request(server.url("/")))
|
||||
val call = client.newCall(Request(serverRule.server.url("/")))
|
||||
|
||||
call.execute().use { response ->
|
||||
assertThat(response.code).isEqualTo(200)
|
||||
@@ -107,7 +104,7 @@ class AndroidAsyncDnsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("No results on CI for localhost")
|
||||
@Ignore("No results on CI for localhost")
|
||||
fun testDnsRequest() {
|
||||
val (allAddresses, exception) = dnsQuery("localhost")
|
||||
|
||||
@@ -165,7 +162,7 @@ class AndroidAsyncDnsTest {
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
val network =
|
||||
connectivityManager.activeNetwork ?: throw TestAbortedException("No active network")
|
||||
connectivityManager.activeNetwork ?: throw AssumptionViolatedException("No active network")
|
||||
|
||||
val client = OkHttpClient.Builder()
|
||||
.dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6))
|
||||
@@ -184,7 +181,7 @@ class AndroidAsyncDnsTest {
|
||||
try {
|
||||
InetAddress.getByName("www.google.com")
|
||||
} catch (uhe: UnknownHostException) {
|
||||
throw TestAbortedException(uhe.message, uhe)
|
||||
throw AssumptionViolatedException(uhe.message, uhe)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import okhttp3.CacheControl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.android.OkHttpClientContext.okHttpClient
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.AssumptionViolatedException
|
||||
import org.junit.Before
|
||||
@@ -31,17 +30,17 @@ import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.GraphicsMode
|
||||
import java.net.InetAddress
|
||||
import java.net.UnknownHostException
|
||||
import okhttp3.Cache
|
||||
import okio.Path.Companion.toPath
|
||||
import okio.fakefilesystem.FakeFileSystem
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(
|
||||
sdk = [30],
|
||||
qualifiers = "w221dp-h221dp-small-notlong-round-watch-xhdpi-keyshidden-nonav"
|
||||
)
|
||||
@GraphicsMode(GraphicsMode.Mode.NATIVE)
|
||||
class RobolectricClientBuilderTest {
|
||||
class RobolectricOkHttpClientTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var client: OkHttpClient
|
||||
@@ -49,7 +48,9 @@ class RobolectricClientBuilderTest {
|
||||
@Before
|
||||
fun setUp() {
|
||||
context = ApplicationProvider.getApplicationContext<Application>()
|
||||
client = context.okHttpClient
|
||||
client = OkHttpClient.Builder()
|
||||
.cache(Cache("/cache".toPath(), 10_000_000, FakeFileSystem()))
|
||||
.build()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,21 +60,20 @@ class RobolectricClientBuilderTest {
|
||||
val request = Request("https://www.google.com/robots.txt".toHttpUrl())
|
||||
|
||||
val networkRequest = request.newBuilder()
|
||||
.cacheControl(CacheControl.FORCE_NETWORK)
|
||||
.build()
|
||||
|
||||
val call = client.newCall(networkRequest)
|
||||
|
||||
call.execute().use { response ->
|
||||
assertThat(response.code).isEqualTo(200)
|
||||
assertThat(response.networkResponse).isNull()
|
||||
assertThat(response.cacheResponse).isNull()
|
||||
}
|
||||
|
||||
val cachedCall = client.newCall(request)
|
||||
|
||||
cachedCall.execute().use { response ->
|
||||
assertThat(response.code).isEqualTo(200)
|
||||
assertThat(response.networkResponse).isNotNull()
|
||||
assertThat(response.cacheResponse).isNotNull()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user