gpt4 book ai didi

Android 仪器测试 - 使用 OkHttp MockServer.useHttps(...) 时出现 SSLHandshakeException

转载 作者:行者123 更新时间:2023-12-04 08:56:52 25 4
gpt4 key购买 nike

我正在尝试使用 OkHttps 模拟服务器测试和 REST API。由于 anroid 的限制(在较新的 android 版本中不允许明文 http 请求),我需要用户 server.useHttps(...)。我尝试使用默认的 SSLSocketFactory

class RetrofitIntegrationTest {

private lateinit var server: MockWebServer
private lateinit var retrofit: Retrofit
private lateinit var service: InstanceApi

@Before
fun setUp() {
server = MockWebServer()
server.useHttps(SSLSocketFactory.getDefault() as SSLSocketFactory, false)
server.start()


retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(server.url("/"))
.build()

service = retrofit
.create(InstanceApi::class.java)
}

@After
fun teardown() {
server.shutdown()
}

@Test
fun testCompleteIntegration() = runBlocking {
server.enqueue(
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody(
"{ test: 'test' } "
)
)

Timber.i(retrofit.baseUrl().toString())
val response = service.apiInfo()

assertEquals(15, response.body()?.version)
}

运行仪器测试会抛出以下异常:

E/TestRunner: javax.net.ssl.SSLHandshakeException: connection closed
at com.android.org.conscrypt.SSLUtils.toSSLHandshakeException(SSLUtils.java:362)
at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:240)
at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:217)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)

我已经做了很多研究,但还没有找到任何类似的博客。感谢您提供任何建议或文档链接。

最佳答案

除了使用 server.useHttps,您还可以简单地尝试创建一个网络安全配置,将本地主机列入白名单并将其添加到您的应用中。

首先,创建一个 network_security_config.xml 并将其放入应用程序的 res/xml 文件夹中

<network-security-config>
<base-config cleartextTrafficPermitted="false" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">127.0.0.1</domain>
</domain-config>
</network-security-config>

然后将其添加到您应用的 list 中

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>

https://developer.android.com/training/articles/security-config

关于Android 仪器测试 - 使用 OkHttp MockServer.useHttps(...) 时出现 SSLHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63774769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com