Skip to content
Open
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
29 changes: 27 additions & 2 deletions test/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<Response> {
// 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()
))
}
1 change: 1 addition & 0 deletions test/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/tests/cf_properties.spec.ts
Original file line number Diff line number Diff line change
@@ -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)/);
});
});
5 changes: 5 additions & 0 deletions test/tests/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ mockAgent
}
);

mockAgent
.get("https://www.google.com")
.intercept({ path: "/" })
.reply(200, "<!doctype html><html><head><title>Google</title></head><body>Mock Google</body></html>");

const mf_instance = new Miniflare({
d1Persist: false,
kvPersist: false,
Expand Down
Loading