|
| 1 | +import { |
| 2 | + getTraceMetaTags, |
| 3 | +} from '@sentry/core'; |
| 4 | +import { PassThrough } from 'stream'; |
| 5 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 6 | +import { getMetaTagTransformer } from '../../src/server/getMetaTagTransformer'; |
| 7 | + |
| 8 | +vi.mock('@opentelemetry/core', () => ({ |
| 9 | + RPCType: { HTTP: 'http' }, |
| 10 | + getRPCMetadata: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('@sentry/core', () => ({ |
| 14 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE: 'sentry.source', |
| 15 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN: 'sentry.origin', |
| 16 | + getActiveSpan: vi.fn(), |
| 17 | + getRootSpan: vi.fn(), |
| 18 | + getTraceMetaTags: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | + |
| 22 | +describe('getMetaTagTransformer', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks(); |
| 25 | + (getTraceMetaTags as unknown as ReturnType<typeof vi.fn>).mockReturnValue( |
| 26 | + '<meta name="sentry-trace" content="test-trace-id">', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + test('should inject meta tags before closing head tag', done => { |
| 31 | + const outputStream = new PassThrough(); |
| 32 | + const bodyStream = new PassThrough(); |
| 33 | + const transformer = getMetaTagTransformer(bodyStream); |
| 34 | + |
| 35 | + let outputData = ''; |
| 36 | + outputStream.on('data', chunk => { |
| 37 | + outputData += chunk.toString(); |
| 38 | + }); |
| 39 | + |
| 40 | + outputStream.on('end', () => { |
| 41 | + expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>'); |
| 42 | + expect(outputData).not.toContain('</head></head>'); |
| 43 | + done(); |
| 44 | + }); |
| 45 | + |
| 46 | + transformer.pipe(outputStream); |
| 47 | + |
| 48 | + bodyStream.write('<html><head></head><body>Test</body></html>'); |
| 49 | + bodyStream.end(); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should not modify chunks without head closing tag', done => { |
| 53 | + const outputStream = new PassThrough(); |
| 54 | + const bodyStream = new PassThrough(); |
| 55 | + const transformer = getMetaTagTransformer(bodyStream); |
| 56 | + |
| 57 | + let outputData = ''; |
| 58 | + outputStream.on('data', chunk => { |
| 59 | + outputData += chunk.toString(); |
| 60 | + }); |
| 61 | + |
| 62 | + outputStream.on('end', () => { |
| 63 | + expect(outputData).toBe('<html><body>Test</body></html>'); |
| 64 | + expect(getTraceMetaTags).toHaveBeenCalled(); |
| 65 | + done(); |
| 66 | + }); |
| 67 | + |
| 68 | + transformer.pipe(outputStream); |
| 69 | + |
| 70 | + bodyStream.write('<html><body>Test</body></html>'); |
| 71 | + bodyStream.end(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should handle buffer input', done => { |
| 75 | + const outputStream = new PassThrough(); |
| 76 | + const bodyStream = new PassThrough(); |
| 77 | + const transformer = getMetaTagTransformer(bodyStream); |
| 78 | + |
| 79 | + let outputData = ''; |
| 80 | + outputStream.on('data', chunk => { |
| 81 | + outputData += chunk.toString(); |
| 82 | + }); |
| 83 | + |
| 84 | + outputStream.on('end', () => { |
| 85 | + expect(outputData).toContain('<meta name="sentry-trace" content="test-trace-id"></head>'); |
| 86 | + done(); |
| 87 | + }); |
| 88 | + |
| 89 | + transformer.pipe(outputStream); |
| 90 | + |
| 91 | + bodyStream.write(Buffer.from('<html><head></head><body>Test</body></html>')); |
| 92 | + bodyStream.end(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments