getCurrentUrl.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {Event} from 'fixtures/js-stubs/event';
  2. import type {Crumb} from 'sentry/types/breadcrumbs';
  3. import {BreadcrumbLevelType, BreadcrumbType} from 'sentry/types/breadcrumbs';
  4. import getCurrentUrl from 'sentry/utils/replays/getCurrentUrl';
  5. import type {ReplayRecord} from 'sentry/views/replays/types';
  6. const START_DATE = new Date('2022-06-15T00:40:00.111Z');
  7. const PAGELOAD_DATE = new Date('2022-06-15T00:45:00.222Z');
  8. const NAVIGATION_DATE = new Date('2022-06-15T00:46:00.333Z');
  9. const NEW_DOMAIN_DATE = new Date('2022-06-15T00:47:00.444Z');
  10. const END_DATE = new Date('2022-06-15T00:50:00.555Z');
  11. const PAGELOAD_CRUMB: Crumb = {
  12. category: 'default',
  13. type: BreadcrumbType.NAVIGATION,
  14. timestamp: PAGELOAD_DATE.toISOString(),
  15. level: BreadcrumbLevelType.INFO,
  16. message: 'https://sourcemaps.io/',
  17. data: {
  18. to: 'https://sourcemaps.io/',
  19. },
  20. id: 6,
  21. color: 'green300',
  22. description: 'Navigation',
  23. };
  24. const NAV_CRUMB: Crumb = {
  25. type: BreadcrumbType.NAVIGATION,
  26. category: 'navigation',
  27. data: {
  28. from: '/',
  29. to: '/report/1655300817078_https%3A%2F%2Fmaxcdn.bootstrapcdn.com%2Fbootstrap%2F3.3.7%2Fjs%2Fbootstrap.min.js',
  30. },
  31. timestamp: NAVIGATION_DATE.toISOString(),
  32. id: 5,
  33. color: 'green300',
  34. description: 'Navigation',
  35. level: BreadcrumbLevelType.UNDEFINED,
  36. };
  37. const NEW_DOMAIN_CRUMB: Crumb = {
  38. category: 'default',
  39. type: BreadcrumbType.NAVIGATION,
  40. timestamp: NEW_DOMAIN_DATE.toISOString(),
  41. level: BreadcrumbLevelType.INFO,
  42. message: 'https://a062-174-94-6-155.ngrok.io/report/jquery.min.js',
  43. data: {
  44. to: 'https://a062-174-94-6-155.ngrok.io/report/jquery.min.js',
  45. },
  46. id: 29,
  47. color: 'green300',
  48. description: 'Navigation',
  49. };
  50. describe('getCurrentUrl', () => {
  51. let replayRecord;
  52. beforeEach(() => {
  53. replayRecord = Event({
  54. tags: {},
  55. urls: [
  56. 'https://sourcemaps.io/#initial',
  57. 'https://sourcemaps.io/report/1655300817078_https%3A%2F%2Fmaxcdn.bootstrapcdn.com%2Fbootstrap%2F3.3.7%2Fjs%2Fbootstrap.min.js',
  58. 'https://a062-174-94-6-155.ngrok.io/report/jquery.min.js',
  59. ],
  60. startedAt: START_DATE,
  61. finishedAt: END_DATE,
  62. }) as ReplayRecord;
  63. });
  64. it('should return the origin of the first url from the url array if the offset is early', () => {
  65. const crumbs = [PAGELOAD_CRUMB, NAV_CRUMB];
  66. const offsetMS = 0;
  67. const url = getCurrentUrl(replayRecord, crumbs, offsetMS);
  68. expect(url).toBe('https://sourcemaps.io');
  69. });
  70. it('should return the first navigation url when the offset is after that', () => {
  71. const crumbs = [PAGELOAD_CRUMB, NAV_CRUMB];
  72. const offsetMS = Number(NAVIGATION_DATE) - Number(START_DATE) + 10;
  73. const url = getCurrentUrl(replayRecord, crumbs, offsetMS);
  74. expect(url).toBe(
  75. 'https://sourcemaps.io/report/1655300817078_https%3A%2F%2Fmaxcdn.bootstrapcdn.com%2Fbootstrap%2F3.3.7%2Fjs%2Fbootstrap.min.js'
  76. );
  77. });
  78. it('should use the domain that is included in the crumb, if the crumb is a valid url', () => {
  79. const crumbs = [NEW_DOMAIN_CRUMB];
  80. const offsetMS = Number(NEW_DOMAIN_DATE) - Number(START_DATE) + 10;
  81. const url = getCurrentUrl(replayRecord, crumbs, offsetMS);
  82. expect(url).toBe('https://a062-174-94-6-155.ngrok.io/report/jquery.min.js');
  83. });
  84. it('should not explode when an invalid urls is found', () => {
  85. const base64EncodedScriptTag =
  86. 'nulltext/html;base64,PHNjcmlwdD4KICAgICAgb25tZXNzYWdlID0gKGV2ZW50KSA9PiB7CiAgICAgICAgY29uc29sZS5sb2coJ2hlbGxvIHdvcmxkJyk7CiAgICAgIH0KICA8L3NjcmlwdD4=';
  87. replayRecord.urls = [base64EncodedScriptTag];
  88. const crumbs = [];
  89. const offsetMS = 0;
  90. const url = getCurrentUrl(replayRecord, crumbs, offsetMS);
  91. expect(url).toBe(base64EncodedScriptTag);
  92. });
  93. });