Skip to content

Commit 0f69282

Browse files
committed
add and fix tests
1 parent 76ce3cb commit 0f69282

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

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

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub, Hub, makeMain } from '@sentry/core';
2-
import { Event, EventProcessor } from '@sentry/types';
2+
import { Event, EventProcessor, PolymorphicRequest } from '@sentry/types';
33
import * as http from 'http';
44

55
import { NodeClient } from '../../src/client';
@@ -102,8 +102,8 @@ describe('`RequestData` integration', () => {
102102
});
103103
});
104104

105-
describe('usage with express request handler', () => {
106-
it('uses options from request handler', async () => {
105+
describe('usage with express request handler and GCP wrapper', () => {
106+
it('uses options from Express request handler', async () => {
107107
const sentryRequestMiddleware = requestHandler({ include: { transaction: 'methodPath' } });
108108
const res = new http.ServerResponse(req);
109109
const next = jest.fn();
@@ -120,5 +120,34 @@ describe('`RequestData` integration', () => {
120120
// `transaction` matches the request middleware's option, not the integration's option
121121
expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'methodPath' }));
122122
});
123+
124+
it('uses options from GCP wrapper', async () => {
125+
type GCPHandler = (req: PolymorphicRequest, res: http.ServerResponse) => void;
126+
const mockGCPWrapper = (origHandler: GCPHandler, options: Record<string, unknown>): GCPHandler => {
127+
const wrappedHandler: GCPHandler = (req, res) => {
128+
getCurrentHub().getScope()?.setSDKProcessingMetadata({
129+
request: req,
130+
requestDataOptionsFromGCPWrapper: options,
131+
});
132+
origHandler(req, res);
133+
};
134+
return wrappedHandler;
135+
};
136+
137+
const wrappedGCPFunction = mockGCPWrapper(jest.fn(), { include: { transaction: 'methodPath' } });
138+
const res = new http.ServerResponse(req);
139+
140+
initWithRequestDataIntegrationOptions({ transactionNamingScheme: 'path' });
141+
142+
wrappedGCPFunction(req, res);
143+
144+
await getCurrentHub().getScope()!.applyToEvent(event, {});
145+
requestDataEventProcessor(event);
146+
147+
const passedOptions = addRequestDataToEventSpy.mock.calls[0][2];
148+
149+
// `transaction` matches the GCP wrapper's option, not the integration's option
150+
expect(passedOptions?.include).toEqual(expect.objectContaining({ transaction: 'methodPath' }));
151+
});
123152
});
124153
});

packages/serverless/test/__mocks__/@sentry/node.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const origSentry = jest.requireActual('@sentry/node');
22
export const defaultIntegrations = origSentry.defaultIntegrations; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
33
export const Handlers = origSentry.Handlers; // eslint-disable-line @typescript-eslint/no-unsafe-member-access
4+
export const Integrations = origSentry.Integrations;
45
export const addRequestDataToEvent = origSentry.addRequestDataToEvent;
56
export const SDK_VERSION = '6.6.6';
67
export const Severity = {
@@ -20,6 +21,7 @@ export const fakeScope = {
2021
setContext: jest.fn(),
2122
setSpan: jest.fn(),
2223
getTransaction: jest.fn(() => fakeTransaction),
24+
setSDKProcessingMetadata: jest.fn(),
2325
};
2426
export const fakeSpan = {
2527
finish: jest.fn(),

packages/serverless/test/gcpfunction.test.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event } from '@sentry/types';
1+
import * as SentryNode from '@sentry/node';
22
import * as domain from 'domain';
33

44
import * as Sentry from '../src';
@@ -230,23 +230,37 @@ describe('GCPFunction', () => {
230230
});
231231
});
232232

233-
test('wrapHttpFunction request data', async () => {
234-
expect.assertions(6);
233+
// This tests that the necessary pieces are in place for request data to get added to event - the `RequestData`
234+
// integration is included in the defaults and the necessary data is stored in `sdkProcessingMetadata`. The
235+
// integration's tests cover testing that it uses that data correctly.
236+
test('wrapHttpFunction request data prereqs', async () => {
237+
expect.assertions(2);
238+
239+
Sentry.GCPFunction.init({});
235240

236241
const handler: HttpFunction = (_req, res) => {
237242
res.end();
238243
};
239-
const wrappedHandler = wrapHttpFunction(handler);
240-
const event: Event = {};
241-
// @ts-ignore see "Why @ts-ignore" note
242-
Sentry.fakeScope.addEventProcessor.mockImplementation(cb => cb(event));
244+
const wrappedHandler = wrapHttpFunction(handler, { addRequestDataToEventOptions: { include: { ip: true } } });
245+
243246
await handleHttp(wrappedHandler);
244-
expect(event.transaction).toEqual('POST /path');
245-
expect(event.request?.method).toEqual('POST');
246-
expect(event.request?.url).toEqual('http://hostname/path?q=query');
247-
expect(event.request?.query_string).toEqual('q=query');
248-
expect(event.request?.headers).toEqual({ host: 'hostname', 'content-type': 'application/json' });
249-
expect(event.request?.data).toEqual('{"foo":"bar"}');
247+
248+
expect(SentryNode.init).toHaveBeenCalledWith(
249+
expect.objectContaining({
250+
defaultIntegrations: expect.arrayContaining([expect.any(SentryNode.Integrations.RequestData)]),
251+
}),
252+
);
253+
254+
// @ts-ignore see "Why @ts-ignore" note
255+
expect(Sentry.fakeScope.setSDKProcessingMetadata).toHaveBeenCalledWith({
256+
request: {
257+
method: 'POST',
258+
url: '/path?q=query',
259+
headers: { host: 'hostname', 'content-type': 'application/json' },
260+
body: { foo: 'bar' },
261+
},
262+
requestDataOptionsFromGCPWrapper: { include: { ip: true } },
263+
});
250264
});
251265

252266
describe('wrapEventFunction() without callback', () => {

0 commit comments

Comments
 (0)