diff --git a/lib/shared/get-blacklist.js b/lib/shared/get-blacklist.js index ca4006b1..a9f39eac 100644 --- a/lib/shared/get-blacklist.js +++ b/lib/shared/get-blacklist.js @@ -4,8 +4,20 @@ var https = require('https'); var concat = require('concat-stream'); +var HttpsProxyAgent = require('https-proxy-agent'); + +// HTTP or HTTPS proxy to use +var proxyUri = process.env.http_proxy || process.env.https_proxy; + var url = 'https://gulpjs.com/plugins/blackList.json'; +var options = { + method: 'GET', + host: 'gulpjs.com', + path: url, + agent: proxyUri !== undefined ? new HttpsProxyAgent(proxyUri) : undefined, +}; + function collect(stream, cb) { stream.on('error', cb); stream.pipe(concat(onSuccess)); @@ -25,7 +37,10 @@ function parse(str, cb) { // TODO: Test this impl function getBlacklist(cb) { - https.get(url, onRequest); + https.get(options, onRequest) + .on('error', function(error) { + cb(error); + }); function onRequest(res) { if (res.statusCode !== 200) { diff --git a/package.json b/package.json index 9740b39a..039379a3 100644 --- a/package.json +++ b/package.json @@ -34,11 +34,12 @@ "ansi-colors": "^1.0.1", "archy": "^1.0.0", "array-sort": "^1.0.0", - "concat-stream": "^1.6.0", "color-support": "^1.1.3", + "concat-stream": "^1.6.0", "copy-props": "^2.0.1", "fancy-log": "^1.3.2", "gulplog": "^1.0.0", + "https-proxy-agent": "^2.2.1", "interpret": "^1.1.0", "isobject": "^3.0.1", "liftoff": "^2.5.0", diff --git a/test/flags-verify.js b/test/flags-verify.js index e296b1ff..ff073623 100644 --- a/test/flags-verify.js +++ b/test/flags-verify.js @@ -4,6 +4,9 @@ var expect = require('expect'); var runner = require('gulp-test-tools').gulpRunner; var eraseTime = require('gulp-test-tools').eraseTime; var path = require('path'); +var http = require('http'); +var net = require('net'); +var assert = require('assert'); describe('flag: --verify', function() { @@ -68,4 +71,81 @@ describe('flag: --verify', function() { } }); + it('proxy: dependencies with valid dependency', function(done) { + var proxyServer = createProxyServer(8881); + proxyServer.on('listening', function (servers) { + testProxyImplementation(function (err, stdout, stderr) { + proxyServer.close(); + + expect(err).toEqual(null); + expect(stderr).toEqual(''); + stdout = eraseTime(stdout); + expect(stdout).toEqual( + 'Verifying plugins in ' + + path.resolve('./test/fixtures/packages/valid-package.json') + + '\n' + + 'There are no blacklisted plugins in this project\n' + + '' + ); + done(); + }); + }); + }); + + it('proxy: proxy server not reachable', function(done) { + testProxyImplementation(function (err, stdout, stderr) { + expect(err).toNotEqual(null); + stderr = eraseTime(stderr); + expect(stderr).toEqual( + 'Error: failed to retrieve plugins black-list\n' + + 'connect ECONNREFUSED 127.0.0.1:8881\n', + 'testing stderr' + ); + stdout = eraseTime(stdout); + expect(stdout).toEqual( + 'Verifying plugins in ' + + path.resolve('./test/fixtures/packages/valid-package.json') + + '\n', 'testing stdout' + ); + done(); + }); + }); + + function testProxyImplementation(cb) { + process.env.http_proxy = 'http://localhost:8881'; + runner({ verbose: false }) + .gulp('--verify valid-package.json', '--cwd ./test/fixtures/packages/') + .run(cb); + } + + function createProxyServer(proxyPort) { + var proxy = http.createServer(function(req, res) { + assert.fail('could not start proxy server'); + }); + + proxy.on('connect', onConnect); + proxy.on('error', onError); + + function onConnect(req, clientSocket, head) { + + expect(req.method).toBe('CONNECT'); + expect(req.url).toEqual('gulpjs.com:443'); + + var serverSocket = net.connect(443, 'gulpjs.com', function() { + clientSocket.write('HTTP/1.1 200 Connection established\r\n\r\n'); + clientSocket.pipe(serverSocket); + serverSocket.write(head); + serverSocket.pipe(clientSocket); + }); + } + + function onError(error) { + assert.fail('could not start proxy server (' + JSON.stringify(error) + ')'); + } + + proxy.listen(proxyPort); + + return proxy; + } + });