【问题标题】:Hoverfly Ktor client Apache KotlinHoverfly Ktor 客户端 Apache Kotlin
【发布时间】:2018-11-22 22:32:53
【问题描述】:

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

【问题讨论】:

  • 这是什么行为?它抛出异常?您是否有堆栈跟踪或可以提供其他信息?

标签: kotlin ktor hoverfly


【解决方案1】:

您应该在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()
    }
  }
}

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 2020-06-23
    • 1970-01-01
    • 2020-11-01
    • 2022-10-24
    • 2021-05-03
    • 2020-02-25
    • 2020-04-15
    • 2019-12-07
    相关资源
    最近更新 更多