Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-do-redirect-response.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/vitest-pool-workers": patch
---

fix: `runInDurableObject` now correctly returns redirect responses (3xx) from Durable Object callbacks instead of throwing "Expected callback for X" errors
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Counter implements DurableObject {
}

fetch(request: Request) {
const url = new URL(request.url);
if (url.pathname === "/redirect") {
return Response.redirect("https://example.com/redirected", 302);
}
this.increment();
return new Response(this.count.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import {
import { it } from "vitest";
import { Counter } from "../src/";

// Regression test for https://github.com/cloudflare/workers-sdk/issues/9907
it("handles redirect responses returned from runInDurableObject callback", async ({
expect,
}) => {
const id = env.COUNTER.idFromName("/redirect-test");
const stub = env.COUNTER.get(id);
const response = await runInDurableObject(stub, (instance: Counter) => {
const request = new Request("https://example.com/redirect");
return instance.fetch(request);
});
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe(
"https://example.com/redirected"
);
});

it("increments count and allows direct access to instance/storage", async ({
expect,
}) => {
Expand All @@ -32,6 +48,5 @@ it("increments count and allows direct access to instance/storage", async ({

// Check IDs can be listed
const ids = await listDurableObjectIds(env.COUNTER);
expect(ids.length).toBe(1);
expect(ids[0].equals(id)).toBe(true);
expect(ids.map((i) => i.toString())).toContain(id.toString());
});
3 changes: 3 additions & 0 deletions packages/vitest-pool-workers/src/worker/durable-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ async function runInStub<O extends DurableObject, R>(

const response = await stub.fetch("http://x", {
cf: { [CF_KEY_ACTION]: id },
// Prevent the runtime from following redirects returned by the callback,
// which would re-enter `maybeHandleRunRequest` with a consumed action ID.
redirect: "manual",
});

// `result` may be `undefined`
Expand Down
Loading