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>
pull/1036/head
Christian Tellnes 2015-03-03 04:41:07 +01:00
parent 31142415de
commit abb00cc915
2 changed files with 24 additions and 1 deletions

View File

@ -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();
}

View File

@ -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({}) === '');