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:
install.packages("webfakes")
If you need the development version of the package, install it from GitHub:
::pak("r-lib/webfakes") pak
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.
<- webfakes::new_app()
app $get("/hello/:user", function(req, res) {
app$send(paste0("Hello ", req$params$user, "!"))
res
})<- webfakes::local_app_process(app)
web
test_that("can use hello API", {
<- web$url("/hello/Gabor")
url expect_equal(get_hello(url), "Hello Gabor!")
})
When testing HTTP clients you can often use the built in
httpbin_app()
:
<- webfakes::local_app_process(webfakes::httpbin_app()) httpbin
test_that("HTTP errors are caught", {
<- httpbin$url("/status/404")
url <- httr::GET(url)
resp 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 webfakes project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
MIT © RStudio