diff --git a/DESCRIPTION b/DESCRIPTION index 9fe6d46e6..359386744 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,7 @@ License: MIT + file LICENSE URL: https://httr2.r-lib.org, https://github.com/r-lib/httr2 BugReports: https://github.com/r-lib/httr2/issues Depends: - R (>= 4.0) + R (>= 4.1) Imports: cli (>= 3.0.0), curl (>= 6.2.1), diff --git a/R/curl.R b/R/curl.R index f63f4c0e9..0f4605ad5 100644 --- a/R/curl.R +++ b/R/curl.R @@ -98,7 +98,7 @@ curl_translate <- function(cmd, simplify_headers = TRUE) { main_args = perform_args, keep_if_empty = TRUE ) - out <- paste0(steps, collapse = paste0(pipe(), "\n ")) + out <- paste0(steps, collapse = paste0(" |>\n ")) if (clip) { cli::cli_alert_success("Copying to clipboard:") @@ -109,10 +109,6 @@ curl_translate <- function(cmd, simplify_headers = TRUE) { structure(out, class = "httr2_cmd") } -pipe <- function() { - if (getRversion() >= "4.1.0") " |> " else " %>% " -} - #' @export print.httr2_cmd <- function(x, ...) { cat(x) diff --git a/R/iterate-helpers.R b/R/iterate-helpers.R index f8a076974..c5051cd8c 100644 --- a/R/iterate-helpers.R +++ b/R/iterate-helpers.R @@ -83,7 +83,7 @@ iterate_with_offset <- function( if (!isTRUE(resp_complete(resp))) { i <<- i + offset - req %>% req_url_query(!!param_name := i) + req |> req_url_query(!!param_name := i) } } } @@ -100,7 +100,7 @@ iterate_with_cursor <- function(param_name, resp_param_value) { function(resp, req) { value <- resp_param_value(resp) if (!is.null(value)) { - req %>% req_url_query(!!param_name := value) + req |> req_url_query(!!param_name := value) } } } @@ -114,7 +114,7 @@ iterate_with_link_url <- function(rel = "next") { function(resp, req) { url <- resp_link_url(resp, rel) if (!is.null(url)) { - req %>% req_url(url) + req |> req_url(url) } } } diff --git a/R/oauth-flow-auth-code.R b/R/oauth-flow-auth-code.R index 5aec478fc..a7d609650 100644 --- a/R/oauth-flow-auth-code.R +++ b/R/oauth-flow-auth-code.R @@ -492,7 +492,7 @@ oauth_flow_auth_code_fetch <- function(state) { max_seconds = 60, # The endpoint may temporarily return a 404 when no code is found for a # given state because the user hasn't finished clicking through yet. - is_transient = ~ resp_status(.x) %in% c(404, 429, 503) + is_transient = \(resp) resp_status(resp) %in% c(404, 429, 503) ) resp <- req_perform(req) body <- resp_body_json(resp) diff --git a/R/oauth-flow.R b/R/oauth-flow.R index 7680e6884..f16765930 100644 --- a/R/oauth-flow.R +++ b/R/oauth-flow.R @@ -1,5 +1,5 @@ oauth_flow_fetch <- function(req, source, error_call = caller_env()) { - req <- req_error(req, is_error = ~FALSE) + req <- req_error(req, is_error = \(resp) FALSE) resp <- req_perform(req, error_call = current_call()) oauth_flow_parse(resp, source, error_call = error_call) diff --git a/R/resp-headers.R b/R/resp-headers.R index 6e826df59..c22a5cc44 100644 --- a/R/resp-headers.R +++ b/R/resp-headers.R @@ -182,7 +182,7 @@ resp_link_url <- function(resp, rel) { link_headers <- headers[tolower(names(headers)) == "link"] links <- unlist(lapply(link_headers, parse_link), recursive = FALSE) - sel <- map_lgl(links, ~ .$rel == rel) + sel <- map_lgl(links, \(x) x$rel == rel) if (sum(sel) != 1L) { return() } diff --git a/R/resp-status.R b/R/resp-status.R index f3abf64ea..f0027263a 100644 --- a/R/resp-status.R +++ b/R/resp-status.R @@ -16,7 +16,7 @@ #' `req |> req_options(followlocation = FALSE)`. #' * 4xx client and 5xx server errors are automatically turned into R errors. #' You can stop them from being turned into R errors with [req_error()], -#' e.g. `req |> req_error(is_error = ~ FALSE)`. +#' e.g. `req |> req_error(is_error = \(resp) FALSE)`. #' #' @return #' * `resp_status()` returns a scalar integer diff --git a/cleanup b/cleanup deleted file mode 100755 index bdaa683c9..000000000 --- a/cleanup +++ /dev/null @@ -1,3 +0,0 @@ -#! /usr/bin/env sh - -rm -f man/macros/examples.Rd diff --git a/configure b/configure deleted file mode 100755 index b85ddabc4..000000000 --- a/configure +++ /dev/null @@ -1,16 +0,0 @@ -#! /usr/bin/env sh - -# Check that this is not just ./configure. We need to run this -# from R CMD INSTALL, to have the R env vars set. - -if [ -z "$R_HOME" ]; then - echo >&2 R_HOME is not set, are you running R CMD INSTALL? - exit 1 -fi - -# Find the R binary we need to use. This is a bit trickier on -# Windows, because it has two architectures. On windows R_ARCH_BIN -# is set, so this should work everywhere. -RBIN="${R_HOME}/bin${R_ARCH_BIN}/R" - - "$RBIN" --vanilla --slave -f tools/examples.R diff --git a/configure.win b/configure.win deleted file mode 100644 index 994f9fdcd..000000000 --- a/configure.win +++ /dev/null @@ -1,3 +0,0 @@ -#! /usr/bin/env sh - -sh ./configure diff --git a/man/resp_status.Rd b/man/resp_status.Rd index b55524b3e..93c1a82e5 100644 --- a/man/resp_status.Rd +++ b/man/resp_status.Rd @@ -53,7 +53,7 @@ have deliberately suppressed redirects with \code{req |> req_options(followlocation = FALSE)}. \item 4xx client and 5xx server errors are automatically turned into R errors. You can stop them from being turned into R errors with \code{\link[=req_error]{req_error()}}, -e.g. \code{req |> req_error(is_error = ~ FALSE)}. +e.g. \verb{req |> req_error(is_error = \\(resp) FALSE)}. } } \examples{ diff --git a/tests/testthat/_snaps/curl.md b/tests/testthat/_snaps/curl.md index 62dfd909c..9076767d1 100644 --- a/tests/testthat/_snaps/curl.md +++ b/tests/testthat/_snaps/curl.md @@ -38,40 +38,40 @@ Code curl_translate("curl http://x.com") Output - request("http://x.com/") |> + request("http://x.com/") |> req_perform() Code curl_translate("curl http://x.com -X DELETE") Output - request("http://x.com/") |> - req_method("DELETE") |> + request("http://x.com/") |> + req_method("DELETE") |> req_perform() Code curl_translate("curl http://x.com -H A:1") Output - request("http://x.com/") |> + request("http://x.com/") |> req_headers( A = "1", - ) |> + ) |> req_perform() Code curl_translate("curl http://x.com -H 'A B:1'") Output - request("http://x.com/") |> + request("http://x.com/") |> req_headers( `A B` = "1", - ) |> + ) |> req_perform() Code curl_translate("curl http://x.com -u u:p") Output - request("http://x.com/") |> - req_auth_basic("u", "p") |> + request("http://x.com/") |> + req_auth_basic("u", "p") |> req_perform() Code curl_translate("curl http://x.com --verbose") Output - request("http://x.com/") |> + request("http://x.com/") |> req_perform(verbosity = 1) # can translate query @@ -79,11 +79,11 @@ Code curl_translate("curl http://x.com?string=abcde&b=2") Output - request("http://x.com/") |> + request("http://x.com/") |> req_url_query( string = "abcde", b = "2", - ) |> + ) |> req_perform() # can translate data @@ -91,15 +91,15 @@ Code curl_translate("curl http://example.com --data abcdef") Output - request("http://example.com/") |> - req_body_raw("abcdef", "application/x-www-form-urlencoded") |> + request("http://example.com/") |> + req_body_raw("abcdef", "application/x-www-form-urlencoded") |> req_perform() Code curl_translate( "curl http://example.com --data abcdef -H Content-Type:text/plain") Output - request("http://example.com/") |> - req_body_raw("abcdef", "text/plain") |> + request("http://example.com/") |> + req_body_raw("abcdef", "text/plain") |> req_perform() # can translate ocokies @@ -107,12 +107,12 @@ Code curl_translate("curl 'http://test' -H 'Cookie: x=1; y=2;z=3'") Output - request("http://test/") |> + request("http://test/") |> req_cookies_set( x = "1", y = "2", z = "3", - ) |> + ) |> req_perform() # can translate json @@ -121,18 +121,18 @@ curl_translate( "curl http://example.com --data-raw '{\"a\": 1, \"b\": \"text\"}' -H Content-Type:application/json") Output - request("http://example.com/") |> + request("http://example.com/") |> req_body_json( data = list(a = 1L, b = "text"), - ) |> + ) |> req_perform() Code curl_translate("curl http://example.com --json '{\"a\": 1, \"b\": \"text\"}'") Output - request("http://example.com/") |> + request("http://example.com/") |> req_body_json( data = list(a = 1L, b = "text"), - ) |> + ) |> req_perform() # content type stays in header if no data @@ -140,10 +140,10 @@ Code curl_translate("curl http://example.com -H Content-Type:text/plain") Output - request("http://example.com/") |> + request("http://example.com/") |> req_headers( `Content-Type` = "text/plain", - ) |> + ) |> req_perform() # can read from clipboard @@ -153,20 +153,20 @@ Message v Copying to clipboard: Output - request("http://example.com/") |> + request("http://example.com/") |> req_headers( A = "1", B = "2", - ) |> + ) |> req_perform() Code writeLines(clipr::read_clip()) Output - request("http://example.com/") |> + request("http://example.com/") |> req_headers( A = "1", B = "2", - ) |> + ) |> req_perform() # encode_string2() produces simple strings @@ -174,10 +174,10 @@ Code curl_translate(cmd) Output - request("http://example.com/") |> - req_method("PATCH") |> + request("http://example.com/") |> + req_method("PATCH") |> req_body_json( data = list(data = list(x = 1L, y = "a", nested = list(z = list(1L, 2L, 3L)))), - ) |> + ) |> req_perform() diff --git a/tests/testthat/_snaps/req-body.md b/tests/testthat/_snaps/req-body.md index 6c85db0cc..c43271485 100644 --- a/tests/testthat/_snaps/req-body.md +++ b/tests/testthat/_snaps/req-body.md @@ -1,7 +1,7 @@ # can't change body type Code - req %>% req_body_json(list(x = 1)) + req_body_json(req, list(x = 1)) Condition Error in `req_body_json()`: ! Can't change body type from raw to json. diff --git a/tests/testthat/_snaps/req-error.md b/tests/testthat/_snaps/req-error.md index e58b2a078..76b593f01 100644 --- a/tests/testthat/_snaps/req-error.md +++ b/tests/testthat/_snaps/req-error.md @@ -8,8 +8,8 @@ Code req <- request_test("/status/404") - req <- req %>% req_error(body = ~ resp_body_json(.x)$error) - req %>% req_perform() + req <- req_error(req, body = function(resp) resp_body_json(resp)$error) + req_perform(req) Condition Error in `req_perform()`: ! Failed to parse error body with method defined in `req_error()`. diff --git a/tests/testthat/_snaps/req-mock.md b/tests/testthat/_snaps/req-mock.md index 3b77c31a0..3828880e6 100644 --- a/tests/testthat/_snaps/req-mock.md +++ b/tests/testthat/_snaps/req-mock.md @@ -1,13 +1,13 @@ # local_mock and with_mock are deprecated Code - local_mock(~ response(404)) + local_mock(function(req) response(404)) Condition Warning: `local_mock()` was deprecated in httr2 1.1.0. i Please use `local_mocked_responses()` instead. Code - . <- with_mock(NULL, ~ response(404)) + . <- with_mock(NULL, function(req) response(404)) Condition Error: ! `with_mock()` was deprecated in httr2 1.1.0 and is now defunct. diff --git a/tests/testthat/_snaps/req-options.md b/tests/testthat/_snaps/req-options.md index ccc5bf890..6faa3ad23 100644 --- a/tests/testthat/_snaps/req-options.md +++ b/tests/testthat/_snaps/req-options.md @@ -1,12 +1,12 @@ # validates inputs Code - request_test() %>% req_timeout("x") + req_timeout(request_test(), "x") Condition Error in `req_timeout()`: ! `seconds` must be a number, not the string "x". Code - request_test() %>% req_timeout(0) + req_timeout(request_test(), 0) Condition Error in `req_timeout()`: ! `seconds` must be >1 ms. @@ -14,12 +14,12 @@ # req_proxy gives helpful errors Code - req %>% req_proxy(port = "abc") + req_proxy(req, port = "abc") Condition Error in `req_proxy()`: ! `port` must be a whole number or `NULL`, not the string "abc". Code - req %>% req_proxy("abc", auth = "bsc") + req_proxy(req, "abc", auth = "bsc") Condition Error in `req_proxy()`: ! `auth` must be one of "basic", "digest", "gssnegotiate", "ntlm", "digest_ie", or "any", not "bsc". diff --git a/tests/testthat/_snaps/req-url.md b/tests/testthat/_snaps/req-url.md index b67f376b7..1ee19c970 100644 --- a/tests/testthat/_snaps/req-url.md +++ b/tests/testthat/_snaps/req-url.md @@ -18,23 +18,23 @@ # errors are forwarded correctly Code - req %>% req_url_query(1) + req_url_query(req, 1) Condition Error in `url_modify_query()`: ! All components of `...` must be named. Code - req %>% req_url_query(a = I(1)) + req_url_query(req, a = I(1)) Condition Error in `url_modify_query()`: ! Escaped query value `a` must be a single string, not the number 1. Code - req %>% req_url_query(a = 1:2) + req_url_query(req, a = 1:2) Condition Error in `url_modify_query()`: ! All vector elements of `...` must be length 1. i Use `.multi` to choose a strategy for handling vectors. Code - req %>% req_url_query(a = mean) + req_url_query(req, a = mean) Condition Error in `url_modify_query()`: ! All elements of `...` must be either an atomic vector or NULL. diff --git a/tests/testthat/_snaps/req.md b/tests/testthat/_snaps/req.md index bc72349d8..bbb5412fb 100644 --- a/tests/testthat/_snaps/req.md +++ b/tests/testthat/_snaps/req.md @@ -8,13 +8,13 @@ GET https://example.com Body: empty Code - req %>% req_body_raw("Test") + req_body_raw(req, "Test") Message POST https://example.com Body: a string Code - req %>% req_body_multipart(Test = 1) + req_body_multipart(req, Test = 1) Message POST https://example.com @@ -35,7 +35,7 @@ # individually prints repeated headers Code - request("https://example.com") %>% req_headers(A = 1:3) + req_headers(request("https://example.com"), A = 1:3) Message GET https://example.com diff --git a/tests/testthat/_snaps/resp-body.md b/tests/testthat/_snaps/resp-body.md index e70b85dc8..d2c34c532 100644 --- a/tests/testthat/_snaps/resp-body.md +++ b/tests/testthat/_snaps/resp-body.md @@ -30,13 +30,13 @@ # content types are checked Code - request_test("/xml") %>% req_perform() %>% resp_body_json() + resp_body_json(req_perform(request_test("/xml"))) Condition Error in `resp_body_json()`: ! Unexpected content type "application/xml". * Expecting type "application/json" or suffix "json". Code - request_test("/json") %>% req_perform() %>% resp_body_xml() + resp_body_xml(req_perform(request_test("/json"))) Condition Error in `resp_body_xml()`: ! Unexpected content type "application/json". diff --git a/tests/testthat/test-oauth-flow-jwt.R b/tests/testthat/test-oauth-flow-jwt.R index 37e7959db..f8aae05c8 100644 --- a/tests/testthat/test-oauth-flow-jwt.R +++ b/tests/testthat/test-oauth-flow-jwt.R @@ -26,9 +26,9 @@ test_that("can generate token and use it automatically", { expect_s3_class(token, "httr2_token") # Can use it in request - resp <- request("https://openidconnect.googleapis.com/v1/userinfo") %>% - req_oauth_bearer_jwt(client, claim) %>% - req_perform() %>% + resp <- request("https://openidconnect.googleapis.com/v1/userinfo") |> + req_oauth_bearer_jwt(client, claim) |> + req_perform() |> resp_body_json() expect_type(resp, "list") diff --git a/tests/testthat/test-oauth-flow-refresh.R b/tests/testthat/test-oauth-flow-refresh.R index ffd18f653..14cf355d2 100644 --- a/tests/testthat/test-oauth-flow-refresh.R +++ b/tests/testthat/test-oauth-flow-refresh.R @@ -3,9 +3,9 @@ test_that("cache considers refresh_token", { req <- request("https://example.com") # create 2 requests with different refresh token - req1 <- req %>% + req1 <- req |> req_oauth_refresh(client, refresh_token = "rt1") - req2 <- req %>% + req2 <- req |> req_oauth_refresh(client, refresh_token = "rt2") # cache must be empty diff --git a/tests/testthat/test-req-auth-aws.R b/tests/testthat/test-req-auth-aws.R index 8d3cc4a6c..20e1b6703 100644 --- a/tests/testthat/test-req-auth-aws.R +++ b/tests/testthat/test-req-auth-aws.R @@ -38,7 +38,7 @@ test_that("can correctly sign a request with live credentials", { }) test_that('aws_v4_signature calculates correct signature', { - req <- request("https://example.execute-api.us-east-1.amazonaws.com/v0/") %>% + req <- request("https://example.execute-api.us-east-1.amazonaws.com/v0/") |> req_method('POST') body_sha256 <- openssl::sha256(req_body_get(req) %||% "") diff --git a/tests/testthat/test-req-auth.R b/tests/testthat/test-req-auth.R index 49f788244..221d421b6 100644 --- a/tests/testthat/test-req-auth.R +++ b/tests/testthat/test-req-auth.R @@ -2,7 +2,7 @@ test_that("can send username/password", { user <- "u" password <- "p" req1 <- request_test("/basic-auth/:user/:password") - req2 <- req1 %>% req_auth_basic(user, password) + req2 <- req1 |> req_auth_basic(user, password) expect_redacted(req2, "Authorization") expect_error(req_perform(req1), class = "httr2_http_401") diff --git a/tests/testthat/test-req-body.R b/tests/testthat/test-req-body.R index 6c056b563..26dc95193 100644 --- a/tests/testthat/test-req-body.R +++ b/tests/testthat/test-req-body.R @@ -1,6 +1,6 @@ test_that("can't change body type", { - req <- request("http://example.com") %>% req_body_raw(raw(1)) - expect_snapshot(req %>% req_body_json(list(x = 1)), error = TRUE) + req <- request("http://example.com") |> req_body_raw(raw(1)) + expect_snapshot(req |> req_body_json(list(x = 1)), error = TRUE) }) test_that("useful values for empty body", { @@ -13,7 +13,7 @@ test_that("useful values for empty body", { # req_body_raw() --------------------------------------------------------------- test_that("can send string", { - req <- request_test("/post") %>% req_body_raw("test", type = "text/plain") + req <- request_test("/post") |> req_body_raw("test", type = "text/plain") expect_equal(req_body_type(req), "string") expect_equal(req_body_get(req), "test") expect_equal(req_body_info(req), "a string") @@ -26,7 +26,7 @@ test_that("can send string", { test_that("can send raw vector", { data <- charToRaw("abcdef") - req <- request_test("/post") %>% req_body_raw(data) + req <- request_test("/post") |> req_body_raw(data) expect_equal(req_body_type(req), "raw") expect_equal(req_body_get(req), data) expect_equal(req_body_info(req), "a 6 byte raw vector") @@ -43,8 +43,8 @@ test_that("can't send anything else", { }) test_that("can override body content type", { - req <- request_test("/post") %>% - req_body_raw('{"x":"y"}') %>% + req <- request_test("/post") |> + req_body_raw('{"x":"y"}') |> req_headers("content-type" = "application/json") resp <- req_perform(req) headers <- resp_body_json(resp)$headers @@ -61,7 +61,7 @@ test_that("can send file", { x <- strrep("x", 128 * 1024) writeChar(x, path, nchar(x)) - req <- request_test("/post") %>% req_body_file(path, type = "text/plain") + req <- request_test("/post") |> req_body_file(path, type = "text/plain") expect_equal(req_body_type(req), "file") expect_equal(rawToChar(req_body_get(req)), x) expect_equal(req_body_info(req), glue::glue("a path '{path}'")) @@ -77,8 +77,8 @@ test_that("can send file with redirect", { path <- tempfile() writeChar(str, path) - resp <- request_test("/redirect-to?url=/post&status_code=307") %>% - req_body_file(path, type = "text/plain") %>% + resp <- request_test("/redirect-to?url=/post&status_code=307") |> + req_body_file(path, type = "text/plain") |> req_perform() expect_equal(resp_status(resp), 200) @@ -96,11 +96,11 @@ test_that("errors on invalid input", { # req_body_json() -------------------------------------------------------------- test_that("can send any type of object as json", { - req <- request_test("/post") %>% req_body_json(mtcars) + req <- request_test("/post") |> req_body_json(mtcars) expect_equal(req$body$data, mtcars) data <- list(a = "1", b = "2") - req <- request_test("/post") %>% req_body_json(data) + req <- request_test("/post") |> req_body_json(data) expect_equal(req_body_type(req), "json") expect_equal(req_body_info(req), "JSON data") expect_equal(req_body_get(req), '{"a":"1","b":"2"}') @@ -109,16 +109,16 @@ test_that("can send any type of object as json", { json <- resp_body_json(resp) expect_equal(json$json, data) - resp <- request_test("/post") %>% - req_body_json(letters) %>% + resp <- request_test("/post") |> + req_body_json(letters) |> req_perform() json <- resp_body_json(resp) expect_equal(json$json, as.list(letters)) }) test_that("can use custom json type", { - resp <- request_test("/post") %>% - req_body_json(mtcars, type = "application/ld+json") %>% + resp <- request_test("/post") |> + req_body_json(mtcars, type = "application/ld+json") |> req_perform() expect_equal( @@ -135,25 +135,25 @@ test_that("non-json type errors", { }) test_that("can modify json data", { - req <- request_test() %>% - req_body_json(data = list(a = 1, b = 2, d = 4)) %>% + req <- request_test() |> + req_body_json(data = list(a = 1, b = 2, d = 4)) |> req_body_json_modify(a = 10, b = NULL, c = 3) expect_equal(req$body$data, list(a = 10, d = 4, c = 3)) - req <- request_test() %>% - req_body_json(data = list(a = list(b = list(c = 1, d = 2), e = 3))) %>% + req <- request_test() |> + req_body_json(data = list(a = list(b = list(c = 1, d = 2), e = 3))) |> req_body_json_modify(a = list(b = list(c = 101), e = 103)) expect_equal(req$body$data, list(a = list(b = list(c = 101, d = 2), e = 103))) }) test_that("can modify empty body", { - req <- request_test() %>% + req <- request_test() |> req_body_json_modify(a = 10, b = 20) expect_equal(req$body$data, list(a = 10, b = 20)) }) test_that("can't modify non-json data", { - req <- request_test() %>% req_body_raw("abc") + req <- request_test() |> req_body_raw("abc") expect_snapshot(req |> req_body_json_modify(a = 1), error = TRUE) }) @@ -162,7 +162,7 @@ test_that("can't modify non-json data", { test_that("can send named elements as form", { data <- list(a = "1", b = "2") - req <- request_test("/post") %>% req_body_form(!!!data) + req <- request_test("/post") |> req_body_form(!!!data) expect_equal(req_body_type(req), "form") expect_equal(req_body_info(req), "form data") expect_equal(req_body_get(req), "a=1&b=2") @@ -174,13 +174,13 @@ test_that("can send named elements as form", { }) test_that("can modify body data", { - req1 <- request_test() %>% req_body_form(a = 1) + req1 <- request_test() |> req_body_form(a = 1) expect_equal(req1$body$data, list(a = I("1"))) - req2 <- req1 %>% req_body_form(b = 2) + req2 <- req1 |> req_body_form(b = 2) expect_equal(req2$body$data, list(a = I("1"), b = I("2"))) - req3 <- req1 %>% req_body_form(a = 3, a = 4) + req3 <- req1 |> req_body_form(a = 3, a = 4) expect_equal(req3$body$data, list(a = I("3"), a = I("4"))) }) @@ -189,7 +189,7 @@ test_that("can modify body data", { test_that("can send named elements as multipart", { data <- list(a = "1", b = "2") - req <- request_test("/post") %>% req_body_multipart(!!!data) + req <- request_test("/post") |> req_body_multipart(!!!data) expect_equal(req_body_type(req), "multipart") expect_equal(req_body_info(req), "multipart data") expect_snapshot( @@ -209,8 +209,8 @@ test_that("can upload file with multipart", { path <- tempfile() writeLines("this is a test", path) - resp <- request_test("/post") %>% - req_body_multipart(file = curl::form_file(path)) %>% + resp <- request_test("/post") |> + req_body_multipart(file = curl::form_file(path)) |> req_perform() json <- resp_body_json(resp) expect_equal( @@ -223,7 +223,7 @@ test_that("can upload file with multipart", { }) test_that("no issues with partial name matching", { - req <- request_test("/get") %>% + req <- request_test("/get") |> req_body_multipart(d = "some data") expect_named(req$body$data, "d") diff --git a/tests/testthat/test-req-cache.R b/tests/testthat/test-req-cache.R index 127799168..fe37c90e8 100644 --- a/tests/testthat/test-req-cache.R +++ b/tests/testthat/test-req-cache.R @@ -7,8 +7,8 @@ test_that("nothing happens if cache not enabled", { }) test_that("never retrieves POST request from cache", { - req <- request("http://example.com") %>% - req_method("POST") %>% + req <- request("http://example.com") |> + req_method("POST") |> req_cache(tempfile()) # Fake an equivalent GET request in the cache @@ -23,7 +23,7 @@ test_that("never retrieves POST request from cache", { }) test_that("immutable objects retrieved directly from cache", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response( 200, headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT", @@ -35,7 +35,7 @@ test_that("immutable objects retrieved directly from cache", { }) test_that("cached cache header added to request", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) # If not cached, request returned as is req2 <- cache_pre_fetch(req) expect_equal(req2, req) @@ -57,7 +57,7 @@ test_that("cached cache header added to request", { }) test_that("error can use cached value", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response(200, body = charToRaw("OK")) cache_set(req, resp) @@ -68,7 +68,7 @@ test_that("error can use cached value", { }) test_that("304 retains headers but gets cached body", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response(200, headers = "X: 1", body = charToRaw("OK")) cache_set(req, resp) @@ -82,7 +82,7 @@ test_that("304 retains headers but gets cached body", { }) test_that("automatically adds to cache", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) expect_true(is.null(cache_get(req))) resp <- response(200, headers = 'Etag: "abc"', body = charToRaw("OK")) @@ -92,7 +92,7 @@ test_that("automatically adds to cache", { }) test_that("cache emits useful debugging info", { - req <- request("http://example.com") %>% req_cache(tempfile(), debug = TRUE) + req <- request("http://example.com") |> req_cache(tempfile(), debug = TRUE) resp <- response( 200, headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT", @@ -106,7 +106,7 @@ test_that("cache emits useful debugging info", { invisible(cache_pre_fetch(req)) }) - req <- request("http://example.com") %>% + req <- request("http://example.com") |> req_cache(tempfile(), debug = TRUE, use_on_error = TRUE) resp <- response(200, headers = "X: 1", body = charToRaw("OK")) cache_set(req, resp) @@ -121,7 +121,7 @@ test_that("cache emits useful debugging info", { # cache ------------------------------------------------------------------- test_that("can get and set from cache", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response( 200, headers = list( @@ -164,7 +164,7 @@ test_that("can get and set from cache", { }) test_that("handles responses with files", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) path <- local_write_lines("Hi there") resp <- response(200, headers = "Etag: ABC", body = new_path(path)) @@ -190,7 +190,7 @@ test_that("handles responses with files", { test_that("corrupt files are ignored", { cache_dir <- withr::local_tempdir() - req <- request("http://example.com") %>% req_cache(cache_dir) + req <- request("http://example.com") |> req_cache(cache_dir) writeLines(letters, req_cache_path(req)) expect_true(is.null(cache_get(req))) diff --git a/tests/testthat/test-req-cookies.R b/tests/testthat/test-req-cookies.R index f77e3c1e2..25ee726da 100644 --- a/tests/testthat/test-req-cookies.R +++ b/tests/testthat/test-req-cookies.R @@ -2,8 +2,8 @@ test_that("can read/write cookies", { cookie_path <- withr::local_tempfile() set_cookie <- function(req, name, value) { - request_test("/cookies/set/:name/:value", name = name, value = value) %>% - req_cookie_preserve(cookie_path) %>% + request_test("/cookies/set/:name/:value", name = name, value = value) |> + req_cookie_preserve(cookie_path) |> req_perform() } set_cookie(req, "x", "a") @@ -12,25 +12,24 @@ test_that("can read/write cookies", { expect_snapshot(readLines(cookie_path)[-(1:4)]) - cookies <- request_test("/cookies") %>% - req_cookie_preserve(cookie_path) %>% - req_perform() %>% - resp_body_json() %>% - .$cookies - expect_mapequal(cookies, list(x = "a", y = "b", z = "c")) + json <- request_test("/cookies") |> + req_cookie_preserve(cookie_path) |> + req_perform() |> + resp_body_json() + expect_mapequal(json$cookies, list(x = "a", y = "b", z = "c")) }) test_that("can set cookies", { - resp <- request(example_url("/cookies")) %>% - req_cookies_set(a = 1, b = 1) %>% + resp <- request(example_url("/cookies")) |> + req_cookies_set(a = 1, b = 1) |> req_perform() expect_equal(resp_body_json(resp), list(cookies = list(a = "1", b = "1"))) }) test_that("cookie values are usually escaped", { - resp <- request(example_url("/cookies")) %>% - req_cookies_set(a = I("%20"), b = "%") %>% + resp <- request(example_url("/cookies")) |> + req_cookies_set(a = I("%20"), b = "%") |> req_perform() expect_equal(resp_body_json(resp), list(cookies = list(a = "%20", b = "%25"))) diff --git a/tests/testthat/test-req-dry-run.R b/tests/testthat/test-req-dry-run.R index db859e8f0..900491566 100644 --- a/tests/testthat/test-req-dry-run.R +++ b/tests/testthat/test-req-dry-run.R @@ -1,5 +1,5 @@ test_that("req_dry_run() returns useful data", { - resp <- request("http://example.com") %>% + resp <- request("http://example.com") |> req_dry_run(quiet = TRUE, testing_headers = FALSE) expect_equal(resp$method, "GET") expect_equal(resp$path, "/") @@ -24,7 +24,7 @@ test_that("body is shown", { }) test_that("authorization headers are redacted", { - req <- request("http://example.com") %>% req_auth_basic("user", "password") + req <- request("http://example.com") |> req_auth_basic("user", "password") expect_snapshot(req_dry_run(req)) }) diff --git a/tests/testthat/test-req-error.R b/tests/testthat/test-req-error.R index 2eefcc0ab..d37d1a852 100644 --- a/tests/testthat/test-req-error.R +++ b/tests/testthat/test-req-error.R @@ -3,7 +3,7 @@ test_that("can customise what statuses are errors", { expect_equal(error_is_error(req, response(404)), TRUE) expect_equal(error_is_error(req, response(200)), FALSE) - req <- req %>% req_error(is_error = ~ !resp_is_error(.x)) + req <- req |> req_error(is_error = \(resp) !resp_is_error(resp)) expect_equal(error_is_error(req, response(404)), FALSE) expect_equal(error_is_error(req, response(200)), TRUE) }) @@ -12,17 +12,17 @@ test_that("can customise error info", { req <- request_test() expect_equal(error_body(req, response(404)), NULL) - req <- req %>% req_error(body = ~"Hi!") + req <- req |> req_error(body = \(resp) "Hi!") expect_equal(error_body(req, response(404)), "Hi!") }) test_that("failing callback still generates useful body", { - req <- request_test() %>% req_error(body = ~ abort("This is an error!")) + req <- request_test() |> req_error(body = \(resp) abort("This is an error!")) expect_snapshot_error(error_body(req, response(404))) out <- expect_snapshot(error = TRUE, { req <- request_test("/status/404") - req <- req %>% req_error(body = ~ resp_body_json(.x)$error) - req %>% req_perform() + req <- req |> req_error(body = \(resp) resp_body_json(resp)$error) + req |> req_perform() }) }) diff --git a/tests/testthat/test-req-headers.R b/tests/testthat/test-req-headers.R index 195046c02..fbc9c57ec 100644 --- a/tests/testthat/test-req-headers.R +++ b/tests/testthat/test-req-headers.R @@ -1,14 +1,14 @@ test_that("can add and remove headers", { req <- request("http://example.com") - req <- req %>% req_headers(x = 1) + req <- req |> req_headers(x = 1) expect_equal(req$headers, new_headers(list(x = 1))) - req <- req %>% req_headers(x = NULL) + req <- req |> req_headers(x = NULL) expect_equal(req$headers, new_headers(list())) }) test_that("simple vectors automatically converted to strings", { req <- request("http://example.com") - req <- req %>% req_headers(lgl = TRUE, int = 1L, dbl = 1.1, chr = "a") + req <- req |> req_headers(lgl = TRUE, int = 1L, dbl = 1.1, chr = "a") resp <- req_dry_run(req, quiet = TRUE) expect_equal(resp$headers$lgl, "TRUE") @@ -27,13 +27,13 @@ test_that("bad inputs get clear error", { test_that("can add header called req", { req <- request("http://example.com") - req <- req %>% req_headers(req = 1) + req <- req |> req_headers(req = 1) expect_equal(req$headers, new_headers(list(req = 1))) }) test_that("can add repeated headers", { - resp <- request_test() %>% - req_headers(a = c("a", "b")) %>% + resp <- request_test() |> + req_headers(a = c("a", "b")) |> req_dry_run(quiet = TRUE) # https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2 expect_equal(resp$headers$a, "a,b") @@ -41,8 +41,8 @@ test_that("can add repeated headers", { test_that("replacing headers is case-insensitive", { req <- request("http://example.com") - req <- req %>% req_headers(A = 1) - req <- req %>% req_headers(a = 2) + req <- req |> req_headers(A = 1) + req <- req |> req_headers(a = 2) expect_equal(req$headers, new_headers(list(a = 2))) }) diff --git a/tests/testthat/test-req-mock.R b/tests/testthat/test-req-mock.R index 453caecda..570a2d979 100644 --- a/tests/testthat/test-req-mock.R +++ b/tests/testthat/test-req-mock.R @@ -2,14 +2,14 @@ test_that("can override requests through mocking", { resp <- response() req <- request("https://google.com") - expect_equal(with_mocked_responses(~resp, req_perform(req)), resp) + expect_equal(with_mocked_responses(\(req) resp, req_perform(req)), resp) local_mocked_responses(function(req) resp) expect_equal(req_perform(req), resp) }) test_that("can generate errors with mocking", { - local_mocked_responses(~ response(404)) + local_mocked_responses(\(req) response(404)) req <- request("https://google.com") expect_error(req_perform(req), class = "httr2_http_404") @@ -17,8 +17,8 @@ test_that("can generate errors with mocking", { test_that("local_mock and with_mock are deprecated", { expect_snapshot(error = TRUE, { - local_mock(~ response(404)) - . <- with_mock(NULL, ~ response(404)) + local_mock(\(req) response(404)) + . <- with_mock(NULL, \(req) response(404)) }) }) diff --git a/tests/testthat/test-req-options.R b/tests/testthat/test-req-options.R index eec5c2c71..da0e99fe2 100644 --- a/tests/testthat/test-req-options.R +++ b/tests/testthat/test-req-options.R @@ -1,26 +1,26 @@ test_that("can add and remove options", { req <- request("http://example.com") - req <- req %>% req_options(x = 1) + req <- req |> req_options(x = 1) expect_equal(req$options, list(x = 1)) - req <- req %>% req_options(x = NULL) + req <- req |> req_options(x = NULL) expect_equal(req$options, list()) }) test_that("can add header called req", { req <- request("http://example.com") - req <- req %>% req_options(req = 1) + req <- req |> req_options(req = 1) expect_equal(req$options, list(req = 1)) }) test_that("default user agent includes httr2 + libcurl versions", { - req <- request("http://example.com") %>% req_user_agent() + req <- request("http://example.com") |> req_user_agent() expect_match(req$options$useragent, "httr2") expect_match(req$options$useragent, "libcurl") }) test_that("can override default user agent", { - req <- request("http://example.com") %>% req_user_agent("abc") + req <- request("http://example.com") |> req_user_agent("abc") expect_equal(req$options$useragent, "abc") }) @@ -32,22 +32,22 @@ test_that("default user agent works with dev curl", { }) test_that("can set timeout", { - req <- request_test("/delay/:secs", secs = 1) %>% req_timeout(0.1) + req <- request_test("/delay/:secs", secs = 1) |> req_timeout(0.1) expect_error(req_perform(req), "timed out") }) test_that("validates inputs", { expect_snapshot(error = TRUE, { - request_test() %>% req_timeout("x") - request_test() %>% req_timeout(0) + request_test() |> req_timeout("x") + request_test() |> req_timeout(0) }) }) test_that("req_proxy gives helpful errors", { req <- request_test("/get") expect_snapshot(error = TRUE, { - req %>% req_proxy(port = "abc") - req %>% req_proxy("abc", auth = "bsc") + req |> req_proxy(port = "abc") + req |> req_proxy("abc", auth = "bsc") }) }) diff --git a/tests/testthat/test-req-perform-connection.R b/tests/testthat/test-req-perform-connection.R index 2f0e56d04..46dd59cde 100644 --- a/tests/testthat/test-req-perform-connection.R +++ b/tests/testthat/test-req-perform-connection.R @@ -6,13 +6,13 @@ test_that("validates inputs", { }) test_that("correctly prepares request", { - req <- request_test("/post") %>% req_method("POST") + req <- request_test("/post") |> req_method("POST") expect_no_error(resp <- req_perform_connection(req)) close(resp) }) test_that("can read all data from a connection", { - resp <- request_test("/stream-bytes/2048") %>% req_perform_connection() + resp <- request_test("/stream-bytes/2048") |> req_perform_connection() withr::defer(close(resp)) out <- resp_body_raw(resp) diff --git a/tests/testthat/test-req-perform-iterative.R b/tests/testthat/test-req-perform-iterative.R index 8c0ebff98..3a6afae8f 100644 --- a/tests/testthat/test-req-perform-iterative.R +++ b/tests/testthat/test-req-perform-iterative.R @@ -1,5 +1,5 @@ test_that("can perform multiple requests", { - req <- request(example_url("/iris")) %>% + req <- request(example_url("/iris")) |> req_url_query(limit = 5) resps <- req_perform_iterative( @@ -16,7 +16,7 @@ test_that("can perform multiple requests", { }) test_that("can save results to disk", { - req <- request(example_url("/iris")) %>% + req <- request(example_url("/iris")) |> req_url_query(limit = 5) dir <- withr::local_tempdir() @@ -33,7 +33,7 @@ test_that("can save results to disk", { }) test_that("user temination still returns data", { - req <- request(example_url("/iris")) %>% + req <- request(example_url("/iris")) |> req_url_query(limit = 5) next_req <- function(resp, req) interrupt() @@ -45,14 +45,14 @@ test_that("user temination still returns data", { test_that("can retrieve all pages", { - req <- request(example_url("/iris")) %>% + req <- request(example_url("/iris")) |> req_url_query(limit = 1) i <- 1 next_req <- function(resp, req) { i <<- i + 1 if (i <= 120) { - req %>% req_url_query(page_index = 1) + req |> req_url_query(page_index = 1) } } expect_condition( diff --git a/tests/testthat/test-req-perform-parallel.R b/tests/testthat/test-req-perform-parallel.R index 580ce613f..f48979b24 100644 --- a/tests/testthat/test-req-perform-parallel.R +++ b/tests/testthat/test-req-perform-parallel.R @@ -39,7 +39,7 @@ test_that("requests happen in parallel", { test_that("can perform >128 file uploads in parallel", { temp <- withr::local_tempfile(lines = letters) - req <- request(example_url()) %>% req_body_file(temp) + req <- request(example_url()) |> req_body_file(temp) reqs <- rep(list(req), 130) expect_no_error(req_perform_parallel(reqs, on_error = "continue")) @@ -68,7 +68,7 @@ test_that("can download 0 byte file", { test_that("objects are cached", { temp <- withr::local_tempdir() - req <- request_test("etag/:etag", etag = "abcd") %>% req_cache(temp) + req <- request_test("etag/:etag", etag = "abcd") |> req_cache(temp) expect_condition( resps1 <- req_perform_parallel(list(req)), @@ -82,7 +82,7 @@ test_that("objects are cached", { }) test_that("immutable objects retrieved from cache", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response( 200, headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT", @@ -138,8 +138,14 @@ test_that("errors can cancel outstanding requests", { test_that("req_perform_parallel resspects http_error() error override", { reqs <- list2( - req_error(request_test("/status/:status", status = 404), is_error = ~FALSE), - req_error(request_test("/status/:status", status = 500), is_error = ~FALSE) + req_error( + request_test("/status/:status", status = 404), + is_error = \(resp) FALSE + ), + req_error( + request_test("/status/:status", status = 500), + is_error = \(resp) FALSE + ) ) resps <- req_perform_parallel(reqs) @@ -149,7 +155,10 @@ test_that("req_perform_parallel resspects http_error() error override", { test_that("req_perform_parallel respects http_error() body message", { reqs <- list2( - req_error(request_test("/status/:status", status = 404), body = ~"hello") + req_error( + request_test("/status/:status", status = 404), + body = \(resp) "hello" + ) ) expect_snapshot(req_perform_parallel(reqs), error = TRUE) }) @@ -164,7 +173,7 @@ test_that("requests are throttled", { ) req <- request_test("/status/:status", status = 200) - req <- req %>% req_throttle(capacity = 1, fill_time_s = 1) + req <- req |> req_throttle(capacity = 1, fill_time_s = 1) reqs <- rep(list(req), 5) queue <- RequestQueue$new(reqs, progress = FALSE) diff --git a/tests/testthat/test-req-perform-stream.R b/tests/testthat/test-req-perform-stream.R index 93e9392f9..ff12b9da5 100644 --- a/tests/testthat/test-req-perform-stream.R +++ b/tests/testthat/test-req-perform-stream.R @@ -30,11 +30,11 @@ test_that("HTTP errors become R errors", { }) test_that("can override error handling", { - req <- request_test("/base64/:value", value = "YWJj") %>% + req <- request_test("/base64/:value", value = "YWJj") |> req_error(is_error = function(resp) TRUE) expect_error( - req %>% req_perform_stream(function(x) NULL), + req |> req_perform_stream(function(x) NULL), class = "httr2_http_200" ) @@ -53,7 +53,7 @@ test_that("can buffer to lines", { } # Each line is 225 bytes, should should be split into ~2 pieces - resp <- request_test("/stream/10") %>% + resp <- request_test("/stream/10") |> req_perform_stream(accumulate_lines, buffer_kb = 0.1, round = "line") expect_equal(length(lines), 10) @@ -68,7 +68,7 @@ test_that("can supply custom rounding", { TRUE } - resp <- request_test("/stream-bytes/1024") %>% + resp <- request_test("/stream-bytes/1024") |> req_perform_stream( accumulate, buffer_kb = 0.1, @@ -84,7 +84,7 @@ test_that("eventually terminates even if never rounded", { TRUE } - resp <- request_test("/stream-bytes/1024") %>% + resp <- request_test("/stream-bytes/1024") |> req_perform_stream( accumulate, buffer_kb = 0.1, diff --git a/tests/testthat/test-req-perform.R b/tests/testthat/test-req-perform.R index 1a432b0c8..4d410d4db 100644 --- a/tests/testthat/test-req-perform.R +++ b/tests/testthat/test-req-perform.R @@ -56,8 +56,8 @@ test_that("http errors become errors", { req <- request_test("/status/:status", status = 429) expect_snapshot(req_perform(req), error = TRUE) - req_perform(req) %>% - expect_error(class = "httr2_http_429") %>% + req_perform(req) |> + expect_error(class = "httr2_http_429") |> expect_no_condition(class = "httr2_sleep") # non-standard status codes don't get descriptions @@ -66,28 +66,28 @@ test_that("http errors become errors", { }) test_that("can force successful HTTP statuses to error", { - req <- request_test("/status/:status", status = 200) %>% + req <- request_test("/status/:status", status = 200) |> req_error(is_error = function(resp) TRUE) expect_error(req_perform(req), class = "httr2_http_200") }) test_that("persistent HTTP errors only get single attempt", { - req <- request_test("/status/:status", status = 404) %>% + req <- request_test("/status/:status", status = 404) |> req_retry(max_tries = 5) - cnd <- req_perform(req) %>% - expect_error(class = "httr2_http_404") %>% + cnd <- req_perform(req) |> + expect_error(class = "httr2_http_404") |> catch_cnd("httr2_fetch") expect_equal(cnd$n, 1) }) test_that("don't retry curl errors by default", { - req <- request("") %>% req_retry(max_tries = 2, failure_realm = "x") + req <- request("") |> req_retry(max_tries = 2, failure_realm = "x") expect_error(req_perform(req), class = "httr2_failure") # But can opt-in to it - req <- request("") %>% + req <- request("") |> req_retry(max_tries = 2, retry_on_failure = TRUE, failure_realm = "x") cnd <- catch_cnd(req_perform(req), "httr2_retry") expect_equal(cnd$tries, 1) @@ -113,11 +113,11 @@ test_that("can retry a transient error", { test_that("repeated transient errors still fail", { - req <- request_test("/status/:status", status = 429) %>% - req_retry(max_tries = 3, backoff = ~0) + req <- request_test("/status/:status", status = 429) |> + req_retry(max_tries = 3, backoff = \(i) 0) - cnd <- req_perform(req) %>% - expect_error(class = "httr2_http_429") %>% + cnd <- req_perform(req) |> + expect_error(class = "httr2_http_429") |> catch_cnd("httr2_fetch") expect_equal(cnd$n, 3) }) @@ -130,7 +130,7 @@ test_that("can download 0 byte file", { }) test_that("can cache requests with etags", { - req <- request_test("/etag/:etag", etag = "abc") %>% req_cache(tempfile()) + req <- request_test("/etag/:etag", etag = "abc") |> req_cache(tempfile()) resp1 <- req_perform(req) expect_condition( @@ -143,19 +143,19 @@ test_that("can cache requests with etags", { }) test_that("can cache requests with paths (cache-control)", { - req <- request(example_url("/cache/2")) %>% + req <- request(example_url("/cache/2")) |> req_cache(withr::local_tempfile()) path1 <- withr::local_tempfile() expect_condition( - resp1 <- req %>% req_perform(path = path1), + resp1 <- req |> req_perform(path = path1), class = "httr2_cache_save" ) expect_equal(resp1$body[[1]], path1) path2 <- withr::local_tempfile() expect_condition( - resp2 <- req %>% req_perform(path = path2), + resp2 <- req |> req_perform(path = path2), class = "httr2_cache_cached" ) expect_equal(resp2$body[[1]], path2) @@ -167,19 +167,19 @@ test_that("can cache requests with paths (cache-control)", { path3 <- withr::local_tempfile() expect_condition( - resp3 <- req %>% req_perform(path = path3), + resp3 <- req |> req_perform(path = path3), class = "httr2_cache_save" ) expect_equal(resp3$body[[1]], path3) }) test_that("can cache requests with paths (if-modified-since)", { - req <- request(example_url("/cache")) %>% + req <- request(example_url("/cache")) |> req_cache(tempfile()) path1 <- tempfile() expect_condition( - resp1 <- req %>% req_perform(path = path1), + resp1 <- req |> req_perform(path = path1), class = "httr2_cache_save" ) expect_equal(resp1$body[[1]], path1) @@ -187,7 +187,7 @@ test_that("can cache requests with paths (if-modified-since)", { path2 <- tempfile() expect_condition( expect_condition( - resp2 <- req %>% req_perform(path = path2), + resp2 <- req |> req_perform(path = path2), class = "httr2_cache_not_modified" ), class = "httr2_cache_save" diff --git a/tests/testthat/test-req-promise.R b/tests/testthat/test-req-promise.R index 52f3171ed..2d54a599b 100644 --- a/tests/testthat/test-req-promise.R +++ b/tests/testthat/test-req-promise.R @@ -21,7 +21,7 @@ test_that("returns a promise that resolves", { }) test_that("correctly prepares request", { - req <- request_test("/post") %>% req_method("POST") + req <- request_test("/post") |> req_method("POST") prom <- req_perform_promise(req) expect_no_error(extract_promise(prom)) }) @@ -53,7 +53,7 @@ test_that("can promise to download files", { }) test_that("promises can retrieve from cache", { - req <- request("http://example.com") %>% req_cache(tempfile()) + req <- request("http://example.com") |> req_cache(tempfile()) resp <- response( 200, headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT", diff --git a/tests/testthat/test-req-retries.R b/tests/testthat/test-req-retries.R index 82190ca56..8c7b8f5d7 100644 --- a/tests/testthat/test-req-retries.R +++ b/tests/testthat/test-req-retries.R @@ -28,7 +28,11 @@ test_that("can override default is_transient", { expect_equal(retry_is_transient(req, response(404)), FALSE) expect_equal(retry_is_transient(req, response(429)), TRUE) - req <- req_retry(req, max_tries = 2, is_transient = ~ resp_status(.x) == 404) + req <- req_retry( + req, + max_tries = 2, + is_transient = \(resp) resp_status(resp) == 404 + ) expect_equal(retry_is_transient(req, response(404)), TRUE) expect_equal(retry_is_transient(req, response(429)), FALSE) }) @@ -41,7 +45,7 @@ test_that("can override default backoff", { expect_equal(retry_backoff(req, 5), 26.9) expect_equal(retry_backoff(req, 10), 60) - req <- req_retry(req, max_tries = 2, backoff = ~10) + req <- req_retry(req, max_tries = 2, backoff = \(i) 10) expect_equal(retry_backoff(req, 1), 10) expect_equal(retry_backoff(req, 5), 10) expect_equal(retry_backoff(req, 10), 10) @@ -55,20 +59,20 @@ test_that("can override default retry wait", { req <- req_retry( req, max_tries = 2, - after = ~ as.numeric(resp_header(.x, "Wait-For")) + after = \(resp) as.numeric(resp_header(resp, "Wait-For")) ) expect_equal(retry_after(req, resp, 1), 20) }) test_that("missing retry-after uses backoff", { req <- request_test() - req <- req_retry(req, max_tries = 2, backoff = ~10) + req <- req_retry(req, max_tries = 2, backoff = \(i) 10) expect_equal(retry_after(req, response(429), 1), 10) }) test_that("useful message if `after` wrong", { - req <- request_test() %>% + req <- request_test() |> req_retry( is_transient = function(resp) TRUE, after = function(resp) resp, @@ -102,7 +106,7 @@ test_that("is_number_or_na implemented correctly", { # circuit breaker -------------------------------------------------------- test_that("triggered after specified requests", { - req <- request_test("/status/:status", status = 429) %>% + req <- request_test("/status/:status", status = 429) |> req_retry( after = function(resp) 0, max_tries = 10, @@ -110,20 +114,20 @@ test_that("triggered after specified requests", { ) # First attempt performs, retries, then errors - req_perform(req) %>% - expect_condition(class = "httr2_perform") %>% - expect_condition(class = "httr2_retry") %>% + req_perform(req) |> + expect_condition(class = "httr2_perform") |> + expect_condition(class = "httr2_retry") |> expect_error(class = "httr2_breaker") # Second attempt errors without performing - req_perform(req) %>% - expect_no_condition(class = "httr2_perform") %>% + req_perform(req) |> + expect_no_condition(class = "httr2_perform") |> expect_error(class = "httr2_breaker") # Attempt on same realm errors without trying at all - req2 <- request_test("/status/:status", status = 200) %>% + req2 <- request_test("/status/:status", status = 200) |> req_retry(max_tries = 2) - req_perform(req) %>% - expect_no_condition(class = "httr2_perform") %>% + req_perform(req) |> + expect_no_condition(class = "httr2_perform") |> expect_error(class = "httr2_breaker") }) diff --git a/tests/testthat/test-req-template.R b/tests/testthat/test-req-template.R index 892970f0f..75c13f8b6 100644 --- a/tests/testthat/test-req-template.R +++ b/tests/testthat/test-req-template.R @@ -1,25 +1,25 @@ test_that("can set path", { - req <- request("http://test.com") %>% req_template("/x") + req <- request("http://test.com") |> req_template("/x") expect_equal(req$url, "http://test.com/x") }) test_that("can set method and path", { - req <- request("http://test.com") %>% req_template("PATCH /x") + req <- request("http://test.com") |> req_template("PATCH /x") expect_equal(req$url, "http://test.com/x") expect_equal(req$method, "PATCH") }) test_that("can use args or env", { x <- "x" - req <- request("http://test.com") %>% req_template("/:x") + req <- request("http://test.com") |> req_template("/:x") expect_equal(req$url, "http://test.com/x") - req <- request("http://test.com") %>% req_template("/:x", x = "y") + req <- request("http://test.com") |> req_template("/:x", x = "y") expect_equal(req$url, "http://test.com/y") }) test_that("will append rather than replace path", { - req <- request("http://test.com/x") %>% req_template("PATCH /y") + req <- request("http://test.com/x") |> req_template("PATCH /y") expect_equal(req$url, "http://test.com/x/y") }) diff --git a/tests/testthat/test-req-throttle.R b/tests/testthat/test-req-throttle.R index cfc346eee..3fe807537 100644 --- a/tests/testthat/test-req-throttle.R +++ b/tests/testthat/test-req-throttle.R @@ -3,7 +3,7 @@ test_that("throttling affects request performance", { on.exit(throttle_reset()) local_mocked_bindings(unix_time = function() 0) - req <- request_test() %>% req_throttle(capacity = 4, fill_time_s = 1) + req <- request_test() |> req_throttle(capacity = 4, fill_time_s = 1) . <- replicate(4, req_perform(req)) local_mocked_bindings(unix_time = function() 0.1) @@ -17,7 +17,7 @@ test_that("first request isn't throttled", { mock_time <- 0 local_mocked_bindings(unix_time = function() mock_time) - req <- request_test() %>% req_throttle(rate = 1, fill_time_s = 1) + req <- request_test() |> req_throttle(rate = 1, fill_time_s = 1) expect_equal(throttle_delay(req), 0) mock_time <- 0.1 @@ -32,11 +32,11 @@ test_that("realm defaults to hostname but can be overridden", { expect_named(the$throttle, character()) - request_test() %>% req_throttle(100 / 1) + request_test() |> req_throttle(100 / 1) expect_named(the$throttle, "127.0.0.1") throttle_reset() - request_test() %>% req_throttle(100 / 1, realm = "custom") + request_test() |> req_throttle(100 / 1, realm = "custom") expect_named(the$throttle, "custom") }) diff --git a/tests/testthat/test-req-url.R b/tests/testthat/test-req-url.R index 20e1a259a..75dd4bf36 100644 --- a/tests/testthat/test-req-url.R +++ b/tests/testthat/test-req-url.R @@ -132,10 +132,10 @@ test_that("can handle multi query params", { test_that("errors are forwarded correctly", { req <- request("http://example.com/") expect_snapshot(error = TRUE, { - req %>% req_url_query(1) - req %>% req_url_query(a = I(1)) - req %>% req_url_query(a = 1:2) - req %>% req_url_query(a = mean) + req |> req_url_query(1) + req |> req_url_query(a = I(1)) + req |> req_url_query(a = 1:2) + req |> req_url_query(a = mean) }) }) diff --git a/tests/testthat/test-req-verbose.R b/tests/testthat/test-req-verbose.R index 7fb665901..4f9ae0e17 100644 --- a/tests/testthat/test-req-verbose.R +++ b/tests/testthat/test-req-verbose.R @@ -2,12 +2,12 @@ test_that("can request verbose record of request", { req <- local_app_request(method = "post", function(req, res) { res$send_json(list(x = 1), auto_unbox = TRUE) }) - req <- req %>% - req_body_raw("This is some text", "text/plain") %>% + req <- req |> + req_body_raw("This is some text", "text/plain") |> req_verbose_test() # Snapshot test of response - verbose_resp <- req %>% + verbose_resp <- req |> req_verbose(header_resp = TRUE, body_resp = TRUE, header_req = FALSE) expect_snapshot( . <- req_perform(verbose_resp), @@ -15,29 +15,29 @@ test_that("can request verbose record of request", { ) # Snapshot test of request - verbose_req <- req %>% + verbose_req <- req |> req_verbose(header_req = TRUE, body_req = TRUE, header_resp = FALSE) expect_snapshot(. <- req_perform(verbose_req)) # Lightweight test for everything else - verbose_info <- req %>% + verbose_info <- req |> req_verbose(info = TRUE, header_req = FALSE, header_resp = FALSE) expect_output(. <- req_perform(verbose_info)) }) test_that("redacts headers as needed", { - req <- request(example_url()) %>% - req_verbose_test() %>% - req_verbose(header_req = TRUE, header_resp = FALSE) %>% + req <- request(example_url()) |> + req_verbose_test() |> + req_verbose(header_req = TRUE, header_resp = FALSE) |> req_headers_redacted(Authorization = "abc") expect_snapshot(. <- req_perform(req)) }) test_that("can display compressed bodies", { - req <- request(example_url()) %>% - req_url_path("gzip") %>% - req_verbose_test() %>% + req <- request(example_url()) |> + req_url_path("gzip") |> + req_verbose_test() |> req_verbose(header_req = FALSE, header_resp = TRUE, body_resp = TRUE) expect_snapshot(. <- req_perform(req), transform = transform_verbose_response) @@ -49,8 +49,8 @@ test_that("response json is automatically prettified", { res$send('{"foo":"bar","baz":[1,2,3]}') }) - req <- req %>% - req_verbose_test() %>% + req <- req |> + req_verbose_test() |> req_verbose(body_resp = TRUE, header_resp = FALSE, header_req = FALSE) expect_snapshot(. <- req_perform(req)) @@ -60,9 +60,9 @@ test_that("response json is automatically prettified", { }) test_that("request json is automatically prettified", { - req <- request(example_url("/post")) %>% - req_verbose_test() %>% - req_body_json(list(foo = "bar", baz = c(1, 2, 3))) %>% + req <- request(example_url("/post")) |> + req_verbose_test() |> + req_body_json(list(foo = "bar", baz = c(1, 2, 3))) |> req_verbose(body_req = TRUE, header_resp = FALSE, header_req = FALSE) expect_snapshot(. <- req_perform(req)) diff --git a/tests/testthat/test-req.R b/tests/testthat/test-req.R index e002dcb86..8ae7dc882 100644 --- a/tests/testthat/test-req.R +++ b/tests/testthat/test-req.R @@ -2,8 +2,8 @@ test_that("req has basic print method", { expect_snapshot({ req <- request("https://example.com") req - req %>% req_body_raw("Test") - req %>% req_body_multipart("Test" = 1) + req |> req_body_raw("Test") + req |> req_body_multipart("Test" = 1) }) }) @@ -12,11 +12,11 @@ test_that("printing headers works with {}", { }) test_that("individually prints repeated headers", { - expect_snapshot(request("https://example.com") %>% req_headers(A = 1:3)) + expect_snapshot(request("https://example.com") |> req_headers(A = 1:3)) }) test_that("print method obfuscates Authorization header unless requested", { - req <- request("https://example.com") %>% + req <- request("https://example.com") |> req_auth_basic("user", "SECRET") output <- testthat::capture_messages(print(req)) expect_false(any(grepl("SECRET", output, fixed = TRUE))) diff --git a/tests/testthat/test-resp-body.R b/tests/testthat/test-resp-body.R index 3a00681ab..f580fdade 100644 --- a/tests/testthat/test-resp-body.R +++ b/tests/testthat/test-resp-body.R @@ -1,50 +1,50 @@ test_that("read body from disk/memory", { - resp1 <- request_test("base64/:value", value = "SGk=") %>% req_perform() + resp1 <- request_test("base64/:value", value = "SGk=") |> req_perform() expect_true(resp_has_body(resp1)) expect_equal(resp_body_raw(resp1), charToRaw("Hi")) expect_equal(resp_body_string(resp1), "Hi") - resp2 <- request_test("base64/:value", value = "SGk=") %>% + resp2 <- request_test("base64/:value", value = "SGk=") |> req_perform(tempfile()) expect_true(resp_has_body(resp2)) expect_equal(resp_body_string(resp2), "Hi") }) test_that("empty body generates error", { - resp1 <- request_test("HEAD /get") %>% req_perform() + resp1 <- request_test("HEAD /get") |> req_perform() expect_false(resp_has_body(resp1)) expect_snapshot(resp_body_raw(resp1), error = TRUE) - resp2 <- request_test("HEAD /get") %>% req_perform(tempfile()) + resp2 <- request_test("HEAD /get") |> req_perform(tempfile()) expect_false(resp_has_body(resp2)) expect_snapshot(resp_body_raw(resp2), error = TRUE) }) test_that("can retrieve parsed body", { - resp <- request_test("/json") %>% req_perform() + resp <- request_test("/json") |> req_perform() expect_type(resp_body_json(resp), "list") - resp <- request_test("/html") %>% req_perform() + resp <- request_test("/html") |> req_perform() expect_s3_class(resp_body_html(resp), "xml_document") - resp <- request_test("/xml") %>% req_perform() + resp <- request_test("/xml") |> req_perform() expect_s3_class(resp_body_xml(resp), "xml_document") }) test_that("can retrieve parsed body when saved to a file", { path <- withr::local_tempfile() - resp <- request_test("/json") %>% req_perform(path) + resp <- request_test("/json") |> req_perform(path) expect_type(resp_body_json(resp), "list") - resp <- request_test("/html") %>% req_perform(path) + resp <- request_test("/html") |> req_perform(path) expect_s3_class(resp_body_html(resp), "xml_document") - resp <- request_test("/xml") %>% req_perform(path) + resp <- request_test("/xml") |> req_perform(path) expect_s3_class(resp_body_xml(resp), "xml_document") }) test_that("resp_body_json stores parsed result", { - resp <- request_test("/json") %>% req_perform() + resp <- request_test("/json") |> req_perform() json1 <- resp_body_json(resp) # check it's saved expect_length(resp$cache, 1) @@ -60,7 +60,7 @@ test_that("resp_body_json stores parsed result", { }) test_that("resp_body_xml stores parsed result", { - resp <- request_test("/xml") %>% req_perform() + resp <- request_test("/xml") |> req_perform() xml1 <- resp_body_xml(resp) # check it's saved expect_length(resp$cache, 1) @@ -79,11 +79,11 @@ test_that("check argument types before caching", { test_that("content types are checked", { expect_snapshot(error = TRUE, { - request_test("/xml") %>% req_perform() %>% resp_body_json() - request_test("/json") %>% req_perform() %>% resp_body_xml() + request_test("/xml") |> req_perform() |> resp_body_json() + request_test("/json") |> req_perform() |> resp_body_xml() }) - resp <- request_test("/json") %>% req_perform() + resp <- request_test("/json") |> req_perform() resp$headers$`Content-Type` <- "application/xml" expect_error(resp_body_json(resp)) expect_no_error(resp_body_json(resp, check_type = FALSE)) diff --git a/tests/testthat/test-resp-stream.R b/tests/testthat/test-resp-stream.R index 9e72046b9..408df5aa9 100644 --- a/tests/testthat/test-resp-stream.R +++ b/tests/testthat/test-resp-stream.R @@ -1,5 +1,5 @@ test_that("can stream bytes from a connection", { - resp <- request_test("/stream-bytes/2048") %>% req_perform_connection() + resp <- request_test("/stream-bytes/2048") |> req_perform_connection() withr::defer(close(resp)) expect_s3_class(resp, "httr2_response") @@ -16,7 +16,7 @@ test_that("can stream bytes from a connection", { }) test_that("can determine if a stream is complete (blocking)", { - resp <- request_test("/stream-bytes/2048") %>% req_perform_connection() + resp <- request_test("/stream-bytes/2048") |> req_perform_connection() withr::defer(close(resp)) expect_false(resp_stream_is_complete(resp)) @@ -26,7 +26,7 @@ test_that("can determine if a stream is complete (blocking)", { }) test_that("can determine if a stream is complete (non-blocking)", { - resp <- request_test("/stream-bytes/2048") %>% + resp <- request_test("/stream-bytes/2048") |> req_perform_connection(blocking = FALSE) withr::defer(close(resp)) @@ -42,7 +42,7 @@ test_that("can determine if incomplete data is complete", { res$send_chunk("data: ") }) - con <- req %>% req_perform_connection(blocking = TRUE) + con <- req |> req_perform_connection(blocking = TRUE) withr::defer(close(con)) expect_equal( @@ -54,7 +54,7 @@ test_that("can determine if incomplete data is complete", { }) test_that("can't read from a closed connection", { - resp <- request_test("/stream-bytes/1024") %>% req_perform_connection() + resp <- request_test("/stream-bytes/1024") |> req_perform_connection() close(resp) expect_false(resp_has_body(resp)) diff --git a/tests/testthat/test-url.R b/tests/testthat/test-url.R index dea7a0ec5..ae39fe474 100644 --- a/tests/testthat/test-url.R +++ b/tests/testthat/test-url.R @@ -17,7 +17,7 @@ test_that("can round trip urls", { "svn+ssh://my.svn.server/repo/trunk" ) - expect_equal(map(urls, ~ url_build(url_parse(.x))), urls) + expect_equal(map(urls, \(url) url_build(url_parse(url))), urls) }) test_that("can parse relative urls", { diff --git a/tools/examples.R b/tools/examples.R deleted file mode 100644 index fed67ea7a..000000000 --- a/tools/examples.R +++ /dev/null @@ -1,15 +0,0 @@ -if (getRversion() < "4.1") { - dir.create("man/macros", showWarnings = FALSE, recursive = TRUE) - cat( - paste( - "\\renewcommand{\\examples}{\\section{Examples}{", - "These examples are designed to work in R >= 4.1 so that we can take", - "advantage of modern syntax like the base pipe (\\verb{|>}) and the ", - "function shorthand (\\verb{\\(x) x + 1}). They might not work on the ", - "version of R that you're using.", - "\\preformatted{#1}}}", - collapse = "" - ), - file = "man/macros/examples.Rd" - ) -} diff --git a/vignettes/articles/wrapping-apis.Rmd b/vignettes/articles/wrapping-apis.Rmd index 4de090971..fcc8e1a0a 100644 --- a/vignettes/articles/wrapping-apis.Rmd +++ b/vignettes/articles/wrapping-apis.Rmd @@ -38,12 +38,12 @@ library(httr2) # by only evaluating other chunks if a simple request succeeds. faker_status_images <- request("https://fakerapi.it/api/v1") |> req_url_path_append("images") |> - req_error(is_error = ~FALSE) |> + req_error(is_error = \(resp) FALSE) |> req_perform() |> resp_status() faker_status_persons <- request("https://fakerapi.it/api/v1") |> req_url_path_append("persons") |> - req_error(is_error = ~FALSE) |> + req_error(is_error = \(resp) FALSE) |> req_perform() |> resp_status() diff --git a/vignettes/httr2.Rmd b/vignettes/httr2.Rmd index 5ae5ceba4..e1a5b07d8 100644 --- a/vignettes/httr2.Rmd +++ b/vignettes/httr2.Rmd @@ -187,7 +187,7 @@ request(example_url()) |> ``` This is another important difference to httr, which required that you explicitly call `httr::stop_for_status()` to turn HTTP errors into R errors. -You can revert to the httr behaviour with `req_error(req, is_error = ~ FALSE)`. +You can revert to the httr behaviour with `req_error(req, is_error = \(resp) FALSE)`. ## Control the request process