Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# httr2 (development version)

* `req_url_path()` and `req_url_path_append()` can now handle `NULL` or empty
`...` and the elements of `...` can also have length > 1 (@mgirlich, #177).

* `req_perform()` now has an `error_call` argument and communicates more clearly
where the error occurred (@mgirlich, #187).

Expand Down
19 changes: 10 additions & 9 deletions R/req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ req_url_query <- function(.req, ...) {
#' @rdname req_url
req_url_path <- function(req, ...) {
check_request(req)
path <- paste(..., sep = "/")

if (!grepl("^/", path)) {
path <- paste0("/", path)
}
path <- dots_to_path(...)

req_url(req, url_modify(req$url, path = path))
}
Expand All @@ -66,15 +62,20 @@ req_url_path <- function(req, ...) {
#' @rdname req_url
req_url_path_append <- function(req, ...) {
check_request(req)
path <- paste(..., sep = "/")
path <- dots_to_path(...)

url <- url_parse(req$url)
url$path <- paste0(sub("/$", "", url$path), path)

req_url(req, url_build(url))
}

dots_to_path <- function(...) {
path <- paste(c(...), collapse = "/")
# Ensure we don't add duplicate /s
if (!grepl("^/", path)) {
if (path != "" && !grepl("^/", path)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary b/c paste(collapse = "/") returns "" instead of character().

path <- paste0("/", path)
}
url$path <- paste0(sub("/$", "", url$path), path)

req_url(req, url_build(url))
path
}
18 changes: 18 additions & 0 deletions tests/testthat/test-req-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ test_that("can append multiple components", {
expect_equal(req_url_path_append(req, "a", "b")$url, "http://example.com/x/a/b")
})

test_that("can handle empty path", {
req <- request("http://example.com/x")
expect_equal(req_url_path(req)$url, "http://example.com")
expect_equal(req_url_path_append(req)$url, "http://example.com/x")
expect_equal(req_url_path(req, NULL)$url, "http://example.com")
expect_equal(req_url_path_append(req, NULL)$url, "http://example.com/x")

expect_equal(req_url_path(req, "")$url, "http://example.com")
expect_equal(req_url_path_append(req, "")$url, "http://example.com/x")
})

test_that("can handle path vector", {
req <- request("http://example.com/x")
expect_equal(req_url_path(req, c("a", "b"))$url, "http://example.com/a/b")
expect_equal(req_url_path_append(req, c("a", "b"))$url, "http://example.com/x/a/b")
expect_equal(req_url_path_append(req, c("a", "b"), NULL)$url, "http://example.com/x/a/b")
})

test_that("can set query params", {
req <- request("http://example.com/")
expect_equal(req_url_query(req, a = 1, b = 2)$url, "http://example.com/?a=1&b=2")
Expand Down