Skip to content

Commit e37b881

Browse files
authored
Merge pull request #15 from mohlendo/master
Use the configured proxy host and port for the download
2 parents a89874d + f5c58d0 commit e37b881

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

lib/Local.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Local(){
4040
callback(new LocalError('No output received'));
4141

4242
if(data['state'] != 'connected'){
43-
callback(new LocalError(data['message']));
43+
callback(new LocalError(data['message']['message']));
4444
} else {
4545
that.pid = data['pid'];
4646
callback();
@@ -199,7 +199,12 @@ function Local(){
199199
this.getBinaryPath = function(callback){
200200
if(typeof(this.binaryPath) == 'undefined'){
201201
this.binary = new LocalBinary();
202-
this.binary.binaryPath(callback);
202+
var conf = {};
203+
if(this.proxyHost && this.proxyPort){
204+
conf.proxyHost = this.proxyHost;
205+
conf.proxyPort = this.proxyPort;
206+
}
207+
this.binary.binaryPath(conf, callback);
203208
} else {
204209
callback(this.binaryPath);
205210
}

lib/LocalBinary.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var https = require('https'),
2+
url = require('url'),
23
fs = require('fs'),
34
path = require('path'),
45
os = require('os'),
6+
HttpsProxyAgent = require('https-proxy-agent'),
57
LocalError = require('./LocalError');
68

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

23-
this.download = function(destParentDir, callback){
25+
this.download = function(conf, destParentDir, callback){
2426
if(!this.checkPath(destParentDir))
2527
fs.mkdirSync(destParentDir);
2628

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

31-
https.get(this.httpPath, function (response) {
33+
var options = url.parse(this.httpPath);
34+
if(conf.proxyHost && conf.proxyPort){
35+
options.agent = new HttpsProxyAgent({
36+
host: conf.proxyHost,
37+
port: conf.proxyPort
38+
});
39+
}
40+
41+
https.get(options, function (response) {
3242
response.on('end', function () {
3343
fs.chmod(binaryPath, '0755', function() {
3444
callback(binaryPath);
@@ -38,14 +48,14 @@ function LocalBinary(){
3848
});
3949
};
4050

41-
this.binaryPath = function(callback){
51+
this.binaryPath = function(conf, callback){
4252
var destParentDir = this.getAvailableDirs();
4353
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
4454
var binaryPath = path.join(destParentDir, destBinaryName);
4555
if(this.checkPath(binaryPath, fs.X_OK)){
4656
callback(binaryPath);
4757
} else {
48-
this.download(destParentDir, callback);
58+
this.download(conf, destParentDir, callback);
4959
}
5060
};
5161

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
"author": "BrowserStack",
1818
"license": "MIT",
1919
"dependencies": {
20+
"https-proxy-agent": "^1.0.0",
2021
"is-running": "^2.0.0"
2122
},
2223
"devDependencies": {
2324
"eslint": "1.10.3",
2425
"expect.js": "0.3.1",
2526
"mocha": "2.4.5",
26-
"mocks": "0.0.15"
27+
"mocks": "0.0.15",
28+
"proxy": "^0.2.4",
29+
"rimraf": "^2.5.4"
2730
},
2831
"bugs": "https://github.com/browserstack/browserstack-local-nodejs/issues",
2932
"homepage": "https://github.com/browserstack/browserstack-local-nodejs",

test/local.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ var expect = require('expect.js'),
22
mocks = require('mocks'),
33
path = require('path'),
44
fs = require('fs'),
5-
browserstack = require('../index');
5+
rimraf = require('rimraf'),
6+
Proxy = require('proxy'),
7+
browserstack = require('../index'),
8+
LocalBinary = require('../lib/LocalBinary');
69

710
describe('Local', function () {
811
var bsLocal;
@@ -152,3 +155,55 @@ describe('Local', function () {
152155
});
153156

154157
});
158+
159+
describe('LocalBinary', function () {
160+
var proxy;
161+
var proxyPort;
162+
var binary;
163+
var tempDownloadPath;
164+
165+
before(function (done) {
166+
// setup HTTP proxy server
167+
proxy = new Proxy();
168+
proxy.listen(function () {
169+
proxyPort = proxy.address().port;
170+
done();
171+
});
172+
});
173+
174+
after(function (done) {
175+
proxy.once('close', function () { done(); });
176+
proxy.close();
177+
});
178+
179+
beforeEach(function () {
180+
binary = new LocalBinary();
181+
tempDownloadPath = path.join(process.cwd(), 'download');
182+
});
183+
184+
afterEach(function () {
185+
rimraf.sync(tempDownloadPath);
186+
});
187+
188+
it('should download binaries without proxy', function (done) {
189+
this.timeout(600000);
190+
var conf = {};
191+
binary.download(conf, tempDownloadPath, function (result) {
192+
expect(fs.existsSync(result)).to.equal(true);
193+
done();
194+
});
195+
});
196+
197+
it('should download binaries with proxy', function (done) {
198+
this.timeout(600000);
199+
var conf = {
200+
proxyHost: '127.0.0.1',
201+
proxyPort: proxyPort
202+
};
203+
binary.download(conf, tempDownloadPath, function (result) {
204+
// test for file existence
205+
expect(fs.existsSync(result)).to.equal(true);
206+
done();
207+
});
208+
});
209+
});

0 commit comments

Comments
 (0)