|
1 | 1 | import * as sentryCore from '@sentry/core'; |
2 | | -import { Hub, Scope } from '@sentry/core'; |
| 2 | +import { Hub, makeMain, Scope } from '@sentry/core'; |
3 | 3 | import { Transaction } from '@sentry/tracing'; |
4 | 4 | import { Event } from '@sentry/types'; |
5 | 5 | import { SentryError } from '@sentry/utils'; |
@@ -136,6 +136,22 @@ describe('requestHandler', () => { |
136 | 136 | done(); |
137 | 137 | }); |
138 | 138 | }); |
| 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 | + }); |
139 | 155 | }); |
140 | 156 |
|
141 | 157 | describe('tracingHandler', () => { |
@@ -392,6 +408,19 @@ describe('tracingHandler', () => { |
392 | 408 | done(); |
393 | 409 | }); |
394 | 410 | }); |
| 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 | + }); |
395 | 424 | }); |
396 | 425 |
|
397 | 426 | describe('errorHandler()', () => { |
@@ -498,4 +527,23 @@ describe('errorHandler()', () => { |
498 | 527 | const requestSession = scope?.getRequestSession(); |
499 | 528 | expect(requestSession).toEqual(undefined); |
500 | 529 | }); |
| 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 monkeypatch |
| 539 | + // `captureException` in order to examine the scope as it exists inside the `withScope` callback |
| 540 | + hub.captureException = function (this: Hub, _exception: any) { |
| 541 | + const scope = this.getScope(); |
| 542 | + expect((scope as any)._sdkProcessingMetadata.request).toEqual(req); |
| 543 | + } as any; |
| 544 | + |
| 545 | + sentryErrorMiddleware({ name: 'error', message: 'this is an error' }, req, res, next); |
| 546 | + |
| 547 | + expect.assertions(1); |
| 548 | + }); |
501 | 549 | }); |
0 commit comments