UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

172 lines (171 loc) 9.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var test_1 = require("../test"); var Url_1 = require("./Url"); var origin = 'https://domain.com/'; describe('Url', function () { describe('static', function () { it('parse: origin', function () { var test = function (input, host, port, protocol, origin) { var res = Url_1.Url.parse(input); var hostname = host.replace(/:\d*$/, ''); (0, test_1.expect)(res.origin.protocol).to.eql(protocol); (0, test_1.expect)(res.origin.hostname).to.eql(hostname); (0, test_1.expect)(res.origin.host).to.eql(host); (0, test_1.expect)(res.origin.port).to.eql(port); (0, test_1.expect)(res.origin.toString()).to.eql(origin); }; test('foo.com:1234', 'foo.com:1234', 1234, 'https', 'https://foo.com:1234'); test('foo.com', 'foo.com', 80, 'https', 'https://foo.com'); test('foo.com///', 'foo.com', 80, 'https', 'https://foo.com'); test('http://foo.com', 'foo.com', 80, 'https', 'https://foo.com'); test('https://foo.com/', 'foo.com', 80, 'https', 'https://foo.com'); test('foo.com:8080', 'foo.com:8080', 8080, 'https', 'https://foo.com:8080'); test('localhost.foo.com', 'localhost.foo.com', 80, 'https', 'https://localhost.foo.com'); test(undefined, 'localhost', 80, 'http', 'http://localhost'); test('', 'localhost', 80, 'http', 'http://localhost'); test(' ', 'localhost', 80, 'http', 'http://localhost'); test('1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test(1234, 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test(80, 'localhost', 80, 'http', 'http://localhost'); test('80', 'localhost', 80, 'http', 'http://localhost'); test('localhost', 'localhost', 80, 'http', 'http://localhost'); test('localhost:1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test('localhost/', 'localhost', 80, 'http', 'http://localhost'); test('localhost/foo', 'localhost', 80, 'http', 'http://localhost'); test('/foo/', 'localhost', 80, 'http', 'http://localhost'); test('//foo/', 'localhost', 80, 'http', 'http://localhost'); test('http://localhost', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost//', 'localhost', 80, 'http', 'http://localhost'); test('https://localhost:1234', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test('https://localhost:1234//', 'localhost:1234', 1234, 'http', 'http://localhost:1234'); test('http://localhost/foo', 'localhost', 80, 'http', 'http://localhost'); }); it('parse: path', function () { var test = function (input, path) { var res = Url_1.Url.parse(input); (0, test_1.expect)(res.path).to.eql(path); }; test(80, '/'); test(1234, '/'); test('', '/'); test('/', '/'); test(' /// ', '/'); test('localhost', '/'); test('http://localhost', '/'); test(' http://localhost/ ', '/'); test(' http://localhost:8080/ ', '/'); test(' http://localhost:8080 ', '/'); test('http://192.168.1:1234//', '/'); test('https://domain.com//', '/'); test(' domain.com/foo/bar ', '/foo/bar'); test(' domain.com/foo/bar// ', '/foo/bar//'); test('https://domain.com/foo?q=123', '/foo?q=123'); test(' http://localhost:8080/foo/bar? ', '/foo/bar?'); test('foo/bar', '/bar'); test('/foo/bar', '/foo/bar'); test('///foo/bar', '/foo/bar'); test('///foo/bar/ ', '/foo/bar/'); }); it('isLocal', function () { var test = function (input, expected) { (0, test_1.expect)(Url_1.Url.isLocal(input)).to.eql(expected); }; test(undefined, false); test('localhostess', false); test('localhost', true); test('localhost:8080', true); test('localhost:80', true); test(' localhost ', true); test('http://localhost', true); test('http://localhost/', true); test('https://localhost', true); test('//localhost///', true); test('localhost/foo/bar?q=123', true); test('http://192.168.1', true); test('192.168.1', true); }); }); describe('instance', function () { it('origin/path', function () { var res = new Url_1.Url({ origin: 'https://domain.com:8080//', path: '///foo/bar' }); (0, test_1.expect)(res.origin.toString()).to.eql('https://domain.com:8080'); (0, test_1.expect)(res.origin.host).to.eql('domain.com:8080'); (0, test_1.expect)(res.origin.hostname).to.eql('domain.com'); (0, test_1.expect)(res.origin.port).to.eql(8080); (0, test_1.expect)(res.path).to.eql('/foo/bar'); }); it('no path', function () { var test = function (path) { var res = new Url_1.Url({ origin: 'https://domain.com', path: path }); (0, test_1.expect)(res.path).to.eql('/'); (0, test_1.expect)(res.toString()).to.eql('https://domain.com/'); }; test(undefined); test(''); test(' '); test('/'); test('///'); test(' /// '); }); it('querystring (from constructor string)', function () { var test = function (input, expected) { var res = new Url_1.Url({ origin: origin, querystring: input }); (0, test_1.expect)(res.querystring).to.eql(expected); }; test(undefined, ''); test('', ''); test(' ', ''); test('foo', '?foo'); test('foo=bar', '?foo=bar'); test(' foo=bar&force ', '?foo=bar&force'); }); it('querystring (from constructor object)', function () { var test = function (query, expected) { var res = new Url_1.Url({ origin: origin, query: query }); (0, test_1.expect)(res.querystring).to.eql(expected); }; test(undefined, ''); test({ color: 'red' }, '?color=red'); test({ force: true }, '?force=true'); test({ force: true, color: ' blue' }, '?force=true&color=blue'); test({ color: 'blue', force: true }, '?color=blue&force=true'); test({ force: true, toString: function () { return 'hello'; } }, '?force=true'); test({ text: '' }, ''); test({ text: '', force: true }, '?force=true'); test({ thing: [] }, ''); test({ thing: ['one', true, false] }, '?thing=one&thing=true&thing=false'); test({ thing: ['same', ' same ', 'diff', 'same'] }, '?thing=same&thing=diff'); }); it('add [query] returns a new instance', function () { var url1 = new Url_1.Url({ origin: origin }); var url2 = url1.query({ force: true }); (0, test_1.expect)(url1).to.not.equal(url2); (0, test_1.expect)(url1.querystring).to.eql(''); (0, test_1.expect)(url2.querystring).to.eql('?force=true'); }); it('add [query] array (multiple keys)', function () { var url = new Url_1.Url({ origin: origin }).query({ thing: [true, ' foo ', 123] }); (0, test_1.expect)(url.querystring).to.eql('?thing=true&thing=foo&thing=123'); }); it('does not add [query] if value is undefined', function () { var url = new Url_1.Url({ origin: origin }).query({ thing: undefined }); (0, test_1.expect)(url.querystring).to.eql(''); }); it('build query-string (mixed from object and/or string)', function () { var res = new Url_1.Url({ origin: origin, querystring: 'boom=bam' }) .query({ color: 'red' }) .query({ force: true, color: ' blue ' }); (0, test_1.expect)(res.querystring).to.eql('?color=blue&force=true&boom=bam'); }); it('toPath | toString', function () { var url = new Url_1.Url({ origin: origin, path: '//foo/' }); (0, test_1.expect)(url.toString({ origin: false })).to.eql('/foo/'); (0, test_1.expect)(url.toString()).to.eql('https://domain.com/foo/'); url = url.query({ force: true }); (0, test_1.expect)(url.toString({ origin: false })).to.eql('/foo/?force=true'); (0, test_1.expect)(url.toString()).to.eql('https://domain.com/foo/?force=true'); }); }); });