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

Record requests in responses and errors #359

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Check inputs before caching
  • Loading branch information
hadley committed Oct 27, 2023
commit 5e1572889a580e7a7c461becbf5925ce20f0ab30
11 changes: 6 additions & 5 deletions R/resp-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ resp_body_string <- function(resp, encoding = NULL) {
#' @rdname resp_body_raw
#' @export
resp_body_json <- function(resp, check_type = TRUE, simplifyVector = FALSE, ...) {
check_response(resp)
check_installed("jsonlite")

key <- body_cache_key("json", simplifyVector = simplifyVector, ...)
if (env_has(resp$cache, key)) {
return(resp$cache[[key]])
}

check_response(resp)
check_installed("jsonlite")
resp_check_content_type(
resp,
valid_types = "application/json",
Expand Down Expand Up @@ -114,14 +115,14 @@ resp_body_html <- function(resp, check_type = TRUE, ...) {
#' @rdname resp_body_raw
#' @export
resp_body_xml <- function(resp, check_type = TRUE, ...) {
check_response(resp)
check_installed("xml2")

key <- body_cache_key("xml", ...)
if (env_has(resp$cache, key)) {
return(resp$cache[[key]])
}


check_response(resp)
check_installed("xml2")
resp_check_content_type(
resp,
valid_types = c("application/xml", "text/xml"),
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/_snaps/resp-body.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
Error in `resp_body_raw()`:
! Can't retrieve empty body.

# check argument types before caching

Code
resp_body_json(1)
Condition
Error in `resp_body_json()`:
! `resp` must be an HTTP response object, not the number 1.
Code
resp_body_xml(1)
Condition
Error in `resp_body_xml()`:
! `resp` must be an HTTP response object, not the number 1.

# content types are checked

Code
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-resp-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ test_that("resp_body_xml stores parsed result", {
expect_true(is_reference(xml2, xml1))
})

test_that("check argument types before caching", {
expect_snapshot(error = TRUE, {
resp_body_json(1)
resp_body_xml(1)
})
})

test_that("content types are checked", {
expect_snapshot(error = TRUE, {
request_test("/xml") %>% req_perform() %>% resp_body_json()
Expand Down
Loading