File: mocking.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 969 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#' Mocking HTTP requests
#'
#' @export
#' @param on (logical) turn mocking on with `TRUE` or turn off with `FALSE`.
#' By default is `FALSE`
#' @details `webmockr` package required for mocking behavior
#' @examples \dontrun{
#' 
#' 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"))
#' }
#' 
#' }
mock <- function(on = TRUE) {
  check_for_package("webmockr")
  crul_opts$mock <- on
}