From bcd5f6ff8bb488f37302070702e8728288e209aa Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 27 Mar 2024 15:37:51 +0800 Subject: [PATCH] test: rand-user-agent test --- lib/setup.test.ts | 5 +++++ lib/utils/rand-user-agent.test.ts | 23 ++++------------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/setup.test.ts b/lib/setup.test.ts index a3120aca6..8e19dfc2c 100644 --- a/lib/setup.test.ts +++ b/lib/setup.test.ts @@ -47,6 +47,11 @@ const server = setupServer( `var ct = "${1_636_626_300}";\n` + '' ) + ), + http.get(`http://rsshub.test/ua`, ({ request }) => + HttpResponse.json({ + ua: request.headers.get('user-agent'), + }) ) ); server.listen(); diff --git a/lib/utils/rand-user-agent.test.ts b/lib/utils/rand-user-agent.test.ts index 4ccea6ad9..91169f159 100644 --- a/lib/utils/rand-user-agent.test.ts +++ b/lib/utils/rand-user-agent.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from 'vitest'; import got from '@/utils/got'; import { config } from '@/config'; -import nock from 'nock'; import randUserAgent from '@/utils/rand-user-agent'; const mobileUa = randUserAgent({ browser: 'mobile safari', os: 'ios', device: 'mobile' }); @@ -23,30 +22,16 @@ describe('rand-user-agent', () => { }); it('should has default random ua', async () => { - nock('https://rsshub.test') - .get('/test') - .reply(function () { - expect(this.req.headers['user-agent']).toBe(config.ua); - expect(this.req.headers['user-agent']).not.toBe(mobileUa); - expect(this.req.headers['user-agent']).not.toBe('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); - return [200, '']; - }); - await got('https://rsshub.test/test'); + const { data } = await got('http://rsshub.test/ua'); + expect(data.ua).toBe(config.ua); }); it('should match ua configurated', async () => { - nock('https://rsshub.test') - .get('/test') - .reply(function () { - return [200, { ua: this.req.headers['user-agent'] }]; - }); - - const resonse = await got('https://rsshub.test/test', { + const { data } = await got('http://rsshub.test/ua', { headers: { 'user-agent': mobileUa, }, }); - // @ts-expect-error custom field - expect(resonse.data.ua).toBe(mobileUa); + expect(data.ua).toBe(mobileUa); }); });