|
| 1 | +import type { RequestDataIntegrationOptions } from '@sentry/core'; |
| 2 | +import { getCurrentHub, Hub, makeMain, RequestData } from '@sentry/core'; |
| 3 | +import type { Event, EventProcessor } from '@sentry/types'; |
| 4 | +import * as sentryUtils from '@sentry/utils'; |
| 5 | +import type { IncomingMessage } from 'http'; |
| 6 | + |
| 7 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
| 8 | + |
| 9 | +const addRequestDataToEventSpy = jest.spyOn(sentryUtils, 'addRequestDataToEvent'); |
| 10 | +const requestDataEventProcessor = jest.fn(); |
| 11 | + |
| 12 | +const headers = { ears: 'furry', nose: 'wet', tongue: 'spotted', cookie: 'favorite=zukes' }; |
| 13 | +const method = 'wagging'; |
| 14 | +const protocol = 'mutualsniffing'; |
| 15 | +const hostname = 'the.dog.park'; |
| 16 | +const path = '/by/the/trees/'; |
| 17 | +const queryString = 'chase=me&please=thankyou'; |
| 18 | + |
| 19 | +function initWithRequestDataIntegrationOptions(integrationOptions: RequestDataIntegrationOptions): void { |
| 20 | + const setMockEventProcessor = (eventProcessor: EventProcessor) => |
| 21 | + requestDataEventProcessor.mockImplementationOnce(eventProcessor); |
| 22 | + |
| 23 | + const requestDataIntegration = new RequestData({ |
| 24 | + ...integrationOptions, |
| 25 | + }); |
| 26 | + |
| 27 | + const client = new TestClient( |
| 28 | + getDefaultTestClientOptions({ |
| 29 | + dsn: 'https://[email protected]/12312012', |
| 30 | + integrations: [requestDataIntegration], |
| 31 | + }), |
| 32 | + ); |
| 33 | + client.setupIntegrations = () => requestDataIntegration.setupOnce(setMockEventProcessor, getCurrentHub); |
| 34 | + client.getIntegration = () => requestDataIntegration as any; |
| 35 | + |
| 36 | + const hub = new Hub(client); |
| 37 | + |
| 38 | + makeMain(hub); |
| 39 | +} |
| 40 | + |
| 41 | +describe('`RequestData` integration', () => { |
| 42 | + let req: IncomingMessage, event: Event; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + req = { |
| 46 | + headers, |
| 47 | + method, |
| 48 | + protocol, |
| 49 | + hostname, |
| 50 | + originalUrl: `${path}?${queryString}`, |
| 51 | + } as unknown as IncomingMessage; |
| 52 | + event = { sdkProcessingMetadata: { request: req } }; |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('option conversion', () => { |
| 60 | + it('leaves `ip` and `user` at top level of `include`', () => { |
| 61 | + initWithRequestDataIntegrationOptions({ include: { ip: false, user: true } }); |
| 62 | + |
| 63 | + requestDataEventProcessor(event); |
| 64 | + |
| 65 | + const passedOptions = addRequestDataToEventSpy.mock.calls[0][2]; |
| 66 | + |
| 67 | + expect(passedOptions?.include).toEqual(expect.objectContaining({ ip: false, user: true })); |
| 68 | + }); |
| 69 | + |
| 70 | + it('moves `transactionNamingScheme` to `transaction` include', () => { |
| 71 | + initWithRequestDataIntegrationOptions({ transactionNamingScheme: 'path' }); |
| 72 | + |
| 73 | + requestDataEventProcessor(event); |
| 74 | + |
| 75 | + const passedOptions = addRequestDataToEventSpy.mock.calls[0][2]; |
| 76 | + |
| 77 | + expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'path' })); |
| 78 | + }); |
| 79 | + |
| 80 | + it('moves `true` request keys into `request` include, but omits `false` ones', async () => { |
| 81 | + initWithRequestDataIntegrationOptions({ include: { data: true, cookies: false } }); |
| 82 | + |
| 83 | + requestDataEventProcessor(event); |
| 84 | + |
| 85 | + const passedOptions = addRequestDataToEventSpy.mock.calls[0][2]; |
| 86 | + |
| 87 | + expect(passedOptions?.include?.request).toEqual(expect.arrayContaining(['data'])); |
| 88 | + expect(passedOptions?.include?.request).not.toEqual(expect.arrayContaining(['cookies'])); |
| 89 | + }); |
| 90 | + |
| 91 | + it('moves `true` user keys into `user` include, but omits `false` ones', async () => { |
| 92 | + initWithRequestDataIntegrationOptions({ include: { user: { id: true, email: false } } }); |
| 93 | + |
| 94 | + requestDataEventProcessor(event); |
| 95 | + |
| 96 | + const passedOptions = addRequestDataToEventSpy.mock.calls[0][2]; |
| 97 | + |
| 98 | + expect(passedOptions?.include?.user).toEqual(expect.arrayContaining(['id'])); |
| 99 | + expect(passedOptions?.include?.user).not.toEqual(expect.arrayContaining(['email'])); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments