Skip to content

Commit

Permalink
Overhaul modify_list() (#130)
Browse files Browse the repository at this point in the history
Now uses strategy suggested by @jchrom, where we operate on "homonym groups". This makes it possible to use duplicate names in url parameters and form/multipart bodies.

Fixes #97. Fixes #107.
  • Loading branch information
hadley committed Apr 28, 2022
1 parent 64c7948 commit 46c194f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# httr2 (development version)

* `req_body_form()`, `req_body_multipart()`, and `req_url_query()` now
support multiple arguments with the same name (#97, #107).

* `req_throttle()` correctly sets throttle rate (@jchrom, #101).

* `req_error()` can now correct force successful HTTP statuses to fail (#98).
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Expand Up @@ -28,8 +28,10 @@ modify_list <- function(.x, ...) {
if (!is_named(dots)) {
abort("All components of ... must be named")
}
.x[names(dots)] <- dots
out <- compact(.x)

out <- .x[!names(.x) %in% names(dots)]
out <- c(out, compact(dots))

if (length(out) == 0) {
names(out) <- NULL
}
Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/test-req-body.R
Expand Up @@ -49,10 +49,13 @@ test_that("can send named list as json/form/multipart", {

test_that("can modify body data", {
req1 <- request_test() %>% req_body_form(list(a = 1))
req2 <- req1 %>% req_body_form(list(b = 2))

expect_equal(req1$body$data, list(a = 1))

req2 <- req1 %>% req_body_form(list(b = 2))
expect_equal(req2$body$data, list(a = 1, b = 2))

req3 <- req1 %>% req_body_form(list(a = 3, a = 4))
expect_equal(req3$body$data, list(a = 3, a = 4))
})

test_that("can upload file with multipart", {
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-req-url.R
Expand Up @@ -30,6 +30,9 @@ test_that("can set query params", {
expect_equal(req_url_query(req, a = 1, b = 2)$url, "http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, a = 1, b = 2, c = NULL)$url, "http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, !!!list(a = 1, b = 2))$url, "http://example.com/?a=1&b=2")

expect_equal(req_url_query(req, a = 1, a = 2)$url, "http://example.com/?a=1&a=2")
expect_equal(req_url_query(req, !!!list(a = 1, a = 2))$url, "http://example.com/?a=1&a=2")
})

test_that("empty query doesn't affect url", {
Expand All @@ -41,7 +44,8 @@ test_that("empty query doesn't affect url", {
test_that("can modify query params iteratively", {
req <- request("http://example.com/?a=1&b=2")
expect_equal(req_url_query(req, c = 3)$url, "http://example.com/?a=1&b=2&c=3")
expect_equal(req_url_query(req, a = 2)$url, "http://example.com/?a=2&b=2")
expect_equal(req_url_query(req, a = 2)$url, "http://example.com/?b=2&a=2")
expect_equal(req_url_query(req, a = 1, a = 2)$url, "http://example.com/?b=2&a=1&a=2")
expect_equal(req_url_query(req, b = NULL)$url, "http://example.com/?a=1")
})

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-utils.R
Expand Up @@ -9,6 +9,13 @@ test_that("modify list adds, removes, and overrides", {
expect_snapshot(modify_list(x, a = 1, 2), error = TRUE)
})

test_that("replacement affects all components with name", {
x <- list(a = 1, a = 2)
expect_equal(modify_list(x, a = NULL), list())
expect_equal(modify_list(x, a = 3), list(a = 3))
expect_equal(modify_list(x, a = 3, a = 4), list(a = 3, a =4))
})

test_that("can check arg types", {
expect_snapshot(error = TRUE, {
check_string(1, "x")
Expand Down

0 comments on commit 46c194f

Please sign in to comment.