mock_download <- function(req) {
if (req$method == "PROPFIND") {
if (req$headers$Depth == 0) {
if (req$url == "https://cloud.example.com/test") {
httr2::response(status_code = 207,
header = list("Content-Type" = "application/xml"),
body = charToRaw('
/test/
HTTP/1.1 200 OK
'))
} else if (req$url != "https://cloud.example.com/missing") {
httr2::response(status_code = 207,
header = list("Content-Type" = "application/xml"),
body = charToRaw('
/file.txt
HTTP/1.1 200 OK
'))
} else {
httr2::response(status_code = 404)
}
} else {
if (req$url == "https://cloud.example.com/test") {
httr2::response(status_code = 207,
header = list("Content-Type" = "application/xml"),
body = charToRaw('
/test/
Wed, 28 Feb 2024 21:30:45 GMT
41
527309756657
"65dfa6056af07"
HTTP/1.1 200 OK
/test/a.csv
Wed, 28 Feb 2024 21:30:45 GMT
26
"433f89eafeada7a93501f5b2d0c05651"
text/csv
HTTP/1.1 200 OK
/test/file.txt
Wed, 28 Feb 2024 21:28:32 GMT
15
"ff938d81afc36821bb417c98a8e4f2eb"
text/plain
HTTP/1.1 200 OK
/test/tmp/
Wed, 28 Feb 2024 21:29:42 GMT
0
527309756657
"65dfa5c6d4cbe"
HTTP/1.1 200 OK
'))
} else if (req$url == "https://cloud.example.com/file.txt") {
httr2::response(status_code = 207,
header = list("Content-Type" = "application/xml"),
body = charToRaw('
/file.txt
Wed, 28 Feb 2024 21:17:51 GMT
15
"763a11253670561a3f03defbbb2f5921"
text/plain
HTTP/1.1 200 OK
'))
} else {
httr2::response(status_code = 404)
}
}
} else {
if (
req$url != "https://cloud.example.com/missing" &&
req$url != "https://cloud.example.com/notthere"
) {
httr2::response_json(status_code = 200, body = list(a = "123"))
} else {
httr2::response(status_code = 404)
}
}
}
test_that("download works", {
r <- httr2::request("https://cloud.example.com/")
local <- paste0(tempdir(), "/dl")
expect_equal(
httr2::with_mocked_responses(mock_download, wd_download(r, "test", local)),
paste0(local, c("/a.csv", "/file.txt", "/tmp"))
)
unlink(local, recursive = TRUE)
})
test_that("download missing warning works", {
r <- httr2::request("https://cloud.example.com/")
local <- paste0(tempdir(), "/dl")
expect_warning(
httr2::with_mocked_responses(
mock_download,
wd_download(r, "missing", local)
),
"Not Found"
)
unlink(local, recursive = TRUE)
})
test_that("download missing works", {
r <- httr2::request("https://cloud.example.com/")
local <- paste0(tempdir(), "/dl")
expect_equal(
httr2::with_mocked_responses(
mock_download,
suppressWarnings(wd_download(r, "missing", local))
),
character(0)
)
unlink(local, recursive = TRUE)
})
test_that("download wrong file warning works", {
r <- httr2::request("https://cloud.example.com/")
local <- paste0(tempdir(), "/dl")
dir.create(local)
expect_equal(
httr2::with_mocked_responses(
mock_download,
wd_isdir(r, "notthere")
),
FALSE
)
expect_equal(dir.exists(local), TRUE)
expect_warning(
httr2::with_mocked_responses(
mock_download,
wd_download(r, "notthere", local)
),
"Not Found"
)
unlink(local, recursive = TRUE)
})
test_that("download without target works", {
r <- httr2::request("https://cloud.example.com/")
expect_equal(
httr2::with_mocked_responses(
mock_download,
suppressWarnings(wd_download(r, "missing"))
),
character(0)
)
})