52 lines
2.3 KiB
TypeScript
52 lines
2.3 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { buildCorsOriginPatterns, parseExtraCorsOrigins } from '../../src/config.js';
|
|
|
|
const allows = (patterns: RegExp[], origin: string) => patterns.some((re) => re.test(origin));
|
|
|
|
describe('parseExtraCorsOrigins', () => {
|
|
it('zwraca pustą listę dla braku zmiennej', () => {
|
|
expect(parseExtraCorsOrigins(undefined)).toEqual([]);
|
|
});
|
|
|
|
it('dzieli po przecinku, trykuje i pomija puste elementy', () => {
|
|
expect(
|
|
parseExtraCorsOrigins(' http://192.168.50.127:13002, ,http://192.168.50.127:13003,'),
|
|
).toEqual(['http://192.168.50.127:13002', 'http://192.168.50.127:13003']);
|
|
});
|
|
});
|
|
|
|
describe('buildCorsOriginPatterns', () => {
|
|
it('zawsze dopuszcza główną domenę (dowolny port)', () => {
|
|
const patterns = buildCorsOriginPatterns('production', []);
|
|
expect(allows(patterns, 'http://tisachi.ddns.net:13002')).toBe(true);
|
|
expect(allows(patterns, 'https://tisachi.ddns.net')).toBe(true);
|
|
});
|
|
|
|
it('dodaje dokładne origins z listy (np. alt-host po IP)', () => {
|
|
const patterns = buildCorsOriginPatterns('production', [
|
|
'http://192.168.50.127:13002',
|
|
'http://192.168.50.127:13003',
|
|
]);
|
|
expect(allows(patterns, 'http://192.168.50.127:13002')).toBe(true);
|
|
expect(allows(patterns, 'http://192.168.50.127:13003')).toBe(true);
|
|
});
|
|
|
|
it('extra origin dopasowuje dokładnie — nie jako prefiks ani inny port', () => {
|
|
const patterns = buildCorsOriginPatterns('production', ['http://192.168.50.127:13002']);
|
|
expect(allows(patterns, 'http://192.168.50.127:130022')).toBe(false);
|
|
expect(allows(patterns, 'http://192.168.50.127')).toBe(false);
|
|
expect(allows(patterns, 'http://evil.com/?x=http://192.168.50.127:13002')).toBe(false);
|
|
});
|
|
|
|
it('metacharaktery w origin nie stają się wzorcem regex', () => {
|
|
const patterns = buildCorsOriginPatterns('production', ['http://a.example.com:13002']);
|
|
expect(allows(patterns, 'http://a.example.com:13002')).toBe(true);
|
|
expect(allows(patterns, 'http://aXexample.com:13002')).toBe(false);
|
|
});
|
|
|
|
it('localhost tylko poza produkcją', () => {
|
|
expect(allows(buildCorsOriginPatterns('development', []), 'http://localhost:5173')).toBe(true);
|
|
expect(allows(buildCorsOriginPatterns('production', []), 'http://localhost:5173')).toBe(false);
|
|
});
|
|
});
|