-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathimperative.test.ts
More file actions
104 lines (89 loc) · 2.95 KB
/
Copy pathimperative.test.ts
File metadata and controls
104 lines (89 loc) · 2.95 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import events from "node:events";
import { exports } from "cloudflare:workers";
import { afterEach, assert, it, vi } from "vitest";
afterEach(() => {
vi.restoreAllMocks();
});
it("mocks GET requests", async ({ expect }) => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
const request = new Request(input, init);
const url = new URL(request.url);
if (
request.method === "GET" &&
url.origin === "https://cloudflare.com" &&
url.pathname === "/path"
) {
return new Response("✅");
}
throw new Error("No mock found");
});
// Host `example.com` will be rewritten to `cloudflare.com` by the Worker
let response = await exports.default.fetch("https://example.com/path");
expect(response.status).toBe(200);
expect(await response.text()).toBe("✅");
// Invalid paths shouldn't match
response = await exports.default.fetch("https://example.com/bad");
expect(response.status).toBe(500);
expect(await response.text()).toMatch("No mock found");
});
it("mocks POST requests", async ({ expect }) => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
const request = new Request(input, init);
const url = new URL(request.url);
const body = await request.text();
if (
request.method === "POST" &&
url.origin === "https://cloudflare.com" &&
url.pathname === "/path" &&
body === "✨"
) {
return new Response("✅");
}
throw new Error("No mock found");
});
const response = await exports.default.fetch("https://example.com/path", {
method: "POST",
body: "✨",
});
expect(response.status).toBe(200);
expect(await response.text()).toBe("✅");
});
it("mocks WebSocket requests", async ({ expect }) => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
const request = new Request(input, init);
const url = new URL(request.url);
if (
request.method === "GET" &&
url.origin === "https://cloudflare.com" &&
url.pathname === "/ws" &&
request.headers.get("Upgrade") === "websocket"
) {
const { 0: socket, 1: responseSocket } = new WebSocketPair();
socket.addEventListener("message", (event) => {
assert(typeof event.data === "string");
socket.send(event.data.toUpperCase());
});
socket.accept();
return new Response(null, {
status: 101,
webSocket: responseSocket,
});
}
throw new Error("No mock found");
});
// Send WebSocket request and assert WebSocket response received...
const response = await exports.default.fetch("https://example.com/ws", {
headers: { Upgrade: "websocket" },
});
expect(response.status).toBe(101);
const webSocket = response.webSocket;
assert(webSocket !== null); // Using `assert()` for type narrowing
// ...then accept WebSocket and send/receive message
const eventPromise = events.once(webSocket, "message") as Promise<
[MessageEvent] /* args */
>;
webSocket.accept();
webSocket.send("hello");
const args = await eventPromise;
expect(args[0].data).toBe("HELLO");
});