问题标签 [mockwebserver]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
mockwebserver - MockWebServer 未完成
我使用MockWebServer
如下代码。但MockWebServer
没有完成。测试正在无限进行中。我错过了什么?
结果:
Jul 02, 2018 7:30:59 PM okhttp3.mockwebserver.MockWebServer$2 execute
정보: MockWebServer[49728] starting to accept connections
android - 使用自签名证书的 mockwebserver 握手失败
我正在寻找使用 android 应用程序使用带有 ssl 的 mockwebserver 编写 OkHttpClient 的集成测试。我能够使用 HeldCertificate 和 HandshakeCertificates 来实现这一点,但我对使用自签名证书很感兴趣。在尝试使用自签名证书时,我遇到了握手失败。以下是重现此内容的详细信息。
一个。使用此命令创建自签名证书
openssl req -x509 -out localhost.crt -keyout localhost.key -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
湾。转换为 der 用于 android。openssl x509 -in localhost.crt -outform der -out localhost.der.crt
C。使用此处给出的代码获取 SSLSocketFactory:https : //gist.github.com/erickok/7692592 以将其传递给 mockWebserver.useHttps() 和 OkHttpClientBuilder..sslSocketFactory()。
尝试发送和接收非常简单的请求时遇到握手失败。
I/MockWebServer(16935): MockWebServer[38515] connection from null failed: javax.net.ssl.SSLHandshakeException: Handshake failed
I/System.out(16935): TEMP: 7. Exception thrown javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xeef2cd80: Failure in SSL library, usually a protocol error
I/System.out(16935): error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xee9c4b80:0x00000001)
I/System.out(16935): error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xea1a6827:0x00000000) Handshake failed
W/System.err(16935): javax.net.ssl.SSLHandshakeException: Handshake failed
W/System.err(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:441)
W/System.err(16935): at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:337)
W/System.err(16935): at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:297)
W/System.err(16935): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:178)
W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:272)
W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
W/System.err(16935): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
W/System.err(16935): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err(16935): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err(16935): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err(16935): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err(16935): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
W/System.err(16935): at okhttp3.RealCall.execute(RealCall.java:77)
W/System.err(16935): at com.xyz.okhttp3_abc.sample.test.SampleUiTest.setup(SampleUiTest.java:180)
W/System.err(16935): at java.lang.reflect.Method.invoke(Native Method)
W/System.err(16935): at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
W/System.err(16935): at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
W/System.err(16935): at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
W/System.err(16935): at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
W/System.err(16935): at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
W/System.err(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
W/System.err(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:128)
W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:27)
W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:128)
W/System.err(16935): at org.junit.runners.Suite.runChild(Suite.java:27)
W/System.err(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
W/System.err(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
W/System.err(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
W/System.err(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
W/System.err(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
W/System.err(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
W/System.err(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
W/System.err(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
W/System.err(16935): at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
W/System.err(16935): at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
W/System.err(16935): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1959)
W/System.err(16935): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xeef2cd80: Failure in SSL library, usually a protocol error
W/System.err(16935): error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xee9c4b80:0x00000001)
W/System.err(16935): error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xea1a6827:0x00000000)
W/System.err(16935): at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
W/System.err(16935): ... 55 more
I/TestRunner(16935): failed: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest)
I/TestRunner(16935): failed: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest)
I/TestRunner(16935): ----- begin exception -----
I/TestRunner(16935): javax.net.ssl.SSLHandshakeException: Handshake failed
I/TestRunner(16935): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:441)
I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:337)
I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:297)
I/TestRunner(16935): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:178)
I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:272)
I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
I/TestRunner(16935): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
I/TestRunner(16935): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
I/TestRunner(16935): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
I/TestRunner(16935): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
I/TestRunner(16935): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
I/TestRunner(16935): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
I/TestRunner(16935): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
I/TestRunner(16935): at okhttp3.RealCall.execute(RealCall.java:77)
I/TestRunner(16935): at com.xyz.okhttp3_abc.sample.test.SampleUiTest.setup(SampleUiTest.java:180)
I/TestRunner(16935): at java.lang.reflect.Method.invoke(Native Method)
I/TestRunner(16935): at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
I/TestRunner(16935): at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
I/TestRunner(16935): at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
I/TestRunner(16935): at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
I/TestRunner(16935): at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
I/TestRunner(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
I/TestRunner(16935): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:128)
I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:27)
I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:128)
I/TestRunner(16935): at org.junit.runners.Suite.runChild(Suite.java:27)
I/TestRunner(16935): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
I/TestRunner(16935): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
I/TestRunner(16935): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
I/TestRunner(16935): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
I/TestRunner(16935): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
I/TestRunner(16935): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
I/TestRunner(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
I/TestRunner(16935): at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
I/TestRunner(16935): at android.support.test.internal.runner.TestExecutor.execute(TestExecutor
I/TestRunner(16935): ----- end exception -----
I/TestRunner(16935): finished: simpleTest[0](com.xyz.okhttp3_abc.sample.test.SampleUiTest)
I/MonitoringInstr(16935): Activities that are still in CREATED to STOPPED: 0
android - Android Espresso MockWebserver 设置 cookie
我试图在我的 Android Instrumentationtest 中设置 cookie,如下所示:
android 客户端是一个 org.apache.http.impl.client.DefaultHttpClient,它读取的 cookie 如下所示:
client.getCookieStore().getCookies();
但结果为空。如何为该客户端设置 cookie?
谢谢
扎梅克
java - OkHttp MockWebServer 无法接受 android P (API 28) Instrumentation Test 中的连接
当我将我的 sdk 版本从 API 26 更新到 API 28 时,OKHttp 模拟服务器无法在我的 android 仪器测试中的设备/模拟器中运行。它在旧版本中完美运行。我已经检查了网络配置
java - 用于间隔通量的模拟服务器?
我尝试为一个服务编写一个测试函数,该服务消耗来自另一个服务的间隔 Flux,创建如Flux.interval(Duration.ofSeconds(1)).map( ... )
.
如果看到Support of MockRestServiceServer for WebClient,这正是我所需要的。但是,目前推荐使用 OkHttp MockWebServer 代替。我现在的问题是:如何模拟提供间隔 Flux 的服务器响应?
MockResponsesetBody()
函数接受 String 和 Buffer。可能缓冲区是要走的路。但是,我不知道如何将其转换Flux
为InputStream
可以读入缓冲区的说。
unit-testing - 如何为 MockWebServer 设置默认响应?
MockWebServer是一个很棒的库,但是有一件事情令人惊讶地难以做到:设置默认响应。
具体来说:如果没有使用指定响应,我希望能够设置返回的响应server.enqueue(response)
。
我希望能够做类似的事情:
然后当我的测试调用服务器两次(或更多)时,第一个响应之后的每个响应都是okResponse
.
当测试变得更加复杂并且需要对服务器进行多次调用时,有时指定每个响应(通常是简单的 200 OK)是乏味的并且会污染测试。
有没有比创建自己的Dispatcher更简单的方法?正确创建它(支持多个响应)对于这么小的东西来说听起来有点矫枉过正。
android - 在 InstrumentedTest 上使用 SSL 和 MockWebServer
我想使用 MockWebServer 创建 Android InstrumentedTest 并启用 SSL。我使用以下方法创建了“BKS”证书:Portecle。
我使用以下代码来创建 SSLSocketFactory:
接着:
我不断收到以下异常:
知道出了什么问题吗?
android - 如何在存储库中初始化 okHttp 的 MockServer()?
Server.kt(是一个将返回用户列表的模拟服务器) https://github.com/shalbert94/androidinterview/blob/master/server/src/main/java/com/jobrapp/server/Server.kt
出于某种原因, val server = Server()
在我的存储库中调用(https://github.com/shalbert94/androidinterview/blob/master/app/src/main/java/com/jobrapp/androidinterview/MockServerRepository.kt)抛出:
Unable to start activity ComponentInfo{com.jobrapp.androidinterview/com.jobrapp.androidinterview.MainActivity}: android.os.NetworkOnMainThreadException
该问题似乎源于调用在'块中调用MockWebServer()
的 'start()
方法。如何在不触发 NetworkOnMainThreadException 的情况下在我的存储库中进行初始化?Server
init
Server
android - 试图用 MocWebSerevr 测试它得到了错误
任何人都知道错误是什么意思?或者也许错过了什么?
试图从这个样本开始,
添加了依赖项:testImplementation 'com.squareup.okhttp3:mockwebserver:3.11.0'
代码只是findMessage_receivesAScriptedResponseFromMockBackend
在 AndroidStudio 的示例单元测试中添加了一个 @Test:
运行测试时显示错误: