-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathintegration-self.test.ts
More file actions
71 lines (63 loc) · 2.25 KB
/
Copy pathintegration-self.test.ts
File metadata and controls
71 lines (63 loc) · 2.25 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { exports } from "cloudflare:workers";
import { describe, it } from "vitest";
describe("functions", () => {
it("calls function", async ({ expect }) => {
// `exports.default` here points to the worker running in the current isolate.
// This gets its handler from the `main` option in `vitest.config.mts`.
const response = await exports.default.fetch("http://example.com/api/ping");
// All `/api/*` requests go through `functions/api/_middleware.ts`,
// which makes all response bodies uppercase
expect(await response.text()).toBe("GET PONG");
});
it("calls function with params", async ({ expect }) => {
let response = await exports.default.fetch(
"https://example.com/api/kv/key",
{
method: "PUT",
body: "value",
}
);
expect(response.status).toBe(204);
response = await exports.default.fetch("https://example.com/api/kv/key");
expect(response.status).toBe(200);
expect(await response.text()).toBe("VALUE");
});
});
describe("assets", () => {
it("serves static assets", async ({ expect }) => {
const response = await exports.default.fetch("http://example.com/");
expect(await response.text()).toMatchInlineSnapshot(`
"<p>Homepage 🏡</p>
"
`);
});
it("respects 404.html", async ({ expect }) => {
// `404.html` should be served for all unmatched requests
const response = await exports.default.fetch(
"http://example.com/not-found"
);
expect(await response.text()).toMatchInlineSnapshot(`
"<p>Not found 😭</p>
"
`);
});
it("respects _redirects", async ({ expect }) => {
const response = await exports.default.fetch(
"http://example.com/take-me-home",
{
redirect: "manual",
}
);
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe("/");
});
it("respects _headers", async ({ expect }) => {
let response = await exports.default.fetch("http://example.com/secure");
expect(response.headers.get("X-Frame-Options")).toBe("DENY");
expect(response.headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(response.headers.get("Referrer-Policy")).toBe("no-referrer");
// Check headers only added to matching requests
response = await exports.default.fetch("http://example.com/");
expect(response.headers.get("X-Frame-Options")).toBe(null);
});
});