|
| 1 | +import type { EventEnvelope, EventItem } from '@sentry/core'; |
| 2 | +import { createEnvelope, getTunnelPath, isTunnelRequest } from '@sentry/core'; |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { getClient, getCurrentScope, init } from '../../src'; |
| 5 | +import { makeFetchTransport } from '../../src/transports/fetch'; |
| 6 | +import { resolveBrowserTunnelOption } from '../../src/utils/managedTunnel'; |
| 7 | + |
| 8 | +const DSN = 'https://public@o123.ingest.sentry.io/1337'; |
| 9 | + |
| 10 | +describe('resolveBrowserTunnelOption', () => { |
| 11 | + it('resolves `tunnel: true` to the path the server derives from the same DSN', () => { |
| 12 | + const { tunnel } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: true }); |
| 13 | + |
| 14 | + expect(tunnel).toBe(getTunnelPath(DSN)); |
| 15 | + expect(isTunnelRequest({ method: 'POST', url: tunnel! }, { allowedDsns: [DSN] })).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('prefixes the base path and appends a trailing slash when asked', () => { |
| 19 | + const { tunnel } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: { basePath: '/docs/', trailingSlash: true } }); |
| 20 | + |
| 21 | + expect(tunnel).toBe(`/docs${getTunnelPath(DSN)}/`); |
| 22 | + expect(isTunnelRequest({ method: 'POST', url: tunnel! }, { allowedDsns: [DSN], basePath: '/docs' })).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('uses a fixed path instead of the DSN-derived one', () => { |
| 26 | + const { tunnel, transportOptions } = resolveBrowserTunnelOption({ dsn: DSN, tunnel: { path: '/monitor' } }); |
| 27 | + |
| 28 | + expect(tunnel).toBe('/monitor'); |
| 29 | + expect(transportOptions?.headers).toEqual({ 'Content-Type': 'application/x-sentry-envelope' }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('keeps a URL tunnel and its transport options untouched', () => { |
| 33 | + const transportOptions = { headers: { 'x-custom': '1' } }; |
| 34 | + |
| 35 | + expect(resolveBrowserTunnelOption({ dsn: DSN, tunnel: '/my-tunnel', transportOptions })).toEqual({ |
| 36 | + tunnel: '/my-tunnel', |
| 37 | + transportOptions, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('turns the tunnel off without a valid DSN', () => { |
| 42 | + expect(resolveBrowserTunnelOption({ tunnel: true }).tunnel).toBeUndefined(); |
| 43 | + expect(resolveBrowserTunnelOption({ dsn: 'not-a-dsn', tunnel: true }).tunnel).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('sends envelopes as application/x-sentry-envelope so CSRF checks do not treat them as form posts', async () => { |
| 47 | + const { tunnel, transportOptions } = resolveBrowserTunnelOption({ |
| 48 | + dsn: DSN, |
| 49 | + tunnel: true, |
| 50 | + transportOptions: { headers: { 'x-custom': '1' } }, |
| 51 | + }); |
| 52 | + const nativeFetch = vi.fn().mockResolvedValue(new Response(null, { status: 200 })); |
| 53 | + |
| 54 | + const transport = makeFetchTransport( |
| 55 | + { ...transportOptions, url: tunnel!, recordDroppedEvent: () => undefined }, |
| 56 | + nativeFetch, |
| 57 | + ); |
| 58 | + await transport.send( |
| 59 | + createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ |
| 60 | + [{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, |
| 61 | + ]), |
| 62 | + ); |
| 63 | + |
| 64 | + expect(nativeFetch).toHaveBeenCalledWith( |
| 65 | + getTunnelPath(DSN), |
| 66 | + expect.objectContaining({ |
| 67 | + headers: { 'Content-Type': 'application/x-sentry-envelope', 'x-custom': '1' }, |
| 68 | + }), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('lets a user-provided Content-Type header win', () => { |
| 73 | + const { transportOptions } = resolveBrowserTunnelOption({ |
| 74 | + dsn: DSN, |
| 75 | + tunnel: true, |
| 76 | + transportOptions: { headers: { 'Content-Type': 'text/plain' } }, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(transportOptions?.headers).toEqual({ 'Content-Type': 'text/plain' }); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('init with `tunnel: true`', () => { |
| 84 | + afterEach(() => { |
| 85 | + getCurrentScope().setClient(undefined); |
| 86 | + }); |
| 87 | + |
| 88 | + it('gives the client a resolved tunnel URL', () => { |
| 89 | + init({ dsn: DSN, tunnel: true, defaultIntegrations: false }); |
| 90 | + |
| 91 | + expect(getClient()?.getOptions().tunnel).toBe(getTunnelPath(DSN)); |
| 92 | + }); |
| 93 | +}); |
0 commit comments