Skip to content

Commit

Permalink
Parse multiple link headers
Browse files Browse the repository at this point in the history
Fixes #587
  • Loading branch information
hadley committed Dec 23, 2024
1 parent 9db8f7e commit 3f2e33a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 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)

* `resp_link_url()` now works if there are multiple `Link` headers (#587).
* `url_parse()` now uses `curl::curl_parse_url()` which is much faster and more correct (#577).
* `req_retry()` now defaults to `max_tries = 2` with a message.
Set to `max_tries = 1` to disable retries.
Expand Down
4 changes: 3 additions & 1 deletion R/resp-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ resp_link_url <- function(resp, rel) {
return()
}

links <- parse_link(resp_header(resp, "Link"))
headers <- resp_headers(resp)
link_headers <- headers[names(headers) == "Link"]
links <- unlist(lapply(link_headers, parse_link), recursive = FALSE)
sel <- map_lgl(links, ~ .$rel == rel)
if (sum(sel) != 1L) {
return()
Expand Down
13 changes: 0 additions & 13 deletions man/oauth_token_cached.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-resp-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ test_that("can extract specified link url", {
expect_equal(resp_link_url(resp, "first"), NULL)
expect_equal(resp_link_url(response(), "first"), NULL)
})

test_that("can extract from multiple link headers", {
resp <- response(headers = c(
'Link: <https://example.com/1>; rel="next"',
'Link: <https://example.com/2>; rel="last"'
))
expect_equal(resp_link_url(resp, "next"), "https://example.com/1")
expect_equal(resp_link_url(resp, "last"), "https://example.com/2")
})

0 comments on commit 3f2e33a

Please sign in to comment.