Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix requester usage #70

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# httptest 4.1.0.9000

* `with_mock_dir()` now uses the requester set by `set_requester()` (#70, @maelle).

# httptest 4.1.0

* Function alias `with_mock_API`, deprecated in 3.0.0, has been removed.
Expand Down
2 changes: 1 addition & 1 deletion R/capture-requests.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ start_capturing <- function(path = NULL, simplify = TRUE) {
#' @importFrom jsonlite prettify
save_response <- function(response, simplify = TRUE) {
# Construct the mock file path
mock_file <- build_mock_url(response$request)
mock_file <- build_mock_url(get_current_requester()(response$request))
# Track separately the actual full path we're going to write to
dst_file <- file.path(.mockPaths()[1], mock_file)
mkdir_p(dst_file)
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-mock-dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,24 @@ public({
expect_identical(.mockPaths(), "/asdf")
})
})

test_that("with_mock_dir uses the set requester", {
set_requester(function (request) {
gsub_request(
request,
"http://httpbin.org/status/",
"api/",
fixed = TRUE
)
})
d <- tempfile()
dir.create(file.path(d, "tests", "testthat"), recursive = TRUE)
old <- setwd(d)
on.exit(setwd(old))

with_mock_dir("asdf", {
resp <- GET("http://httpbin.org/status/204")
expect_true(dir.exists(file.path("tests", "testthat", "asdf", "api")))
})
})
})