Skip to content

Commit

Permalink
Support multi-line cmd in curl_translate() (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich authored Aug 2, 2023
1 parent 7f795cd commit 817ca32
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# httr2 (development version)

* `curl_translate()` now works with multiline commands from the clipboard
(@mgirlich, #254).

* New `resp_has_body()` returns a `TRUE` or `FALSE` depending on whether
or not the response has a body (#205).

Expand Down
1 change: 1 addition & 0 deletions R/curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ curl_translate <- function(cmd) {
if (is_interactive() && is_installed("clipr")) {
clip <- TRUE
cmd <- clipr::read_clip()
cmd <- paste0(cmd, collapse = "\n")
} else {
abort("Must supply `cmd`")
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/curl.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,26 @@
req_body_raw("abcdef", "text/plain") %>%
req_perform()

# can read from clipboard

Code
curl_translate()
Message
v Copying to clipboard:
Output
request("http://example.com") %>%
req_headers(
A = "1",
B = "2",
) %>%
req_perform()
Code
clipr::read_clip()
Output
[1] "request(\"http://example.com\") %>% "
[2] " req_headers("
[3] " A = \"1\","
[4] " B = \"2\","
[5] " ) %>% "
[6] " req_perform()"

20 changes: 20 additions & 0 deletions tests/testthat/test-curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ test_that("can evaluate simple calls", {
body <- resp_body_json(resp)
expect_true(body$authenticated)
})

test_that("can read from clipboard", {
# need to skip on CI as can't read from clipboard there on Linux
skip_on_ci()
skip_if_not_installed("clipr")
# need to set env var so that `read/write_clip()` works in non-interactive mode
withr::local_envvar(CLIPR_ALLOW = TRUE)

# suppress warning because the clipboard might contain no readable text
old_clip <- suppressWarnings(clipr::read_clip())
withr::defer(clipr::write_clip(old_clip))
rlang::local_interactive()

clipr::write_clip("curl 'http://example.com' \\\n -H 'A: 1' \\\n -H 'B: 2'")
expect_snapshot({
curl_translate()
# also writes to clip
clipr::read_clip()
})
})

0 comments on commit 817ca32

Please sign in to comment.