Your own web server for happy HTTP testing
Lightweight web apps for testing. Built using the civetweb embedded web server.
mw_json()
middleware, the response$send_json()
method and the httpbin_app()
app.tmpl_glue()
template engine.new_app_process()
to work.Once on CRAN, install the package as usual:
Start a web app at the beginning of your tests or test file, and stop it after. Here is an example with the testthat package. Suppose you want to test that your get_hello()
function can query an API:
web <- setup({
app <- presser::new_app()
app$get("/hello/:user", function(req, res) {
res$send(paste0("Hello ", req$params$user, "!"))
})
presser::new_app_process(app)
})
teardown(web$stop())
test_that("can use hello API", {
url <- web$url("/hello/Gabor")
expect_equal(get_hello(url), "Hello Gabor!")
})
When testing HTTP clients you can often use the built in httpbin_app()
:
httpbin <- setup(presser::new_app_process(presser::httpbin_app()))
teardown(httpbin$stop())
test_that("HTTP errors are caught", {
url <- httpbin$url("/status/404")
resp <- httr::GET(url)
expect_error(httr::stop_for_status(resp), class = "http_404")
})
See https://r-lib.github.io/presser/
MIT © RStudio