|
7 | 7 | BrowserTracing, |
8 | 8 | BrowserTracingOptions, |
9 | 9 | DEFAULT_MAX_TRANSACTION_DURATION_SECONDS, |
| 10 | + getHeaderContext, |
10 | 11 | getMetaContent, |
11 | 12 | } from '../../src/browser/browsertracing'; |
12 | 13 | import { defaultRequestInstrumentionOptions } from '../../src/browser/request'; |
@@ -340,22 +341,84 @@ describe('BrowserTracing', () => { |
340 | 341 | }); |
341 | 342 | }); |
342 | 343 | }); |
343 | | -}); |
344 | 344 |
|
345 | | -describe('getMeta', () => { |
346 | | - it('returns a found meta tag contents', () => { |
347 | | - const name = 'sentry-trace'; |
348 | | - const content = '126de09502ae4e0fb26c6967190756a4-b6e54397b12a2a0f-1'; |
349 | | - document.head.innerHTML = `<meta name="${name}" content="${content}">`; |
| 345 | + describe('sentry-trace <meta> element', () => { |
| 346 | + describe('getMetaContent', () => { |
| 347 | + it('finds the specified tag and extracts the value', () => { |
| 348 | + const name = 'sentry-trace'; |
| 349 | + const content = '126de09502ae4e0fb26c6967190756a4-b6e54397b12a2a0f-1'; |
| 350 | + document.head.innerHTML = `<meta name="${name}" content="${content}">`; |
350 | 351 |
|
351 | | - const meta = getMetaContent(name); |
352 | | - expect(meta).toBe(content); |
353 | | - }); |
| 352 | + const meta = getMetaContent(name); |
| 353 | + expect(meta).toBe(content); |
| 354 | + }); |
| 355 | + |
| 356 | + it("doesn't return meta tags other than the one specified", () => { |
| 357 | + document.head.innerHTML = `<meta name="not-test">`; |
| 358 | + |
| 359 | + const meta = getMetaContent('test'); |
| 360 | + expect(meta).toBe(null); |
| 361 | + }); |
| 362 | + }); |
| 363 | + |
| 364 | + describe('getHeaderContext', () => { |
| 365 | + it('correctly parses a valid sentry-trace meta header', () => { |
| 366 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 367 | + |
| 368 | + const headerContext = getHeaderContext(); |
| 369 | + |
| 370 | + expect(headerContext).toBeDefined(); |
| 371 | + expect(headerContext!.traceId).toEqual('12312012123120121231201212312012'); |
| 372 | + expect(headerContext!.parentSpanId).toEqual('1121201211212012'); |
| 373 | + expect(headerContext!.parentSampled).toEqual(false); |
| 374 | + }); |
| 375 | + |
| 376 | + it('returns undefined if the header is malformed', () => { |
| 377 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012-112120121-0">`; |
| 378 | + |
| 379 | + const headerContext = getHeaderContext(); |
| 380 | + |
| 381 | + expect(headerContext).toBeUndefined(); |
| 382 | + }); |
| 383 | + |
| 384 | + it("returns undefined if the header isn't there", () => { |
| 385 | + document.head.innerHTML = `<meta name="dogs" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 386 | + |
| 387 | + const headerContext = getHeaderContext(); |
| 388 | + |
| 389 | + expect(headerContext).toBeUndefined(); |
| 390 | + }); |
| 391 | + }); |
| 392 | + |
| 393 | + describe('using the data', () => { |
| 394 | + it('uses the data for pageload transactions', () => { |
| 395 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 396 | + |
| 397 | + // pageload transactions are created as part of the BrowserTracing integration's initialization |
| 398 | + createBrowserTracing(true); |
| 399 | + const transaction = getActiveTransaction(hub) as IdleTransaction; |
354 | 400 |
|
355 | | - it('only returns meta tags queried for', () => { |
356 | | - document.head.innerHTML = `<meta name="not-test">`; |
| 401 | + expect(transaction).toBeDefined(); |
| 402 | + expect(transaction.op).toBe('pageload'); |
| 403 | + expect(transaction.traceId).toEqual('12312012123120121231201212312012'); |
| 404 | + expect(transaction.parentSpanId).toEqual('1121201211212012'); |
| 405 | + expect(transaction.sampled).toBe(false); |
| 406 | + }); |
357 | 407 |
|
358 | | - const meta = getMetaContent('test'); |
359 | | - expect(meta).toBe(null); |
| 408 | + it('ignores the data for navigation transactions', () => { |
| 409 | + mockChangeHistory = () => undefined; |
| 410 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 411 | + |
| 412 | + createBrowserTracing(true); |
| 413 | + |
| 414 | + mockChangeHistory({ to: 'here', from: 'there' }); |
| 415 | + const transaction = getActiveTransaction(hub) as IdleTransaction; |
| 416 | + |
| 417 | + expect(transaction).toBeDefined(); |
| 418 | + expect(transaction.op).toBe('navigation'); |
| 419 | + expect(transaction.traceId).not.toEqual('12312012123120121231201212312012'); |
| 420 | + expect(transaction.parentSpanId).toBeUndefined(); |
| 421 | + }); |
| 422 | + }); |
360 | 423 | }); |
361 | 424 | }); |
0 commit comments