Skip to content

Commit

Permalink
Support full URLs, not just file paths, in 'with_mock_API' (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nealrichardson committed Feb 5, 2017
1 parent 2882491 commit 57694bb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Description: Testing code and packages that communicate with remote servers can
not--made. Using these tools, one can test that code is making the intended
requests and that it handles the expected responses correctly, all without
depending on a connection to a remote API.
Version: 1.1.0
Date: 2017-02-03
Version: 1.1.1
Date: 2017-02-05
Authors@R: person("Neal", "Richardson", role=c("aut", "cre"), email="neal.p.richardson@gmail.com")
Author: Neal Richardson [aut, cre]
Maintainer: Neal Richardson <neal.p.richardson@gmail.com>
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### httptest 1.1.1 (under development)
* Support full URLs, not just file paths, in `with_mock_API`

## httptest 1.1.0

* `expect_header` to assert that a HTTP request has a header
Expand Down
2 changes: 2 additions & 0 deletions R/mock-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ mockRequest <- function (req, handle, refresh) {
#' @importFrom digest digest
#' @export
buildMockURL <- function (url, method="GET") {
## Remove protocol
url <- sub("^.*?://", "", url)
## Handle query params
parts <- unlist(strsplit(url, "?", fixed=TRUE))
f <- sub("\\/$", "", parts[1])
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/example.com/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loaded": true
}
11 changes: 11 additions & 0 deletions tests/testthat/test-mock-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,16 @@ public({
expect_error(dl <- download.file("NOTAFILE", f2),
"DOWNLOAD NOTAFILE")
})

test_that("mock API with http:// URL, not file path", {
expect_GET(GET("http://httpbin.org/get"),
"http://httpbin.org/get",
"(httpbin.org/get.json)")
expect_GET(GET("https://httpbin.org/get"),
"https://httpbin.org/get",
"(httpbin.org/get.json)")
expect_identical(content(GET("http://example.com/get")),
list(loaded=TRUE))
})
})
})

0 comments on commit 57694bb

Please sign in to comment.