|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | + |
| 3 | +import { TEST_HOST, sentryTest } from '../../../../utils/fixtures'; |
| 4 | +import { |
| 5 | + envelopeRequestParser, |
| 6 | + shouldSkipTracingTest, |
| 7 | + waitForTransactionRequestOnUrl, |
| 8 | +} from '../../../../utils/helpers'; |
| 9 | + |
| 10 | +sentryTest('should create spans for fetch requests', async ({ getLocalTestUrl, page }) => { |
| 11 | + if (shouldSkipTracingTest()) { |
| 12 | + sentryTest.skip(); |
| 13 | + } |
| 14 | + |
| 15 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 16 | + const req = await waitForTransactionRequestOnUrl(page, url); |
| 17 | + const tracingEvent = envelopeRequestParser(req); |
| 18 | + |
| 19 | + const requestSpans = tracingEvent.spans?.filter(({ op }) => op === 'http.client'); |
| 20 | + |
| 21 | + expect(requestSpans).toHaveLength(3); |
| 22 | + |
| 23 | + requestSpans?.forEach((span, index) => |
| 24 | + expect(span).toMatchObject({ |
| 25 | + description: `GET /test-req/${index}`, |
| 26 | + parent_span_id: tracingEvent.contexts?.trace?.span_id, |
| 27 | + span_id: expect.any(String), |
| 28 | + start_timestamp: expect.any(Number), |
| 29 | + timestamp: expect.any(Number), |
| 30 | + trace_id: tracingEvent.contexts?.trace?.trace_id, |
| 31 | + data: { |
| 32 | + 'http.method': 'GET', |
| 33 | + 'http.url': `${TEST_HOST}/test-req/${index}`, |
| 34 | + url: `/test-req/${index}`, |
| 35 | + 'server.address': 'sentry-test.io', |
| 36 | + type: 'fetch', |
| 37 | + }, |
| 38 | + }), |
| 39 | + ); |
| 40 | +}); |
| 41 | + |
| 42 | +sentryTest('should attach `sentry-trace` header to fetch requests', async ({ getLocalTestUrl, page }) => { |
| 43 | + if (shouldSkipTracingTest()) { |
| 44 | + sentryTest.skip(); |
| 45 | + } |
| 46 | + |
| 47 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 48 | + |
| 49 | + const requests = ( |
| 50 | + await Promise.all([ |
| 51 | + page.goto(url), |
| 52 | + Promise.all([0, 1, 2].map(idx => page.waitForRequest(`${TEST_HOST}/test-req/${idx}`))), |
| 53 | + ]) |
| 54 | + )[1]; |
| 55 | + |
| 56 | + expect(requests).toHaveLength(3); |
| 57 | + |
| 58 | + const request1 = requests[0]; |
| 59 | + const requestHeaders1 = request1.headers(); |
| 60 | + expect(requestHeaders1).toMatchObject({ |
| 61 | + 'sentry-trace': expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})-1$/), |
| 62 | + baggage: expect.any(String), |
| 63 | + }); |
| 64 | + |
| 65 | + const request2 = requests[1]; |
| 66 | + const requestHeaders2 = request2.headers(); |
| 67 | + expect(requestHeaders2).toMatchObject({ |
| 68 | + 'sentry-trace': expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})-1$/), |
| 69 | + baggage: expect.any(String), |
| 70 | + 'x-test-header': 'existing-header', |
| 71 | + }); |
| 72 | + |
| 73 | + const request3 = requests[2]; |
| 74 | + const requestHeaders3 = request3.headers(); |
| 75 | + expect(requestHeaders3).toMatchObject({ |
| 76 | + 'sentry-trace': expect.stringMatching(/^([a-f0-9]{32})-([a-f0-9]{16})-1$/), |
| 77 | + baggage: expect.any(String), |
| 78 | + }); |
| 79 | +}); |
0 commit comments