From 2a6e656c1dafc0ccbbacf2e9836f312e093c6b7c Mon Sep 17 00:00:00 2001 From: David Frank Date: Thu, 26 May 2016 02:19:16 +0800 Subject: [PATCH] allow any url for new request, but still reject non-http url in fetch --- index.js | 8 ++++++++ lib/request.js | 8 -------- test/test.js | 6 ++++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 9c45cae..f633f33 100644 --- a/index.js +++ b/index.js @@ -56,6 +56,14 @@ function Fetch(url, opts) { return; } + if (!options.protocol || !options.hostname) { + throw new Error('only absolute urls are supported'); + } + + if (options.protocol !== 'http:' && options.protocol !== 'https:') { + throw new Error('only http(s) protocols are supported'); + } + var send; if (options.protocol === 'https:') { send = https.request; diff --git a/lib/request.js b/lib/request.js index ed1e8db..9d1e25a 100644 --- a/lib/request.js +++ b/lib/request.js @@ -31,14 +31,6 @@ function Request(input, init) { url_parsed = parse_url(url); } - if (!url_parsed.protocol || !url_parsed.hostname) { - throw new Error('only absolute urls are supported'); - } - - if (url_parsed.protocol !== 'http:' && url_parsed.protocol !== 'https:') { - throw new Error('only http(s) protocols are supported'); - } - // normalize init init = init || {}; diff --git a/test/test.js b/test/test.js index 4113eb6..2251023 100644 --- a/test/test.js +++ b/test/test.js @@ -1307,6 +1307,12 @@ describe('node-fetch', function() { }); });
 + it('should support arbitrary url in Request constructor', function() { + url = 'anything'; + var req = new Request(url); + expect(req.url).to.equal('anything'); + }); + it('should support clone() method in Request constructor', function() { url = base; var body = resumer().queue('a=1').end();