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. └─purrr::map(...)
#>   7. └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   8. ├─purrr:::with_indexed_errors(...)
#>   9. │ └─base::withCallingHandlers(...)
#>  10. ├─purrr:::call_with_cleanup(...)
#>  11. └─pkgdown (local) .f(.x[[i]], ...)
#>  12. ├─base::withCallingHandlers(...)
#>  13. └─pkgdown:::data_reference_topic(...)
#>  14. └─pkgdown:::run_examples(...)
#>  15. └─pkgdown:::highlight_examples(code, topic, env = env)
#>  16. └─downlit::evaluate_and_highlight(...)
#>  17. └─evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>  18. └─evaluate:::evaluate_call(...)
#>  19. ├─evaluate (local) timing_fn(...)
#>  20. ├─evaluate (local) handle(...)
#>  21. │ └─base::try(f, silent = TRUE)
#>  22. │   └─base::tryCatch(...)
#>  23. │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  24. │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  25. │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  26. ├─base::withCallingHandlers(...)
#>  27. ├─base::withVisible(...)
#>  28. └─evaluate:::eval_with_user_handlers(expr, envir, enclos, user_handlers)
#>  29. └─base::eval(expr, envir, enclos)
#>  30. └─base::eval(expr, envir, enclos)
#>  31. ├─httptest2::with_mock_api(...)
#>  32. │ └─base::eval.parent(expr)
#>  33. │   └─base::eval(expr, p)
#>  34. ├─httptest2::expect_GET(...)
#>  35. │ └─httptest2:::expect_request(object, "GET ", url, " ", ...)
#>  36. │   ├─base::withCallingHandlers(...)
#>  37. │   └─testthat::expect_error(...)
#>  38. │     └─testthat:::quasi_capture(...)
#>  39. │       ├─testthat (local) .capture(...)
#>  40. │       │ └─base::withCallingHandlers(...)
#>  41. │       └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
#>  42. └─request("https://cran.r-project.org") %>% req_perform()
#>  43. └─httr2::req_perform(.)
#>  44.   └─httptest2 (local) mock(req)
#>  45.     └─httptest2:::stop_request(req)