-
Notifications
You must be signed in to change notification settings - Fork 198
/
api.spec.ts
220 lines (207 loc) 路 7.22 KB
/
api.spec.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { AddressInfo } from "net";
import { Response } from "@miniflare/core";
import { Log, LogLevel } from "@miniflare/shared";
import { interceptConsoleLogs, useTmp } from "@miniflare/shared-test";
import test from "ava";
import { Miniflare, VariedStorageFactory } from "miniflare";
import { fetch } from "undici";
function clearArray(arr: any[]): void {
arr.splice(0, arr.length);
}
test.serial("Miniflare: gets log from options", async (t) => {
const logs = interceptConsoleLogs(t);
let mf = new Miniflare({ script: "//" });
await mf.getPlugins();
clearArray(logs);
mf.log.info("info");
mf.log.debug("debug");
mf.log.verbose("verbose");
t.deepEqual(logs, []); // Defaults to NoOpLog
mf = new Miniflare({ script: "//", log: new Log(LogLevel.DEBUG) });
await mf.getPlugins();
clearArray(logs);
mf.log.info("info");
mf.log.debug("debug");
mf.log.verbose("verbose");
t.deepEqual(logs, ["[mf:inf] info", "[mf:dbg] debug"]);
});
test.serial("Miniflare: dispose: disposes plugins and storage", async (t) => {
const logs = interceptConsoleLogs(t);
const mf = new Miniflare({
script: "//",
log: new Log(LogLevel.VERBOSE),
});
await mf.getPlugins();
clearArray(logs);
// Check we're also closing storage connections
const originalDispose = VariedStorageFactory.prototype.dispose;
t.teardown(() => (VariedStorageFactory.prototype.dispose = originalDispose));
VariedStorageFactory.prototype.dispose = function () {
mf.log.verbose("Disposing storage...");
return originalDispose.bind(this)();
};
await mf.dispose();
t.deepEqual(logs, [
"[mf:vrb] - dispose(DurableObjectsPlugin)",
"[mf:vrb] - dispose(WebSocketPlugin)",
"[mf:vrb] - dispose(BindingsPlugin)",
"[mf:vrb] Disposing storage...",
]);
});
test("Miniflare: getKVNamespace: gets KV namespace", async (t) => {
const mf = new Miniflare({
script: `export default {
fetch: async (request, env) => new Response(await env.TEST_NAMESPACE.get("key")),
}`,
modules: true,
kvNamespaces: ["TEST_NAMESPACE"],
});
const ns = await mf.getKVNamespace("TEST_NAMESPACE");
await ns.put("key", "value");
const res = await mf.dispatchFetch("http://localhost/");
t.is(await res.text(), "value");
});
test("Miniflare: getCaches: gets CacheStorage instance", async (t) => {
const mf = new Miniflare({
script: `export default {
fetch: (request) => caches.default.match(request),
}`,
modules: true,
});
const caches = await mf.getCaches();
await caches.default.put(
"http://localhost/",
new Response("cached", { headers: { "Cache-Control": "max-age=3600" } })
);
const res = await mf.dispatchFetch("http://localhost/");
t.is(await res.text(), "cached");
});
test("Miniflare: getDurableObjectNamespace: gets Durable Object namespace", async (t) => {
const mf = new Miniflare({
script: `export class TestObject {
async fetch(request) {
return new Response("body");
}
}`,
modules: true,
durableObjects: { TEST: "TestObject" },
});
const ns = await mf.getDurableObjectNamespace("TEST");
const stub = ns.get(ns.newUniqueId());
const res = await stub.fetch("http://localhost/");
t.is(await res.text(), "body");
});
test("Miniflare: getDurableObjectStorage: gets Durable Object storage for object with ID", async (t) => {
const mf = new Miniflare({
script: `export class TestObject {
constructor(state) {
this.storage = state.storage;
}
async fetch(request) {
return new Response(await this.storage.get("key"));
}
}`,
modules: true,
durableObjects: { TEST: "TestObject" },
});
const ns = await mf.getDurableObjectNamespace("TEST");
const id = ns.newUniqueId();
const storage = await mf.getDurableObjectStorage(id);
await storage.put("key", "value");
const stub = ns.get(id);
const res = await stub.fetch("http://localhost/");
t.is(await res.text(), "value");
});
test("Miniflare: getDurableObjectStorage: doesn't construct object", async (t) => {
// https://github.com/cloudflare/miniflare/issues/300
const mf = new Miniflare({
script: `export class TestObject {
constructor() { throw new Error("Constructed!"); }
}`,
modules: true,
durableObjects: { TEST: "TestObject" },
});
const ns = await mf.getDurableObjectNamespace("TEST");
const id = ns.newUniqueId();
await mf.getDurableObjectStorage(id);
t.pass();
});
test("Miniflare: createServer: creates HTTP server", async (t) => {
const mf = new Miniflare({
script: `export default {
fetch: async () => new Response("body"),
}`,
modules: true,
});
const server = await mf.createServer();
const port = await new Promise((resolve) => {
server.listen(0, () => {
t.teardown(() => server.close());
const port = (server.address() as AddressInfo).port;
resolve(port);
});
});
const res = await fetch(`http://localhost:${port}/`);
t.is(await res.text(), "body");
});
test.serial("Miniflare: startServer: starts HTTP server", async (t) => {
const logs = interceptConsoleLogs(t);
const mf = new Miniflare({
script: `export default {
fetch: async () => new Response("body"),
}`,
modules: true,
port: 0,
log: new Log(LogLevel.INFO),
});
await mf.getPlugins();
clearArray(logs);
const server = await mf.startServer();
const port = (server.address() as AddressInfo).port;
const res = await fetch(`http://localhost:${port}/`);
t.is(await res.text(), "body");
t.regex(logs[0], /\[mf:inf\] Listening on :\d+/);
t.regex(logs[logs.length - 1], /^GET \/ 200 OK/);
});
test.serial("Miniflare: startScheduler: starts CRON scheduler", async (t) => {
t.plan(2); // expression is "* * * * *" and callback
const mf = new Miniflare({
globals: { callback: () => t.pass() },
script: `export default {
scheduled: callback,
}`,
modules: true,
crons: ["* * * * *"],
});
// Wait for plugins to load, this includes CRON validation
await mf.getPlugins();
const {
TimerBasedCronScheduler,
}: typeof import("cron-schedule") = require("cron-schedule");
const originalSetInterval = TimerBasedCronScheduler.setInterval;
t.teardown(() => (TimerBasedCronScheduler.setInterval = originalSetInterval));
TimerBasedCronScheduler.setInterval = (expression, func) => {
t.is(expression.toString(), "* * * * *");
// Immediately invoke the task because we're impatient
func();
return 0 as any;
};
await mf.startScheduler();
});
test("Miniflare: getOpenURL: returns URL to open browser at", async (t) => {
// Check returns undefined if open disabled
const mf = new Miniflare({ script: "//" });
t.is(await mf.getOpenURL(), undefined);
await mf.setOptions({ open: false });
t.is(await mf.getOpenURL(), undefined);
// Check returns custom string URL
await mf.setOptions({ open: "https://some.other.website/" });
t.is(await mf.getOpenURL(), "https://some.other.website/");
// Check returns http URL with default host and port
await mf.setOptions({ open: true });
t.is(await mf.getOpenURL(), "http://localhost:8787/");
// Check returns https URL with custom host and port
const tmp = await useTmp(t);
await mf.setOptions({ open: true, https: tmp, host: "test.mf", port: 3000 });
t.is(await mf.getOpenURL(), "https://test.mf:3000/");
});