Browse Source

url: throw for invalid values to url.format

`'use strict'` changes the behavior for `Function.prototype.call` when
the context is `undefined`. In earlier versions of node the value
`undefined` would make `url.format` look for fields in the global scope.

The docs states that `url.format` takes a parsed URL object and returns
a formatted URL string. So with this change it will now throw for other
values.

The exception is if the input is a string. Then it will call `url.parse`
on the string and then format it. The reason for that is that you can
call `url.format` on strings to clean up potentially wonky urls.

Fixes: https://github.com/iojs/io.js/issues/1033
PR-URL: https://github.com/iojs/io.js/pull/1036
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Julian Duque <julianduquej@gmail.com>
v1.8.0-commit
Christian Tellnes 10 years ago
parent
commit
abb00cc915
  1. 8
      lib/url.js
  2. 17
      test/parallel/test-url.js

8
lib/url.js

@ -353,7 +353,13 @@ function urlFormat(obj) {
// this way, you can call url_format() on strings
// to clean up potentially wonky urls.
if (typeof obj === 'string') obj = urlParse(obj);
if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
else if (typeof obj !== 'object' || obj === null)
throw new TypeError("Parameter 'urlObj' must be an object, not " +
obj === null ? 'null' : typeof obj);
else if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
return obj.format();
}

17
test/parallel/test-url.js

@ -1557,3 +1557,20 @@ relativeTests2.forEach(function(relativeTest) {
'format(' + relativeTest[1] + ') == ' + expected +
'\nactual:' + actual);
});
// https://github.com/iojs/io.js/pull/1036
var throws = [
undefined,
null,
true,
false,
0,
function () {}
];
for (var i = 0; i < throws.length; i++) {
assert.throws(function () { url.format(throws[i]); }, TypeError);
};
assert(url.format('') === '');
assert(url.format({}) === '');

Loading…
Cancel
Save