resolve-url.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import QUnit from 'qunit';
  2. import window from 'global/window';
  3. import resolveUrl from '../src/resolve-url';
  4. // A modified subset of tests from https://github.com/tjenkinson/url-toolkit
  5. QUnit.module('URL resolver');
  6. QUnit.test('works with a selection of valid urls', function(assert) {
  7. let currentLocation = '';
  8. let currentPath = '';
  9. if (window.location && window.location.protocol) {
  10. currentLocation = window.location.protocol + '//' + window.location.host;
  11. currentPath = window.location.pathname.split('/').slice(0, -1).join('/');
  12. }
  13. assert.equal(
  14. resolveUrl('http://a.com/b/cd/e.m3u8', 'https://example.com/z.ts'),
  15. 'https://example.com/z.ts'
  16. );
  17. assert.equal(resolveUrl('http://a.com/b/cd/e.m3u8', 'z.ts'), 'http://a.com/b/cd/z.ts');
  18. assert.equal(resolveUrl('//a.com/b/cd/e.m3u8', 'z.ts'), '//a.com/b/cd/z.ts');
  19. assert.equal(
  20. resolveUrl('/a/b/cd/e.m3u8', 'https://example.com:8080/z.ts'),
  21. 'https://example.com:8080/z.ts'
  22. );
  23. assert.equal(resolveUrl('/a/b/cd/e.m3u8', 'z.ts'), currentLocation + '/a/b/cd/z.ts');
  24. assert.equal(resolveUrl('/a/b/cd/e.m3u8', '../../../z.ts'), currentLocation + '/z.ts');
  25. assert.equal(
  26. resolveUrl('data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com', 'hello.m3u8'),
  27. // we need to add the currentPath because we're actually working relative to window.location
  28. currentLocation + currentPath + '/hello.m3u8',
  29. 'resolves urls relative to window when given a data base url'
  30. );
  31. assert.equal(
  32. resolveUrl('data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com', 'http://example.com/hello.m3u8'),
  33. 'http://example.com/hello.m3u8',
  34. 'absolute urls should still be absolute even with a data uri'
  35. );
  36. assert.equal(
  37. resolveUrl('http://example.com', 'data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com'),
  38. 'data:application/dash+xml;charset=utf-8,http%3A%2F%2Fexample.com',
  39. 'data uri is treated as an absolute url'
  40. );
  41. });