|
| 1 | +import * as SentryCore from '@sentry/core'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { addHeadersAsAttributes } from '../../src/common/utils/addHeadersAsAttributes'; |
| 4 | + |
| 5 | +describe('addHeadersAsAttributes', () => { |
| 6 | + it('returns empty object when headers are undefined', () => { |
| 7 | + expect(addHeadersAsAttributes(undefined)).toEqual({}); |
| 8 | + }); |
| 9 | + |
| 10 | + it('returns empty object when httpHeaders.request is false', () => { |
| 11 | + vi.spyOn(SentryCore, 'getClient').mockReturnValue({ |
| 12 | + getDataCollectionOptions: () => ({ |
| 13 | + httpHeaders: { request: false, response: true }, |
| 14 | + }), |
| 15 | + } as unknown as SentryCore.Client); |
| 16 | + |
| 17 | + const result = addHeadersAsAttributes({ 'content-type': 'application/json' }); |
| 18 | + expect(result).toEqual({}); |
| 19 | + }); |
| 20 | + |
| 21 | + it('includes all headers with sensitive filtering when httpHeaders.request is true', () => { |
| 22 | + vi.spyOn(SentryCore, 'getClient').mockReturnValue({ |
| 23 | + getDataCollectionOptions: () => ({ |
| 24 | + httpHeaders: { request: true, response: true }, |
| 25 | + }), |
| 26 | + } as unknown as SentryCore.Client); |
| 27 | + |
| 28 | + const result = addHeadersAsAttributes({ |
| 29 | + 'content-type': 'application/json', |
| 30 | + accept: 'text/html', |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result).toMatchObject({ |
| 34 | + 'http.request.header.content_type': 'application/json', |
| 35 | + 'http.request.header.accept': 'text/html', |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('applies stricter PII filtering when httpHeaders.request is a deny list', () => { |
| 40 | + vi.spyOn(SentryCore, 'getClient').mockReturnValue({ |
| 41 | + getDataCollectionOptions: () => ({ |
| 42 | + httpHeaders: { request: { deny: [] }, response: true }, |
| 43 | + }), |
| 44 | + } as unknown as SentryCore.Client); |
| 45 | + |
| 46 | + const result = addHeadersAsAttributes({ |
| 47 | + 'content-type': 'application/json', |
| 48 | + accept: 'text/html', |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result).toMatchObject({ |
| 52 | + 'http.request.header.content_type': 'application/json', |
| 53 | + 'http.request.header.accept': 'text/html', |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns empty object when no client is available', () => { |
| 58 | + vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined); |
| 59 | + |
| 60 | + const result = addHeadersAsAttributes({ 'content-type': 'application/json' }); |
| 61 | + expect(result).toEqual({}); |
| 62 | + }); |
| 63 | +}); |
0 commit comments