Files
RSSHub/lib/utils/rand-user-agent.test.ts
T
Tony 03969f3867 refactor: fix lint issue (#18963)
* fix: unicorn/no-empty-file

* fix: unicorn/prefer-node-protocol

* fix: unicorn/prefer-number-properties

* fix: unicorn/consistent-destructuring

* fix: unicorn/no-array-callback-reference

* fix: unicorn/new-for-builtins

* fix: set checkNaN to false in unicorn/prefer-number-properties

* fix: unicorn/prefer-string-slice

* fix: update eslint ignore
2025-04-29 03:00:16 +08:00

38 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import ofetch from '@/utils/ofetch';
import { config } from '@/config';
import randUserAgent from '@/utils/rand-user-agent';
const mobileUa = randUserAgent({ browser: 'mobile safari', os: 'ios', device: 'mobile' });
describe('rand-user-agent', () => {
it('chrome should not include headlesschrome', () => {
const uaArr = Array.from({ length: 100 })
.fill(null)
.map(() => randUserAgent({ browser: 'chrome', os: 'windows' }));
const match = uaArr.find((e) => !!(e.includes('Chrome-Lighthouse') || e.includes('HeadlessChrome')));
expect(match).toBeFalsy();
});
it('chrome should not include electron', () => {
const uaArr = Array.from({ length: 100 })
.fill(null)
.map(() => randUserAgent({ browser: 'chrome', os: 'windows' }));
const match = uaArr.find((e) => !!e.includes('Electron'));
expect(match).toBeFalsy();
});
it('should has default random ua', async () => {
const response = await ofetch('http://rsshub.test/headers');
expect(response['user-agent']).toBe(config.ua);
});
it('should match ua configurated', async () => {
const response = await ofetch('http://rsshub.test/headers', {
headers: {
'user-agent': mobileUa,
},
});
expect(response['user-agent']).toBe(mobileUa);
});
});