Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fake web apps for HTTP testing R packages

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
NotificationsYou must be signed in to change notification settings

r-lib/webfakes

Repository files navigation

Your own web server for happy HTTP testing

R build statusCRAN statusR-CMD-checkCodecov test coverage

Lightweight fake web apps for testing. Built using thecivetweb 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 tothehttps://httpbin.org API, so often you don’t need to write yourown 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 templateengine.
  • 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 toanother 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 fromrequests to the fake app.
  • After a web app is launched from R, you can interact with it from Rbut 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 localfirewalls.
  • Multi-threaded web server supports concurrent HTTP requests.
  • Limit download speed to simulate low bandwidth.

Optional dependencies

  • The jsonlite package is needed for themw_json() middleware, theresponse$send_json() method and thehttpbin_app() app.
  • The glue package is needed for thetmpl_glue() template engine.
  • The callr package is needed fornew_app_process() andlocal_app_process to work.
  • The/brotli endpoint ofhttpbin_app() needs the brotli package.
  • The/deflate endpoint ofhttpbin_app() needs the zip package.
  • The/digest-auth endpoint ofhttpbin_app() needs the digestpackage.
  • 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 fromGitHub:

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

Usage

Start a web app at the beginning of your tests or test file, and stop itafter. Here is an example with the testthat package. Suppose you want totest that yourget_hello() function can query an API:

local_app_process() helps you clean up the web server process afterthe test block, or test file. It is similar to thewithr::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 inhttpbin_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

Seehttps://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 webapps:

Code of Conduct

Please note that the webfakes project is released with aContributor Code ofConduct. Bycontributing to this project, you agree to abide by its terms.

License

MIT © RStudio

About

Fake web apps for HTTP testing R packages

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp