Skip to content

Commit

Permalink
add https-proxy-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
chewiebug committed Aug 20, 2018
1 parent e9af812 commit 236948e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/shared/get-blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
80 changes: 80 additions & 0 deletions test/flags-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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;
}

});

0 comments on commit 236948e

Please sign in to comment.