|
| 1 | +import * as SentryNode from '@sentry/node'; |
| 2 | +import * as SentryUtils from '@sentry/utils'; |
| 3 | +import { vi } from 'vitest'; |
| 4 | + |
| 5 | +import { handleRequest, interpolateRouteFromUrlAndParams } from '../../src/server/middleware'; |
| 6 | + |
| 7 | +describe('sentryMiddleware', () => { |
| 8 | + const startSpanSpy = vi.spyOn(SentryNode, 'startSpan'); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.clearAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('creates a span for an incoming request', async () => { |
| 15 | + const middleware = handleRequest(); |
| 16 | + const ctx = { |
| 17 | + request: { |
| 18 | + method: 'GET', |
| 19 | + url: '/users/123/details', |
| 20 | + headers: new Headers(), |
| 21 | + }, |
| 22 | + url: new URL('https://myDomain.io/users/123/details'), |
| 23 | + params: { |
| 24 | + id: '123', |
| 25 | + }, |
| 26 | + }; |
| 27 | + const nextResult = Promise.resolve({ status: 200 }); |
| 28 | + const next = vi.fn(() => nextResult); |
| 29 | + |
| 30 | + // @ts-expect-error, a partial ctx object is fine here |
| 31 | + const resultFromNext = middleware(ctx, next); |
| 32 | + |
| 33 | + expect(startSpanSpy).toHaveBeenCalledWith( |
| 34 | + { |
| 35 | + data: { |
| 36 | + method: 'GET', |
| 37 | + url: 'https://mydomain.io/users/123/details', |
| 38 | + }, |
| 39 | + metadata: { |
| 40 | + source: 'route', |
| 41 | + }, |
| 42 | + name: 'GET /users/[id]/details', |
| 43 | + op: 'http.server.get', |
| 44 | + origin: 'auto.http.astro', |
| 45 | + status: 'ok', |
| 46 | + }, |
| 47 | + expect.any(Function), // the `next` function |
| 48 | + ); |
| 49 | + |
| 50 | + expect(next).toHaveBeenCalled(); |
| 51 | + expect(resultFromNext).toStrictEqual(nextResult); |
| 52 | + }); |
| 53 | + |
| 54 | + it('throws and sends an error to sentry if `next()` throws', async () => { |
| 55 | + const scope = { |
| 56 | + addEventProcessor: vi.fn().mockImplementation(cb => cb({})), |
| 57 | + }; |
| 58 | + // @ts-expect-error, just testing the callback, this is okay for this test |
| 59 | + const captureExceptionSpy = vi.spyOn(SentryNode, 'captureException').mockImplementation((ex, cb) => cb(scope)); |
| 60 | + const addExMechanismSpy = vi.spyOn(SentryUtils, 'addExceptionMechanism'); |
| 61 | + |
| 62 | + const middleware = handleRequest(); |
| 63 | + const ctx = { |
| 64 | + request: { |
| 65 | + method: 'GET', |
| 66 | + url: '/users', |
| 67 | + headers: new Headers(), |
| 68 | + }, |
| 69 | + url: new URL('https://myDomain.io/users/'), |
| 70 | + params: {}, |
| 71 | + }; |
| 72 | + |
| 73 | + const error = new Error('Something went wrong'); |
| 74 | + |
| 75 | + const next = vi.fn(() => { |
| 76 | + throw error; |
| 77 | + }); |
| 78 | + |
| 79 | + // @ts-expect-error, a partial ctx object is fine here |
| 80 | + await expect(async () => middleware(ctx, next)).rejects.toThrowError(); |
| 81 | + |
| 82 | + expect(captureExceptionSpy).toHaveBeenCalledWith(error, expect.any(Function)); |
| 83 | + expect(scope.addEventProcessor).toHaveBeenCalledTimes(1); |
| 84 | + expect(addExMechanismSpy).toHaveBeenCalledWith( |
| 85 | + {}, // the mocked event |
| 86 | + { |
| 87 | + handled: false, |
| 88 | + type: 'astro', |
| 89 | + data: { function: 'astroMiddleware' }, |
| 90 | + }, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('attaches tracing headers', async () => { |
| 95 | + const scope = { setUser: vi.fn(), setPropagationContext: vi.fn() }; |
| 96 | + // @ts-expect-error, only passing a partial Scope object |
| 97 | + const configureScopeSpy = vi.spyOn(SentryNode, 'configureScope').mockImplementation(cb => cb(scope)); |
| 98 | + |
| 99 | + const middleware = handleRequest(); |
| 100 | + const ctx = { |
| 101 | + request: { |
| 102 | + method: 'GET', |
| 103 | + url: '/users', |
| 104 | + headers: new Headers({ |
| 105 | + 'sentry-trace': '12345678901234567890123456789012-1234567890123456-1', |
| 106 | + baggage: 'sentry-release=1.0.0', |
| 107 | + }), |
| 108 | + }, |
| 109 | + params: {}, |
| 110 | + url: new URL('https://myDomain.io/users/'), |
| 111 | + }; |
| 112 | + const next = vi.fn(); |
| 113 | + |
| 114 | + // @ts-expect-error, a partial ctx object is fine here |
| 115 | + await middleware(ctx, next); |
| 116 | + |
| 117 | + expect(configureScopeSpy).toHaveBeenCalledTimes(1); |
| 118 | + expect(scope.setPropagationContext).toHaveBeenCalledWith({ |
| 119 | + dsc: { |
| 120 | + release: '1.0.0', |
| 121 | + }, |
| 122 | + parentSpanId: '1234567890123456', |
| 123 | + sampled: true, |
| 124 | + spanId: expect.any(String), |
| 125 | + traceId: '12345678901234567890123456789012', |
| 126 | + }); |
| 127 | + |
| 128 | + expect(startSpanSpy).toHaveBeenCalledWith( |
| 129 | + expect.objectContaining({ |
| 130 | + metadata: { |
| 131 | + source: 'route', |
| 132 | + dynamicSamplingContext: { |
| 133 | + release: '1.0.0', |
| 134 | + }, |
| 135 | + }, |
| 136 | + parentSampled: true, |
| 137 | + parentSpanId: '1234567890123456', |
| 138 | + traceId: '12345678901234567890123456789012', |
| 139 | + }), |
| 140 | + expect.any(Function), // the `next` function |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it('attaches client IP and request headers if options are set', async () => { |
| 145 | + const scope = { setUser: vi.fn(), setPropagationContext: vi.fn() }; |
| 146 | + // @ts-expect-error, only passing a partial Scope object |
| 147 | + const configureScopeSpy = vi.spyOn(SentryNode, 'configureScope').mockImplementation(cb => cb(scope)); |
| 148 | + |
| 149 | + const middleware = handleRequest({ trackClientIp: true, trackHeaders: true }); |
| 150 | + const ctx = { |
| 151 | + request: { |
| 152 | + method: 'GET', |
| 153 | + url: '/users', |
| 154 | + headers: new Headers({ |
| 155 | + 'some-header': 'some-value', |
| 156 | + }), |
| 157 | + }, |
| 158 | + clientAddress: '192.168.0.1', |
| 159 | + params: {}, |
| 160 | + url: new URL('https://myDomain.io/users/'), |
| 161 | + }; |
| 162 | + const next = vi.fn(); |
| 163 | + |
| 164 | + // @ts-expect-error, a partial ctx object is fine here |
| 165 | + await middleware(ctx, next); |
| 166 | + |
| 167 | + expect(configureScopeSpy).toHaveBeenCalledTimes(1); |
| 168 | + expect(scope.setUser).toHaveBeenCalledWith({ ip_address: '192.168.0.1' }); |
| 169 | + |
| 170 | + expect(startSpanSpy).toHaveBeenCalledWith( |
| 171 | + expect.objectContaining({ |
| 172 | + data: expect.objectContaining({ |
| 173 | + headers: { |
| 174 | + 'some-header': 'some-value', |
| 175 | + }, |
| 176 | + }), |
| 177 | + }), |
| 178 | + expect.any(Function), // the `next` function |
| 179 | + ); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('interpolateRouteFromUrlAndParams', () => { |
| 184 | + it.each([ |
| 185 | + ['/foo/bar', {}, '/foo/bar'], |
| 186 | + ['/users/123', { id: '123' }, '/users/[id]'], |
| 187 | + ['/users/123', { id: '123', foo: 'bar' }, '/users/[id]'], |
| 188 | + ['/lang/en-US', { lang: 'en', region: 'US' }, '/lang/[lang]-[region]'], |
| 189 | + ['/lang/en-US/posts', { lang: 'en', region: 'US' }, '/lang/[lang]-[region]/posts'], |
| 190 | + ])('interpolates route from URL and params %s', (rawUrl, params, expectedRoute) => { |
| 191 | + expect(interpolateRouteFromUrlAndParams(rawUrl, params)).toEqual(expectedRoute); |
| 192 | + }); |
| 193 | + |
| 194 | + it('handles params across multiple URL segments in catchall routes', () => { |
| 195 | + // Ideally, Astro would let us know that this is a catchall route so we can make the param [...catchall] but it doesn't |
| 196 | + expect( |
| 197 | + interpolateRouteFromUrlAndParams('/someroute/catchall-123/params/foo/bar', { |
| 198 | + catchall: 'catchall-123/params/foo', |
| 199 | + params: 'foo', |
| 200 | + }), |
| 201 | + ).toEqual('/someroute/[catchall]/bar'); |
| 202 | + }); |
| 203 | + |
| 204 | + it("doesn't replace partially matching route segments", () => { |
| 205 | + const rawUrl = '/usernames/username'; |
| 206 | + const params = { name: 'username' }; |
| 207 | + const expectedRoute = '/usernames/[name]'; |
| 208 | + expect(interpolateRouteFromUrlAndParams(rawUrl, params)).toEqual(expectedRoute); |
| 209 | + }); |
| 210 | +}); |
0 commit comments