MyNixOS website logo
Description

Fake Web Apps for HTTP Testing.

Create a web app that makes it easier to test web clients without using the internet. It includes a web app framework with path matching, parameters and templates. Can parse various 'HTTP' request bodies. Can send 'JSON' data or files from the disk. Includes a web app that implements the 'httpbin.org' web service.

webfakes

Your own web server for happy HTTP testing

R buildstatus CRANstatus Codecov testcoverage R-CMD-check

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

Features

  • Complete web app framework, define handlers for HTTP requests in R.
  • Write your own app for your custom test cases; our use app similar to the 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).
  • Run one web app per test suite, per test file or per test case.
  • Flexible path matching, with parameters and regular expressions.
  • Built in templating system using glue or bring your own template engine.
  • Middleware to parse JSON, multipart and URL encoded request bodies.
  • A web app is just an R object. It can be saved to disk, copied to another R process, etc.
  • A web app is extensible, by adding new routes and middleware to it.
  • Helper functions for sending JSON, files from disk, etc.
  • App-specific environment to store any data including data from requests to the fake app.
  • After a web app is launched from R, you can interact with it from R but also from the command line, your browser, etc. Nice for debugging.
  • The web server runs in the R process, so it has no problems with local firewalls.
  • Multi-threaded web server supports concurrent HTTP requests.
  • Limit download speed to simulate low bandwidth.

Optional dependencies

  • The jsonlite package is needed for the mw_json() middleware, the response$send_json() method and the httpbin_app() app.
  • The glue package is needed for the tmpl_glue() template engine.
  • The callr package is needed for new_app_process() and local_app_process to work.
  • The /brotli endpoint of httpbin_app() needs the brotli package.
  • The /deflate endpoint of httpbin_app() needs the zip package.
  • The /digest-auth endpoint of httpbin_app() needs the digest package.
  • git_app() requires the processx package.

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.

Metadata

Version

1.3.1

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows