25 Components.utils.import(
"resource://app/jsmodules/URLUtils.jsm");
36 'newURI':
function testNewURI(
aName) {
38 function assertURI(aSpec, aMessage) {
39 let
url = URLUtils.newURI(aSpec).QueryInterface(
Ci.nsIURL);
43 assertURI(
'http://test.com/',
44 'should create a new URI properly');
46 assertURI(
'http://test.com/?a=1&b=2&c=3',
47 'should handle query parameters');
51 'addQuery':
function testAddQuery(
aName) {
53 function assertAdded(aURL, aParams, aExpected, aMessage) {
54 let url_spec = URLUtils.addQuery(aURL, aParams);
60 assertAdded(
'http://test.com',
62 'http://test.com/?a=1&b=2&c=3',
63 'should add query params to an url spec');
65 assertAdded(
'http://test.com?d=4',
67 'http://test.com/?d=4&a=1&b=2&c=3',
68 'should add query params to an url with existing params');
70 assertAdded(
'http://test.com/?a=has%20space',
71 { b:
'has&', c:
'has=eq',
d:
'http://test.com' },
72 'http://test.com/?a=has%20space&b=has%26amp&c=has%3Deq&d=http%3A%2F%2Ftest.com',
73 'should handle URI-encoding properly');
75 assertAdded(
'http://test.com/',
78 'should handle an empty params argument');
80 assertAdded(
'http://test.com/',
83 'should handle a null params argument');
87 'produceQuery':
function testProduceQuery(
aName) {
89 function assertProduced(aParams, aExpected, aMessage) {
90 let query = URLUtils.produceQuery(aParams);
94 assertProduced({ a: 1, b: 2, c: 3 },
96 'should produce a query string from an object');
104 'a=has%20space&b=has%26amp&c=has%3Deq&d=http%3A%2F%2Ftest.com',
105 'should URI encode values');
109 'should return an empty string when passed empty params');
113 'should return an empty string when passed null params');
117 'extractQuery':
function testExtractQuery(
aName) {
120 function assertParamsEqual(aParams, aExpected, aMessage) {
123 function keysForObj(obj) {
126 for (let
key in obj) {
127 if (obj.hasOwnProperty(
key)) {
134 var param_keys = keysForObj(aParams),
135 expected_keys = keysForObj(aExpected);
141 for (let
key in aParams) {
147 function assertExtracted(aURLSpec, aExpected, aMessage) {
149 let rv = URLUtils.extractQuery(aURLSpec, params);
151 assertParamsEqual(params,
153 [
aName, aMessage.params].join(
' '));
158 base_message =
'for an url with a query string';
159 assertExtracted(
'http://test.com?a=1&b=2&c=3',
162 params: { a:
'1', b:
'2', c:
'3' }
165 rv:
'should return true ' + base_message,
166 params:
'should modify the passed params object ' +
170 base_message =
'for URI-encoded params';
171 assertExtracted(
'http://test.com?a=has%20space&b=has%26amp&c=has%3Deq&d=http%3A%2F%2Ftest.com',
182 rv:
'should return true ' + base_message,
183 params:
'should URI-decode params ' + base_message
186 base_message =
'for an url with no query string';
187 assertExtracted(
'http://test.com',
193 rv:
'should return false ' + base_message,
194 params:
'should not modify the params ' + base_message
199 'convertURLToDisplayName':
function testConvertUrlToDisplayName(
aName) {
206 for (let test_case in test_cases) {
207 test_cases[test_case](test_case);
function assertSetsEqual(s1, s2)
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.