在Ktor中测试发布请求

Ktor(kotlin web框架)有一个很棒的可测试模式,可以在unit testing中包装http请求。 他们举了一个很好的例子来说明如何在这里测试一个GET端点,但是我在使用http POST时遇到了麻烦。

我试过,但后参数似乎并没有被添加到请求:

@Test fun testSomePostThing() = withTestApplication(Application::myModule) { with(handleRequest(HttpMethod.Post, "/api/v2/processing") { addHeader("content-type", "application/x-www-form-urlencoded") addHeader("Accept", "application/json") body = "param1=cool7&param2=awesome4" }) { assertEquals(HttpStatusCode.OK, response.status()) val resp = mapper.readValue(response.content ?: "") assertEquals(TriggerResponse("cool7", "awesome4", true), resp) } } 

有人有主意吗?

好的愚蠢的错误,我会张贴在这里,以防万一浪费时间;)unit testing实际上捕捉一个真正的问题(多数民众赞成他们是我猜)在我的路由我使用:

 install(Routing) { post("/api/v2/processing") { val params = call.parameters ... } } 

但是,只适用于“获取”参数。 后参数需要:

 install(Routing) { post("/api/v2/processing") { val params = call.receive() ... } } 

call.parameters也适用于邮政路线。

 get("api/{country}") { val country = call.parameters["country"]!! ... } 

这会给你任何通过后在uri通过uri。

call.receive是一个请求的主体。