File: test-download.r

package info (click to toggle)
r-cran-downloader 0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 132 kB
  • sloc: sh: 10; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
context("download")

# Download from a url, and return the contents of the file as a string
download_result <- function(url) {
  tfile <- tempfile()
  download(url, tfile, mode = "wb")

  # Read the file
  tfile_fd <- file(tfile, "r")
  dl_text <- readLines(tfile_fd, warn = FALSE)
  dl_text <- paste(dl_text, collapse = "\n")
  close(tfile_fd)
  unlink(tfile)

  dl_text
}

# CRAN has intermittent problems with these tests, since they rely on a
# particular website being accessible. This makes it run with devtools::test()
# but not on CRAN
if (Sys.getenv('NOT_CRAN') == "true") {

test_that("downloading http and https works properly", {
  # Download http from httpbin.org
  result <- download_result("http://httpbin.org/ip")
  # Check that it has the string "origin" in the text
  expect_true(grepl("origin", result))

  # Download https from httpbin.org
  result <- download_result("https://httpbin.org/ip")
  # Check that it has the string "origin" in the text
  expect_true(grepl("origin", result))
})

test_that("follows redirects", {
  # Download http redirect from httpbin.org
  result <- download_result("http://httpbin.org/redirect/3")
  # Check that it has the string "origin" in the text
  expect_true(grepl("origin", result))

  # Download https redirect from httpbin.org
  result <- download_result("https://httpbin.org/redirect/3")
  # Check that it has the string "origin" in the text
  expect_true(grepl("origin", result))
})

}