|
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,28 @@ 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 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 | + }); |
501 | 554 | });
|
0 commit comments