index.test.ts
3.07 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
import { describe, expect, it } from 'vitest';
import {
DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS,
DEFAULT_PRELEARNING_ALLOWED_ORIGINS,
buildBackendApiPath,
resolveBackendApiBasePath,
parseSopPreviewAllowedOrigins,
parsePrelearningAllowedOrigins,
} from './index';
describe('parseSopPreviewAllowedOrigins', () => {
it('falls back to the default allowlist when the env value is empty', () => {
expect(parseSopPreviewAllowedOrigins(undefined)).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
expect(parseSopPreviewAllowedOrigins('')).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
expect(parseSopPreviewAllowedOrigins(' ')).toEqual([...DEFAULT_SOP_PREVIEW_ALLOWED_ORIGINS]);
});
it('accepts explicit wildcard and normalizes origin strings', () => {
expect(parseSopPreviewAllowedOrigins('*')).toBe('*');
expect(parseSopPreviewAllowedOrigins('https://dj.glsx.com.cn, https://frontend.test.glsx.com.cn/path, http://192.168.1.146:8080')).toEqual([
'https://dj.glsx.com.cn',
'https://frontend.test.glsx.com.cn',
'http://192.168.1.146:8080',
]);
});
});
describe('parsePrelearningAllowedOrigins', () => {
it('falls back to the default allowlist when the env value is empty', () => {
expect(parsePrelearningAllowedOrigins(undefined)).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
expect(parsePrelearningAllowedOrigins('')).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
expect(parsePrelearningAllowedOrigins(' ')).toEqual([...DEFAULT_PRELEARNING_ALLOWED_ORIGINS]);
});
it('accepts explicit wildcard and normalizes origin strings', () => {
expect(parsePrelearningAllowedOrigins('*')).toBe('*');
expect(parsePrelearningAllowedOrigins('https://dj.glsx.com.cn, https://frontend.test.glsx.com.cn/path, http://192.168.1.146:8080')).toEqual([
'https://dj.glsx.com.cn',
'https://frontend.test.glsx.com.cn',
'http://192.168.1.146:8080',
]);
});
});
describe('resolveBackendApiBasePath', () => {
it('does not add a backend prefix for local runtime hosts', () => {
expect(resolveBackendApiBasePath({ hostname: 'localhost' })).toBe('');
expect(resolveBackendApiBasePath({ hostname: '127.0.0.1' })).toBe('');
expect(resolveBackendApiBasePath({ hostname: '192.168.1.146' })).toBe('');
expect(resolveBackendApiBasePath({ hostname: '10.0.0.8' })).toBe('');
expect(resolveBackendApiBasePath({ hostname: '172.16.1.8' })).toBe('');
});
it('adds the backend prefix for remote deployed front-end hosts', () => {
expect(resolveBackendApiBasePath({ hostname: 'single-pages.test.glsx.com.cn' })).toBe('/backend');
expect(buildBackendApiPath('/api/prelearning/init', { hostname: 'single-pages.test.glsx.com.cn' })).toBe(
'/backend/api/prelearning/init',
);
});
it('allows an explicit env base path override', () => {
expect(resolveBackendApiBasePath({ hostname: 'single-pages.test.glsx.com.cn', envBasePath: '' })).toBe('');
expect(buildBackendApiPath('api/prelearning/init', { envBasePath: '/custom-backend/' })).toBe(
'/custom-backend/api/prelearning/init',
);
});
});