-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathtest_utils.ts
More file actions
117 lines (108 loc) · 3.31 KB
/
test_utils.ts
File metadata and controls
117 lines (108 loc) · 3.31 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
105
106
107
108
109
110
111
112
113
114
115
116
117
import { FreshReqContext } from "./context.ts";
import type { FsAdapter } from "./fs.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { ResolvedFreshConfig } from "./config.ts";
import type { WalkEntry } from "@std/fs/walk";
import { DEFAULT_CONN_INFO } from "./app.ts";
const STUB = {} as unknown as Deno.ServeHandlerInfo;
export class FakeServer {
constructor(
public handler: (
req: Request,
info: Deno.ServeHandlerInfo,
) => Response | Promise<Response>,
) {}
async get(path: string, init?: RequestInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, init);
return await this.handler(req, STUB);
}
async post(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "post", body });
return await this.handler(req, STUB);
}
async patch(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "patch", body });
return await this.handler(req, STUB);
}
async put(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "put", body });
return await this.handler(req, STUB);
}
async delete(path: string): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "delete" });
return await this.handler(req, STUB);
}
async head(path: string): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "head" });
return await this.handler(req, STUB);
}
private toUrl(path: string) {
return new URL(path, "http://localhost/");
}
}
const DEFAULT_CONFIG: ResolvedFreshConfig = {
build: {
outDir: "",
},
mode: "production",
basePath: "",
root: "",
staticDir: "",
};
export function serveMiddleware<T>(
middleware: (ctx: FreshReqContext<T>) => Response | Promise<Response>,
options: {
config?: ResolvedFreshConfig;
buildCache?: BuildCache;
next?: () => Promise<Response>;
} = {},
): FakeServer {
return new FakeServer(async (req) => {
const next = options.next ??
(() => new Response("not found", { status: 404 }));
const config = options.config ?? DEFAULT_CONFIG;
const buildCache = options.buildCache ??
new ProdBuildCache(config, new Map(), new Map(), true);
const ctx = new FreshReqContext<T>(
req,
new URL(req.url),
DEFAULT_CONN_INFO,
{},
config,
() => Promise.resolve(next()),
new Map(),
buildCache,
);
return await middleware(ctx);
});
}
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
return {
async *walk(_root) {
// FIXME: ignore
for (const file of Object.keys(files)) {
const entry: WalkEntry = {
isDirectory: false,
isFile: true,
isSymlink: false,
name: file, // FIXME?
path: file,
};
yield entry;
}
},
// deno-lint-ignore require-await
async isDirectory(dir) {
return Object.keys(files).some((file) => file.startsWith(dir + "/"));
},
async mkdirp(_dir: string) {
},
};
}
export const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));