Skip to content

Commit 09dcbd4

Browse files
committed
add tests
1 parent 4f715c9 commit 09dcbd4

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

packages/node/test/handlers.test.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as sentryCore from '@sentry/core';
2-
import { Hub, Scope } from '@sentry/core';
2+
import { Hub, makeMain, Scope } from '@sentry/core';
33
import { Transaction } from '@sentry/tracing';
44
import { Event } from '@sentry/types';
55
import { SentryError } from '@sentry/utils';
@@ -136,6 +136,22 @@ describe('requestHandler', () => {
136136
done();
137137
});
138138
});
139+
140+
it('stores request and request data options in `sdkProcessingMetadata`', () => {
141+
const hub = new Hub(new NodeClient(getDefaultNodeClientOptions()));
142+
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
143+
144+
const requestHandlerOptions = { include: { ip: false } };
145+
const sentryRequestMiddleware = requestHandler(requestHandlerOptions);
146+
147+
sentryRequestMiddleware(req, res, next);
148+
149+
const scope = sentryCore.getCurrentHub().getScope();
150+
expect((scope as any)._sdkProcessingMetadata).toEqual({
151+
request: req,
152+
requestDataOptionsFromExpressHandler: requestHandlerOptions,
153+
});
154+
});
139155
});
140156

141157
describe('tracingHandler', () => {
@@ -392,6 +408,19 @@ describe('tracingHandler', () => {
392408
done();
393409
});
394410
});
411+
412+
it('stores request in transaction metadata', () => {
413+
const options = getDefaultNodeClientOptions({ tracesSampleRate: 1.0 });
414+
const hub = new Hub(new NodeClient(options));
415+
416+
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
417+
418+
sentryTracingMiddleware(req, res, next);
419+
420+
const transaction = sentryCore.getCurrentHub().getScope()?.getTransaction();
421+
422+
expect(transaction?.metadata.request).toEqual(req);
423+
});
395424
});
396425

397426
describe('errorHandler()', () => {
@@ -498,4 +527,28 @@ describe('errorHandler()', () => {
498527
const requestSession = scope?.getRequestSession();
499528
expect(requestSession).toEqual(undefined);
500529
});
530+
531+
it('stores request in `sdkProcessingMetadata`', () => {
532+
const options = getDefaultNodeClientOptions({});
533+
client = new NodeClient(options);
534+
535+
const hub = new Hub(client);
536+
makeMain(hub);
537+
538+
// `sentryErrorMiddleware` uses `withScope`, and we need access to the temporary scope it creates, so we monkeypatch
539+
// `pushScope` in order to store the new scope it creates. (This initial value for `scope` will be replaced when `pushScope` is called by
540+
// `withScope` inside of `sentryErrorMiddleware`, and is just here to prevent the `expect` statement below from
541+
// throwing a `Variable 'scope' is used before being assigned` error.)
542+
let scope: Scope = {} as Scope;
543+
// eslint-disable-next-line @typescript-eslint/unbound-method
544+
const origPushScope = hub.pushScope;
545+
hub.pushScope = function (this: Hub) {
546+
scope = origPushScope.call(this);
547+
return scope;
548+
};
549+
550+
sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next);
551+
552+
expect((scope as any)._sdkProcessingMetadata.request).toEqual(req);
553+
});
501554
});

packages/node/test/integrations/requestdata.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Event, EventProcessor } from '@sentry/types';
33
import * as http from 'http';
44

55
import { NodeClient } from '../../src/client';
6+
import { requestHandler } from '../../src/handlers';
67
import { RequestData, RequestDataIntegrationOptions } from '../../src/integrations/requestdata';
78
import * as requestDataModule from '../../src/requestdata';
89
import { getDefaultNodeClientOptions } from '../helper/node-client-options';
@@ -100,4 +101,24 @@ describe('`RequestData` integration', () => {
100101
expect(passedOptions?.include?.user).not.toEqual(expect.arrayContaining(['email']));
101102
});
102103
});
104+
105+
describe('usage with express request handler', () => {
106+
it('uses options from request handler', async () => {
107+
const sentryRequestMiddleware = requestHandler({ include: { transaction: 'methodPath' } });
108+
const res = new http.ServerResponse(req);
109+
const next = jest.fn();
110+
111+
initWithRequestDataIntegrationOptions({ transactionNamingScheme: 'path' });
112+
113+
sentryRequestMiddleware(req, res, next);
114+
115+
await getCurrentHub().getScope()!.applyToEvent(event, {});
116+
requestDataEventProcessor(event);
117+
118+
const passedOptions = addRequestDataToEventSpy.mock.calls[0][2];
119+
120+
// `transaction` matches the request middleware's option, not the integration's option
121+
expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'methodPath' }));
122+
});
123+
});
103124
});

0 commit comments

Comments
 (0)