-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdirectory.kv.test.ts
More file actions
89 lines (73 loc) · 2.28 KB
/
directory.kv.test.ts
File metadata and controls
89 lines (73 loc) · 2.28 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
import { env, createExecutionContext } from 'cloudflare:test';
import { test, beforeAll, expect, vi } from 'vitest';
import {
populateDirectoryCacheWithDevBucket,
populateR2WithDevBucket,
} from './util';
import worker from '../src/worker';
import type { Env } from '../src/env';
import { CACHE_HEADERS } from '../src/constants/cache';
const mockedEnv: Env = {
...env,
ENVIRONMENT: 'e2e-tests',
CACHING: false,
LOG_ERRORS: true,
USE_KV: true,
};
beforeAll(async () => {
await populateR2WithDevBucket();
await populateDirectoryCacheWithDevBucket();
vi.mock(
import('../src/constants/latestVersions.json'),
async importOriginal => {
const original = await importOriginal();
// Point all `latest-` directories to one that exists in the dev bucket
Object.keys(original.default).forEach(branch => {
let updatedValue: string;
if (branch === 'node-latest.tar.gz') {
updatedValue = 'latest/node-v20.0.0.tar.gz';
} else {
updatedValue = 'v20.0.0';
}
// @ts-expect-error
original.default[branch] = updatedValue;
});
return original;
}
);
});
// Ensure essential endpoints are routable
for (const path of ['/dist/', '/docs/', '/api/', '/download/', '/metrics/']) {
test(`GET \`${path}\` returns 200`, async () => {
const ctx = createExecutionContext();
const res = await worker.fetch(
new Request(`https://localhost${path}`),
mockedEnv,
ctx
);
// Consume body promise
await res.text();
expect(res.status).toBe(200);
});
}
test('GET `/dist/unknown-directory/` returns 404', async () => {
const ctx = createExecutionContext();
const res = await worker.fetch(
new Request('https://localhost/dist/unknown-directory/'),
mockedEnv,
ctx
);
expect(res.status).toBe(404);
expect(res.headers.get('cache-control')).toStrictEqual(CACHE_HEADERS.failure);
expect(await res.text()).toStrictEqual('Directory not found');
});
test('GET `/dist` redirects to `/dist/`', async () => {
const ctx = createExecutionContext();
const res = await worker.fetch(
new Request('https://localhost/dist'),
mockedEnv,
ctx
);
expect(res.status).toBe(301);
expect(res.headers.get('location')).toStrictEqual('https://localhost/dist/');
});