Skip to content

Commit 059272e

Browse files
committed
1 parent 9806fe7 commit 059272e

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

test/src/fetch.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use futures_util::future::Either;
33
use serde::{Deserialize, Serialize};
44
use std::time::Duration;
55
use worker::{
6-
wasm_bindgen_futures, AbortController, Delay, EncodeBody, Env, Fetch, Method, Request,
7-
RequestInit, Response, Result,
6+
wasm_bindgen_futures, AbortController, CacheMode, CfProperties, Delay, EncodeBody, Env, Fetch,
7+
Method, Request, RequestInit, Response, Result,
88
};
99

1010
#[worker::send]
@@ -209,3 +209,28 @@ pub async fn handle_cloned_response_attributes(
209209

210210
Response::ok("true")
211211
}
212+
213+
#[worker::send]
214+
pub async fn handle_fetch_with_cache_ttl_negative(
215+
_req: Request,
216+
_env: Env,
217+
_data: SomeSharedData,
218+
) -> Result<Response> {
219+
// Test that cache_ttl: Some(-1) works with CacheMode::NoStore
220+
// According to Cloudflare docs, a negative cache_ttl instructs Cloudflare not to cache at all
221+
let mut cf_props = CfProperties::new();
222+
cf_props.cache_ttl = Some(-1);
223+
224+
let mut init = RequestInit::new();
225+
init.with_cf_properties(cf_props);
226+
init.with_cache(CacheMode::NoStore);
227+
228+
let resp = Fetch::Request(Request::new_with_init("https://www.google.com", &init)?)
229+
.send()
230+
.await?;
231+
232+
Response::ok(format!(
233+
"fetch with cache_ttl=-1 and CacheMode::NoStore succeeded with status {}",
234+
resp.status_code()
235+
))
236+
}

test/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ macro_rules! add_routes (
135135
add_route!($obj, get, "/fetch",fetch::handle_fetch);
136136
add_route!($obj, get, "/fetch_json",fetch::handle_fetch_json);
137137
add_route!($obj, get, format_route!("/proxy_request/{}", "*url") ,fetch::handle_proxy_request);
138+
add_route!($obj, get, "/fetch-cache-ttl-negative", fetch::handle_fetch_with_cache_ttl_negative);
138139
add_route!($obj, get, "/durable/alarm", alarm::handle_alarm);
139140
add_route!($obj, get, format_route!("/durable/{}", "id"), counter::handle_id);
140141
add_route!($obj, get, "/durable/put-raw", put_raw::handle_put_raw);

test/tests/cf_properties.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, test, expect } from "vitest";
2+
import { mf, mfUrl } from "./mf";
3+
4+
describe("CfProperties", () => {
5+
test("fetch with cache_ttl=-1 and CacheMode::NoStore", async () => {
6+
const resp = await mf.dispatchFetch(`${mfUrl}fetch-cache-ttl-negative`);
7+
expect(resp.status).toBe(200);
8+
const text = await resp.text();
9+
expect(text).toContain("succeeded with status");
10+
// Verify the fetch to google.com was successful (should return 200 or similar)
11+
expect(text).toMatch(/status (200|301|302)/);
12+
});
13+
});

test/tests/mf.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ mockAgent
3232
}
3333
);
3434

35+
mockAgent
36+
.get("https://www.google.com")
37+
.intercept({ path: "/" })
38+
.reply(200, "<!doctype html><html><head><title>Google</title></head><body>Mock Google</body></html>");
39+
3540
const mf_instance = new Miniflare({
3641
d1Persist: false,
3742
kvPersist: false,

0 commit comments

Comments
 (0)