Skip to content

Commit

Permalink
When updating cache, need to include body (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Sep 9, 2024
1 parent 772d08c commit a546ac7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ cache_post_fetch <- function(req, resp, path = NULL) {
signal("", "httr2_cache_not_modified")
if (debug) cli::cli_text("Cached value still ok; retrieving body from cache")

# Combine headers & re-cache
# Combine headers
resp$headers <- cache_headers(cached_resp, resp)
cache_set(req, resp)

# Replace body with cached result
resp$body <- cache_body(cached_resp, path)

# Re-cache, so we get any new headers
cache_set(req, resp)
resp
} else if (resp_is_cacheable(resp)) {
if (debug) cli::cli_text("Saving response to cache {.val {hash(req$url)}}")
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ test_that("304 retains headers but gets cached body", {
cached <- cache_post_fetch(req, response(304, headers = "X: 2"))
expect_equal(cached$headers$x, "2")
expect_equal(cached$body, resp$body)

cached <- cache_post_fetch(req, response(304, headers = "X: 3"))
expect_equal(cached$headers$x, "3")
expect_equal(cached$body, resp$body)
})

test_that("automatically adds to cache", {
Expand Down

0 comments on commit a546ac7

Please sign in to comment.