-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfallback.test.ts
More file actions
106 lines (89 loc) · 2.6 KB
/
fallback.test.ts
File metadata and controls
106 lines (89 loc) · 2.6 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
import { env, fetchMock, createExecutionContext } from 'cloudflare:test';
import { test, beforeAll, afterEach, expect, vi } from 'vitest';
import { 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: false,
S3_ENDPOINT: 'https://s3.mock',
S3_ACCESS_KEY_ID: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
S3_ACCESS_KEY_SECRET:
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
ORIGIN_HOST: 'https://origin.mock',
};
const s3Url = new URL(mockedEnv.S3_ENDPOINT);
s3Url.host = `${mockedEnv.BUCKET_NAME}.${s3Url.host}`;
beforeAll(async () => {
fetchMock.activate();
fetchMock.disableNetConnect();
await populateR2WithDevBucket();
});
afterEach(() => {
fetchMock.assertNoPendingInterceptors();
});
test('grabs file from fallback server if r2 request fails', async () => {
vi.spyOn(env.R2_BUCKET, 'get').mockImplementation(() => {
throw new TypeError('This should be thrown.');
});
let originCalled = false;
const originResponse = '{ "asd": true }';
fetchMock
.get(mockedEnv.ORIGIN_HOST)
.intercept({
path: '/dist/index.json',
})
.reply(() => {
originCalled = true;
return {
statusCode: 200,
data: originResponse,
};
});
const ctx = createExecutionContext();
const res = await worker.fetch(
new Request('https://localhost/dist/index.json'),
mockedEnv,
ctx
);
expect(res.status).toBe(200);
expect(res.headers.get('cache-control')).toStrictEqual(CACHE_HEADERS.success);
expect(originCalled).toBeTruthy();
expect(await res.text()).toStrictEqual(originResponse);
});
test('grabs directory from fallback server if r2 request fails', async () => {
fetchMock
.get(s3Url.origin)
.intercept({
path: /.*/,
})
.reply(500, '')
.times(1);
let originCalled = false;
const originResponse = '';
fetchMock
.get(mockedEnv.ORIGIN_HOST)
.intercept({
path: '/dist/v20.0.0/',
})
.reply(() => {
originCalled = true;
return {
statusCode: 200,
data: originResponse,
};
});
const ctx = createExecutionContext();
const res = await worker.fetch(
new Request('https://localhost/dist/v20.0.0/'),
mockedEnv,
ctx
);
expect(res.status).toBe(200);
expect(res.headers.get('cache-control')).toStrictEqual(CACHE_HEADERS.failure);
expect(originCalled).toBeTruthy();
expect(await res.text()).toStrictEqual(originResponse);
});