|
1 | 1 | import { expect, test } from '@playwright/test'; |
2 | | -import { getSpanOp, waitForStreamedSpan, waitForStreamedSpans } from '@sentry-internal/test-utils'; |
3 | | -import type { SerializedStreamedSpan } from '@sentry/core'; |
| 2 | +import { getSpanOp, waitForRequest, waitForStreamedSpan, waitForStreamedSpans } from '@sentry-internal/test-utils'; |
| 3 | +import type { Event, SerializedStreamedSpan } from '@sentry/core'; |
4 | 4 |
|
5 | 5 | function getStringAttribute(span: SerializedStreamedSpan, key: string): string | undefined { |
6 | 6 | const attribute = span.attributes[key]; |
@@ -120,3 +120,68 @@ test('Tunnel requests should not create middleware or fetch spans', async ({ pag |
120 | 120 |
|
121 | 121 | expect(sentryFetchSpans).toHaveLength(0); |
122 | 122 | }); |
| 123 | + |
| 124 | +test.describe('Tunnel route handler', () => { |
| 125 | + test.skip(process.env.NEXT_PUBLIC_E2E_TEST_TUNNEL_MODE !== 'handler', 'Only the handler variants use the handler'); |
| 126 | + |
| 127 | + test('forwards events without the cookies of the browser', async ({ page, context, baseURL }) => { |
| 128 | + await context.addCookies([{ name: 'session', value: '8f2c1d', url: baseURL }]); |
| 129 | + const proxyRequestPromise = waitForRequest('nextjs-16-tunnel', ({ envelope }) => |
| 130 | + envelope[1].some( |
| 131 | + ([itemHeader, item]) => |
| 132 | + itemHeader.type === 'event' && |
| 133 | + (item as Event).exception?.values?.[0]?.value === 'Tunnel route handler cookie test', |
| 134 | + ), |
| 135 | + ); |
| 136 | + |
| 137 | + await page.goto('/'); |
| 138 | + await page.waitForFunction(() => typeof (window as { __SENTRY__?: unknown }).__SENTRY__ === 'object'); |
| 139 | + const tunnelRequestPromise = page.waitForRequest( |
| 140 | + request => |
| 141 | + request.method() === 'POST' && |
| 142 | + new URL(request.url()).origin === baseURL && |
| 143 | + (request.postData() ?? '').includes('Tunnel route handler cookie test'), |
| 144 | + ); |
| 145 | + await page.evaluate(() => { |
| 146 | + setTimeout(() => { |
| 147 | + throw new Error('Tunnel route handler cookie test'); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + const tunnelRequest = await tunnelRequestPromise; |
| 152 | + const proxyRequest = await proxyRequestPromise; |
| 153 | + |
| 154 | + expect(new URL(tunnelRequest.url()).search).toBe(''); |
| 155 | + expect((await tunnelRequest.allHeaders()).cookie).toBe('session=8f2c1d'); |
| 156 | + expect(proxyRequest.rawProxyRequestHeaders.cookie).toBeUndefined(); |
| 157 | + expect(proxyRequest.rawProxyRequestHeaders['content-type']).toBe('application/x-sentry-envelope'); |
| 158 | + }); |
| 159 | + |
| 160 | + test('does not create server spans for tunnel requests', async ({ page }) => { |
| 161 | + // The callback never returns true, so this promise is deliberately left unsettled. |
| 162 | + const allSpans: SerializedStreamedSpan[] = []; |
| 163 | + void waitForStreamedSpans('nextjs-16-tunnel', spans => { |
| 164 | + allSpans.push(...spans); |
| 165 | + return false; |
| 166 | + }); |
| 167 | + const pageloadSpanPromise = waitForStreamedSpan('nextjs-16-tunnel', span => { |
| 168 | + return getSpanOp(span) === 'pageload' && span.is_segment; |
| 169 | + }); |
| 170 | + |
| 171 | + await page.goto('/'); |
| 172 | + await pageloadSpanPromise; |
| 173 | + // Wait for the server to send the spans that the tunnel request would wrongly produce |
| 174 | + await page.waitForTimeout(3000); |
| 175 | + |
| 176 | + const postSpans = allSpans.filter( |
| 177 | + span => span.name.startsWith('POST') || getStringAttribute(span, 'http.request.method') === 'POST', |
| 178 | + ); |
| 179 | + const ingestSpans = allSpans.filter( |
| 180 | + span => |
| 181 | + getSpanOp(span) === 'http.client' && |
| 182 | + !!getStringAttribute(span, 'url.full')?.startsWith('http://localhost:3031/'), |
| 183 | + ); |
| 184 | + expect(postSpans).toEqual([]); |
| 185 | + expect(ingestSpans).toEqual([]); |
| 186 | + }); |
| 187 | +}); |
0 commit comments