forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.scheduled.test.ts
165 lines (147 loc) · 4.09 KB
/
middleware.scheduled.test.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as fs from "node:fs";
import { vi } from "vitest";
import { unstable_dev } from "../api";
import { runInTempDir } from "./helpers/run-in-tmp";
vi.unmock("child_process");
vi.unmock("undici");
describe("run scheduled events with middleware", () => {
describe("module workers", () => {
runInTempDir();
beforeEach(() => {
const scriptContent = `
export default {
fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/__scheduled") {
return new Response("Fetch triggered at /__scheduled");
}
return new Response("Hello world!");
},
scheduled(controller, env, ctx) {
// Doing something scheduled in modules...
},
};
`;
fs.writeFileSync("index.js", scriptContent);
});
it("should not intercept when middleware is not enabled", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
},
});
const resp = await worker.fetch("/__scheduled");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Fetch triggered at /__scheduled"`);
await worker.stop();
});
it("should intercept when middleware is enabled", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
testScheduled: true,
},
});
const resp = await worker.fetch("/__scheduled");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Ran scheduled event"`);
await worker.stop();
});
it("should not trigger scheduled event on wrong route", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
testScheduled: true,
},
});
const resp = await worker.fetch("/test");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Hello world!"`);
await worker.stop();
});
});
describe("service workers", () => {
runInTempDir();
beforeEach(() => {
const scriptContent = `
addEventListener("scheduled", (event) => {
// Doing something scheduled in service worker...
});
addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
if (url.pathname === "/__scheduled") {
event.respondWith(new Response("Fetch triggered at /__scheduled"));
} else {
event.respondWith(new Response("Hello world!"));
}
});
`;
fs.writeFileSync("index.js", scriptContent);
});
it("should not intercept when middleware is not enabled", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
},
});
const resp = await worker.fetch("/__scheduled");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Fetch triggered at /__scheduled"`);
await worker.stop();
});
it("should intercept when middleware is enabled", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
testScheduled: true,
},
});
const resp = await worker.fetch("/__scheduled");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Ran scheduled event"`);
await worker.stop();
});
it("should not trigger scheduled event on wrong route", async () => {
const worker = await unstable_dev("index.js", {
ip: "127.0.0.1",
experimental: {
disableExperimentalWarning: true,
disableDevRegistry: true,
testScheduled: true,
},
});
const resp = await worker.fetch("/test");
let text;
if (resp) {
text = await resp.text();
}
expect(text).toMatchInlineSnapshot(`"Hello world!"`);
await worker.stop();
});
});
});