Skip to content

Commit fa2bae9

Browse files
committed
Finish writting controller tests
1 parent ad02462 commit fa2bae9

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

workers/controller/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ async function handlePurgeRequest(
6161
);
6262
}
6363

64+
const { data } = Purge.safeParse(await request.json());
65+
if (!data) {
66+
return Response.json(
67+
{
68+
error: "Malformed request",
69+
},
70+
{ status: 400 },
71+
);
72+
}
73+
const { tags } = data;
74+
6475
const client = new Cloudflare({
6576
apiToken: token,
6677
});
@@ -76,7 +87,6 @@ async function handlePurgeRequest(
7687
);
7788
}
7889

79-
const { tags } = Purge.parse(await request.json());
8090
console.debug("[Cache Purge Request] Purge Tags", tags);
8191

8292
// If no zone is present, then all zones will be purged.

workers/controller/test/purge.spec.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SELF, env } from "cloudflare:test";
2-
import { it, expect, vi } from "vitest";
2+
import { it, expect, vi, afterEach } from "vitest";
33
import type Cloudflare from "cloudflare";
44

55
const verify = vi.fn<
@@ -17,6 +17,10 @@ vi.mock("cloudflare", () => ({
1717
})),
1818
}));
1919

20+
afterEach(() => {
21+
vi.clearAllMocks();
22+
});
23+
2024
it("adds cache tags to the purge queue", async () => {
2125
verify.mockResolvedValueOnce({ status: "active", id: "foo" });
2226

@@ -85,3 +89,64 @@ it("adds cache tags to the purge queue with no zone", async () => {
8589
},
8690
]);
8791
});
92+
93+
it("returns a 401 when the wrong API Token is provided", async () => {
94+
const response = await SELF.fetch(
95+
"https://cache-tag.example.workers.dev/purge",
96+
{
97+
method: "POST",
98+
body: JSON.stringify({
99+
tags: ["test"],
100+
}),
101+
headers: {
102+
Authorization: `Bearer wrong`,
103+
"CF-Worker": "example.com",
104+
},
105+
},
106+
);
107+
108+
expect(response.status).toBe(401);
109+
110+
expect(verify).not.toBeCalled();
111+
});
112+
113+
it("returns a 401 when the wrong API Token is expired", async () => {
114+
verify.mockResolvedValueOnce({ status: "expired", id: "foo" });
115+
116+
const response = await SELF.fetch(
117+
"https://cache-tag.example.workers.dev/purge",
118+
{
119+
method: "POST",
120+
body: JSON.stringify({
121+
tags: ["test"],
122+
}),
123+
headers: {
124+
Authorization: `Bearer ${env.API_TOKEN}`,
125+
},
126+
},
127+
);
128+
129+
expect(response.status).toBe(401);
130+
131+
expect(verify).toBeCalled();
132+
});
133+
134+
it("returns a 400 when request is malformed", async () => {
135+
const response = await SELF.fetch(
136+
"https://cache-tag.example.workers.dev/purge",
137+
{
138+
method: "POST",
139+
body: JSON.stringify({
140+
url: "https://example.com",
141+
}),
142+
headers: {
143+
Authorization: `Bearer ${env.API_TOKEN}`,
144+
"CF-Worker": "example.com",
145+
},
146+
},
147+
);
148+
149+
expect(response.status).toBe(400);
150+
151+
expect(verify).not.toBeCalled();
152+
});

0 commit comments

Comments
 (0)