README.md

webfakes

Your own web server for happy HTTP testing

R build
status CRAN
status Codecov test
coverage R-CMD-check

Lightweight fake web apps for testing. Built using the civetweb embedded web server.

Features

Optional dependencies

Installation

Install the release version from CRAN:

install.packages("webfakes")

If you need the development version of the package, install it from GitHub:

pak::pak("r-lib/webfakes")

Usage

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():

httpbin <- webfakes::local_app_process(webfakes::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 😸

Documentation

See https://webfakes.r-lib.org

Links

Other solutions for HTTP testing in R:

R web application frameworks

webfakes focuses on testing, these packages are for writing real web apps:

Code of Conduct

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.

License

MIT © RStudio



Try the webfakes package in your browser

Any scripts or data that you put into this service are public.

webfakes documentation built on May 29, 2024, 2:24 a.m.