test_url_utils.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2011 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 Components.utils.import("resource://app/jsmodules/URLUtils.jsm");
26 
27 function runTest() {
28 
29  // xxx slloyd Many of these tests assume the order of enumeration for object
30  // properties in params objects. The Javascript standard does not
31  // guarantee this, but in practice our JS engine enumerates in the
32  // order that properties are added, so the assumption holds.
33 
34  var test_cases = {
35 
36  'newURI': function testNewURI(aName) {
37 
38  function assertURI(aSpec, aMessage) {
39  let url = URLUtils.newURI(aSpec).QueryInterface(Ci.nsIURL);
40  assertEqual(url.spec, aSpec, [aName, aMessage].join(' '));
41  }
42 
43  assertURI('http://test.com/',
44  'should create a new URI properly');
45 
46  assertURI('http://test.com/?a=1&b=2&c=3',
47  'should handle query parameters');
48 
49  },
50 
51  'addQuery': function testAddQuery(aName) {
52 
53  function assertAdded(aURL, aParams, aExpected, aMessage) {
54  let url_spec = URLUtils.addQuery(aURL, aParams);
55  assertEqual(url_spec, aExpected, [aName, aMessage].join(' '));
56  }
57 
58  // The expected url spec has a forward slash before the question mark due
59  // to the way that nsIStandardURL.init creates urls.
60  assertAdded('http://test.com',
61  { a: 1, b: 2, c: 3 },
62  'http://test.com/?a=1&b=2&c=3',
63  'should add query params to an url spec');
64 
65  assertAdded('http://test.com?d=4',
66  { a: 1, b: 2, c: 3 },
67  'http://test.com/?d=4&a=1&b=2&c=3',
68  'should add query params to an url with existing params');
69 
70  assertAdded('http://test.com/?a=has%20space',
71  { b: 'has&amp', 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');
74 
75  assertAdded('http://test.com/',
76  {},
77  'http://test.com/',
78  'should handle an empty params argument');
79 
80  assertAdded('http://test.com/',
81  null,
82  'http://test.com/',
83  'should handle a null params argument');
84 
85  },
86 
87  'produceQuery': function testProduceQuery(aName) {
88 
89  function assertProduced(aParams, aExpected, aMessage) {
90  let query = URLUtils.produceQuery(aParams);
91  assertEqual(query, aExpected, [aName, aMessage].join(' '));
92  }
93 
94  assertProduced({ a: 1, b: 2, c: 3 },
95  'a=1&b=2&c=3',
96  'should produce a query string from an object');
97 
98  assertProduced({
99  a: 'has space',
100  b: 'has&amp',
101  c: 'has=eq',
102  d: 'http://test.com'
103  },
104  'a=has%20space&b=has%26amp&c=has%3Deq&d=http%3A%2F%2Ftest.com',
105  'should URI encode values');
106 
107  assertProduced({},
108  '',
109  'should return an empty string when passed empty params');
110 
111  assertProduced(null,
112  '',
113  'should return an empty string when passed null params');
114 
115  },
116 
117  'extractQuery': function testExtractQuery(aName) {
118 
119  // simple one-level deep object equality check for params objects
120  function assertParamsEqual(aParams, aExpected, aMessage) {
121 
122  // We don't have JS 1.8.5 yet, so we don't get Object.keys
123  function keysForObj(obj) {
124  var key_array = [];
125 
126  for (let key in obj) {
127  if (obj.hasOwnProperty(key)) {
128  key_array.push(key);
129  }
130  }
131  return key_array;
132  }
133 
134  var param_keys = keysForObj(aParams),
135  expected_keys = keysForObj(aExpected);
136 
137  // First make sure the keys are the same
138  assertSetsEqual(param_keys, expected_keys);
139 
140  // Now compare the values
141  for (let key in aParams) {
142  assertEqual(aParams[key], aExpected[key], aMessage);
143  }
144  }
145 
146  // check an expected params object given an url spec
147  function assertExtracted(aURLSpec, aExpected, aMessage) {
148  let params = {};
149  let rv = URLUtils.extractQuery(aURLSpec, params);
150  assertEqual(rv, aExpected.rv, [aName, aMessage.rv].join(' '));
151  assertParamsEqual(params,
152  aExpected.params,
153  [aName, aMessage.params].join(' '));
154  }
155 
156  var base_message;
157 
158  base_message = 'for an url with a query string';
159  assertExtracted('http://test.com?a=1&b=2&c=3',
160  {
161  rv: true,
162  params: { a: '1', b: '2', c: '3' }
163  },
164  {
165  rv: 'should return true ' + base_message,
166  params: 'should modify the passed params object ' +
167  base_message
168  });
169 
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',
172  {
173  rv: true,
174  params: {
175  a: 'has space',
176  b: 'has&amp',
177  c: 'has=eq',
178  d: 'http://test.com'
179  }
180  },
181  {
182  rv: 'should return true ' + base_message,
183  params: 'should URI-decode params ' + base_message
184  });
185 
186  base_message = 'for an url with no query string';
187  assertExtracted('http://test.com',
188  {
189  rv: false,
190  params: {},
191  },
192  {
193  rv: 'should return false ' + base_message,
194  params: 'should not modify the params ' + base_message
195  });
196 
197  },
198 
199  'convertURLToDisplayName': function testConvertUrlToDisplayName(aName) {
200  // xxx slloyd This method doesn't indicate anything about the expected
201  // inputs or outputs, so I'm punting on testing it now.
202  }
203 
204  };
205 
206  for (let test_case in test_cases) {
207  test_cases[test_case](test_case);
208  }
209 
210 }
211 
function assertSetsEqual(s1, s2)
function assertEqual(aExpected, aActual, aMessage)
function d(s)
return null
Definition: FeedWriter.js:1143
_updateCookies aName
function url(spec)
const Ci
function runTest()
Advanced DataRemote unit tests.