import { mm, type MockApplication } from '@eggjs/mock'; import { describe, it, expect, afterAll, beforeAll } from 'vitest'; import { getFixtures } from './utils.ts'; describe('test/inject.test.ts', () => { let app: MockApplication; beforeAll(async () => { app = mm.app({ baseDir: getFixtures('apps/inject'), }); await app.ready(); }); afterAll(() => app.close()); describe('csrfInject', () => { it('should support inject csrf', async () => { const res = await app.httpRequest().get('/testcsrf').expect(200); expect(res.text).toBe('
'); }); it('should not inject csrf when user write a csrf hidden area', async () => { const res = await app.httpRequest().get('/testcsrf2').expect(200); expect(res.text).toBe(''); }); it('should not inject csrf when user write a csrf hidden area within a single dot area', async () => { const res = await app.httpRequest().get('/testcsrf3').expect(200); expect(res.text).toBe(''); }); }); describe('nonceInject', () => { it('should inject nonce', async () => { const res = await app.httpRequest().get('/testnonce').expect(200); const body = res.text; const parts = body.split('|'); const expectedNonce = parts[0]; const scriptTag = parts[1]; expect(scriptTag).toBe( ``, ); }); it('should not inject nonce when existed', async () => { const res = await app.httpRequest().get('/testnonce2').expect(200); expect(res.text).toBe(''); }); }); describe('IspInjectDefence', () => { it('should inject IspInjectDefence', async () => { const res = await app.httpRequest().get('/testispInjection').expect(200); expect(res.text).toBe( '\n \n \n