Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure resp_body_html() and resp_body_xml() work with saved path #529

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_body_html()` and `resp_body_xml()` now work when `req_perform()` is given a path (#448).
* `req_body_file()` now works with files >64kb once more (#524).
* New `req_perform_connection()` for working with streaming data. Unlike `req_perform_stream()` which uses callbacks, `req_perform_connection()` returns a regular response object with a connection as the body. It's paired with `resp_stream_bytes()`, `resp_stream_lines()`, and `resp_stream_sse()` that allows you to stream chunks as you want them. Unlike `req_perform_stream()` it supports `req_retry()` (with @jcheng5, #519).

Expand Down
6 changes: 4 additions & 2 deletions R/resp-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
check_type = check_type
)

xml2::read_html(resp$body, ...)
body <- resp_body_raw(resp)
xml2::read_html(body, ...)

Check warning on line 129 in R/resp-body.R

View check run for this annotation

Codecov / codecov/patch

R/resp-body.R#L128-L129

Added lines #L128 - L129 were not covered by tests
}

#' @rdname resp_body_raw
Expand All @@ -146,7 +147,8 @@
check_type = check_type
)

resp$cache[[key]] <- xml2::read_xml(resp$body, ...)
body <- resp_body_raw(resp)
resp$cache[[key]] <- xml2::read_xml(body, ...)

Check warning on line 151 in R/resp-body.R

View check run for this annotation

Codecov / codecov/patch

R/resp-body.R#L150-L151

Added lines #L150 - L151 were not covered by tests
resp$cache[[key]]
}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-resp-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ test_that("can retrieve parsed body", {
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)
expect_type(resp_body_json(resp), "list")

resp <- request_test("/html") %>% req_perform(path)
expect_s3_class(resp_body_html(resp), "xml_document")

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()
json1 <- resp_body_json(resp)
Expand Down
Loading