[英]Test a header was added using Mockk
I am trying to write a test that checks if a header was added to a request when I call the intercept method on my custom interceptor class. I am getting a no answer found exception when I run the sendsNewContractHeaderToRequest test and am not sure how to modify my test so that it passes.当我在我的自定义拦截器 class 上调用拦截方法时,我正在尝试编写一个测试来检查是否将 header 添加到请求中。当我运行sendsNewContractHeaderToRequest测试时,我收到一个没有找到答案的异常,我不确定如何修改我的测试,以便它通过。 I think it's because I am not mockking the proceed call but I'm not sure how to do that in this situation.我认为这是因为我没有嘲笑继续调用,但我不确定在这种情况下该怎么做。
no answer found for: Chain(#1).proceed(Request{method=GET, url=http://somehost/somepath, headers=[foo:bar, Contract:new]}) io.mockk.MockKException: no answer found for: Chain(#1).proceed(Request{method=GET, url=http://somehost/somepath, headers=[foo:bar, Contract:new]})没有找到答案:Chain(#1).proceed(Request{method=GET, url=http://somehost/somepath, headers=[foo:bar, Contract:new]}) io.mockk.MockKException: 没有答案找到:Chain(#1).proceed(Request{method=GET, url=http://somehost/somepath, headers=[foo:bar, Contract:new]})
Here are my 2 classes:这是我的2节课:
class ContractInterceptor(private val contractType: ContractType) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = try {
chain.proceed(
chain.request()
.newBuilder()
.addHeader("Contract", contractType.headerValue)
.build()
)
} catch (ex: IOException) {
Response.Builder()
.protocol(Protocol.HTTP_2)
.request(chain.request())
.code(HttpURLConnection.HTTP_INTERNAL_ERROR)
.message(ex.localizedMessage)
.body("Internal Server Error".toResponseBody("application/json".toMediaType()))
.build()
}
enum class ContractType(val headerValue: String) {
NEW("new"), V2("v2"), V3("v3")
}}
And this is my test class这是我的测试 class
class ContractInterceptorTest {
private val mockChain: Interceptor.Chain = mockk {
every { request() } returns Request.Builder()
.url("http://somehost/somepath")
.addHeader("foo", "bar")
.build()
}
@Test
fun sendsNewContractHeaderToRequest() {
ContractInterceptor(ContractInterceptor.ContractType.NEW)
.intercept(mockChain)
val slot = slot<Request>()
verify { mockChain.proceed(capture(slot)) }
assertThat(slot.captured.headers.size == 2)
assertThat(slot.captured.headers["Contract"] == "new")
assertThat(slot.captured.headers["foo"] == "bar")
}
@Test
fun returns500WhenIOExceptionThrown() {
every { mockChain.proceed(any()) } throws IOException("An Exception!")
val response = ContractInterceptor(ContractInterceptor.ContractType.NEW)
.intercept(mockChain)
assertThat(response).isNotNull
assertThat(response.code).isEqualTo(HttpURLConnection.HTTP_INTERNAL_ERROR)
}
} }
it is necessary to make the mockking of the proceed method.有必要模拟 proceed 方法。
private val mockChain: Interceptor.Chain = mockk(relaxed = true) {
every { request() } returns Request.Builder()
.url("http://somehost/somepath")
.addHeader("foo", "bar")
.build()
every { proceed(any()) } returns Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("Test")
.body("abc".toResponseBody())
.build()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.