michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { michael@0: URL, michael@0: toFilename, michael@0: fromFilename, michael@0: isValidURI, michael@0: getTLD, michael@0: DataURL, michael@0: isLocalURL } = require('sdk/url'); michael@0: michael@0: const { pathFor } = require('sdk/system'); michael@0: const file = require('sdk/io/file'); michael@0: const tabs = require('sdk/tabs'); michael@0: const { decode } = require('sdk/base64'); michael@0: michael@0: const httpd = require('sdk/test/httpd'); michael@0: const port = 8099; michael@0: michael@0: const defaultLocation = '{\'scheme\':\'about\',\'userPass\':null,\'host\':null,\'hostname\':null,\'port\':null,\'path\':\'addons\',\'pathname\':\'addons\',\'hash\':\'\',\'href\':\'about:addons\',\'origin\':\'about:\',\'protocol\':\'about:\',\'search\':\'\'}'.replace(/'/g, '"'); michael@0: michael@0: exports.testResolve = function(assert) { michael@0: assert.equal(URL('bar', 'http://www.foo.com/').toString(), michael@0: 'http://www.foo.com/bar'); michael@0: michael@0: assert.equal(URL('bar', 'http://www.foo.com'), michael@0: 'http://www.foo.com/bar'); michael@0: michael@0: assert.equal(URL('http://bar.com/', 'http://foo.com/'), michael@0: 'http://bar.com/', michael@0: 'relative should override base'); michael@0: michael@0: assert.throws(function() { URL('blah'); }, michael@0: /malformed URI: blah/i, michael@0: 'url.resolve() should throw malformed URI on base'); michael@0: michael@0: assert.throws(function() { URL('chrome://global'); }, michael@0: /invalid URI: chrome:\/\/global/i, michael@0: 'url.resolve() should throw invalid URI on base'); michael@0: michael@0: assert.throws(function() { URL('chrome://foo/bar'); }, michael@0: /invalid URI: chrome:\/\/foo\/bar/i, michael@0: 'url.resolve() should throw on bad chrome URI'); michael@0: michael@0: assert.equal(URL('', 'http://www.foo.com'), michael@0: 'http://www.foo.com/', michael@0: 'url.resolve() should add slash to end of domain'); michael@0: }; michael@0: michael@0: exports.testParseHttp = function(assert) { michael@0: var aUrl = 'http://sub.foo.com/bar?locale=en-US&otherArg=%20x%20#myhash'; michael@0: var info = URL(aUrl); michael@0: michael@0: assert.equal(info.scheme, 'http'); michael@0: assert.equal(info.protocol, 'http:'); michael@0: assert.equal(info.host, 'sub.foo.com'); michael@0: assert.equal(info.hostname, 'sub.foo.com'); michael@0: assert.equal(info.port, null); michael@0: assert.equal(info.userPass, null); michael@0: assert.equal(info.path, '/bar?locale=en-US&otherArg=%20x%20#myhash'); michael@0: assert.equal(info.pathname, '/bar'); michael@0: assert.equal(info.href, aUrl); michael@0: assert.equal(info.hash, '#myhash'); michael@0: assert.equal(info.search, '?locale=en-US&otherArg=%20x%20'); michael@0: }; michael@0: michael@0: exports.testParseHttpSearchAndHash = function (assert) { michael@0: var info = URL('https://www.moz.com/some/page.html'); michael@0: assert.equal(info.hash, ''); michael@0: assert.equal(info.search, ''); michael@0: michael@0: var hashOnly = URL('https://www.sub.moz.com/page.html#justhash'); michael@0: assert.equal(hashOnly.search, ''); michael@0: assert.equal(hashOnly.hash, '#justhash'); michael@0: michael@0: var queryOnly = URL('https://www.sub.moz.com/page.html?my=query'); michael@0: assert.equal(queryOnly.search, '?my=query'); michael@0: assert.equal(queryOnly.hash, ''); michael@0: michael@0: var qMark = URL('http://www.moz.org?'); michael@0: assert.equal(qMark.search, ''); michael@0: assert.equal(qMark.hash, ''); michael@0: michael@0: var hash = URL('http://www.moz.org#'); michael@0: assert.equal(hash.search, ''); michael@0: assert.equal(hash.hash, ''); michael@0: michael@0: var empty = URL('http://www.moz.org?#'); michael@0: assert.equal(hash.search, ''); michael@0: assert.equal(hash.hash, ''); michael@0: michael@0: var strange = URL('http://moz.org?test1#test2?test3'); michael@0: assert.equal(strange.search, '?test1'); michael@0: assert.equal(strange.hash, '#test2?test3'); michael@0: }; michael@0: michael@0: exports.testParseHttpWithPort = function(assert) { michael@0: var info = URL('http://foo.com:5/bar'); michael@0: assert.equal(info.port, 5); michael@0: }; michael@0: michael@0: exports.testParseChrome = function(assert) { michael@0: var info = URL('chrome://global/content/blah'); michael@0: assert.equal(info.scheme, 'chrome'); michael@0: assert.equal(info.host, 'global'); michael@0: assert.equal(info.port, null); michael@0: assert.equal(info.userPass, null); michael@0: assert.equal(info.path, '/content/blah'); michael@0: }; michael@0: michael@0: exports.testParseAbout = function(assert) { michael@0: var info = URL('about:boop'); michael@0: assert.equal(info.scheme, 'about'); michael@0: assert.equal(info.host, null); michael@0: assert.equal(info.port, null); michael@0: assert.equal(info.userPass, null); michael@0: assert.equal(info.path, 'boop'); michael@0: }; michael@0: michael@0: exports.testParseFTP = function(assert) { michael@0: var info = URL('ftp://1.2.3.4/foo'); michael@0: assert.equal(info.scheme, 'ftp'); michael@0: assert.equal(info.host, '1.2.3.4'); michael@0: assert.equal(info.port, null); michael@0: assert.equal(info.userPass, null); michael@0: assert.equal(info.path, '/foo'); michael@0: }; michael@0: michael@0: exports.testParseFTPWithUserPass = function(assert) { michael@0: var info = URL('ftp://user:pass@1.2.3.4/foo'); michael@0: assert.equal(info.userPass, 'user:pass'); michael@0: }; michael@0: michael@0: exports.testToFilename = function(assert) { michael@0: assert.throws( michael@0: function() { toFilename('resource://nonexistent'); }, michael@0: /resource does not exist: resource:\/\/nonexistent\//i, michael@0: 'toFilename() on nonexistent resources should throw' michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() { toFilename('http://foo.com/'); }, michael@0: /cannot map to filename: http:\/\/foo.com\//i, michael@0: 'toFilename() on http: URIs should raise error' michael@0: ); michael@0: michael@0: try { michael@0: assert.ok( michael@0: /.*console\.xul$/.test(toFilename('chrome://global/content/console.xul')), michael@0: 'toFilename() w/ console.xul works when it maps to filesystem' michael@0: ); michael@0: } michael@0: catch (e) { michael@0: if (/chrome url isn\'t on filesystem/.test(e.message)) michael@0: assert.pass('accessing console.xul in jar raises exception'); michael@0: else michael@0: assert.fail('accessing console.xul raises ' + e); michael@0: } michael@0: michael@0: // TODO: Are there any chrome URLs that we're certain exist on the michael@0: // filesystem? michael@0: // assert.ok(/.*main\.js$/.test(toFilename('chrome://myapp/content/main.js'))); michael@0: }; michael@0: michael@0: exports.testFromFilename = function(assert) { michael@0: var profileDirName = require('sdk/system').pathFor('ProfD'); michael@0: var fileUrl = fromFilename(profileDirName); michael@0: assert.equal(URL(fileUrl).scheme, 'file', michael@0: 'toFilename() should return a file: url'); michael@0: assert.equal(fromFilename(toFilename(fileUrl)), fileUrl); michael@0: }; michael@0: michael@0: exports.testURL = function(assert) { michael@0: assert.ok(URL('h:foo') instanceof URL, 'instance is of correct type'); michael@0: assert.throws(function() URL(), michael@0: /malformed URI: undefined/i, michael@0: 'url.URL should throw on undefined'); michael@0: assert.throws(function() URL(''), michael@0: /malformed URI: /i, michael@0: 'url.URL should throw on empty string'); michael@0: assert.throws(function() URL('foo'), michael@0: /malformed URI: foo/i, michael@0: 'url.URL should throw on invalid URI'); michael@0: assert.ok(URL('h:foo').scheme, 'has scheme'); michael@0: assert.equal(URL('h:foo').toString(), michael@0: 'h:foo', michael@0: 'toString should roundtrip'); michael@0: // test relative + base michael@0: assert.equal(URL('mypath', 'http://foo').toString(), michael@0: 'http://foo/mypath', michael@0: 'relative URL resolved to base'); michael@0: // test relative + no base michael@0: assert.throws(function() URL('path').toString(), michael@0: /malformed URI: path/i, michael@0: 'no base for relative URI should throw'); michael@0: michael@0: let a = URL('h:foo'); michael@0: let b = URL(a); michael@0: assert.equal(b.toString(), michael@0: 'h:foo', michael@0: 'a URL can be initialized from another URL'); michael@0: assert.notStrictEqual(a, b, michael@0: 'a URL initialized from another URL is not the same object'); michael@0: assert.ok(a == 'h:foo', michael@0: 'toString is implicit when a URL is compared to a string via =='); michael@0: assert.strictEqual(a + '', 'h:foo', michael@0: 'toString is implicit when a URL is concatenated to a string'); michael@0: }; michael@0: michael@0: exports.testStringInterface = function(assert) { michael@0: var EM = 'about:addons'; michael@0: var a = URL(EM); michael@0: michael@0: // make sure the standard URL properties are enumerable and not the String interface bits michael@0: assert.equal(Object.keys(a), michael@0: 'scheme,userPass,host,hostname,port,path,pathname,hash,href,origin,protocol,search', michael@0: 'enumerable key list check for URL.'); michael@0: assert.equal( michael@0: JSON.stringify(a), michael@0: defaultLocation, michael@0: 'JSON.stringify should return a object with correct props and vals.'); michael@0: michael@0: // make sure that the String interface exists and works as expected michael@0: assert.equal(a.indexOf(':'), EM.indexOf(':'), 'indexOf on URL works'); michael@0: assert.equal(a.valueOf(), EM.valueOf(), 'valueOf on URL works.'); michael@0: assert.equal(a.toSource(), EM.toSource(), 'toSource on URL works.'); michael@0: assert.equal(a.lastIndexOf('a'), EM.lastIndexOf('a'), 'lastIndexOf on URL works.'); michael@0: assert.equal(a.match('t:').toString(), EM.match('t:').toString(), 'match on URL works.'); michael@0: assert.equal(a.toUpperCase(), EM.toUpperCase(), 'toUpperCase on URL works.'); michael@0: assert.equal(a.toLowerCase(), EM.toLowerCase(), 'toLowerCase on URL works.'); michael@0: assert.equal(a.split(':').toString(), EM.split(':').toString(), 'split on URL works.'); michael@0: assert.equal(a.charAt(2), EM.charAt(2), 'charAt on URL works.'); michael@0: assert.equal(a.charCodeAt(2), EM.charCodeAt(2), 'charCodeAt on URL works.'); michael@0: assert.equal(a.concat(EM), EM.concat(a), 'concat on URL works.'); michael@0: assert.equal(a.substr(2,3), EM.substr(2,3), 'substr on URL works.'); michael@0: assert.equal(a.substring(2,3), EM.substring(2,3), 'substring on URL works.'); michael@0: assert.equal(a.trim(), EM.trim(), 'trim on URL works.'); michael@0: assert.equal(a.trimRight(), EM.trimRight(), 'trimRight on URL works.'); michael@0: assert.equal(a.trimLeft(), EM.trimLeft(), 'trimLeft on URL works.'); michael@0: } michael@0: michael@0: exports.testDataURLwithouthURI = function (assert) { michael@0: let dataURL = new DataURL(); michael@0: michael@0: assert.equal(dataURL.base64, false, 'base64 is false for empty uri') michael@0: assert.equal(dataURL.data, '', 'data is an empty string for empty uri') michael@0: assert.equal(dataURL.mimeType, '', 'mimeType is an empty string for empty uri') michael@0: assert.equal(Object.keys(dataURL.parameters).length, 0, 'parameters is an empty object for empty uri'); michael@0: michael@0: assert.equal(dataURL.toString(), 'data:,'); michael@0: } michael@0: michael@0: exports.testDataURLwithMalformedURI = function (assert) { michael@0: assert.throws(function() { michael@0: let dataURL = new DataURL('http://www.mozilla.com/'); michael@0: }, michael@0: /Malformed Data URL: http:\/\/www.mozilla.com\//i, michael@0: 'DataURL raises an exception for malformed data uri' michael@0: ); michael@0: } michael@0: michael@0: exports.testDataURLparse = function (assert) { michael@0: let dataURL = new DataURL('data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E'); michael@0: michael@0: assert.equal(dataURL.base64, false, 'base64 is false for non base64 data uri') michael@0: assert.equal(dataURL.data, '

Hello!

', 'data is properly decoded') michael@0: assert.equal(dataURL.mimeType, 'text/html', 'mimeType is set properly') michael@0: assert.equal(Object.keys(dataURL.parameters).length, 1, 'one parameters specified'); michael@0: assert.equal(dataURL.parameters['charset'], 'US-ASCII', 'charset parsed'); michael@0: michael@0: assert.equal(dataURL.toString(), 'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E'); michael@0: } michael@0: michael@0: exports.testDataURLparseBase64 = function (assert) { michael@0: let text = 'Awesome!'; michael@0: let b64text = 'QXdlc29tZSE='; michael@0: let dataURL = new DataURL('data:text/plain;base64,' + b64text); michael@0: michael@0: assert.equal(dataURL.base64, true, 'base64 is true for base64 encoded data uri') michael@0: assert.equal(dataURL.data, text, 'data is properly decoded') michael@0: assert.equal(dataURL.mimeType, 'text/plain', 'mimeType is set properly') michael@0: assert.equal(Object.keys(dataURL.parameters).length, 1, 'one parameters specified'); michael@0: assert.equal(dataURL.parameters['base64'], '', 'parameter set without value'); michael@0: assert.equal(dataURL.toString(), 'data:text/plain;base64,' + encodeURIComponent(b64text)); michael@0: } michael@0: michael@0: exports.testIsValidURI = function (assert) { michael@0: validURIs().forEach(function (aUri) { michael@0: assert.equal(isValidURI(aUri), true, aUri + ' is a valid URL'); michael@0: }); michael@0: }; michael@0: michael@0: exports.testIsInvalidURI = function (assert) { michael@0: invalidURIs().forEach(function (aUri) { michael@0: assert.equal(isValidURI(aUri), false, aUri + ' is an invalid URL'); michael@0: }); michael@0: }; michael@0: michael@0: exports.testURLFromURL = function(assert) { michael@0: let aURL = URL('http://mozilla.org'); michael@0: let bURL = URL(aURL); michael@0: assert.equal(aURL.toString(), bURL.toString(), 'Making a URL from a URL works'); michael@0: }; michael@0: michael@0: exports.testTLD = function(assert) { michael@0: let urls = [ michael@0: { url: 'http://my.sub.domains.mozilla.co.uk', tld: 'co.uk' }, michael@0: { url: 'http://my.mozilla.com', tld: 'com' }, michael@0: { url: 'http://my.domains.mozilla.org.hk', tld: 'org.hk' }, michael@0: { url: 'chrome://global/content/blah', tld: 'global' }, michael@0: { url: 'data:text/plain;base64,QXdlc29tZSE=', tld: null }, michael@0: { url: 'https://1.2.3.4', tld: null } michael@0: ]; michael@0: michael@0: urls.forEach(function (uri) { michael@0: assert.equal(getTLD(uri.url), uri.tld); michael@0: assert.equal(getTLD(URL(uri.url)), uri.tld); michael@0: }); michael@0: } michael@0: michael@0: exports.testWindowLocationMatch = function (assert, done) { michael@0: let server = httpd.startServerAsync(port); michael@0: server.registerPathHandler('/index.html', function (request, response) { michael@0: response.write('

url tests

'); michael@0: }); michael@0: michael@0: let aUrl = 'http://localhost:' + port + '/index.html?q=aQuery#somehash'; michael@0: let urlObject = URL(aUrl); michael@0: michael@0: tabs.open({ michael@0: url: aUrl, michael@0: onReady: function (tab) { michael@0: tab.attach({ michael@0: onMessage: function (loc) { michael@0: for (let prop in loc) { michael@0: assert.equal(urlObject[prop], loc[prop], prop + ' matches'); michael@0: } michael@0: michael@0: tab.close(function() server.stop(done)); michael@0: }, michael@0: contentScript: '(' + function () { michael@0: let res = {}; michael@0: // `origin` is `null` in this context??? michael@0: let props = 'hostname,port,pathname,hash,href,protocol,search'.split(','); michael@0: props.forEach(function (prop) { michael@0: res[prop] = window.location[prop]; michael@0: }); michael@0: self.postMessage(res); michael@0: } + ')()' michael@0: }); michael@0: } michael@0: }) michael@0: }; michael@0: michael@0: exports.testURLInRegExpTest = function(assert) { michael@0: let url = 'https://mozilla.org'; michael@0: assert.equal((new RegExp(url).test(URL(url))), true, 'URL instances work in a RegExp test'); michael@0: } michael@0: michael@0: exports.testLocalURL = function(assert) { michael@0: [ michael@0: 'data:text/html;charset=utf-8,foo and bar', michael@0: 'data:text/plain,foo and bar', michael@0: 'resource://gre/modules/commonjs/', michael@0: 'chrome://browser/content/browser.xul' michael@0: ].forEach(aUri => { michael@0: assert.ok(isLocalURL(aUri), aUri + ' is a Local URL'); michael@0: }) michael@0: michael@0: } michael@0: michael@0: exports.testLocalURLwithRemoteURL = function(assert) { michael@0: validURIs().filter(url => !url.startsWith('data:')).forEach(aUri => { michael@0: assert.ok(!isLocalURL(aUri), aUri + ' is an invalid Local URL'); michael@0: }); michael@0: } michael@0: michael@0: exports.testLocalURLwithInvalidURL = function(assert) { michael@0: invalidURIs().concat([ michael@0: 'data:foo and bar', michael@0: 'resource:// must fail', michael@0: 'chrome:// here too' michael@0: ]).forEach(aUri => { michael@0: assert.ok(!isLocalURL(aUri), aUri + ' is an invalid Local URL'); michael@0: }); michael@0: } michael@0: michael@0: function validURIs() { michael@0: return [ michael@0: 'http://foo.com/blah_blah', michael@0: 'http://foo.com/blah_blah/', michael@0: 'http://foo.com/blah_blah_(wikipedia)', michael@0: 'http://foo.com/blah_blah_(wikipedia)_(again)', michael@0: 'http://www.example.com/wpstyle/?p=364', michael@0: 'https://www.example.com/foo/?bar=baz&inga=42&quux', michael@0: 'http://✪df.ws/123', michael@0: 'http://userid:password@example.com:8080', michael@0: 'http://userid:password@example.com:8080/', michael@0: 'http://userid@example.com', michael@0: 'http://userid@example.com/', michael@0: 'http://userid@example.com:8080', michael@0: 'http://userid@example.com:8080/', michael@0: 'http://userid:password@example.com', michael@0: 'http://userid:password@example.com/', michael@0: 'http://142.42.1.1/', michael@0: 'http://142.42.1.1:8080/', michael@0: 'http://➡.ws/䨹', michael@0: 'http://⌘.ws', michael@0: 'http://⌘.ws/', michael@0: 'http://foo.com/blah_(wikipedia)#cite-1', michael@0: 'http://foo.com/blah_(wikipedia)_blah#cite-1', michael@0: 'http://foo.com/unicode_(✪)_in_parens', michael@0: 'http://foo.com/(something)?after=parens', michael@0: 'http://☺.damowmow.com/', michael@0: 'http://code.google.com/events/#&product=browser', michael@0: 'http://j.mp', michael@0: 'ftp://foo.bar/baz', michael@0: 'http://foo.bar/?q=Test%20URL-encoded%20stuff', michael@0: 'http://مثال.إختبار', michael@0: 'http://例子.测试', michael@0: 'http://उदाहरण.परीक्षा', michael@0: 'http://-.~_!$&\'()*+,;=:%40:80%2f::::::@example.com', michael@0: 'http://1337.net', michael@0: 'http://a.b-c.de', michael@0: 'http://223.255.255.254', michael@0: // Also want to validate data-uris, localhost michael@0: 'http://localhost:8432/some-file.js', michael@0: 'data:text/plain;base64,', michael@0: 'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E', michael@0: 'data:text/html;charset=utf-8,' michael@0: ]; michael@0: } michael@0: michael@0: // Some invalidURIs are valid according to the regex used, michael@0: // can be improved in the future, but better to pass some michael@0: // invalid URLs than prevent valid URLs michael@0: michael@0: function invalidURIs () { michael@0: return [ michael@0: // 'http://', michael@0: // 'http://.', michael@0: // 'http://..', michael@0: // 'http://../', michael@0: // 'http://?', michael@0: // 'http://??', michael@0: // 'http://??/', michael@0: // 'http://#', michael@0: // 'http://##', michael@0: // 'http://##/', michael@0: // 'http://foo.bar?q=Spaces should be encoded', michael@0: 'not a url', michael@0: '//', michael@0: '//a', michael@0: '///a', michael@0: '///', michael@0: // 'http:///a', michael@0: 'foo.com', michael@0: 'http:// shouldfail.com', michael@0: ':// should fail', michael@0: // 'http://foo.bar/foo(bar)baz quux', michael@0: // 'http://-error-.invalid/', michael@0: // 'http://a.b--c.de/', michael@0: // 'http://-a.b.co', michael@0: // 'http://a.b-.co', michael@0: // 'http://0.0.0.0', michael@0: // 'http://10.1.1.0', michael@0: // 'http://10.1.1.255', michael@0: // 'http://224.1.1.1', michael@0: // 'http://1.1.1.1.1', michael@0: // 'http://123.123.123', michael@0: // 'http://3628126748', michael@0: // 'http://.www.foo.bar/', michael@0: // 'http://www.foo.bar./', michael@0: // 'http://.www.foo.bar./', michael@0: // 'http://10.1.1.1', michael@0: // 'http://10.1.1.254' michael@0: ]; michael@0: } michael@0: michael@0: require('sdk/test').run(exports);