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