diff --git a/lib/Local.js b/lib/Local.js index 1f00ac4..d976320 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -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(); @@ -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); } diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index c81260c..aa6a503 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -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(){ @@ -20,7 +22,7 @@ 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); @@ -28,7 +30,15 @@ function LocalBinary(){ 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); @@ -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); } }; diff --git a/package.json b/package.json index 50d0d6e..fa3e062 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/local.js b/test/local.js index f73a260..5fd442d 100644 --- a/test/local.js +++ b/test/local.js @@ -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; @@ -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(); + }); + }); +});