| 1 |
// Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.uri.utilsTest'); |
| 16 |
goog.setTestOnly('goog.uri.utilsTest'); |
| 17 |
|
| 18 |
goog.require('goog.functions'); |
| 19 |
goog.require('goog.string'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.uri.utils'); |
| 22 |
|
| 23 |
|
| 24 |
var utils = goog.uri.utils; |
| 25 |
|
| 26 |
|
| 27 |
function setUpPage() { |
| 28 |
goog.string.getRandomString = goog.functions.constant('RANDOM'); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
function tearDown() {} |
| 33 |
|
| 34 |
|
| 35 |
function testSplit() { |
| 36 |
var uri = 'http://www.google.com:80/path%20path+path?q=query&hl=en#fragment'; |
| 37 |
assertEquals('http', utils.getScheme(uri)); |
| 38 |
assertNull(utils.getUserInfoEncoded(uri)); |
| 39 |
assertNull(utils.getUserInfo(uri)); |
| 40 |
assertEquals('www.google.com', utils.getDomainEncoded(uri)); |
| 41 |
assertEquals('www.google.com', utils.getDomain(uri)); |
| 42 |
assertEquals(80, utils.getPort(uri)); |
| 43 |
assertEquals('/path%20path+path', utils.getPathEncoded(uri)); |
| 44 |
assertEquals('/path path+path', utils.getPath(uri)); |
| 45 |
assertEquals('q=query&hl=en', utils.getQueryData(uri)); |
| 46 |
assertEquals('fragment', utils.getFragmentEncoded(uri)); |
| 47 |
assertEquals('fragment', utils.getFragment(uri)); |
| 48 |
|
| 49 |
assertEquals( |
| 50 |
utils.getDomain('http://[2607:f8b0:4006:802::1006]'), |
| 51 |
'[2607:f8b0:4006:802::1006]'); |
| 52 |
assertEquals( |
| 53 |
utils.getDomain('http://[2607:f8b0:4006:802::1006]:80'), |
| 54 |
'[2607:f8b0:4006:802::1006]'); |
| 55 |
assertEquals(utils.getPort('http://[2607:f8b0:4006:802::1006]:80'), 80); |
| 56 |
assertEquals(utils.getDomain('http://[2607]:80/?q=]'), '[2607]'); |
| 57 |
assertEquals(utils.getDomain('http://!!!'), '!!!'); |
| 58 |
assertNull(utils.getPath('http://!!!')); |
| 59 |
assertNull(utils.getScheme('www.x.com:80')); |
| 60 |
assertEquals( |
| 61 |
'Query data with no fragment identifier', 'foo=bar&baz=bin', |
| 62 |
utils.getQueryData('http://google.com?foo=bar&baz=bin')); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
function testSplitWithNewline() { |
| 67 |
var uri = 'http://www.google.com:80/path%20path+path?q=query#frag\nment'; |
| 68 |
assertEquals('http', utils.getScheme(uri)); |
| 69 |
assertNull(utils.getUserInfoEncoded(uri)); |
| 70 |
assertNull(utils.getUserInfo(uri)); |
| 71 |
assertEquals('www.google.com', utils.getDomainEncoded(uri)); |
| 72 |
assertEquals('www.google.com', utils.getDomain(uri)); |
| 73 |
assertEquals(80, utils.getPort(uri)); |
| 74 |
assertEquals('/path%20path+path', utils.getPathEncoded(uri)); |
| 75 |
assertEquals('/path path+path', utils.getPath(uri)); |
| 76 |
assertEquals('q=query', utils.getQueryData(uri)); |
| 77 |
assertEquals('frag\nment', utils.getFragmentEncoded(uri)); |
| 78 |
assertEquals('frag\nment', utils.getFragment(uri)); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
function testMailtoUri() { |
| 83 |
var uri = 'mailto:[email protected]'; |
| 84 |
assertNull(utils.getDomain(uri)); |
| 85 |
assertEquals('mailto', utils.getScheme(uri)); |
| 86 |
assertEquals('[email protected]', utils.getPath(uri)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
function testSplitRelativeUri() { |
| 91 |
var uri = '/path%20path+path?q=query&hl=en#fragment'; |
| 92 |
assertNull(utils.getScheme(uri)); |
| 93 |
assertNull(utils.getDomain(uri)); |
| 94 |
assertNull(utils.getDomainEncoded(uri)); |
| 95 |
assertNull(utils.getPort(uri)); |
| 96 |
assertEquals('/path%20path+path', utils.getPathEncoded(uri)); |
| 97 |
assertEquals('/path path+path', utils.getPath(uri)); |
| 98 |
assertEquals('q=query&hl=en', utils.getQueryData(uri)); |
| 99 |
assertEquals('fragment', utils.getFragmentEncoded(uri)); |
| 100 |
assertEquals('fragment', utils.getFragment(uri)); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testSplitBadAuthority() { |
| 105 |
// This URL has a syntax error per the RFC (port number must be digits, and |
| 106 |
// host cannot contain a colon except in [...]). This test is solely to |
| 107 |
// 'document' the current behavior, which may affect application handling |
| 108 |
// of erroneous URLs. |
| 109 |
assertEquals(utils.getDomain('http://host:port/'), 'host:port'); |
| 110 |
assertNull(utils.getPort('http://host:port/')); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
function testSplitIntoHostAndPath() { |
| 115 |
// Splitting into host and path takes care of one of the major use cases |
| 116 |
// of resolve, without implementing a generic algorithm that undoubtedly |
| 117 |
// requires a huge footprint. |
| 118 |
var uri = 'http://www.google.com:80/path%20path+path?q=query&hl=en#fragment'; |
| 119 |
assertEquals('http://www.google.com:80', goog.uri.utils.getHost(uri)); |
| 120 |
assertEquals( |
| 121 |
'/path%20path+path?q=query&hl=en#fragment', |
| 122 |
goog.uri.utils.getPathAndAfter(uri)); |
| 123 |
|
| 124 |
var uri2 = 'http://www.google.com/calendar'; |
| 125 |
assertEquals( |
| 126 |
'should handle missing fields', 'http://www.google.com', |
| 127 |
goog.uri.utils.getHost(uri2)); |
| 128 |
assertEquals( |
| 129 |
'should handle missing fields', '/calendar', |
| 130 |
goog.uri.utils.getPathAndAfter(uri2)); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
function testGetOrigin() { |
| 135 |
var uri = |
| 136 |
'http://foo:[email protected]:80/path%20path+path?q=query&hl=en#fragment'; |
| 137 |
assertEquals('http://www.google.com:80', goog.uri.utils.getOrigin(uri)); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
function testRelativeUrisHaveNoPath() { |
| 142 |
assertNull(utils.getPathEncoded('?hello')); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
function testReservedCharacters() { |
| 147 |
var o = '%6F'; |
| 148 |
var uri = 'http://www.g' + o + 'ogle.com%40/xxx%2feee/ccc'; |
| 149 |
assertEquals( |
| 150 |
'Should not decode reserved characters in path', '/xxx%2feee/ccc', |
| 151 |
goog.uri.utils.getPath(uri)); |
| 152 |
assertEquals( |
| 153 |
'Should not decode reserved characters in domain', 'www.google.com%40', |
| 154 |
goog.uri.utils.getDomain(uri)); |
| 155 |
} |
| 156 |
|
| 157 |
function testSetFragmentEncoded() { |
| 158 |
var expected = 'http://www.google.com/path#bar'; |
| 159 |
assertEquals( |
| 160 |
expected, |
| 161 |
utils.setFragmentEncoded('http://www.google.com/path#foo', 'bar')); |
| 162 |
|
| 163 |
assertEquals( |
| 164 |
expected, utils.setFragmentEncoded('http://www.google.com/path', 'bar')); |
| 165 |
|
| 166 |
assertEquals( |
| 167 |
'http://www.google.com/path', |
| 168 |
utils.setFragmentEncoded('http://www.google.com/path', '')); |
| 169 |
|
| 170 |
assertEquals( |
| 171 |
'http://www.google.com/path', |
| 172 |
utils.setFragmentEncoded('http://www.google.com/path', null)); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
function testGetParamValue() { |
| 177 |
assertEquals( |
| 178 |
'v1', |
| 179 |
utils.getParamValue('/path?key=v1&c=d&keywithsuffix=v3&key=v2', 'key')); |
| 180 |
|
| 181 |
assertEquals( |
| 182 |
'v1', |
| 183 |
utils.getParamValue('/path?kEY=v1&c=d&keywithsuffix=v3&key=v2', 'kEY')); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
function testGetParamValues() { |
| 188 |
assertArrayEquals( |
| 189 |
'should ignore confusing suffixes', ['v1', 'v2'], |
| 190 |
utils.getParamValues( |
| 191 |
'/path?a=b&key=v1&c=d&key=v2&keywithsuffix=v3', 'key')); |
| 192 |
assertArrayEquals( |
| 193 |
'should be case sensitive', ['v2'], |
| 194 |
utils.getParamValues( |
| 195 |
'/path?a=b&keY=v1&c=d&KEy=v2&keywithsuffix=v3', 'KEy')); |
| 196 |
assertArrayEquals( |
| 197 |
'should work for the first parameter', ['v1', 'v2'], |
| 198 |
utils.getParamValues('/path?key=v1&c=d&key=v2&keywithsuffix=v3', 'key')); |
| 199 |
assertArrayEquals( |
| 200 |
'should work for the last parameter', ['v1', 'v2'], |
| 201 |
utils.getParamValues('/path?key=v1&c=d&keywithsuffix=v3&key=v2', 'key')); |
| 202 |
assertArrayEquals( |
| 203 |
['1'], utils.getParamValues('http://foo.com?q=1#?q=2&q=3', 'q')); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
function testGetParamValueAllowsEqualInValues() { |
| 208 |
assertEquals( |
| 209 |
'equals signs can appear unencoded', 'v1=v2', |
| 210 |
utils.getParamValue('/path?key=v1=v2', 'key')); |
| 211 |
assertArrayEquals( |
| 212 |
['v1=v2=v3'], utils.getParamValues('/path?key=v1=v2=v3', 'key')); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
function testGetParamValueNoSuchKey() { |
| 217 |
var uri = '/path?key=v1&c=d&keywithsuffix=v3&key=v2'; |
| 218 |
assertNull(utils.getParamValue(uri, 'nosuchkey')); |
| 219 |
assertArrayEquals([], utils.getParamValues(uri, 'nosuchkey')); |
| 220 |
assertFalse(utils.hasParam(uri, 'nosuchkey')); |
| 221 |
assertNull(utils.getParamValue('q=1', 'q')); |
| 222 |
assertEquals('1', utils.getParamValue('?q=1', 'q')); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
function testGetParamValueEmptyAndMissingValueStrings() { |
| 227 |
assertEquals('', utils.getParamValue('/path?key&bar', 'key')); |
| 228 |
assertEquals('', utils.getParamValue('/path?foo=bar&key', 'key')); |
| 229 |
assertEquals('', utils.getParamValue('/path?key', 'key')); |
| 230 |
assertEquals('', utils.getParamValue('/path?key=', 'key')); |
| 231 |
assertArrayEquals([''], utils.getParamValues('/path?key', 'key')); |
| 232 |
assertArrayEquals([''], utils.getParamValues('/path?key&bar', 'key')); |
| 233 |
assertArrayEquals([''], utils.getParamValues('/path?foo=bar&key', 'key')); |
| 234 |
assertArrayEquals([''], utils.getParamValues('/path?foo=bar&key=', 'key')); |
| 235 |
assertArrayEquals( |
| 236 |
['', '', '', 'j', ''], |
| 237 |
utils.getParamValues('/path?key&key&key=&key=j&key', 'key')); |
| 238 |
assertArrayEquals( |
| 239 |
['', '', '', '', ''], |
| 240 |
utils.getParamValues('/pathqqq?q&qq&q&q=&q&q', 'q')); |
| 241 |
assertTrue(utils.hasParam('/path?key=', 'key')); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
function testGetParamValueDecoding() { |
| 246 |
assertEquals( |
| 247 |
'plus should be supported as alias of space', 'foo bar baz', |
| 248 |
utils.getParamValue('/path?key=foo+bar%20baz', 'key')); |
| 249 |
assertArrayEquals( |
| 250 |
['foo bar baz'], |
| 251 |
utils.getParamValues('/path?key=foo%20bar%20baz', 'key')); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
function testGetParamIgnoresParamsInFragmentIdentifiers() { |
| 256 |
assertFalse(utils.hasParam('/path?bah#a&key=foo', 'key')); |
| 257 |
assertEquals(null, utils.getParamValue('/path?bah#a&key=bar', 'key')); |
| 258 |
assertArrayEquals([], utils.getParamValues('/path?bah#a&key=bar', 'key')); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
function testGetParamIgnoresExcludesFragmentFromParameterValue() { |
| 263 |
// Make sure the '#' doesn't get included anywhere, for parameter values |
| 264 |
// of different lengths. |
| 265 |
assertEquals( |
| 266 |
'foo', utils.getParamValue('/path?key=foo#key=bar&key=baz', 'key')); |
| 267 |
assertArrayEquals( |
| 268 |
['foo'], utils.getParamValues('/path?key=foo#key=bar&key=baz', 'key')); |
| 269 |
assertEquals('', utils.getParamValue('/path?key#key=bar&key=baz', 'key')); |
| 270 |
assertArrayEquals( |
| 271 |
[''], utils.getParamValues('/path?key#key=bar&key=baz', 'key')); |
| 272 |
assertEquals('x', utils.getParamValue('/path?key=x#key=bar&key=baz', 'key')); |
| 273 |
assertArrayEquals( |
| 274 |
['x'], utils.getParamValues('/path?key=x#key=bar&key=baz', 'key')); |
| 275 |
|
| 276 |
// Simply make sure hasParam doesn't die in this case. |
| 277 |
assertTrue(utils.hasParam('/path?key=foo#key=bar&key=baz', 'key')); |
| 278 |
assertTrue(utils.hasParam('/path?key=foo#key&key=baz', 'key')); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
function testSameDomainPathsDiffer() { |
| 283 |
var uri1 = 'http://www.google.com/a'; |
| 284 |
var uri2 = 'http://www.google.com/b'; |
| 285 |
assertTrue(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 286 |
assertTrue(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
function testSameDomainSchemesDiffer() { |
| 291 |
var uri1 = 'http://www.google.com'; |
| 292 |
var uri2 = 'https://www.google.com'; |
| 293 |
assertFalse(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 294 |
assertFalse(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
function testSameDomainPortsDiffer() { |
| 299 |
var uri1 = 'http://www.google.com:1234/a'; |
| 300 |
var uri2 = 'http://www.google.com/b'; |
| 301 |
var uri3 = 'http://www.google.com:2345/b'; |
| 302 |
assertFalse(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 303 |
assertFalse(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 304 |
assertFalse(goog.uri.utils.haveSameDomain(uri1, uri3)); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
function testSameDomainDomainsDiffer() { |
| 309 |
var uri1 = '/a'; |
| 310 |
var uri2 = 'http://www.google.com/b'; |
| 311 |
assertFalse(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 312 |
assertFalse(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
function testSameDomainSubDomainDiffers() { |
| 317 |
var uri1 = 'http://www.google.com/a'; |
| 318 |
var uri2 = 'http://mail.google.com/b'; |
| 319 |
assertFalse(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 320 |
assertFalse(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
function testSameDomainNoDomain() { |
| 325 |
var uri1 = '/a'; |
| 326 |
var uri2 = '/b'; |
| 327 |
assertTrue(goog.uri.utils.haveSameDomain(uri1, uri2)); |
| 328 |
assertTrue(goog.uri.utils.haveSameDomain(uri2, uri1)); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Simple class with a constant toString. |
| 335 |
* @param {string} stringValue The result of toString. |
| 336 |
* @constructor |
| 337 |
*/ |
| 338 |
function HasString(stringValue) { |
| 339 |
this.value_ = stringValue; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
/** @override */ |
| 344 |
HasString.prototype.toString = function() { |
| 345 |
return this.value_; |
| 346 |
}; |
| 347 |
|
| 348 |
|
| 349 |
function testBuildFromEncodedParts() { |
| 350 |
assertEquals( |
| 351 |
'should handle full URL', |
| 352 |
'http://[email protected]:80/path?q=query#fragment', |
| 353 |
utils.buildFromEncodedParts( |
| 354 |
'http', 'foo', 'www.google.com', 80, '/path', 'q=query', 'fragment')); |
| 355 |
assertEquals( |
| 356 |
'should handle unspecified parameters', '/search', |
| 357 |
utils.buildFromEncodedParts(null, null, undefined, null, '/search')); |
| 358 |
assertEquals( |
| 359 |
'should handle params of non-primitive types', |
| 360 |
'http://[email protected]:80/path?q=query#fragment', |
| 361 |
utils.buildFromEncodedParts( |
| 362 |
new HasString('http'), new HasString('foo'), |
| 363 |
new HasString('www.google.com'), new HasString('80'), |
| 364 |
new HasString('/path'), new HasString('q=query'), |
| 365 |
new HasString('fragment'))); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
function testAppendParam() { |
| 370 |
assertEquals( |
| 371 |
'http://foo.com?q=1', utils.appendParam('http://foo.com', 'q', 1)); |
| 372 |
assertEquals( |
| 373 |
'http://foo.com?q=1#preserve', |
| 374 |
utils.appendParam('http://foo.com#preserve', 'q', 1)); |
| 375 |
assertEquals( |
| 376 |
'should tolerate a lone question mark', 'http://foo.com?q=1', |
| 377 |
utils.appendParam('http://foo.com?', 'q', 1)); |
| 378 |
assertEquals( |
| 379 |
'http://foo.com?q=1&r=2', |
| 380 |
utils.appendParam('http://foo.com?q=1', 'r', 2)); |
| 381 |
assertEquals( |
| 382 |
'http://foo.com?q=1&r=2&s=3#preserve', |
| 383 |
utils.appendParam('http://foo.com?q=1&r=2#preserve', 's', 3)); |
| 384 |
assertEquals('?q=1#preserve', utils.appendParam('#preserve', 'q', 1)); |
| 385 |
} |
| 386 |
|
| 387 |
function testBuildQueryData() { |
| 388 |
assertEquals( |
| 389 |
'q=1&r=2&s=3&s=4', utils.buildQueryData(['q', 1, 'r', 2, 's', [3, 4]])); |
| 390 |
assertEquals('', utils.buildQueryData([])); |
| 391 |
} |
| 392 |
|
| 393 |
function testAppendParams() { |
| 394 |
assertEquals('http://foo.com', utils.appendParams('http://foo.com')); |
| 395 |
assertEquals( |
| 396 |
'http://foo.com?q=1&r=2&s=3&s=4#preserve', |
| 397 |
utils.appendParams( |
| 398 |
'http://foo.com#preserve', 'q', 1, 'r', 2, 's', [3, 4])); |
| 399 |
assertEquals( |
| 400 |
'http://foo.com?a=1&q=1&r=2&s=3&s=4#preserve', |
| 401 |
utils.appendParams( |
| 402 |
'http://foo.com?a=1#preserve', 'q', 1, 'r', 2, 's', [3, 4])); |
| 403 |
assertEquals( |
| 404 |
'http://foo.com?q=1&r=2&s=3&s=4#preserve', |
| 405 |
utils.appendParams( |
| 406 |
'http://foo.com?#preserve', 'q', 1, 'r', 2, 's', [3, 4])); |
| 407 |
assertEquals( |
| 408 |
'?q=1&r=2&s=3&s=4#preserve', |
| 409 |
utils.appendParams('#preserve', 'q', 1, 'r', 2, 's', [3, 4])); |
| 410 |
assertEquals( |
| 411 |
'A question mark must not be appended if there are no ' + |
| 412 |
'parameters, otherwise repeated appends will be broken.', |
| 413 |
'http://foo.com#test', utils.appendParams('http://foo.com#test')); |
| 414 |
assertEquals( |
| 415 |
'If a ? is already in the URL, it should be preserved when appending ' + |
| 416 |
'0 params', |
| 417 |
'http://foo.com?#test', utils.appendParams('http://foo.com?#test')); |
| 418 |
assertEquals( |
| 419 |
'should handle objects with to-string', 'http://foo.com?q=a&r=b', |
| 420 |
utils.appendParams( |
| 421 |
'http://foo.com', 'q', new HasString('a'), 'r', |
| 422 |
[new HasString('b')])); |
| 423 |
|
| 424 |
assertThrows( |
| 425 |
'appendParams should fail with an odd number of arguments.', |
| 426 |
function() { utils.appendParams('http://foo.com', 'a', 1, 'b'); }); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
function testValuelessParam() { |
| 431 |
assertEquals('http://foo.com?q', utils.appendParam('http://foo.com', 'q')); |
| 432 |
assertEquals( |
| 433 |
'http://foo.com?q', |
| 434 |
utils.appendParam('http://foo.com', 'q', null /* opt_value */)); |
| 435 |
assertEquals( |
| 436 |
'http://foo.com?q#preserve', |
| 437 |
utils.appendParam('http://foo.com#preserve', 'q')); |
| 438 |
assertEquals( |
| 439 |
'should tolerate a lone question mark', 'http://foo.com?q', |
| 440 |
utils.appendParam('http://foo.com?', 'q')); |
| 441 |
assertEquals( |
| 442 |
'http://foo.com?q=1&r', utils.appendParam('http://foo.com?q=1', 'r')); |
| 443 |
assertEquals( |
| 444 |
'http://foo.com?q=1&r=2&s#preserve', |
| 445 |
utils.appendParam('http://foo.com?q=1&r=2#preserve', 's')); |
| 446 |
assertTrue(utils.hasParam('http://foo.com?q=1&r=2&s#preserve', 's')); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
function testAppendParamsAsArray() { |
| 451 |
assertEquals( |
| 452 |
'http://foo.com?q=1&r=2&s=3&s=4#preserve', |
| 453 |
utils.appendParams( |
| 454 |
'http://foo.com#preserve', ['q', 1, 'r', 2, 's', [3, 4]])); |
| 455 |
assertEquals( |
| 456 |
'http://foo.com?q=1&s=3&s=4#preserve', |
| 457 |
utils.appendParams( |
| 458 |
'http://foo.com#preserve', ['q', 1, 'r', null, 's', [3, 4]])); |
| 459 |
assertEquals( |
| 460 |
'http://foo.com?q=1&s=3&s=4#preserve', |
| 461 |
utils.appendParams( |
| 462 |
'http://foo.com#preserve', ['q', 1, 'r', undefined, 's', [3, 4]])); |
| 463 |
assertEquals( |
| 464 |
'http://foo.com?q=1&r=2&s=3&s=4&s=null&s=undefined#preserve', |
| 465 |
utils.appendParams( |
| 466 |
'http://foo.com#preserve', |
| 467 |
['q', 1, 'r', 2, 's', [3, new HasString('4'), null, undefined]])); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
function testAppendParamEscapes() { |
| 472 |
assertEquals( |
| 473 |
'http://foo.com?h=a%20b', |
| 474 |
utils.appendParams('http://foo.com', 'h', 'a b')); |
| 475 |
assertEquals('h=a%20b', utils.buildQueryData(['h', 'a b'])); |
| 476 |
assertEquals('h=a%20b', utils.buildQueryDataFromMap({'h': 'a b'})); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
function testAppendParamsFromMap() { |
| 481 |
var uri = utils.appendParamsFromMap( |
| 482 |
'http://www.foo.com', |
| 483 |
{'a': 1, 'b': 'bob', 'c': [1, 2, new HasString('3')]}); |
| 484 |
assertArrayEquals(['1'], utils.getParamValues(uri, 'a')); |
| 485 |
assertArrayEquals(['bob'], utils.getParamValues(uri, 'b')); |
| 486 |
assertArrayEquals(['1', '2', '3'], utils.getParamValues(uri, 'c')); |
| 487 |
} |
| 488 |
|
| 489 |
function testBuildQueryDataFromMap() { |
| 490 |
assertEquals('a=1', utils.buildQueryDataFromMap({'a': 1})); |
| 491 |
var uri = 'foo.com?' + |
| 492 |
utils.buildQueryDataFromMap( |
| 493 |
{'a': 1, 'b': 'bob', 'c': [1, 2, new HasString('3')]}); |
| 494 |
assertArrayEquals(['1'], utils.getParamValues(uri, 'a')); |
| 495 |
assertArrayEquals(['bob'], utils.getParamValues(uri, 'b')); |
| 496 |
assertArrayEquals(['1', '2', '3'], utils.getParamValues(uri, 'c')); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
function testMultiParamSkipsNullParams() { |
| 501 |
// For the multi-param functions, null and undefined keys should be |
| 502 |
// skipped, but null within a parameter array should still be appended. |
| 503 |
assertEquals( |
| 504 |
'buildQueryDataFromMap', 'a=null', |
| 505 |
utils.buildQueryDataFromMap({'a': [null], 'b': null, 'c': undefined})); |
| 506 |
assertEquals( |
| 507 |
'buildQueryData', 'a=null', |
| 508 |
utils.buildQueryData(['a', [null], 'b', null, 'c', undefined])); |
| 509 |
assertEquals( |
| 510 |
'appendParams', 'foo.com?a=null', |
| 511 |
utils.appendParams('foo.com', 'a', [null], 'b', null, 'c', undefined)); |
| 512 |
assertEquals( |
| 513 |
'empty strings should NOT be skipped', 'foo.com?a&b', |
| 514 |
utils.appendParams('foo.com', 'a', [''], 'b', '')); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
function testRemoveParam() { |
| 519 |
assertEquals( |
| 520 |
'remove middle', 'http://foo.com?q=1&s=3', |
| 521 |
utils.removeParam('http://foo.com?q=1&r=2&s=3', 'r')); |
| 522 |
assertEquals( |
| 523 |
'remove first', 'http://foo.com?r=2&s=3', |
| 524 |
utils.removeParam('http://foo.com?q=1&r=2&s=3', 'q')); |
| 525 |
assertEquals( |
| 526 |
'remove last', 'http://foo.com?q=1&r=2', |
| 527 |
utils.removeParam('http://foo.com?q=1&r=2&s=3', 's')); |
| 528 |
assertEquals( |
| 529 |
'remove only param', 'http://foo.com', |
| 530 |
utils.removeParam('http://foo.com?q=1', 'q')); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
function testRemoveParamWithFragment() { |
| 535 |
assertEquals( |
| 536 |
'remove middle', 'http://foo.com?q=1&s=3#?r=1&r=1', |
| 537 |
utils.removeParam('http://foo.com?q=1&r=2&s=3#?r=1&r=1', 'r')); |
| 538 |
assertEquals( |
| 539 |
'remove first', 'http://foo.com?r=2&s=3#?q=1&q=1', |
| 540 |
utils.removeParam('http://foo.com?q=1&r=2&s=3#?q=1&q=1', 'q')); |
| 541 |
assertEquals( |
| 542 |
'remove only param', 'http://foo.com#?q=1&q=1', |
| 543 |
utils.removeParam('http://foo.com?q=1#?q=1&q=1', 'q')); |
| 544 |
assertEquals( |
| 545 |
'remove last', 'http://foo.com?q=1&r=2#?s=1&s=1', |
| 546 |
utils.removeParam('http://foo.com?q=1&r=2&s=3#?s=1&s=1', 's')); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
function testRemoveNonExistent() { |
| 551 |
assertEquals( |
| 552 |
'remove key not present', 'http://foo.com?q=1', |
| 553 |
utils.removeParam('http://foo.com?q=1', 'nosuchkey')); |
| 554 |
assertEquals( |
| 555 |
'remove key not present', 'http://foo.com#q=1', |
| 556 |
utils.removeParam('http://foo.com#q=1', 'q')); |
| 557 |
assertEquals( |
| 558 |
'remove key from empty string', '', utils.removeParam('', 'nosuchkey')); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
function testRemoveMultiple() { |
| 563 |
assertEquals( |
| 564 |
'remove four of the same', 'http://foo.com', |
| 565 |
utils.removeParam('http://foo.com?q=1&q=2&q=3&q=4', 'q')); |
| 566 |
assertEquals( |
| 567 |
'remove four of the same with another one in the middle', |
| 568 |
'http://foo.com?a=99', |
| 569 |
utils.removeParam('http://foo.com?q=1&q=2&a=99&q=3&q=4', 'q')); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
function testSetParam() { |
| 574 |
assertEquals( |
| 575 |
'middle, no fragment', 'http://foo.com?q=1&s=3&r=999', |
| 576 |
utils.setParam('http://foo.com?q=1&r=2&s=3', 'r', 999)); |
| 577 |
assertEquals( |
| 578 |
'middle', 'http://foo.com?q=1&s=3&r=999#?r=1&r=1', |
| 579 |
utils.setParam('http://foo.com?q=1&r=2&s=3#?r=1&r=1', 'r', 999)); |
| 580 |
assertEquals( |
| 581 |
'first', 'http://foo.com?r=2&s=3&q=999#?q=1&q=1', |
| 582 |
utils.setParam('http://foo.com?q=1&r=2&s=3#?q=1&q=1', 'q', 999)); |
| 583 |
assertEquals( |
| 584 |
'only param', 'http://foo.com?q=999#?q=1&q=1', |
| 585 |
utils.setParam('http://foo.com?q=1#?q=1&q=1', 'q', 999)); |
| 586 |
assertEquals( |
| 587 |
'last', 'http://foo.com?q=1&r=2&s=999#?s=1&s=1', |
| 588 |
utils.setParam('http://foo.com?q=1&r=2&s=3#?s=1&s=1', 's', 999)); |
| 589 |
assertEquals( |
| 590 |
'multiple', 'http://foo.com?s=999#?s=1&s=1', |
| 591 |
utils.setParam('http://foo.com?s=1&s=2&s=3#?s=1&s=1', 's', 999)); |
| 592 |
assertEquals( |
| 593 |
'none', 'http://foo.com?r=1&s=999#?s=1&s=1', |
| 594 |
utils.setParam('http://foo.com?r=1#?s=1&s=1', 's', 999)); |
| 595 |
} |
| 596 |
|
| 597 |
function testSetParamsFromMap() { |
| 598 |
// These helper assertions are needed because the input is an Object and |
| 599 |
// we cannot gaurauntee an order. |
| 600 |
function assertQueryEquals(message, expected, actual) { |
| 601 |
var expectedQuery = goog.uri.utils.getQueryData(expected); |
| 602 |
var actualQuery = goog.uri.utils.getQueryData(actual); |
| 603 |
assertEquals( |
| 604 |
'Unmatched param count. ' + message, expectedQuery.split('&').length, |
| 605 |
actualQuery.split('&').length); |
| 606 |
|
| 607 |
// Build a map of all of the params for actual. |
| 608 |
var actualParams = {}; |
| 609 |
goog.uri.utils.parseQueryData(actualQuery, function(key, value) { |
| 610 |
if (actualParams[key]) { |
| 611 |
actualParams[key].push(value); |
| 612 |
} else { |
| 613 |
actualParams[key] = [value]; |
| 614 |
} |
| 615 |
}); |
| 616 |
|
| 617 |
for (key in actualParams) { |
| 618 |
var expectedParams = goog.uri.utils.getParamValues(actual, key); |
| 619 |
assertArrayEquals( |
| 620 |
'Unmatched param ' + key + ', ' + message, expectedParams.sort(), |
| 621 |
actualParams[key].sort()); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
function assertUriEquals(message, expected, actual) { |
| 626 |
message = ' for expected URI: "' + expected + '", actual: "' + actual + '"'; |
| 627 |
var expectedComps = utils.split(expected); |
| 628 |
var actualComps = utils.split(actual); |
| 629 |
for (var i = 1; i < expectedComps.length; i++) { |
| 630 |
if (i === goog.uri.utils.ComponentIndex.QUERY_DATA) { |
| 631 |
assertQueryEquals(message, expected, actual); |
| 632 |
} else { |
| 633 |
assertEquals(message, expectedComps[i], actualComps[i]); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
assertEquals( |
| 639 |
'remove some params', 'http://foo.com/bar?b=2#b=5', |
| 640 |
goog.uri.utils.setParamsFromMap( |
| 641 |
'http://foo.com/bar?a=1&b=2&c=3#b=5', {a: null, c: undefined})); |
| 642 |
assertEquals( |
| 643 |
'remove all params', 'http://foo.com#b=5', |
| 644 |
goog.uri.utils.setParamsFromMap( |
| 645 |
'http://foo.com?a=1&b=2&c=3#b=5', {a: null, b: null, c: undefined})); |
| 646 |
assertEquals( |
| 647 |
'update one param', 'http://foo.com?b=2&c=3&a=999#b=5', |
| 648 |
goog.uri.utils.setParamsFromMap( |
| 649 |
'http://foo.com?a=1&b=2&c=3#b=5', {a: 999})); |
| 650 |
assertEquals( |
| 651 |
'remove one param, update one param', 'http://foo.com?b=2&a=999', |
| 652 |
goog.uri.utils.setParamsFromMap( |
| 653 |
'http://foo.com?a=1&b=2&c=3', {a: 999, c: null})); |
| 654 |
assertEquals( |
| 655 |
'multiple params unmodified', 'http://foo.com?b=2&b=20&b&a=999', |
| 656 |
goog.uri.utils.setParamsFromMap( |
| 657 |
'http://foo.com?a=1&b=2&b=20&b', {a: 999})); |
| 658 |
assertEquals( |
| 659 |
'update multiple values', 'http://foo.com?a=1&c=3&b=5&b&b=10', |
| 660 |
goog.uri.utils.setParamsFromMap( |
| 661 |
'http://foo.com?a=1&b=2&c=3', {b: [5, '', 10]})); |
| 662 |
// Tests that update/add multiple params must use assertUriEquals. |
| 663 |
assertUriEquals( |
| 664 |
'add from blank query', 'http://foo.com?a=100&b=200#hash', |
| 665 |
goog.uri.utils.setParamsFromMap('http://foo.com#hash', {a: 100, b: 200})); |
| 666 |
assertUriEquals( |
| 667 |
'replace multiple params', 'http://foo.com?d=4&a=100&b=200&c=300', |
| 668 |
goog.uri.utils.setParamsFromMap( |
| 669 |
'http://foo.com?a=1&b=2&b=20&c=3&d=4', {a: 100, b: 200, c: 300})); |
| 670 |
// update 1, remove b, keep c as is, add d. |
| 671 |
assertUriEquals( |
| 672 |
'add, remove and update', 'http://foo.com?a=100&c=3&d=400', |
| 673 |
goog.uri.utils.setParamsFromMap( |
| 674 |
'http://foo.com?a=1&b=2&b=20&c=3', {a: 100, b: null, d: 400})); |
| 675 |
} |
| 676 |
|
| 677 |
function testModifyQueryParams() { |
| 678 |
var uri = 'http://foo.com?a=A&a=A2&b=B&b=B2&c=C'; |
| 679 |
|
| 680 |
uri = utils.appendParam(uri, 'd', 'D'); |
| 681 |
assertEquals('http://foo.com?a=A&a=A2&b=B&b=B2&c=C&d=D', uri); |
| 682 |
|
| 683 |
uri = utils.removeParam(uri, 'd'); |
| 684 |
uri = utils.appendParam(uri, 'd', 'D2'); |
| 685 |
assertEquals('http://foo.com?a=A&a=A2&b=B&b=B2&c=C&d=D2', uri); |
| 686 |
|
| 687 |
uri = utils.removeParam(uri, 'a'); |
| 688 |
uri = utils.appendParam(uri, 'a', 'A3'); |
| 689 |
assertEquals('http://foo.com?b=B&b=B2&c=C&d=D2&a=A3', uri); |
| 690 |
|
| 691 |
uri = utils.removeParam(uri, 'a'); |
| 692 |
uri = utils.appendParam(uri, 'a', 'A4'); |
| 693 |
assertEquals('A4', utils.getParamValue(uri, 'a')); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
function testBrowserEncoding() { |
| 698 |
// Sanity check borrowed from old code to ensure that encodeURIComponent |
| 699 |
// is good enough. Entire test should be safe to delete. |
| 700 |
var allowedInFragment = /[A-Za-z0-9\-\._~!$&'()*+,;=:@/?]/g; |
| 701 |
|
| 702 |
var sb = []; |
| 703 |
for (var i = 33; i < 500; i++) { // arbitrarily use first 500 chars. |
| 704 |
sb.push(String.fromCharCode(i)); |
| 705 |
} |
| 706 |
var testString = sb.join(''); |
| 707 |
|
| 708 |
var encodedStr = encodeURIComponent(testString); |
| 709 |
|
| 710 |
// Strip all percent encoded characters, as they're ok. |
| 711 |
encodedStr = encodedStr.replace(/%[0-9A-F][0-9A-F]/g, ''); |
| 712 |
|
| 713 |
// Remove allowed characters. |
| 714 |
encodedStr = encodedStr.replace(allowedInFragment, ''); |
| 715 |
|
| 716 |
// Only illegal characters should remain, which is a fail. |
| 717 |
assertEquals('String should be empty', 0, encodedStr.length); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
function testAppendPath() { |
| 722 |
var uri = 'http://www.foo.com'; |
| 723 |
var expected = uri + '/dummy'; |
| 724 |
assertEquals( |
| 725 |
'Path has no trailing "/", adding with leading "/" failed', expected, |
| 726 |
goog.uri.utils.appendPath(uri, '/dummy')); |
| 727 |
assertEquals( |
| 728 |
'Path has no trailing "/", adding with no leading "/" failed', expected, |
| 729 |
goog.uri.utils.appendPath(uri, 'dummy')); |
| 730 |
uri = uri + '/'; |
| 731 |
assertEquals( |
| 732 |
'Path has trailing "/", adding with leading "/" failed', expected, |
| 733 |
goog.uri.utils.appendPath(uri, '/dummy')); |
| 734 |
|
| 735 |
assertEquals( |
| 736 |
'Path has trailing "/", adding with no leading "/" failed', expected, |
| 737 |
goog.uri.utils.appendPath(uri, 'dummy')); |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
function testMakeUnique() { |
| 742 |
assertEquals( |
| 743 |
'http://www.google.com?zx=RANDOM#blob', |
| 744 |
goog.uri.utils.makeUnique('http://www.google.com#blob')); |
| 745 |
assertEquals( |
| 746 |
'http://www.google.com?a=1&b=2&zx=RANDOM#blob', |
| 747 |
goog.uri.utils.makeUnique('http://www.google.com?zx=9&a=1&b=2#blob')); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
function testParseQuery() { |
| 752 |
var result = []; |
| 753 |
goog.uri.utils.parseQueryData( |
| 754 |
'foo=bar&no&empty=&tricky%3D%26=%3D%26&=nothing&=&', |
| 755 |
function(name, value) { result.push(name, value); }); |
| 756 |
assertArrayEquals( |
| 757 |
[ |
| 758 |
'foo', 'bar', 'no', '', 'empty', '', 'tricky%3D%26', '=&', '', |
| 759 |
'nothing', '', '', '', '' |
| 760 |
], |
| 761 |
result); |
| 762 |
|
| 763 |
// Go thought buildQueryData and parseQueryData and see if we get the same |
| 764 |
// result. |
| 765 |
var result2 = []; |
| 766 |
goog.uri.utils.parseQueryData( |
| 767 |
goog.uri.utils.buildQueryData(result), |
| 768 |
function(name, value) { result2.push(name, value); }); |
| 769 |
assertArrayEquals(result, result2); |
| 770 |
|
| 771 |
goog.uri.utils.parseQueryData( |
| 772 |
'', goog.partial(fail, 'Empty string should not run callback')); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
function testSetPath() { |
| 777 |
assertEquals( |
| 778 |
'http://www.google.com/bar', |
| 779 |
goog.uri.utils.setPath('http://www.google.com', 'bar')); |
| 780 |
assertEquals( |
| 781 |
'http://www.google.com/bar', |
| 782 |
goog.uri.utils.setPath('http://www.google.com', '/bar')); |
| 783 |
assertEquals( |
| 784 |
'http://www.google.com/bar/', |
| 785 |
goog.uri.utils.setPath('http://www.google.com', 'bar/')); |
| 786 |
assertEquals( |
| 787 |
'http://www.google.com/bar/', |
| 788 |
goog.uri.utils.setPath('http://www.google.com', '/bar/')); |
| 789 |
assertEquals( |
| 790 |
'http://www.google.com/bar?q=t', |
| 791 |
goog.uri.utils.setPath('http://www.google.com/?q=t', '/bar')); |
| 792 |
assertEquals( |
| 793 |
'http://www.google.com/bar?q=t', |
| 794 |
goog.uri.utils.setPath('http://www.google.com/?q=t', 'bar')); |
| 795 |
assertEquals( |
| 796 |
'http://www.google.com/bar/?q=t', |
| 797 |
goog.uri.utils.setPath('http://www.google.com/?q=t', 'bar/')); |
| 798 |
assertEquals( |
| 799 |
'http://www.google.com/bar/?q=t', |
| 800 |
goog.uri.utils.setPath('http://www.google.com/?q=t', '/bar/')); |
| 801 |
assertEquals( |
| 802 |
'http://www.google.com/bar?q=t', |
| 803 |
goog.uri.utils.setPath('http://www.google.com/foo?q=t', 'bar')); |
| 804 |
assertEquals( |
| 805 |
'http://www.google.com/bar?q=t', |
| 806 |
goog.uri.utils.setPath('http://www.google.com/foo?q=t', '/bar')); |
| 807 |
assertEquals( |
| 808 |
'https://www.google.com/bar?q=t&q1=y', |
| 809 |
goog.uri.utils.setPath('https://www.google.com/foo?q=t&q1=y', 'bar')); |
| 810 |
assertEquals( |
| 811 |
'https://www.google.com:8113/bar?q=t&q1=y', |
| 812 |
goog.uri.utils.setPath('https://www.google.com:8113?q=t&q1=y', 'bar')); |
| 813 |
assertEquals( |
| 814 |
'https://www.google.com:8113/foo/bar?q=t&q1=y', |
| 815 |
goog.uri.utils.setPath( |
| 816 |
'https://www.google.com:8113/foobar?q=t&q1=y', 'foo/bar')); |
| 817 |
assertEquals( |
| 818 |
'https://www.google.com:8113/foo/bar?q=t&q1=y', |
| 819 |
goog.uri.utils.setPath( |
| 820 |
'https://www.google.com:8113/foobar?q=t&q1=y', '/foo/bar')); |
| 821 |
assertEquals( |
| 822 |
'https://www.google.com:8113/foo/bar/?q=t&q1=y', |
| 823 |
goog.uri.utils.setPath( |
| 824 |
'https://www.google.com:8113/foobar?q=t&q1=y', 'foo/bar/')); |
| 825 |
assertEquals( |
| 826 |
'https://www.google.com:8113/foo/bar/?q=t&q1=y', |
| 827 |
goog.uri.utils.setPath( |
| 828 |
'https://www.google.com:8113/foobar?q=t&q1=y', '/foo/bar/')); |
| 829 |
assertEquals( |
| 830 |
'https://www.google.com:8113/?q=t&q1=y', |
| 831 |
goog.uri.utils.setPath( |
| 832 |
'https://www.google.com:8113/foobar?q=t&q1=y', '')); |
| 833 |
} |