getCurrentUrl.spec.tsx 3.3 KB

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