Skip to content

Commit

Permalink
Rely on interpolation to print header values (#616)
Browse files Browse the repository at this point in the history
So that we don't need to worry about escaping. Fixes #586
  • Loading branch information
hadley authored Jan 6, 2025
1 parent 4a52f4f commit 20d1f7c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# httr2 (development version)

* `print.request()` now correctly escapes `{}` in headers (#586).
* New `req_headers_redacted()` provides a user-friendlier way to set redacted headers (#561).
* `resp_link_url()` now works if there are multiple `Link` headers (#587).
* New `url_modify()` makes it easier to modify an existing url (#464).
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bullets_with_header <- function(header, x) {
as_simple <- function(x) {
if (is.atomic(x) && length(x) == 1) {
if (is.character(x)) {
paste0("'", x, "'")
paste0('"', x, '"')
} else {
format(x)
}
Expand All @@ -18,7 +18,9 @@ bullets_with_header <- function(header, x) {
}
vals <- map_chr(x, as_simple)

cli::cli_li(paste0("{.field ", names(x), "}: ", vals))
for (i in seq_along(x)) {
cli::cli_li("{.field {names(x)[[i]]}}: {vals[[i]]}")
}
}

modify_list <- function(.x, ..., error_call = caller_env()) {
Expand Down
18 changes: 15 additions & 3 deletions tests/testthat/_snaps/req.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
POST https://example.com
Body: multipart encoded data

# printing headers works with {}

Code
req_headers(request("http://test"), x = "{z}", `{z}` = "x")
Message
<httr2_request>
GET http://test
Headers:
* x: "{z}"
* {z}: "x"
Body: empty

# individually prints repeated headers

Code
Expand All @@ -28,9 +40,9 @@
<httr2_request>
GET https://example.com
Headers:
* A: '1'
* A: '2'
* A: '3'
* A: "1"
* A: "2"
* A: "3"
Body: empty

# check_request() gives useful error
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ test_that("req has basic print method", {
})
})

test_that("printing headers works with {}", {
expect_snapshot(req_headers(request("http://test"), x = "{z}", `{z}` = "x"))
})

test_that("individually prints repeated headers", {
expect_snapshot(request("https://example.com") %>% req_headers(A = 1:3))
})
Expand All @@ -18,7 +22,7 @@ test_that("print method obfuscates Authorization header unless requested", {
expect_false(any(grepl("SECRET", output, fixed = TRUE)))

output <- testthat::capture_messages(print(req, redact_headers = FALSE))
expect_true(any(grepl("Authorization: 'Basic", output, fixed = TRUE)))
expect_true(any(grepl("Authorization: \"Basic", output, fixed = TRUE)))
expect_false(any(grepl("REDACTED", output, fixed = TRUE)))
})

Expand Down

0 comments on commit 20d1f7c

Please sign in to comment.