Skip to content

Commit

Permalink
Minor curl_translate() polishing/testing (#619)
Browse files Browse the repository at this point in the history
Fixes #500
  • Loading branch information
hadley authored Jan 6, 2025
1 parent b1bb9f3 commit b0e2296
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
4 changes: 3 additions & 1 deletion R/curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ curl_translate <- function(cmd, simplify_headers = TRUE) {

# Content type set with data
type <- data$headers$`Content-Type`
data$headers$`Content-Type` <- NULL
if (!identical(data$data, "")) {
data$headers$`Content-Type` <- NULL
}

headers <- curl_simplify_headers(data$headers, simplify_headers)
steps <- add_curl_step(steps, "req_headers", dots = headers)
Expand Down
14 changes: 5 additions & 9 deletions R/headers.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
as_headers <- function(x, error_call = caller_env()) {
if (is.character(x) || is.raw(x)) {
headers <- curl::parse_headers(x)
headers <- headers[grepl(":", headers, fixed = TRUE)]
parsed <- curl::parse_headers(x)
valid <- parsed[grepl(":", parsed, fixed = TRUE)]
halves <- parse_in_half(valid, ":")

equals <- regexpr(":", headers, fixed = TRUE)
pieces <- regmatches(headers, equals, invert = TRUE)

names <- map_chr(pieces, "[[", 1)
values <- as.list(trimws(map_chr(pieces, "[[", 2)))

new_headers(set_names(values, names), error_call = error_call)
headers <- set_names(trimws(halves$right), halves$left)
new_headers(as.list(headers), error_call = error_call)
} else if (is.list(x)) {
new_headers(x, error_call = error_call)
} else {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/curl.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@
req_body_raw("abcdef", "text/plain") |>
req_perform()

# content type stays in header if no data

Code
curl_translate("curl http://example.com -H Content-Type:text/plain")
Output
request("http://example.com/") |>
req_headers(
`Content-Type` = "text/plain",
) |>
req_perform()

# can read from clipboard

Code
Expand Down
14 changes: 13 additions & 1 deletion tests/testthat/test-curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ test_that("can handle line breaks", {
test_that("headers are parsed", {
expect_equal(
curl_normalize("curl http://x.com -H 'A: 1'")$headers,
as_headers("A: 1")
new_headers(list(A = "1"))
)
expect_equal(
curl_normalize("curl http://x.com -H 'B:'")$headers,
new_headers(list(B = ""))
)
})

Expand Down Expand Up @@ -138,6 +142,14 @@ test_that("can translate data", {
})
})

test_that("content type stays in header if no data", {
skip_if(getRversion() < "4.1")

expect_snapshot(
curl_translate("curl http://example.com -H Content-Type:text/plain")
)
})

test_that("can evaluate simple calls", {
request_test() # hack to start server

Expand Down

0 comments on commit b0e2296

Please sign in to comment.