diff --git a/.Rbuildignore b/.Rbuildignore index 1961791..d0dcb57 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -13,3 +13,4 @@ ^pkgdown$ ^pkgdown.sh$ ^\.github$ +^httptest2\.Rproj$ diff --git a/R/capture-requests.R b/R/capture-requests.R index 2b2f32f..82e2953 100644 --- a/R/capture-requests.R +++ b/R/capture-requests.R @@ -164,6 +164,8 @@ save_response <- function(response, file, simplify = TRUE) { class = "httr2_path" )) } + # Needed for httr2 1.0.0 + response$cache <- quote(new.env(parent = emptyenv())) f <- file(dst_file, "wb", encoding = "UTF-8") on.exit(close(f)) diff --git a/R/expect-request-header.R b/R/expect-request-header.R index 1cc5965..8c9e055 100644 --- a/R/expect-request-header.R +++ b/R/expect-request-header.R @@ -82,5 +82,13 @@ expect_request_header <- function(expr, } } - httr2::with_mock(header_mocker, expr) + with_mock(header_mocker, expr) +} + +with_mock <- function(mock, code) { + if (httr2_1.0.0) { + getFromNamespace("with_mocked_responses", "httr2")(mock, code) + } else { + httr2::with_mock(mock, code) + } } diff --git a/R/mock-api.R b/R/mock-api.R index ac459ea..213601c 100644 --- a/R/mock-api.R +++ b/R/mock-api.R @@ -138,6 +138,9 @@ load_response <- function(file, req) { } adapt_httr_response <- function(resp) { + # Restore httr2 1.0.0 cache + resp$cache <- new.env(parent = emptyenv()) + if (inherits(resp, "httr2_response")) { return(resp) } diff --git a/R/without-internet.R b/R/without-internet.R index 1e2e0a1..8007ab8 100644 --- a/R/without-internet.R +++ b/R/without-internet.R @@ -32,7 +32,7 @@ #' ) #' }) #' @export -without_internet <- function(expr) httr2::with_mock(stop_request, expr) +without_internet <- function(expr) with_mock(stop_request, expr) #' @rdname without_internet #' @export diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..40ef252 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,6 @@ +httr2_1.0.0 <- NULL + +.onLoad <- function(...) { + # packageVersion() can be slow, so let's only do this check once + httr2_1.0.0 <<- utils::packageVersion("httr2") > "0.2.3" +} diff --git a/tests/testthat/test-capture-requests.R b/tests/testthat/test-capture-requests.R index 4a876a2..3b94419 100644 --- a/tests/testthat/test-capture-requests.R +++ b/tests/testthat/test-capture-requests.R @@ -222,7 +222,7 @@ with_mock_api({ test_that("If the httr2 request function exits with an error, capture_requests warns", { capture_requests({ - httr2::with_mock( + with_mock( function(req) stop("Error!"), expect_warning( expect_error( diff --git a/tests/testthat/test-mock-api.R b/tests/testthat/test-mock-api.R index 7d46b00..ba7edaf 100644 --- a/tests/testthat/test-mock-api.R +++ b/tests/testthat/test-mock-api.R @@ -25,7 +25,7 @@ with_mock_api({ request("api/NOTAFILE/") %>% req_url_query(a = 1) %>% req_perform(), - "api/NOTAFILE/?a=1" + if (httr2_1.0.0) "/api/NOTAFILE/?a=1" else "api/NOTAFILE/?a=1" ) }) test_that("POST method reads from correct file", {