1

我尝试使用 Hoverfly 进行单元测试来模拟外部 API。

companion object {
    @ClassRule @JvmField
    val hoverflyRule: HoverflyRule = HoverflyRule.inSimulationMode(dsl(
            service("people.zoho.com")
                    .get("/people/api/forms/P_EmployeeView/records").queryParam("authtoken","TOKEN")
                        .willReturn(success("{test:test}", "application/json"))
    ))
}

当我将 Apache 客户端与 ktor 一起使用时,它不起作用。但是对于像 khttp 这样的另一个客户端,它可以工作。任何想法为什么?

4

1 回答 1

0

Apache您应该在配置 中设置默认系统代理:http: //hoverfly.readthedocs.io/projects/hoverfly-java/en/latest/pages/misc/misc.html

示例ktor(0.9.3-alpha-3)

class ApplicationMockupTest {
  companion object {
    @ClassRule
    @JvmField
    val hoverflyRule: HoverflyRule = HoverflyRule.inSimulationMode(
        dsl(
            service("people.zoho.com:443")
                .get("/people/api/forms/P_EmployeeView/records")
                .queryParam("authtoken", "TOKEN")
                .willReturn(success("{j:gr}", "application/json"))
        )
    )
  }

  @Test
  fun exampleTest() = runBlocking<Unit> {
    val client = HttpClient(Apache.setupDefaultProxy())
    val token = "TOKEN"
    val url = "https://people.zoho.com/people/api/forms/P_EmployeeView/records?authtoken=$token"
    val requestString = client.get<String>(url)
    hoverflyRule.verifyAll()
    Unit
  }

  fun HttpClientEngineFactory<ApacheEngineConfig>.setupDefaultProxy() = config {
    customizeClient {
        useSystemProperties()
    }
  }
}
于 2018-06-13T14:40:48.880 回答