Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

feat(webdriver-manager): use proxy for webdriver-manager #966

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions bin/webdriver-manager
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require('fs');
var os = require('os');
var url = require('url');
var request = require('request');
var http = require('http');
var path = require('path');
Expand Down Expand Up @@ -76,7 +77,6 @@ var binaries = {
}
};


var cli = optimist.
usage('Usage: webdriver-manager <command>\n' +
'Commands:\n' +
Expand All @@ -85,7 +85,8 @@ var cli = optimist.
' status: list the current available drivers').
describe('out_dir', 'Location to output/expect ').
default('out_dir', SELENIUM_DIR).
describe('seleniumPort', 'Optional port for the selenium standalone server');
describe('seleniumPort', 'Optional port for the selenium standalone server').
describe('proxy', 'proxy to use for the install or update command');

for (bin in binaries) {
cli.describe(bin, 'install or update ' + binaries[bin].name).
Expand All @@ -105,6 +106,17 @@ if (!fs.existsSync(argv.out_dir) || !fs.statSync(argv.out_dir).isDirectory()) {
fs.mkdirSync(argv.out_dir);
}

var resolveProxy = function(fileUrl) {
var protocol = url.parse(fileUrl).protocol;
if (argv.proxy) {
return argv.proxy;
} else if (protocol === 'https:') {
return process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support this, or require it to be explicitly entered with --proxy? I haven't had to work behind a corporate proxy, but after a quick search using env.HTTPS_PROXY doesn't seem to be a common pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responding to myself. Looked around, did find a couple uses of this, so let's keep it.

} else if (protocol === 'http:') {
return process.env.HTTP_PROXY;
}
};

/**
* Function to download file using HTTP.get.
* TODO: look into using something instead of request here, to avoid the
Expand All @@ -114,23 +126,20 @@ var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
console.log('downloading ' + fileUrl + '...');
var filePath = path.join(outputDir, fileName);
var file = fs.createWriteStream(filePath);
var req = request(fileUrl);
req.on('response', function(res) {
if (res.statusCode !== 200) {
throw new Error('Got code ' + res.statusCode + ' from ' + fileUrl);
}
});
req.on('data', function(data) {
file.write(data);
});
req.on('end', function() {
file.end(function() {
console.log(fileName + ' downloaded to ' + filePath);
if (callback) {
callback(filePath);
}
});
});

var options = {
url: fileUrl,
proxy: resolveProxy(fileUrl)
}
request(options, function (error, response) {
if (response.statusCode !== 200) {
throw new Error('Got code ' + response.statusCode + ' from ' + fileUrl);
}
console.log(fileName + ' downloaded to ' + filePath);
if (callback) {
callback(filePath);
}
}).pipe(file);
};

/**
Expand Down