Skip to content

Commit

Permalink
url: throw for invalid values to url.format
Browse files Browse the repository at this point in the history
`'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: #1033
PR-URL: #1036
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Julian Duque <julianduquej@gmail.com>
  • Loading branch information
tellnes committed Mar 4, 2015
1 parent 3114241 commit abb00cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}) === '');

0 comments on commit abb00cc

Please sign in to comment.