From 0fc3d5c1bc24b6c303933a0e645af08df5ccf30d Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 26 Apr 2025 13:45:31 +0800 Subject: [PATCH] fix: request rewriter not functioning in transpiled code (#18948) * fix: request rewriter not working in transpiled code * test: add app request-rewriter test case --- lib/{app.tsx => app-bootstrap.tsx} | 2 -- lib/app.test.ts | 16 +++++++++++++++- lib/app.ts | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) rename lib/{app.tsx => app-bootstrap.tsx} (97%) create mode 100644 lib/app.ts diff --git a/lib/app.tsx b/lib/app-bootstrap.tsx similarity index 97% rename from lib/app.tsx rename to lib/app-bootstrap.tsx index 2747dcfd2..f8b9215e3 100644 --- a/lib/app.tsx +++ b/lib/app-bootstrap.tsx @@ -1,5 +1,3 @@ -import '@/utils/request-rewriter'; - import { Hono } from 'hono'; import { compress } from 'hono/compress'; diff --git a/lib/app.test.ts b/lib/app.test.ts index b81d62ee5..5e69fc872 100644 --- a/lib/app.test.ts +++ b/lib/app.test.ts @@ -1,6 +1,9 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import app from '@/app'; +import undici from 'undici'; + +const { config } = await import('@/config'); describe('index', () => { it('serve index', async () => { @@ -9,3 +12,14 @@ describe('index', () => { expect(await res.text()).toContain('Welcome to RSSHub!'); }); }); + +describe('request-rewriter', () => { + it('should rewrite request', async () => { + const fetchSpy = vi.spyOn(undici, 'fetch'); + await app.request('/test/httperror'); + + // headers + const headers: Headers = fetchSpy.mock.lastCall?.[0].headers; + expect(headers.get('user-agent')).toBe(config.ua); + }); +}); diff --git a/lib/app.ts b/lib/app.ts new file mode 100644 index 000000000..84ec9c741 --- /dev/null +++ b/lib/app.ts @@ -0,0 +1,5 @@ +// This file ensures that the request rewriter runs before the app + +import '@/utils/request-rewriter'; + +export default (await import('./app-bootstrap')).default;