如何在R中构建HTTP请求以进行测试?

时间:2018-04-03 20:15:12

标签: r api http testing request

我使用plumber在R中编写了一个小API。 (参见https://www.rplumber.io/)我想编写一个可以对源代码运行的测试,以验证我得到了预期的响应。我希望测试将HTTP请求对象传递给API的端点。用于构造请求的常用库似乎是httr。 (请参阅https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html),但我想要使用的request_build功能并未暴露在最外层。

相关的水管工装饰线是:

#* @post /endpoint
wrapper <- function(req) {

有没有办法访问包的内部功能? library(httr)并未授予我访问其内部request_build

的权限

或者,我正在查看库httptest,但我无法让它生成请求对象。 (请参阅https://cran.r-project.org/web/packages/httptest/vignettes/httptest.html)我发现它应该记录请求供以后使用,但 / p>

with_mock_api({ 
    a <- POST("http://127.0.0.1:9999/endpoint", body=list(raw="This is some sample text."), config=list("content-type"="application/x-www-form-urlencoded"))
    print(a)
})


Error: POST http://127.0.0.1:9999/endpoint list(raw = "This is some sample text.") (127.0.0.1-9999/endpoint-93904a-POST.json)

我不确定这里的问题是什么;邮差(https://www.getpostman.com/)确认API已启动并正常运行。

此外,是否可以记录多个请求并在测试期间指定我想要的?我目前正在使用testthat,但我还没有找到任何非常适合构建/模拟HTTP请求的功能。 (见http://r-pkgs.had.co.nz/tests.html

0 个答案:

没有答案