In this context, HTTP requests attempt to load API response fixtures from files. This allows test code to proceed evaluating code that expects HTTP requests to return meaningful responses. Requests that do not have a corresponding fixture file raise errors, like how without_internet() does.

with_mock_api(expr)

use_mock_api()

stop_mocking()

Arguments

expr

Code to run inside the mock context

Value

with_mock_api() returns the result of expr. use_mock_api() and stop_mocking() return nothing.

Details

use_mock_api() and stop_mocking() allow you to turn on/off request mocking for more convenient use in an interactive session.

Requests are translated to mock file paths according to several rules that incorporate the request method, URL, query parameters, and body. See build_mock_url() for details.

File paths for API fixture files may be relative to the 'tests/testthat' directory, i.e. relative to the .R test files themselves. This is the default location for storing and retrieving mocks, but you can put them anywhere you want as long as you set the appropriate location with .mockPaths().

Examples

library(httr2)
with_mock_api({
  # There are no mocks recorded in this example, so catch this request with
  # expect_GET()
  expect_GET(
    request("https://cran.r-project.org") %>% req_perform(),
    "https://cran.r-project.org"
  )
  # For examples with mocks, see the tests and vignettes
})
#> <error/httptest2_request>
#> Error in `stop_request()`:
#> ! An unexpected request was made:
#> GET https://cran.r-project.org
#> Expected mock file: cran.r-project.org.*
#> ---
#> Backtrace:
#>      
#>   1. ├─pkgdown::deploy_to_branch(new_process = FALSE)
#>   2. └─pkgdown::build_site_github_pages(pkg, ..., clean = clean)
#>   3. └─pkgdown::build_site(...)
#>   4. └─pkgdown:::build_site_local(...)
#>   5. └─pkgdown::build_reference(...)
#>   6. ├─pkgdown:::unwrap_purrr_error(...)
#>   7. │ └─base::withCallingHandlers(...)
#>   8. └─purrr::map(...)
#>   9. └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  10. ├─purrr:::with_indexed_errors(...)
#>  11. │ └─base::withCallingHandlers(...)
#>  12. ├─purrr:::call_with_cleanup(...)
#>  13. └─pkgdown (local) .f(.x[[i]], ...)
#>  14. ├─base::withCallingHandlers(...)
#>  15. └─pkgdown:::data_reference_topic(...)
#>  16. └─pkgdown:::run_examples(...)
#>  17. └─pkgdown:::highlight_examples(code, topic, env = env)
#>  18. └─downlit::evaluate_and_highlight(...)
#>  19. └─evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>  20. └─evaluate:::evaluate_call(...)
#>  21. ├─evaluate (local) timing_fn(...)
#>  22. ├─evaluate (local) handle(...)
#>  23. │ └─base::try(f, silent = TRUE)
#>  24. │   └─base::tryCatch(...)
#>  25. │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  26. │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  27. │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  28. ├─base::withCallingHandlers(...)
#>  29. ├─base::withVisible(...)
#>  30. └─evaluate:::eval_with_user_handlers(expr, envir, enclos, user_handlers)
#>  31. └─base::eval(expr, envir, enclos)
#>  32. └─base::eval(expr, envir, enclos)
#>  33. ├─httptest2::with_mock_api(...)
#>  34. │ └─base::eval.parent(expr)
#>  35. │   └─base::eval(expr, p)
#>  36. ├─httptest2::expect_GET(...)
#>  37. │ └─httptest2:::expect_request(object, "GET ", url, " ", ...)
#>  38. │   ├─base::withCallingHandlers(...)
#>  39. │   └─testthat::expect_error(...)
#>  40. │     └─testthat:::quasi_capture(...)
#>  41. │       ├─testthat (local) .capture(...)
#>  42. │       │ └─base::withCallingHandlers(...)
#>  43. │       └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
#>  44. └─request("https://cran.r-project.org") %>% req_perform()
#>  45. └─httr2::req_perform(.)
#>  46.   └─httptest2 (local) mock(req)
#>  47.     └─httptest2:::stop_request(req)