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

Only retrieve GET quieries from cache #573

Merged
merged 1 commit into from
Oct 25, 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
Expand Up @@ -2,6 +2,7 @@

# httr2 1.0.5

* `req_cache()` no longer retrieves anything but `GET` requests from the cache.
* New `resp_stream_aws()` to retrieve AWS's special streaming format. With thanks to <https://github.com/lifion/lifion-aws-event-stream/> for a simple reference implementation.
* New `req_auth_aws_v4()` signs request using AWS's special format (#562, #566).
* `req_perform_parallel()` and `req_perform_promise()` now correctly set up the method and body (#549).
Expand Down
6 changes: 6 additions & 0 deletions R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ cache_pre_fetch <- function(req, path = NULL) {
return(req)
}

# Only GET requests should be retrieved from cache. It's not sufficient to
# only save GET requests, because the method is not part of the cache key
if (req_method_get(req) != "GET") {
return(req)
}

debug <- cache_debug(req)
cache_prune_if_needed(req, debug = debug)

Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ test_that("nothing happens if cache not enabled", {
expect_equal(cache_post_fetch(req, resp), resp)
})

test_that("never retrieves POST request from cache", {
req <- request("http://example.com") %>%
req_method("POST") %>%
req_cache(tempfile())

# Fake an equivalent GET request in the cache
resp <- response(200,
headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT",
body = charToRaw("abc")
)
cache_set(req, resp)

expect_equal(cache_pre_fetch(req), req)
})

test_that("immutable objects retrieved directly from cache", {
req <- request("http://example.com") %>% req_cache(tempfile())
resp <- response(200,
Expand Down
Loading