Skip to content

Commit

Permalink
Parse multiple link headers (#606)
Browse files Browse the repository at this point in the history
Fixes #587
  • Loading branch information
hadley authored Jan 6, 2025
1 parent cf34644 commit 0dbb703
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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).
* New `url_modify()` makes it easier to modify an existing url (#464).
* New `req_url_relative()` for constructing relative urls (#449).
* `url_parse()` gains `base_url` argument so you can also use it to parse relative URLs (#449).
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
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 0dbb703

Please sign in to comment.