diff --git a/R/req-cache.R b/R/req-cache.R index 0f518ee5..7be568cd 100644 --- a/R/req-cache.R +++ b/R/req-cache.R @@ -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)}}") diff --git a/tests/testthat/test-req-cache.R b/tests/testthat/test-req-cache.R index 63bfaaa3..a77d7d57 100644 --- a/tests/testthat/test-req-cache.R +++ b/tests/testthat/test-req-cache.R @@ -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", {