Hoverfly Ktor客户端Apache Kotlin

时间:2018-06-13 11:02:59

标签: kotlin ktor hoverfly

我尝试用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这样的另一个客户端,它可以工作。有什么想法吗?

1 个答案:

答案 0 :(得分: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()
    }
  }
}