-
-
Notifications
You must be signed in to change notification settings - Fork 896
Expand file tree
/
Copy pathcloudflare-module.test.ts
More file actions
140 lines (135 loc) · 3.44 KB
/
Copy pathcloudflare-module.test.ts
File metadata and controls
140 lines (135 loc) · 3.44 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Miniflare } from "miniflare";
import { resolve } from "pathe";
import { Response as _Response } from "undici";
import { describe, expect, it } from "vitest";
import { setupTest, testNitro } from "../tests";
import { promises as fsp } from "node:fs";
describe("nitro:preset:cloudflare-module", async () => {
const ctx = await setupTest("cloudflare-module", {
config: {
routeRules: {
"/": {
headers: {
"x-test": "test",
"x-cf-test-root": "test",
},
},
"/cf/_header/**": {
headers: {
"x-cf-test": "0",
},
},
"/cf/_header/0": {
headers: {
"x-cf-test": "0",
},
},
"/cf/_header/a": {
headers: {
"x-cf-test": "a",
},
},
"/cf/_header/B": {
headers: {
"X-CF-Test": "0",
},
},
"/cf/_header/1/**": {
headers: {
"x-cf-test": "1",
},
},
"/cf/_header/1/2/**": {
headers: {
"x-cf-test": "2",
},
},
"/cf/_header/1/2/1": {
headers: {
"x-cf-test": "1",
},
},
"/cf/_header/1/2/2": {
headers: {
"x-cf-test": "2",
},
},
"/cf/_header/2/2/0": {
headers: {
"x-cf-test": "0",
},
},
},
},
});
testNitro(ctx, () => {
const mf = new Miniflare({
modules: true,
scriptPath: resolve(ctx.outDir, "server/index.mjs"),
modulesRules: [{ type: "CompiledWasm", include: ["**/*.wasm"] }],
assets: {
directory: resolve(ctx.outDir, "public"),
routerConfig: { has_user_worker: true },
assetConfig: {
// https://developers.cloudflare.com/workers/static-assets/routing/#routing-configuration
html_handling: "auto-trailing-slash" /* default */,
not_found_handling: "none" /* default */,
},
},
compatibilityFlags: [
"streams_enable_constructors",
"nodejs_compat",
"no_nodejs_compat_v2",
],
bindings: { ...ctx.env },
});
return async ({ url, headers, method, body }) => {
const res = await mf.dispatchFetch("http://localhost" + url, {
headers: headers || {},
method: method || "GET",
redirect: "manual",
body,
});
return res as unknown as Response;
};
});
it("should generate a _headers file", async () => {
const config = await fsp.readFile(
resolve(
ctx.nitro?.options.output.publicDir || ctx.outDir + "/public",
"_headers"
),
"utf8"
);
expect(config).toMatchInlineSnapshot(`
"/*
x-test: test
/
x-cf-test-root: test
/build/*
cache-control: public, max-age=3600, immutable
x-build-header: works
/cf/_header/*
x-cf-test: 0
/cf/_header/1/*
! x-cf-test
x-cf-test: 1
/cf/_header/1/2/*
! x-cf-test
x-cf-test: 2
/cf/_header/1/2/1
! x-cf-test
x-cf-test: 1
/cf/_header/a
! x-cf-test
x-cf-test: a
/rules/cors
access-control-allow-origin: *
access-control-allow-methods: GET
access-control-allow-headers: *
access-control-max-age: 0
/rules/headers
cache-control: s-maxage=60"
`);
});
});