directus

Форк
0
/
should-skip-cache.test.ts 
142 строки · 4.2 Кб
1
import { useEnv } from '@directus/env';
2
import type { Request } from 'express';
3
import { expect, test, vi } from 'vitest';
4
import { shouldSkipCache } from './should-skip-cache.js';
5

6
vi.mock('@directus/env');
7

8
test.each([
9
	{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
10
	{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
11
	{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
12
])(
13
	'should always skip cache for requests coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is false',
14
	({ publicURL, refererHost }) => {
15
		vi.mocked(useEnv).mockReturnValue({
16
			PUBLIC_URL: publicURL,
17
			CACHE_SKIP_ALLOWED: false,
18
			CACHE_AUTO_PURGE: false,
19
			CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets'],
20
		});
21

22
		const req = {
23
			get: vi.fn((str) => {
24
				switch (str) {
25
					case 'Referer':
26
						return `${refererHost}${publicURL}/admin/settings/data-model`;
27
					default:
28
						return undefined;
29
				}
30
			}),
31
			originalUrl: 'http://admin.example.com/items/test',
32
		} as unknown as Request;
33

34
		expect(shouldSkipCache(req)).toBe(true);
35
	},
36
);
37

38
test.each([
39
	{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
40
	{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
41
	{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
42
])(
43
	'should not skip cache for requests coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is true',
44
	({ publicURL, refererHost }) => {
45
		vi.mocked(useEnv).mockReturnValue({
46
			PUBLIC_URL: publicURL,
47
			CACHE_SKIP_ALLOWED: false,
48
			CACHE_AUTO_PURGE: true,
49
			CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets', 'ignore_collection'],
50
		});
51

52
		const req = {
53
			get: vi.fn((str) => {
54
				switch (str) {
55
					case 'Referer':
56
						return `${refererHost}${publicURL}/admin/settings/data-model`;
57
					default:
58
						return undefined;
59
				}
60
			}),
61
			originalUrl: 'http://admin.example.com/items/some_collection',
62
		} as unknown as Request;
63

64
		expect(shouldSkipCache(req)).toBe(false);
65
	},
66
);
67

68
test.each([
69
	{ scenario: 'not relative', publicURL: 'http://admin.example.com', refererHost: '' },
70
	{ scenario: 'relative', publicURL: '/', refererHost: 'http://ignore.domain' },
71
	{ scenario: 'relative with subdirectory', publicURL: '/test/subfolder', refererHost: 'http://ignore.domain' },
72
])(
73
	'should skip cache for requests with collections in CACHE_AUTO_PURGE_IGNORE_LIST coming from data studio when public URL is $scenario and CACHE_AUTO_PURGE is true',
74
	({ publicURL, refererHost }) => {
75
		vi.mocked(useEnv).mockReturnValue({
76
			PUBLIC_URL: publicURL,
77
			CACHE_SKIP_ALLOWED: false,
78
			CACHE_AUTO_PURGE: true,
79
			CACHE_AUTO_PURGE_IGNORE_LIST: ['directus_activity', 'directus_presets', 'ignore_collection'],
80
		});
81

82
		const req = {
83
			get: vi.fn((str) => {
84
				switch (str) {
85
					case 'Referer':
86
						return `${refererHost}${publicURL}/admin/settings/data-model`;
87
					default:
88
						return undefined;
89
				}
90
			}),
91
			originalUrl: 'http://admin.example.com/items/ignore_collection',
92
		} as unknown as Request;
93

94
		expect(shouldSkipCache(req)).toBe(true);
95
	},
96
);
97

98
test('should not skip cache for requests coming outside of data studio', () => {
99
	vi.mocked(useEnv).mockReturnValue({
100
		PUBLIC_URL: 'http://admin.example.com',
101
		CACHE_SKIP_ALLOWED: 'false',
102
	});
103

104
	const req = {
105
		get: vi.fn((str) => {
106
			switch (str) {
107
				case 'Referer':
108
					return `http://elsewhere.example.com/admin/settings/data-model`;
109
				default:
110
					return undefined;
111
			}
112
		}),
113
	} as unknown as Request;
114

115
	expect(shouldSkipCache(req)).toBe(false);
116
});
117

118
test.each([
119
	{ scenario: 'accept', value: true },
120
	{ scenario: 'ignore', value: false },
121
])(
122
	'should $scenario Cache-Control request header containing "no-store" when CACHE_SKIP_ALLOWED is $value',
123
	({ value }) => {
124
		vi.mocked(useEnv).mockReturnValue({
125
			PUBLIC_URL: '/',
126
			CACHE_SKIP_ALLOWED: value,
127
		});
128

129
		const req = {
130
			get: vi.fn((str) => {
131
				switch (str) {
132
					case 'cache-control':
133
						return 'no-store';
134
					default:
135
						return undefined;
136
				}
137
			}),
138
		} as unknown as Request;
139

140
		expect(shouldSkipCache(req)).toBe(value);
141
	},
142
);
143

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.