2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-env qunit */
|
2015-05-04 01:12:38 +02:00
|
|
|
import document from 'global/document';
|
|
|
|
import window from 'global/window';
|
|
|
|
import * as Url from '../../../src/js/utils/url.js';
|
|
|
|
|
2022-05-23 22:23:13 +02:00
|
|
|
QUnit.module('utils/url');
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should parse the details of a url correctly', function(assert) {
|
2021-07-22 19:14:26 +02:00
|
|
|
assert.equal(Url.parseUrl('#').protocol, window.location.protocol, 'parsed relative url protocol');
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(Url.parseUrl('#').host, window.location.host, 'parsed relative url host');
|
2021-07-22 19:14:26 +02:00
|
|
|
assert.equal(Url.parseUrl('#foo').hash, '#foo', 'parsed relative url hash');
|
2015-08-03 21:19:36 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(Url.parseUrl('http://example.com').protocol, 'http:', 'parsed example url protocol');
|
|
|
|
assert.equal(Url.parseUrl('http://example.com').hostname, 'example.com', 'parsed example url hostname');
|
2015-05-04 01:12:38 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(Url.parseUrl('http://example.com:1234').port, '1234', 'parsed example url port');
|
2015-05-04 01:12:38 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should strip port from hosts using http or https', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const origDocCreate = document.createElement;
|
2015-05-04 01:12:38 +02:00
|
|
|
|
|
|
|
// attempts to create elements will return an anchor tag that
|
|
|
|
// misbehaves like IE9
|
|
|
|
document.createElement = function() {
|
|
|
|
return {
|
|
|
|
hostname: 'example.com',
|
|
|
|
host: 'example.com:80',
|
|
|
|
protocol: 'http:',
|
|
|
|
port: '80',
|
|
|
|
pathname: '/domain/relative/url',
|
|
|
|
hash: ''
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
const url = Url.parseUrl('/domain/relative/url');
|
2015-05-04 01:12:38 +02:00
|
|
|
|
|
|
|
document.createElement = origDocCreate;
|
2015-08-03 21:19:36 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!(/.*:80$/).test(url.host), ':80 is not appended to the host');
|
2015-08-03 21:19:36 +02:00
|
|
|
|
2015-05-04 01:12:38 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should get an absolute URL', function(assert) {
|
2015-05-04 01:12:38 +02:00
|
|
|
// Errors on compiled tests that don't use unit.html. Need a better solution.
|
2016-08-12 19:51:31 +02:00
|
|
|
// assert.ok(Url.getAbsoluteURL('unit.html') === window.location.href);
|
|
|
|
assert.ok(Url.getAbsoluteURL('http://asdf.com') === 'http://asdf.com');
|
|
|
|
assert.ok(Url.getAbsoluteURL('https://asdf.com/index.html') === 'https://asdf.com/index.html');
|
2015-05-04 01:12:38 +02:00
|
|
|
});
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
// getFileExtension tests
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should get the file extension of the passed path', function(assert) {
|
|
|
|
assert.equal(Url.getFileExtension('/foo/bar/test.video.wgg'), 'wgg');
|
|
|
|
assert.equal(Url.getFileExtension('test./video.mp4'), 'mp4');
|
|
|
|
assert.equal(Url.getFileExtension('.bar/test.video.m4v'), 'm4v');
|
|
|
|
assert.equal(Url.getFileExtension('foo/.bar/test.video.flv'), 'flv');
|
|
|
|
assert.equal(Url.getFileExtension('foo/.bar/test.video.flv?foo=bar'), 'flv');
|
|
|
|
assert.equal(Url.getFileExtension('http://www.test.com/video.mp4'), 'mp4');
|
|
|
|
assert.equal(Url.getFileExtension('http://foo/bar/test.video.wgg'), 'wgg');
|
2016-08-03 21:27:03 +02:00
|
|
|
|
|
|
|
// edge cases
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(Url.getFileExtension('http://...'), '');
|
|
|
|
assert.equal(Url.getFileExtension('foo/.bar/testvideo'), '');
|
|
|
|
assert.equal(Url.getFileExtension(''), '');
|
|
|
|
assert.equal(Url.getFileExtension(null), '');
|
|
|
|
assert.equal(Url.getFileExtension(undefined), '');
|
2016-08-03 21:27:03 +02:00
|
|
|
|
|
|
|
// with capital letters
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(Url.getFileExtension('test.video.MP4'), 'mp4');
|
|
|
|
assert.equal(Url.getFileExtension('test.video.FLV'), 'flv');
|
2015-05-04 01:12:38 +02:00
|
|
|
});
|
2015-09-25 01:58:25 +02:00
|
|
|
|
|
|
|
// isCrossOrigin tests
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('isCrossOrigin can identify cross origin urls', function(assert) {
|
2019-08-30 20:56:41 +02:00
|
|
|
|
|
|
|
assert.ok(!Url.isCrossOrigin(`http://${window.location.host}/example.vtt`), 'http://google.com from http://google.com is not cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin(`https://${window.location.host}/example.vtt`), 'https://google.com from http://google.com is cross origin');
|
|
|
|
assert.ok(!Url.isCrossOrigin(`//${window.location.host}/example.vtt`), '//google.com from http://google.com is not cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('http://example.com/example.vtt'), 'http://example.com from http://google.com is cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('https://example.com/example.vtt'), 'https://example.com from http://google.com is cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('//example.com/example.vtt'), '//example.com from http://google.com is cross origin');
|
2015-09-25 01:58:25 +02:00
|
|
|
// we cannot test that relative urls work on https, though
|
2019-08-30 20:56:41 +02:00
|
|
|
assert.ok(!Url.isCrossOrigin('example.vtt'), 'relative url is not cross origin');
|
|
|
|
|
|
|
|
const location = {
|
|
|
|
protocol: 'https:',
|
|
|
|
host: 'google.com'
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.ok(!Url.isCrossOrigin('https://google.com/example.vtt', location), 'http://google.com from https://google.com is not cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('http://google.com/example.vtt', location), 'http://google.com from https://google.com is cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('http://example.com/example.vtt', location), 'http://example.com from https://google.com is cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('https://example.com/example.vtt', location), 'https://example.com from https://google.com is cross origin');
|
|
|
|
assert.ok(Url.isCrossOrigin('//example.com/example.vtt', location), '//example.com from https://google.com is cross origin');
|
2015-09-25 01:58:25 +02:00
|
|
|
});
|