Skip to content

Commit

Permalink
Fixes for dev httr2 (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Neal Richardson <neal.p.richardson@gmail.com>
  • Loading branch information
hadley and nealrichardson authored Nov 7, 2023
1 parent aec00de commit 48d3b6b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
^pkgdown$
^pkgdown.sh$
^\.github$
^httptest2\.Rproj$
2 changes: 2 additions & 0 deletions R/capture-requests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion R/expect-request-header.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions R/mock-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion R/without-internet.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion tests/testthat/test-capture-requests.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-mock-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 48d3b6b

Please sign in to comment.