Your own web server for happy HTTP testing
Lightweight fake web apps for testing. Built using the civetweb embedded web server.
https://httpbin.org
API, so often you don’t need to write your own web app (e.g. if you are writing an HTTP client (httr, curl, crul).mw_json()
middleware, the response$send_json()
method and the httpbin_app()
app.tmpl_glue()
template engine.new_app_process()
and local_app_process
to work./brotli
endpoint of httpbin_app()
needs the brotli package./deflate
endpoint of httpbin_app()
needs the zip package./digest-auth
endpoint of httpbin_app()
needs the digest package.git_app()
requires the processx package.Install the release version from CRAN:
If you need the development version of the package, install it from GitHub:
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:
local_app_process()
helps you clean up the web server process after the test block, or test file. It is similar to the withr::local_*
functions.
app <- webfakes::new_app()
app$get("/hello/:user", function(req, res) {
res$send(paste0("Hello ", req$params$user, "!"))
})
web <- webfakes::local_app_process(app)
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()
:
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")
})
#> Test passed 😸
See https://webfakes.r-lib.org
webfakes focuses on testing, these packages are for writing real web apps:
Please note that the fs project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
MIT © RStudio