Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mock

Mocking HTTP requests


Description

Mocking HTTP requests

Usage

mock(on = TRUE)

Arguments

on

(logical) turn mocking on with TRUE or turn off with FALSE. By default is FALSE

Details

webmockr package required for mocking behavior

Examples

## Not run: 

if (interactive()) {
  # load webmockr
  library(webmockr)
  library(crul)

  URL <- "https://httpbin.org"

  # turn on mocking
  crul::mock()

  # stub a request
  stub_request("get", file.path(URL, "get"))
  webmockr:::webmockr_stub_registry

  # create an HTTP client
  (x <- HttpClient$new(url = URL))

  # make a request - matches stub - no real request made
  x$get('get')

  # allow net connect
  webmockr::webmockr_allow_net_connect()
  x$get('get', query = list(foo = "bar"))
  webmockr::webmockr_disable_net_connect()
  x$get('get', query = list(foo = "bar"))
}


## End(Not run)

crul

HTTP Client

v1.1.0
MIT + file LICENSE
Authors
Scott Chamberlain [aut, cre] (<https://orcid.org/0000-0003-1444-9135>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.