Skip to content

Commit

Permalink
#128 have HttpRequest print update url when its been updated;
Browse files Browse the repository at this point in the history
and work for handles as well which was broken
  • Loading branch information
sckott committed Feb 26, 2020
1 parent 565a946 commit 9f17e0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions R/httprequest.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ HttpRequest <- R6::R6Class(
#' @param ... ignored
print = function(x, ...) {
cat(paste0("<crul http request> ", self$method()), sep = "\n")
cat(paste0(" url: ", if (is.null(self$url))
self$handle$url else self$url), sep = "\n")
# cat(paste0(" url: ", if (is.null(self$url))
# self$handle$url else self$url), sep = "\n")
cat(paste0(" url: ",
self$payload$url$url %||% self$handle$url %||% self$url), sep = "\n")
cat(" curl options: ", sep = "\n")
for (i in seq_along(self$opts)) {
cat(sprintf(" %s: %s", names(self$opts)[i],
Expand Down Expand Up @@ -273,7 +275,7 @@ make_url_async <- function(url = NULL, handle = NULL, path, query) {
url <- add_query(query, url)

if (!is.null(handle)) {
curl::handle_setopt(handle, url = url)
curl::handle_setopt(handle$handle, url = url)
} else {
handle <- curl::new_handle(url = url)
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-request.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ test_that("HttpRequest - verb", {
expect_error(HttpRequest$new(url = hb())$verb(5), "is not TRUE")
})

test_that("HttpRequest - prints new url after being modified", {
# query modifies url
aa <- HttpRequest$new(url = hb())
bb <- aa$get(query = list(foo = "bar", a = 5))
expect_output(print(aa), hb())
expect_output(print(bb), paste0(hb(), "\\?foo=bar&a=5"))

# handle passed in instead of a url
aa <- HttpRequest$new(handle = handle(file.path(hb(), "foobar")))
expect_output(print(aa), file.path(hb(), "foobar"))

# handle + query
aa <- HttpRequest$new(handle = handle(hb()))
bb <- aa$get(query = list(foo = "bar", a = 5))
expect_output(print(aa), hb())
expect_output(print(bb), paste0(hb(), "\\?foo=bar&a=5"))
})

test_that("HttpRequest fails well", {
expect_error(HttpRequest$new(), "need one of url or handle")
Expand Down

0 comments on commit 9f17e0a

Please sign in to comment.