Skip to content

Commit

Permalink
Ensure url_parse() doesn't decode path
Browse files Browse the repository at this point in the history
Since we don't re-encode it
  • Loading branch information
hadley committed Jan 13, 2025
1 parent ae72fbc commit bbf2a16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ url_parse <- function(url, base_url = NULL) {
check_string(url)
check_string(base_url, allow_null = TRUE)

curl <- curl::curl_parse_url(url, baseurl = base_url)
curl <- curl::curl_parse_url(url, baseurl = base_url, decode = FALSE)

parsed <- list(
scheme = curl$scheme,
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ test_that("url_build validates its input", {
expect_snapshot(url_build("abc"), error = TRUE)
})

test_that("decodes query params but not paths", {
url <- url_parse("http://example.com/a%20b?q=a%20b")
expect_equal(url$path, "/a%20b")
expect_equal(url$query$q, "a b")
})

# modify url -------------------------------------------------------------

test_that("url_modify checks its inputs", {
Expand All @@ -69,6 +75,14 @@ test_that("no arguments is idempotent", {
expect_equal(url_modify(url), url)
})

test_that("can round-trip escaped components", {
url <- "https://example.com/a%20b"
expect_equal(url_modify(url), url)

url <- "https://example.com/?q=a%20b"
expect_equal(url_modify(url), url)
})

test_that("can accept query as a string or list", {
url <- "http://test/"

Expand All @@ -78,6 +92,14 @@ test_that("can accept query as a string or list", {
expect_equal(url_modify(url, query = ""), "http://test/")
expect_equal(url_modify(url, query = list()), "http://test/")
})

test_that("automatically escapes query components", {
expect_equal(
url_modify("https://example.com", query = list(q = "a b")),
"https://example.com/?q=a%20b"
)
})

test_that("checks various query formats", {
url <- "http://example.com"

Expand Down

0 comments on commit bbf2a16

Please sign in to comment.