-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathdirect-access.test.ts
More file actions
48 lines (43 loc) · 1.76 KB
/
Copy pathdirect-access.test.ts
File metadata and controls
48 lines (43 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { listDurableObjectIds, runInDurableObject } from "cloudflare:test";
import { env, exports } from "cloudflare:workers";
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,
}) => {
// Check access through `fetch()` handler
let response = await exports.default.fetch("https://example.com/path");
expect(await response.text()).toBe("1");
// Check sending request directly to instance
const id = env.COUNTER.idFromName("/path");
const stub = env.COUNTER.get(id);
response = await runInDurableObject(stub, (instance: Counter) => {
expect(instance).toBeInstanceOf(Counter); // Exact same class as import
const request = new Request("https://example.com/path");
return instance.fetch(request);
});
expect(await response.text()).toBe("2");
// Check direct access to instance fields and storage
await runInDurableObject(stub, async (instance: Counter, state) => {
expect(instance.count).toBe(2);
expect(await state.storage.get<number>("count")).toBe(2);
});
// Check IDs can be listed
const ids = await listDurableObjectIds(env.COUNTER);
expect(ids.map((i) => i.toString())).toContain(id.toString());
});