From 59327551335c9874f1fb6976e7d471e43dc7ff06 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 18 Oct 2022 20:39:12 -0700 Subject: [PATCH] add tests --- packages/node/test/handlers.test.ts | 50 ++++++++++++++++++- .../test/integrations/requestdata.test.ts | 21 ++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/node/test/handlers.test.ts b/packages/node/test/handlers.test.ts index ed35c01ef25d..1bf422f59621 100644 --- a/packages/node/test/handlers.test.ts +++ b/packages/node/test/handlers.test.ts @@ -1,5 +1,5 @@ import * as sentryCore from '@sentry/core'; -import { Hub, Scope } from '@sentry/core'; +import { Hub, makeMain, Scope } from '@sentry/core'; import { Transaction } from '@sentry/tracing'; import { Event } from '@sentry/types'; import { SentryError } from '@sentry/utils'; @@ -136,6 +136,22 @@ describe('requestHandler', () => { done(); }); }); + + it('stores request and request data options in `sdkProcessingMetadata`', () => { + const hub = new Hub(new NodeClient(getDefaultNodeClientOptions())); + jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub); + + const requestHandlerOptions = { include: { ip: false } }; + const sentryRequestMiddleware = requestHandler(requestHandlerOptions); + + sentryRequestMiddleware(req, res, next); + + const scope = sentryCore.getCurrentHub().getScope(); + expect((scope as any)._sdkProcessingMetadata).toEqual({ + request: req, + requestDataOptionsFromExpressHandler: requestHandlerOptions, + }); + }); }); describe('tracingHandler', () => { @@ -392,6 +408,19 @@ describe('tracingHandler', () => { done(); }); }); + + it('stores request in transaction metadata', () => { + const options = getDefaultNodeClientOptions({ tracesSampleRate: 1.0 }); + const hub = new Hub(new NodeClient(options)); + + jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub); + + sentryTracingMiddleware(req, res, next); + + const transaction = sentryCore.getCurrentHub().getScope()?.getTransaction(); + + expect(transaction?.metadata.request).toEqual(req); + }); }); describe('errorHandler()', () => { @@ -498,4 +527,23 @@ describe('errorHandler()', () => { const requestSession = scope?.getRequestSession(); expect(requestSession).toEqual(undefined); }); + + it('stores request in `sdkProcessingMetadata`', () => { + const options = getDefaultNodeClientOptions({}); + client = new NodeClient(options); + + const hub = new Hub(client); + makeMain(hub); + + // `sentryErrorMiddleware` uses `withScope`, and we need access to the temporary scope it creates, so monkeypatch + // `captureException` in order to examine the scope as it exists inside the `withScope` callback + hub.captureException = function (this: Hub, _exception: any) { + const scope = this.getScope(); + expect((scope as any)._sdkProcessingMetadata.request).toEqual(req); + } as any; + + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next); + + expect.assertions(1); + }); }); diff --git a/packages/node/test/integrations/requestdata.test.ts b/packages/node/test/integrations/requestdata.test.ts index e1fbaa88b556..8ec956beac8b 100644 --- a/packages/node/test/integrations/requestdata.test.ts +++ b/packages/node/test/integrations/requestdata.test.ts @@ -3,6 +3,7 @@ import { Event, EventProcessor } from '@sentry/types'; import * as http from 'http'; import { NodeClient } from '../../src/client'; +import { requestHandler } from '../../src/handlers'; import { RequestData, RequestDataIntegrationOptions } from '../../src/integrations/requestdata'; import * as requestDataModule from '../../src/requestdata'; import { getDefaultNodeClientOptions } from '../helper/node-client-options'; @@ -100,4 +101,24 @@ describe('`RequestData` integration', () => { expect(passedOptions?.include?.user).not.toEqual(expect.arrayContaining(['email'])); }); }); + + describe('usage with express request handler', () => { + it('uses options from request handler', async () => { + const sentryRequestMiddleware = requestHandler({ include: { transaction: 'methodPath' } }); + const res = new http.ServerResponse(req); + const next = jest.fn(); + + initWithRequestDataIntegrationOptions({ transactionNamingScheme: 'path' }); + + sentryRequestMiddleware(req, res, next); + + await getCurrentHub().getScope()!.applyToEvent(event, {}); + requestDataEventProcessor(event); + + const passedOptions = addRequestDataToEventSpy.mock.calls[0][2]; + + // `transaction` matches the request middleware's option, not the integration's option + expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'methodPath' })); + }); + }); });