123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- const URLToolkit = require('../src/url-toolkit');
- describe('url toolkit', () => {
- describe('works with a selection of valid urls', () => {
- // From spec: https://tools.ietf.org/html/rfc1808#section-5.1
- test('http://a/b/c/d;p?q#f', 'g:h', 'g:h');
- test('http://a/b/c/d;p?q#f', 'g', 'http://a/b/c/g');
- test('http://a/b/c/d;p?q#f', './g', 'http://a/b/c/g');
- test('http://a/b/c/d;p?q#f', 'g/', 'http://a/b/c/g/');
- test('http://a/b/c/d;p?q#f', '/g', 'http://a/g');
- test('http://a/b/c/d;p?q#f', '//g', 'http://g');
- test('http://a/b/c/d;p?q#f', '?y', 'http://a/b/c/d;p?y');
- test('http://a/b/c/d;p?q#f', 'g?y', 'http://a/b/c/g?y');
- test('http://a/b/c/d;p?q#f', 'g?y/./x', 'http://a/b/c/g?y/./x');
- test('http://a/b/c/d;p?q#f', '#s', 'http://a/b/c/d;p?q#s');
- test('http://a/b/c/d;p?q#f', 'g#s', 'http://a/b/c/g#s');
- test('http://a/b/c/d;p?q#f', 'g#s/./x', 'http://a/b/c/g#s/./x');
- test('http://a/b/c/d;p?q#f', 'g?y#s', 'http://a/b/c/g?y#s');
- test('http://a/b/c/d;p?q#f', ';x', 'http://a/b/c/d;x');
- test('http://a/b/c/d;p?q#f', 'g;x', 'http://a/b/c/g;x');
- test('http://a/b/c/d;p?q#f', 'g;x?y#s', 'http://a/b/c/g;x?y#s');
- test('http://a/b/c/d;p?q#f', '.', 'http://a/b/c/');
- test('http://a/b/c/d;p?q#f', './', 'http://a/b/c/');
- test('http://a/b/c/d;p?q#f', '..', 'http://a/b/');
- test('http://a/b/c/d;p?q#f', '../', 'http://a/b/');
- test('http://a/b/c/d;p?q#f', '../g', 'http://a/b/g');
- test('http://a/b/c/d;p?q#f', '../..', 'http://a/');
- test('http://a/b/c/d;p?q#f', '../../', 'http://a/');
- test('http://a/b/c/d;p?q#f', '../../g', 'http://a/g');
- test('http://a/b/c/d;p?q#f', '', 'http://a/b/c/d;p?q#f');
- test('http://a/b/c/d;p?q#f', '../../../g', 'http://a/../g');
- test('http://a/b/c/d;p?q#f', '../../../../g', 'http://a/../../g');
- test('http://a/b/c/d;p?q#f', '/./g', 'http://a/./g');
- test('http://a/b/c/d;p?q#f', '/../g', 'http://a/../g');
- test('http://a/b/c/d;p?q#f', 'g.', 'http://a/b/c/g.');
- test('http://a/b/c/d;p?q#f', '.g', 'http://a/b/c/.g');
- test('http://a/b/c/d;p?q#f', 'g..', 'http://a/b/c/g..');
- test('http://a/b/c/d;p?q#f', '..g', 'http://a/b/c/..g');
- test('http://a/b/c/d;p?q#f', './../g', 'http://a/b/g');
- test('http://a/b/c/d;p?q#f', './g/.', 'http://a/b/c/g/');
- test('http://a/b/c/d;p?q#f', 'g/./h', 'http://a/b/c/g/h');
- test('http://a/b/c/d;p?q#f', 'g/../h', 'http://a/b/c/h');
- test('http://a/b/c/d;p?q#f', 'http:g', 'http:g');
- test('http://a/b/c/d;p?q#f', 'http:', 'http:');
- // Custom
- test(
- 'http://a.com/b/cd/./e.m3u8?test=1#something',
- '',
- 'http://a.com/b/cd/./e.m3u8?test=1#something'
- );
- test(
- 'http://a.com/b/cd/./e.m3u8?test=1#something',
- '',
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- { alwaysNormalize: true }
- );
- test(
- 'http://a.com/b/cd/e.m3u8',
- 'https://example.com/z.ts',
- 'https://example.com/z.ts'
- );
- test('http://a.com/b/cd/e.m3u8', 'g:h', 'g:h');
- test(
- 'http://a.com/b/cd/e.m3u8',
- 'https://example.com:8080/z.ts',
- 'https://example.com:8080/z.ts'
- );
- test('http://a.com/b/cd/e.m3u8', 'z.ts', 'http://a.com/b/cd/z.ts');
- test(
- 'http://a.com:8080/b/cd/e.m3u8',
- 'z.ts',
- 'http://a.com:8080/b/cd/z.ts'
- );
- test('http://a.com/b/cd/', 'z.ts', 'http://a.com/b/cd/z.ts');
- test('http://a.com/b/cd', 'z.ts', 'http://a.com/b/z.ts');
- test('http://a.com/', 'z.ts', 'http://a.com/z.ts');
- test('http://a.com/?test=1', 'z.ts', 'http://a.com/z.ts');
- test('http://a.com', 'z.ts', 'http://a.com/z.ts');
- test('http://a.com?test=1', 'z.ts', 'http://a.com/z.ts');
- test('http://a.com/b/cd?test=1', 'z.ts', 'http://a.com/b/z.ts');
- test('http://a.com/b/cd#something', 'z.ts', 'http://a.com/b/z.ts');
- test('http://a.com/b/cd?test=1#something', 'z.ts', 'http://a.com/b/z.ts');
- test(
- 'http://a.com/b/cd?test=1#something',
- 'z.ts?abc=1',
- 'http://a.com/b/z.ts?abc=1'
- );
- test(
- 'http://a.com/b/cd?test=1#something',
- 'z.ts#test',
- 'http://a.com/b/z.ts#test'
- );
- test(
- 'http://a.com/b/cd?test=1#something',
- 'z.ts?abc=1#test',
- 'http://a.com/b/z.ts?abc=1#test'
- );
- test('http://a.com/b/cd?test=1#something', ';x', 'http://a.com/b/cd;x');
- test('http://a.com/b/cd?test=1#something', './;x', 'http://a.com/b/;x');
- test('http://a.com/b/cd?test=1#something', 'g;x', 'http://a.com/b/g;x');
- test('http://a_b.com/b/cd?test=1#something', 'g;x', 'http://a_b.com/b/g;x');
- test('http://a-b.com/b/cd?test=1#something', 'g;x', 'http://a-b.com/b/g;x');
- test('http://a.b.com/b/cd?test=1#something', 'g;x', 'http://a.b.com/b/g;x');
- test('http://a~b.com/b/cd?test=1#something', 'g;x', 'http://a~b.com/b/g;x');
- test('a.com', 'z.ts', 'a.com/z.ts');
- test('a.com/', 'z.ts', 'a.com/z.ts');
- test('a.com/b/cd', 'z.ts', 'a.com/b/z.ts');
- test('a.com/b/cd', '../z.ts', 'a.com/z.ts');
- test('a.com/b/cd', '/z.ts', 'a.com/z.ts');
- test('a.com/b/cd', '/b/z.ts', 'a.com/b/z.ts');
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- 'subdir/z.ts?abc=1#test',
- 'http://a.com/b/cd/subdir/z.ts?abc=1#test'
- );
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- '/subdir/z.ts?abc=1#test',
- 'http://a.com/subdir/z.ts?abc=1#test'
- );
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- '//example.com/z.ts?abc=1#test',
- 'http://example.com/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '//example.com/z.ts?abc=1#test',
- 'https://example.com/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- './z.ts?abc=1#test',
- 'https://a.com/b/cd/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '../z.ts?abc=1#test',
- 'https://a.com/b/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- './../z.ts?abc=1#test',
- 'https://a.com/b/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '././z.ts?abc=1#test',
- 'https://a.com/b/cd/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e/f.m3u8?test=1#something',
- '../../z.ts?abc=1#test',
- 'https://a.com/b/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '../../z.ts?abc=1#test',
- 'https://a.com/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '../../z.ts?abc=1&something=blah/./../test#test',
- 'https://a.com/z.ts?abc=1&something=blah/./../test#test'
- );
- test(
- 'https://a.com/b/cd/e/f.m3u8?test=1#something',
- './../../z.ts?abc=1#test',
- 'https://a.com/b/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- 'subdir/pointless/../z.ts?abc=1#test',
- 'https://a.com/b/cd/subdir/z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '/subdir/pointless/../z.ts?abc=1#test',
- 'https://a.com/subdir/z.ts?abc=1#test',
- { alwaysNormalize: true }
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '/subdir/pointless/../z.ts?abc=1#test',
- 'https://a.com/subdir/pointless/../z.ts?abc=1#test'
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '//example.com/subdir/pointless/../z.ts?abc=1#test',
- 'https://example.com/subdir/z.ts?abc=1#test',
- { alwaysNormalize: true }
- );
- test(
- 'https://a.com/b/cd/e.m3u8?test=1#something',
- '//example.com/subdir/pointless/../z.ts?abc=1#test',
- 'https://example.com/subdir/pointless/../z.ts?abc=1#test'
- );
- test(
- 'https://a-b.something.com/b/cd/e.m3u8?test=1#something',
- '//example.com/subdir/pointless/../z.ts?abc=1#test',
- 'https://example.com/subdir/z.ts?abc=1#test',
- { alwaysNormalize: true }
- );
- test(
- 'https://a-b.something.com/b/cd/e.m3u8?test=1#something',
- '//example.com/subdir/pointless/../z.ts?abc=1#test',
- 'https://example.com/subdir/pointless/../z.ts?abc=1#test'
- );
- test(
- '//a.com/b/cd/e.m3u8',
- 'https://example.com/z.ts',
- 'https://example.com/z.ts'
- );
- test('//a.com/b/cd/e.m3u8', '//example.com/z.ts', '//example.com/z.ts');
- test(
- '//a.com/b/cd/e.m3u8',
- '/example.com/z.ts',
- '//a.com/example.com/z.ts'
- );
- test('//a.com/b/cd/e.m3u8', 'g:h', 'g:h');
- test(
- '//a.com/b/cd/e.m3u8',
- 'https://example.com:8080/z.ts',
- 'https://example.com:8080/z.ts'
- );
- test('//a.com/b/cd/e.m3u8', 'z.ts', '//a.com/b/cd/z.ts');
- test('//a.com/b/cd/e.m3u8', '../../z.ts', '//a.com/z.ts');
- test('//a.com/b/cd/e.m3u8', '../../../z.ts', '//a.com/../z.ts');
- test(
- '/a/b/cd/e.m3u8',
- 'https://example.com/z.ts',
- 'https://example.com/z.ts'
- );
- test('/a/b/cd/e.m3u8', '/example.com/z.ts', '/example.com/z.ts');
- test('/a/b/cd/e.m3u8', '//example.com/z.ts', '//example.com/z.ts');
- test('/a/b/cd/e.m3u8', 'g:h', 'g:h');
- test(
- '/a/b/cd/e.m3u8',
- 'https://example.com:8080/z.ts',
- 'https://example.com:8080/z.ts'
- );
- test('/a/b/cd/e.m3u8', 'z.ts', '/a/b/cd/z.ts');
- test('/a/b/cd/e.m3u8', '../../../z.ts', '/z.ts');
- test('http://ö.de/a/b', 'z.ts', 'http://ö.de/a/z.ts');
- test('http://ö.de/a', 'z.ts', 'http://ö.de/z.ts');
- test('http://ö.de/', 'z.ts', 'http://ö.de/z.ts');
- test('http://ö.de', 'z.ts', 'http://ö.de/z.ts');
- test('ö.de', 'z.ts', 'ö.de/z.ts');
- test('http://a/b/c/d;p?q', './', 'http://a/b/c/');
- test('http://a/b/c/d;p?q', '.', 'http://a/b/c/');
- test('http://a/b/c/d;p?q', '../', 'http://a/b/');
- test('http://a/b/c/d;p?q', '..', 'http://a/b/');
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- '',
- 'http://a.com/b/cd/e.m3u8?test=1#something'
- );
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- 'a_:b',
- 'http://a.com/b/cd/a_:b'
- );
- test('http://a.com/b/cd/e.m3u8?test=1#something', 'a:b', 'a:b');
- test(
- 'http://a.com/b/cd/e.m3u8?test=1#something',
- './a:b',
- 'http://a.com/b/cd/a:b'
- );
- test(
- 'http://a.com/expiretime=111;dirmatch=true/master.m3u8',
- './a:b',
- 'http://a.com/expiretime=111;dirmatch=true/a:b'
- );
- test('http://0.0.0.0/a/b.c', 'd', 'http://0.0.0.0/a/d');
- test('http://[0:0:0:0::0]/a/b.c', 'd', 'http://[0:0:0:0::0]/a/d');
- test('http://example.com/', 'a#\nb', 'http://example.com/a#\nb');
- // in the URL living standard (https://url.spec.whatwg.org/)
- // `http` is a 'special scheme', and that results in
- // the `///` becoming `//`, meaning `netLoc` would essentially be
- // `//example.com` instead of `//`
- // This library is specifically RFC 1808, which does not have these
- // special cases.
- test('http:///example.com/a/', '../../b', 'http:///b');
- });
- });
- function test(base, relative, expected, opts) {
- opts = opts || {};
- it(`"${base}" + "${relative}" ${JSON.stringify(opts)}`, () => {
- expect(URLToolkit.parseURL(base)).toMatchSnapshot();
- expect(URLToolkit.parseURL(relative)).toMatchSnapshot();
- expect(URLToolkit.buildURLFromParts(URLToolkit.parseURL(relative))).toBe(
- relative
- );
- expect(URLToolkit.buildURLFromParts(URLToolkit.parseURL(base))).toBe(base);
- expect(URLToolkit.buildAbsoluteURL(base, relative, opts)).toBe(expected);
- });
- }
|