From abb00cc915b2e336afb73210298f673a00dad9f0 Mon Sep 17 00:00:00 2001 From: Christian Tellnes Date: Tue, 3 Mar 2015 04:41:07 +0100 Subject: [PATCH] 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 Reviewed-By: Brian White Reviewed-By: Julian Duque --- lib/url.js | 8 +++++++- test/parallel/test-url.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/url.js b/lib/url.js index 00e4ec9d7be207..3091aaed572852 100644 --- a/lib/url.js +++ b/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(); } diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 7b46fc96d5acd1..77d2242ff4b071 100644 --- a/test/parallel/test-url.js +++ b/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({}) === '');