|
| 1 | +import type { PlaywrightTestArgs } from '@playwright/test'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import type { TestFixtures } from '../../../utils/fixtures'; |
| 4 | +import { sentryTest } from '../../../utils/fixtures'; |
| 5 | +import { envelopeRequestParser, waitForErrorRequest } from '../../../utils/helpers'; |
| 6 | +import { collectReplayRequests, getReplayPerformanceSpans, shouldSkipReplayTest } from '../../../utils/replayHelpers'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Shared helper to run the common test flow |
| 10 | + */ |
| 11 | +async function runRequestFetchTest( |
| 12 | + { page, getLocalTestUrl }: { page: PlaywrightTestArgs['page']; getLocalTestUrl: TestFixtures['getLocalTestUrl'] }, |
| 13 | + options: { |
| 14 | + evaluateFn: () => void; |
| 15 | + expectedBody: any; |
| 16 | + expectedSize: number | any; |
| 17 | + expectedExtraReplayData?: any; |
| 18 | + }, |
| 19 | +) { |
| 20 | + if (shouldSkipReplayTest()) { |
| 21 | + sentryTest.skip(); |
| 22 | + } |
| 23 | + |
| 24 | + await page.route('http://sentry-test.io/foo', route => route.fulfill({ status: 200 })); |
| 25 | + |
| 26 | + const requestPromise = waitForErrorRequest(page); |
| 27 | + const replayRequestPromise = collectReplayRequests(page, recordingEvents => |
| 28 | + getReplayPerformanceSpans(recordingEvents).some(span => span.op === 'resource.fetch'), |
| 29 | + ); |
| 30 | + |
| 31 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 32 | + await page.goto(url); |
| 33 | + await page.evaluate(options.evaluateFn); |
| 34 | + |
| 35 | + // Envelope/Breadcrumbs |
| 36 | + const eventData = envelopeRequestParser(await requestPromise); |
| 37 | + expect(eventData.exception?.values).toHaveLength(1); |
| 38 | + |
| 39 | + const fetchBreadcrumbs = eventData?.breadcrumbs?.filter(b => b.category === 'fetch'); |
| 40 | + expect(fetchBreadcrumbs).toHaveLength(1); |
| 41 | + expect(fetchBreadcrumbs![0]).toEqual({ |
| 42 | + timestamp: expect.any(Number), |
| 43 | + category: 'fetch', |
| 44 | + type: 'http', |
| 45 | + data: { |
| 46 | + method: 'POST', |
| 47 | + request_body_size: options.expectedSize, |
| 48 | + status_code: 200, |
| 49 | + url: 'http://sentry-test.io/foo', |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + // Replay Spans |
| 54 | + const { replayRecordingSnapshots } = await replayRequestPromise; |
| 55 | + const fetchSpans = getReplayPerformanceSpans(replayRecordingSnapshots).filter(s => s.op === 'resource.fetch'); |
| 56 | + expect(fetchSpans).toHaveLength(1); |
| 57 | + |
| 58 | + expect(fetchSpans[0]).toMatchObject({ |
| 59 | + data: { |
| 60 | + method: 'POST', |
| 61 | + statusCode: 200, |
| 62 | + request: { |
| 63 | + body: options.expectedBody, |
| 64 | + }, |
| 65 | + ...options.expectedExtraReplayData, |
| 66 | + }, |
| 67 | + description: 'http://sentry-test.io/foo', |
| 68 | + endTimestamp: expect.any(Number), |
| 69 | + op: 'resource.fetch', |
| 70 | + startTimestamp: expect.any(Number), |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +sentryTest('captures request body when using Request object with text body', async ({ page, getLocalTestUrl }) => { |
| 75 | + await runRequestFetchTest( |
| 76 | + { page, getLocalTestUrl }, |
| 77 | + { |
| 78 | + evaluateFn: () => { |
| 79 | + const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: 'Request body text' }); |
| 80 | + // @ts-expect-error Sentry is a global |
| 81 | + fetch(request).then(() => Sentry.captureException('test error')); |
| 82 | + }, |
| 83 | + expectedBody: 'Request body text', |
| 84 | + expectedSize: 17, |
| 85 | + }, |
| 86 | + ); |
| 87 | +}); |
| 88 | + |
| 89 | +sentryTest('captures request body when using Request object with JSON body', async ({ page, getLocalTestUrl }) => { |
| 90 | + await runRequestFetchTest( |
| 91 | + { page, getLocalTestUrl }, |
| 92 | + { |
| 93 | + evaluateFn: () => { |
| 94 | + const request = new Request('http://sentry-test.io/foo', { |
| 95 | + method: 'POST', |
| 96 | + body: JSON.stringify({ name: 'John', age: 30 }), |
| 97 | + }); |
| 98 | + // @ts-expect-error Sentry is a global |
| 99 | + fetch(request).then(() => Sentry.captureException('test error')); |
| 100 | + }, |
| 101 | + expectedBody: { name: 'John', age: 30 }, |
| 102 | + expectedSize: expect.any(Number), |
| 103 | + }, |
| 104 | + ); |
| 105 | +}); |
| 106 | + |
| 107 | +sentryTest('prioritizes options body over Request object body', async ({ page, getLocalTestUrl, browserName }) => { |
| 108 | + const additionalHeaders = browserName === 'webkit' ? { 'content-type': 'text/plain' } : undefined; |
| 109 | + |
| 110 | + await runRequestFetchTest( |
| 111 | + { page, getLocalTestUrl }, |
| 112 | + { |
| 113 | + evaluateFn: () => { |
| 114 | + const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: 'original body' }); |
| 115 | + // Second argument body should override the Request body |
| 116 | + // @ts-expect-error Sentry is a global |
| 117 | + fetch(request, { body: 'override body' }).then(() => Sentry.captureException('test error')); |
| 118 | + }, |
| 119 | + expectedBody: 'override body', |
| 120 | + expectedSize: 13, |
| 121 | + expectedExtraReplayData: { |
| 122 | + request: { size: 13, headers: {} }, // Specific override structure check |
| 123 | + ...(additionalHeaders && { response: { headers: additionalHeaders } }), |
| 124 | + }, |
| 125 | + }, |
| 126 | + ); |
| 127 | +}); |
| 128 | + |
| 129 | +sentryTest('captures request body with FormData in Request object', async ({ page, getLocalTestUrl }) => { |
| 130 | + await runRequestFetchTest( |
| 131 | + { page, getLocalTestUrl }, |
| 132 | + { |
| 133 | + evaluateFn: () => { |
| 134 | + const params = new URLSearchParams(); |
| 135 | + params.append('key1', 'value1'); |
| 136 | + params.append('key2', 'value2'); |
| 137 | + const request = new Request('http://sentry-test.io/foo', { method: 'POST', body: params }); |
| 138 | + // @ts-expect-error Sentry is a global |
| 139 | + fetch(request).then(() => Sentry.captureException('test error')); |
| 140 | + }, |
| 141 | + expectedBody: 'key1=value1&key2=value2', |
| 142 | + expectedSize: 23, |
| 143 | + }, |
| 144 | + ); |
| 145 | +}); |
0 commit comments