Skip to content

Commit b0e2296

Browse files
authored
Minor curl_translate() polishing/testing (#619)
Fixes #500
1 parent b1bb9f3 commit b0e2296

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

R/curl.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ curl_translate <- function(cmd, simplify_headers = TRUE) {
5656

5757
# Content type set with data
5858
type <- data$headers$`Content-Type`
59-
data$headers$`Content-Type` <- NULL
59+
if (!identical(data$data, "")) {
60+
data$headers$`Content-Type` <- NULL
61+
}
6062

6163
headers <- curl_simplify_headers(data$headers, simplify_headers)
6264
steps <- add_curl_step(steps, "req_headers", dots = headers)

R/headers.R

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
as_headers <- function(x, error_call = caller_env()) {
22
if (is.character(x) || is.raw(x)) {
3-
headers <- curl::parse_headers(x)
4-
headers <- headers[grepl(":", headers, fixed = TRUE)]
3+
parsed <- curl::parse_headers(x)
4+
valid <- parsed[grepl(":", parsed, fixed = TRUE)]
5+
halves <- parse_in_half(valid, ":")
56

6-
equals <- regexpr(":", headers, fixed = TRUE)
7-
pieces <- regmatches(headers, equals, invert = TRUE)
8-
9-
names <- map_chr(pieces, "[[", 1)
10-
values <- as.list(trimws(map_chr(pieces, "[[", 2)))
11-
12-
new_headers(set_names(values, names), error_call = error_call)
7+
headers <- set_names(trimws(halves$right), halves$left)
8+
new_headers(as.list(headers), error_call = error_call)
139
} else if (is.list(x)) {
1410
new_headers(x, error_call = error_call)
1511
} else {

tests/testthat/_snaps/curl.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@
104104
req_body_raw("abcdef", "text/plain") |>
105105
req_perform()
106106

107+
# content type stays in header if no data
108+
109+
Code
110+
curl_translate("curl http://example.com -H Content-Type:text/plain")
111+
Output
112+
request("http://example.com/") |>
113+
req_headers(
114+
`Content-Type` = "text/plain",
115+
) |>
116+
req_perform()
117+
107118
# can read from clipboard
108119

109120
Code

tests/testthat/test-curl.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ test_that("can handle line breaks", {
5151
test_that("headers are parsed", {
5252
expect_equal(
5353
curl_normalize("curl http://x.com -H 'A: 1'")$headers,
54-
as_headers("A: 1")
54+
new_headers(list(A = "1"))
55+
)
56+
expect_equal(
57+
curl_normalize("curl http://x.com -H 'B:'")$headers,
58+
new_headers(list(B = ""))
5559
)
5660
})
5761

@@ -138,6 +142,14 @@ test_that("can translate data", {
138142
})
139143
})
140144

145+
test_that("content type stays in header if no data", {
146+
skip_if(getRversion() < "4.1")
147+
148+
expect_snapshot(
149+
curl_translate("curl http://example.com -H Content-Type:text/plain")
150+
)
151+
})
152+
141153
test_that("can evaluate simple calls", {
142154
request_test() # hack to start server
143155

0 commit comments

Comments
 (0)