diff --git a/test/src/fetch.rs b/test/src/fetch.rs index d5c351bc6..7b05fb0a5 100644 --- a/test/src/fetch.rs +++ b/test/src/fetch.rs @@ -3,8 +3,8 @@ use futures_util::future::Either; use serde::{Deserialize, Serialize}; use std::time::Duration; use worker::{ - wasm_bindgen_futures, AbortController, Delay, EncodeBody, Env, Fetch, Method, Request, - RequestInit, Response, Result, + wasm_bindgen_futures, AbortController, CacheMode, CfProperties, Delay, EncodeBody, Env, Fetch, + Method, Request, RequestInit, Response, Result, }; #[worker::send] @@ -209,3 +209,28 @@ pub async fn handle_cloned_response_attributes( Response::ok("true") } + +#[worker::send] +pub async fn handle_fetch_with_cache_ttl_negative( + _req: Request, + _env: Env, + _data: SomeSharedData, +) -> Result { + // Test that cache_ttl: Some(-1) works with CacheMode::NoStore + // According to Cloudflare docs, a negative cache_ttl instructs Cloudflare not to cache at all + let mut cf_props = CfProperties::new(); + cf_props.cache_ttl = Some(-1); + + let mut init = RequestInit::new(); + init.with_cf_properties(cf_props); + init.with_cache(CacheMode::NoStore); + + let resp = Fetch::Request(Request::new_with_init("https://www.google.com", &init)?) + .send() + .await?; + + Response::ok(format!( + "fetch with cache_ttl=-1 and CacheMode::NoStore succeeded with status {}", + resp.status_code() + )) +} diff --git a/test/src/router.rs b/test/src/router.rs index 4ca07033e..c80cdac36 100644 --- a/test/src/router.rs +++ b/test/src/router.rs @@ -135,6 +135,7 @@ macro_rules! add_routes ( add_route!($obj, get, "/fetch",fetch::handle_fetch); add_route!($obj, get, "/fetch_json",fetch::handle_fetch_json); add_route!($obj, get, format_route!("/proxy_request/{}", "*url") ,fetch::handle_proxy_request); + add_route!($obj, get, "/fetch-cache-ttl-negative", fetch::handle_fetch_with_cache_ttl_negative); add_route!($obj, get, "/durable/alarm", alarm::handle_alarm); add_route!($obj, get, format_route!("/durable/{}", "id"), counter::handle_id); add_route!($obj, get, "/durable/put-raw", put_raw::handle_put_raw); diff --git a/test/tests/cf_properties.spec.ts b/test/tests/cf_properties.spec.ts new file mode 100644 index 000000000..a1a4325a0 --- /dev/null +++ b/test/tests/cf_properties.spec.ts @@ -0,0 +1,13 @@ +import { describe, test, expect } from "vitest"; +import { mf, mfUrl } from "./mf"; + +describe("CfProperties", () => { + test("fetch with cache_ttl=-1 and CacheMode::NoStore", async () => { + const resp = await mf.dispatchFetch(`${mfUrl}fetch-cache-ttl-negative`); + expect(resp.status).toBe(200); + const text = await resp.text(); + expect(text).toContain("succeeded with status"); + // Verify the fetch to google.com was successful (should return 200 or similar) + expect(text).toMatch(/status (200|301|302)/); + }); +}); diff --git a/test/tests/mf.ts b/test/tests/mf.ts index 7a63c7d39..3981c1955 100644 --- a/test/tests/mf.ts +++ b/test/tests/mf.ts @@ -32,6 +32,11 @@ mockAgent } ); +mockAgent + .get("https://www.google.com") + .intercept({ path: "/" }) + .reply(200, "GoogleMock Google"); + const mf_instance = new Miniflare({ d1Persist: false, kvPersist: false,