Skip to content

Commit

Permalink
Merge pull request #15 from mohlendo/master
Browse files Browse the repository at this point in the history
Use the configured proxy host and port for the download
  • Loading branch information
tr4n2uil authored Aug 3, 2016
2 parents a89874d + f5c58d0 commit e37b881
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Local(){
callback(new LocalError('No output received'));

if(data['state'] != 'connected'){
callback(new LocalError(data['message']));
callback(new LocalError(data['message']['message']));
} else {
that.pid = data['pid'];
callback();
Expand Down Expand Up @@ -199,7 +199,12 @@ function Local(){
this.getBinaryPath = function(callback){
if(typeof(this.binaryPath) == 'undefined'){
this.binary = new LocalBinary();
this.binary.binaryPath(callback);
var conf = {};
if(this.proxyHost && this.proxyPort){
conf.proxyHost = this.proxyHost;
conf.proxyPort = this.proxyPort;
}
this.binary.binaryPath(conf, callback);
} else {
callback(this.binaryPath);
}
Expand Down
18 changes: 14 additions & 4 deletions lib/LocalBinary.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var https = require('https'),
url = require('url'),
fs = require('fs'),
path = require('path'),
os = require('os'),
HttpsProxyAgent = require('https-proxy-agent'),
LocalError = require('./LocalError');

function LocalBinary(){
Expand All @@ -20,15 +22,23 @@ function LocalBinary(){
this.httpPath = 'https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32';
}

this.download = function(destParentDir, callback){
this.download = function(conf, destParentDir, callback){
if(!this.checkPath(destParentDir))
fs.mkdirSync(destParentDir);

var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
var binaryPath = path.join(destParentDir, destBinaryName);
var file = fs.createWriteStream(binaryPath);

https.get(this.httpPath, function (response) {
var options = url.parse(this.httpPath);
if(conf.proxyHost && conf.proxyPort){
options.agent = new HttpsProxyAgent({
host: conf.proxyHost,
port: conf.proxyPort
});
}

https.get(options, function (response) {
response.on('end', function () {
fs.chmod(binaryPath, '0755', function() {
callback(binaryPath);
Expand All @@ -38,14 +48,14 @@ function LocalBinary(){
});
};

this.binaryPath = function(callback){
this.binaryPath = function(conf, callback){
var destParentDir = this.getAvailableDirs();
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
var binaryPath = path.join(destParentDir, destBinaryName);
if(this.checkPath(binaryPath, fs.X_OK)){
callback(binaryPath);
} else {
this.download(destParentDir, callback);
this.download(conf, destParentDir, callback);
}
};

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
"author": "BrowserStack",
"license": "MIT",
"dependencies": {
"https-proxy-agent": "^1.0.0",
"is-running": "^2.0.0"
},
"devDependencies": {
"eslint": "1.10.3",
"expect.js": "0.3.1",
"mocha": "2.4.5",
"mocks": "0.0.15"
"mocks": "0.0.15",
"proxy": "^0.2.4",
"rimraf": "^2.5.4"
},
"bugs": "https://github.com/browserstack/browserstack-local-nodejs/issues",
"homepage": "https://github.com/browserstack/browserstack-local-nodejs",
Expand Down
57 changes: 56 additions & 1 deletion test/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var expect = require('expect.js'),
mocks = require('mocks'),
path = require('path'),
fs = require('fs'),
browserstack = require('../index');
rimraf = require('rimraf'),
Proxy = require('proxy'),
browserstack = require('../index'),
LocalBinary = require('../lib/LocalBinary');

describe('Local', function () {
var bsLocal;
Expand Down Expand Up @@ -152,3 +155,55 @@ describe('Local', function () {
});

});

describe('LocalBinary', function () {
var proxy;
var proxyPort;
var binary;
var tempDownloadPath;

before(function (done) {
// setup HTTP proxy server
proxy = new Proxy();
proxy.listen(function () {
proxyPort = proxy.address().port;
done();
});
});

after(function (done) {
proxy.once('close', function () { done(); });
proxy.close();
});

beforeEach(function () {
binary = new LocalBinary();
tempDownloadPath = path.join(process.cwd(), 'download');
});

afterEach(function () {
rimraf.sync(tempDownloadPath);
});

it('should download binaries without proxy', function (done) {
this.timeout(600000);
var conf = {};
binary.download(conf, tempDownloadPath, function (result) {
expect(fs.existsSync(result)).to.equal(true);
done();
});
});

it('should download binaries with proxy', function (done) {
this.timeout(600000);
var conf = {
proxyHost: '127.0.0.1',
proxyPort: proxyPort
};
binary.download(conf, tempDownloadPath, function (result) {
// test for file existence
expect(fs.existsSync(result)).to.equal(true);
done();
});
});
});

0 comments on commit e37b881

Please sign in to comment.