GithubHelp home page GithubHelp logo

isabella232 / webfakes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from r-lib/webfakes

0.0 0.0 0.0 1.78 MB

Fake web apps for HTTP testing R packages

Home Page: https://webfakes.r-lib.org

License: Other

Makefile 0.01% R 8.34% HTML 57.19% C 34.45% Handlebars 0.02%

webfakes's Introduction

webfakes

Your own web server for happy HTTP testing

R build status

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.

Installation

Once on CRAN, install the package as usual:

install.packages("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:

License

MIT © RStudio

webfakes's People

Contributors

gaborcsardi avatar maelle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.