|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import ofetch from '@/utils/ofetch'; |
| 3 | +import { generateHeaders, PRESETS } from '@/utils/header-generator'; |
| 4 | + |
| 5 | +describe('header-generator', () => { |
| 6 | + it('should has no ua', async () => { |
| 7 | + const response = await ofetch('http://rsshub.test/headers'); |
| 8 | + expect(response['user-agent']).toBeUndefined(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should match ua configurated', async () => { |
| 12 | + const testUa = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1'; |
| 13 | + const response = await ofetch('http://rsshub.test/headers', { |
| 14 | + headers: { |
| 15 | + 'user-agent': testUa, |
| 16 | + }, |
| 17 | + }); |
| 18 | + expect(response['user-agent']).toBe(testUa); |
| 19 | + }); |
| 20 | + |
| 21 | + it('generateHeaders should include sec-ch and sec-fetch headers', () => { |
| 22 | + const headers = generateHeaders(PRESETS.MODERN_MACOS_CHROME); |
| 23 | + |
| 24 | + expect(headers['user-agent']).toBeDefined(); |
| 25 | + expect(headers['sec-ch-ua']).toBeDefined(); |
| 26 | + expect(headers['sec-ch-ua-mobile']).toBeDefined(); |
| 27 | + expect(headers['sec-ch-ua-platform']).toBeDefined(); |
| 28 | + expect(headers['sec-fetch-site']).toBeDefined(); |
| 29 | + expect(headers['sec-fetch-mode']).toBeDefined(); |
| 30 | + expect(headers['sec-fetch-user']).toBeDefined(); |
| 31 | + expect(headers['sec-fetch-dest']).toBeDefined(); |
| 32 | + |
| 33 | + expect(headers['sec-ch-ua-platform']).toBe('"macOS"'); |
| 34 | + expect(headers['sec-ch-ua-mobile']).toBe('?0'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('generateHeaders should work with headerGeneratorOptions', () => { |
| 38 | + const headers = generateHeaders(PRESETS.MODERN_WINDOWS_CHROME); |
| 39 | + |
| 40 | + expect(headers['user-agent']).toBeDefined(); |
| 41 | + expect(headers['sec-ch-ua']).toBeDefined(); |
| 42 | + expect(headers['sec-ch-ua-mobile']).toBeDefined(); |
| 43 | + expect(headers['sec-ch-ua-platform']).toBeDefined(); |
| 44 | + |
| 45 | + expect(headers['sec-ch-ua-platform']).toBe('"Windows"'); |
| 46 | + expect(headers['sec-ch-ua-mobile']).toBe('?0'); |
| 47 | + expect(headers['user-agent']).toMatch(/Chrome/); |
| 48 | + }); |
| 49 | + |
| 50 | + it('generateHeaders should use default preset when no preset is provided', () => { |
| 51 | + const headers = generateHeaders(); |
| 52 | + |
| 53 | + expect(headers['user-agent']).toBeDefined(); |
| 54 | + expect(headers['sec-ch-ua']).toBeDefined(); |
| 55 | + expect(headers['sec-ch-ua-mobile']).toBeDefined(); |
| 56 | + expect(headers['sec-ch-ua-platform']).toBeDefined(); |
| 57 | + |
| 58 | + expect(headers['sec-ch-ua-platform']).toBe('"macOS"'); |
| 59 | + expect(headers['sec-ch-ua-mobile']).toBe('?0'); |
| 60 | + expect(headers['user-agent']).toMatch(/Chrome/); |
| 61 | + }); |
| 62 | +}); |
0 commit comments