|
| 1 | +import { HttpFunction } from '@google-cloud/functions-framework/build/src/functions'; |
| 2 | +import { Event } from '@sentry/types'; |
| 3 | +import * as domain from 'domain'; |
| 4 | +import { Request, Response } from 'express'; |
| 5 | +import { ServerResponse } from 'http'; |
| 6 | + |
| 7 | +import * as Sentry from '../src'; |
| 8 | + |
| 9 | +const { wrapHttp } = Sentry.GCPFunction; |
| 10 | + |
| 11 | +/** |
| 12 | + * Why @ts-ignore some Sentry.X calls |
| 13 | + * |
| 14 | + * A hack-ish way to contain everything related to mocks in the same __mocks__ file. |
| 15 | + * Thanks to this, we don't have to do more magic than necessary. Just add and export desired method and assert on it. |
| 16 | + */ |
| 17 | + |
| 18 | +describe('GCPFunction', () => { |
| 19 | + afterEach(() => { |
| 20 | + // @ts-ignore see "Why @ts-ignore" note |
| 21 | + Sentry.resetMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + async function invokeHttp(fn: HttpFunction): Promise<void> { |
| 25 | + const req = { |
| 26 | + method: 'GET', |
| 27 | + host: '', |
| 28 | + cookies: {}, |
| 29 | + query: {}, |
| 30 | + url: '/path', |
| 31 | + headers: {}, |
| 32 | + } as Request; |
| 33 | + const res = new ServerResponse(req) as Response; |
| 34 | + await new Promise<void>((resolve, _reject) => { |
| 35 | + const d = domain.create(); |
| 36 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 37 | + const _end = res.end; |
| 38 | + res.end = function(...args: any[]): void { |
| 39 | + resolve(); |
| 40 | + _end.call(this, ...args); |
| 41 | + }; |
| 42 | + d.on('error', () => { |
| 43 | + res.end(); |
| 44 | + }); |
| 45 | + d.run(fn, req, res); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + describe('wrapHttp() options', () => { |
| 50 | + test('flushTimeout', async () => { |
| 51 | + expect.assertions(1); |
| 52 | + |
| 53 | + const handler: HttpFunction = (_, res) => { |
| 54 | + res.end(); |
| 55 | + }; |
| 56 | + const wrappedHandler = wrapHttp(handler, { flushTimeout: 1337 }); |
| 57 | + |
| 58 | + await invokeHttp(wrappedHandler); |
| 59 | + expect(Sentry.flush).toBeCalledWith(1337); |
| 60 | + }); |
| 61 | + |
| 62 | + test('tracing enabled', async () => { |
| 63 | + expect.assertions(1); |
| 64 | + |
| 65 | + const handler: HttpFunction = (_, res) => { |
| 66 | + res.end(); |
| 67 | + }; |
| 68 | + await invokeHttp(wrapHttp(handler, { tracing: true })); |
| 69 | + expect(Sentry.startTransaction).toBeCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('tracing disabled', async () => { |
| 73 | + expect.assertions(1); |
| 74 | + |
| 75 | + const handler: HttpFunction = (_, res) => { |
| 76 | + res.end(); |
| 77 | + }; |
| 78 | + await invokeHttp(wrapHttp(handler, { tracing: false })); |
| 79 | + expect(Sentry.startTransaction).not.toBeCalled(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('wrapHandler()', () => { |
| 84 | + test('successful execution', async () => { |
| 85 | + expect.assertions(5); |
| 86 | + |
| 87 | + const handler: HttpFunction = (_, res) => { |
| 88 | + res.statusCode = 200; |
| 89 | + res.end(); |
| 90 | + }; |
| 91 | + const wrappedHandler = wrapHttp(handler); |
| 92 | + await invokeHttp(wrappedHandler); |
| 93 | + expect(Sentry.startTransaction).toBeCalledWith({ name: 'GET /path', op: 'gcp.function.http' }); |
| 94 | + // @ts-ignore see "Why @ts-ignore" note |
| 95 | + expect(Sentry.fakeParentScope.setSpan).toBeCalledWith(Sentry.fakeTransaction); |
| 96 | + // @ts-ignore see "Why @ts-ignore" note |
| 97 | + expect(Sentry.fakeTransaction.setHttpStatus).toBeCalledWith(200); |
| 98 | + // @ts-ignore see "Why @ts-ignore" note |
| 99 | + expect(Sentry.fakeTransaction.finish).toBeCalled(); |
| 100 | + expect(Sentry.flush).toBeCalledWith(2000); |
| 101 | + }); |
| 102 | + |
| 103 | + test('capture error', async () => { |
| 104 | + expect.assertions(5); |
| 105 | + |
| 106 | + const error = new Error('wat'); |
| 107 | + const handler: HttpFunction = (_req, _res) => { |
| 108 | + process.nextTick(() => { |
| 109 | + throw error; |
| 110 | + }); |
| 111 | + }; |
| 112 | + const wrappedHandler = wrapHttp(handler); |
| 113 | + await invokeHttp(wrappedHandler); |
| 114 | + expect(Sentry.startTransaction).toBeCalledWith({ name: 'GET /path', op: 'gcp.function.http' }); |
| 115 | + // @ts-ignore see "Why @ts-ignore" note |
| 116 | + expect(Sentry.fakeParentScope.setSpan).toBeCalledWith(Sentry.fakeTransaction); |
| 117 | + expect(Sentry.captureException).toBeCalledWith(error); |
| 118 | + // @ts-ignore see "Why @ts-ignore" note |
| 119 | + expect(Sentry.fakeTransaction.finish).toBeCalled(); |
| 120 | + expect(Sentry.flush).toBeCalled(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + test('enhance event with SDK info and correct mechanism value', async () => { |
| 125 | + expect.assertions(2); |
| 126 | + |
| 127 | + const error = new Error('wat'); |
| 128 | + const handler: HttpFunction = () => { |
| 129 | + process.nextTick(() => { |
| 130 | + throw error; |
| 131 | + }); |
| 132 | + }; |
| 133 | + const wrappedHandler = wrapHttp(handler); |
| 134 | + |
| 135 | + const eventWithSomeData = { |
| 136 | + exception: { |
| 137 | + values: [{}], |
| 138 | + }, |
| 139 | + sdk: { |
| 140 | + integrations: ['SomeIntegration'], |
| 141 | + packages: [ |
| 142 | + { |
| 143 | + name: 'some:@random/package', |
| 144 | + version: '1337', |
| 145 | + }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + }; |
| 149 | + // @ts-ignore see "Why @ts-ignore" note |
| 150 | + Sentry.fakeScope.addEventProcessor.mockImplementationOnce(cb => cb(eventWithSomeData)); |
| 151 | + await invokeHttp(wrappedHandler); |
| 152 | + expect(eventWithSomeData).toEqual({ |
| 153 | + exception: { |
| 154 | + values: [ |
| 155 | + { |
| 156 | + mechanism: { |
| 157 | + handled: false, |
| 158 | + }, |
| 159 | + }, |
| 160 | + ], |
| 161 | + }, |
| 162 | + sdk: { |
| 163 | + name: 'sentry.javascript.serverless', |
| 164 | + integrations: ['SomeIntegration', 'GCPFunction'], |
| 165 | + packages: [ |
| 166 | + { |
| 167 | + name: 'some:@random/package', |
| 168 | + version: '1337', |
| 169 | + }, |
| 170 | + { |
| 171 | + name: 'npm:@sentry/serverless', |
| 172 | + version: '6.6.6', |
| 173 | + }, |
| 174 | + ], |
| 175 | + version: '6.6.6', |
| 176 | + }, |
| 177 | + }); |
| 178 | + |
| 179 | + const eventWithoutAnyData: Event = { |
| 180 | + exception: { |
| 181 | + values: [{}], |
| 182 | + }, |
| 183 | + }; |
| 184 | + // @ts-ignore see "Why @ts-ignore" note |
| 185 | + Sentry.fakeScope.addEventProcessor.mockImplementationOnce(cb => cb(eventWithoutAnyData)); |
| 186 | + await invokeHttp(wrappedHandler); |
| 187 | + expect(eventWithoutAnyData).toEqual({ |
| 188 | + exception: { |
| 189 | + values: [ |
| 190 | + { |
| 191 | + mechanism: { |
| 192 | + handled: false, |
| 193 | + }, |
| 194 | + }, |
| 195 | + ], |
| 196 | + }, |
| 197 | + sdk: { |
| 198 | + name: 'sentry.javascript.serverless', |
| 199 | + integrations: ['GCPFunction'], |
| 200 | + packages: [ |
| 201 | + { |
| 202 | + name: 'npm:@sentry/serverless', |
| 203 | + version: '6.6.6', |
| 204 | + }, |
| 205 | + ], |
| 206 | + version: '6.6.6', |
| 207 | + }, |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments