fix: request rewriter not functioning in transpiled code (#18948)

* fix: request rewriter not working in transpiled code

* test: add app request-rewriter test case
This commit is contained in:
DIYgod 2025-04-26 13:45:31 +08:00 committed by GitHub
parent 8e15864469
commit 0fc3d5c1bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 3 deletions

View File

@ -1,5 +1,3 @@
import '@/utils/request-rewriter';
import { Hono } from 'hono';
import { compress } from 'hono/compress';

View File

@ -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);
});
});

5
lib/app.ts Normal file
View File

@ -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;