presser

Your own web server for happy HTTP testing

R build status

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

Features

Optional dependencies

Installation

Once on CRAN, install the package as usual:

install.packages("presser")

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:

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")
})

Documentation

See https://r-lib.github.io/presser/

License

MIT © RStudio