Skip to content

Commit

Permalink
add tests for mocking writing to disk #57
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Jul 24, 2019
1 parent 7cd327f commit 5e41591
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions tests/testthat/test-writing-to-disk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
context("mock writing to disk")

enable()

test_that("Write to a file before mocked request: crul", {
skip_on_cran()

library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
expect_is(readLines(f), "character")
expect_match(readLines(f), "world")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = file(f))
## make a request
out <- HttpClient$new("https://httpbin.org/get")$get(disk = f)
expect_is(out$content, "character")
expect_equal(attr(out$content, "type"), "file")
expect_is(readLines(out$content), "character")
expect_match(readLines(out$content), "hello")

# cleanup
unlink(f)
stub_registry_clear()
})

test_that("Write to a file before mocked request: httr", {
skip_on_cran()

library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## write something to the file
cat("{\"hello\":\"world\"}\n", file = f)
expect_is(readLines(f), "character")
expect_match(readLines(f), "world")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = file(f),
headers = list('content-type' = "application/json"))
## make a request
## with httr, you must set overwrite=TRUE or you'll get an errror
out <- GET("https://httpbin.org/get", write_disk(f, overwrite=TRUE))
content(out)
expect_is(out$content, "path")
expect_equal(attr(out$content, "class"), "path")
expect_is(readLines(out$content), "character")
expect_match(readLines(out$content), "hello")

# cleanup
unlink(f)
stub_registry_clear()
})

test_that("Use mock_file to have webmockr handle file and contents: crul", {
skip_on_cran()

library(crul)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(body = mock_file(f, "{\"hello\":\"mars\"}\n"))
## make a request
out <- crul::HttpClient$new("https://httpbin.org/get")$get(disk = f)
out$content
expect_is(out$content, "character")
expect_match(out$content, "json")
expect_is(readLines(out$content), "character")
expect_true(any(grepl("hello", readLines(out$content))))

# cleanup
unlink(f)
stub_registry_clear()
})

test_that("Use mock_file to have webmockr handle file and contents: httr", {
skip_on_cran()

library(httr)
## make a temp file
f <- tempfile(fileext = ".json")
## make the stub
stub_request("get", "https://httpbin.org/get") %>%
to_return(
body = mock_file(path = f, payload = "{\"foo\": \"bar\"}"),
headers = list('content-type' = "application/json")
)
## make a request
out <- GET("https://httpbin.org/get", write_disk(f))
## view stubbed file content
expect_is(out$content, "path")
expect_match(out$content, "json")
expect_is(readLines(out$content), "character")
expect_true(any(grepl("foo", readLines(out$content))))

# cleanup
unlink(f)
stub_registry_clear()
})

0 comments on commit 5e41591

Please sign in to comment.