Skip to content

Redirects need to be cached separately #117

@lukaslihotzki-f

Description

@lukaslihotzki-f

http-cache-reqwest does not cache redirects correctly. Redirects are followed by reqwest invisible to the http-cache, and then the final response of the redirect chain is cached for the original request. The following patch demonstrates this issue. The redirect from / to /final is uncachable, but the cache stores the cachable response for /final as /.

diff --git a/http-cache-reqwest/examples/reqwest_basic.rs b/http-cache-reqwest/examples/reqwest_basic.rs
index 1bc25d3..427ed44 100644
--- a/http-cache-reqwest/examples/reqwest_basic.rs
+++ b/http-cache-reqwest/examples/reqwest_basic.rs
@@ -7,13 +7,24 @@ use http_cache_reqwest::{CACacheManager, Cache};
 use reqwest::Client;
 use reqwest_middleware::ClientBuilder;
 use std::time::Instant;
-use wiremock::{matchers::method, Mock, MockServer, ResponseTemplate};
+use wiremock::{matchers::path, Mock, MockServer, ResponseTemplate};

 #[tokio::main]
 async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
     // Setup mock server with cacheable response
     let mock_server = MockServer::start().await;
-    Mock::given(method("GET"))
+    Mock::given(path("/"))
+        .respond_with(
+            ResponseTemplate::new(307)
+                .set_body_string("Hello from cached response!")
+                .append_header("cache-control", "no-cache, no-store")
+                .append_header("location", "/final")
+                .append_header("content-type", "text/plain"),
+        )
+        .mount(&mock_server)
+        .await;
+
+    Mock::given(path("/final"))
         .respond_with(
             ResponseTemplate::new(200)
                 .set_body_string("Hello from cached response!")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions