Skip to content

Commit a2f56c9

Browse files
authored
Some refactoring; Bump version to 1.1.0 (#8)
1 parent 73af46e commit a2f56c9

3 files changed

Lines changed: 41 additions & 47 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ Tip: you can skip version (`@53.0`) or/and OS name (`:Windows 10`).
3939
## Browserstack Proxy Options
4040
Proxy options can be passed via envrionment variables.
4141

42-
BROWSERSTACK_PROXY - This string needs to be in the form of `user:pass@proxyHostName:port`
43-
BROWERSTACK_LOCAL_PROXY - This string needs to be in the form of `user:pass@proxyHostName:port`
44-
BROWSERSTACK_FORCE_LOCAL - This can be true or omitted for false
45-
BROWSERSTACK_FORCE_PROXY - This can be true or omitted for false
42+
- `BROWSERSTACK_PROXY` - a string that specifies a proxy for the Browserstack local binary. It should have the following structure: `user:pass@proxyHostName:port`,
43+
- `BROWERSTACK_LOCAL_PROXY` - a string that specifies a proxy for the local web server. It should have the following structure: `user:pass@proxyHostName:port`,
44+
- `BROWSERSTACK_FORCE_PROXY` - if it's not empty, forces all traffic of Browserstack local binary to go through the proxy,
45+
- `BROWSERSTACK_FORCE_LOCAL` - if it's not empty, forces all traffic of Browserstack local binary to go through the local machine
4646

4747
## Author
4848
Developer Express Inc. (https://devexpress.com)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "testcafe-browser-provider-browserstack",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "browserstack TestCafe browser provider plugin.",
55
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
66
"homepage": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",

src/index.js

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const TESTCAFE_CLOSING_TIMEOUT = 10000;
1515
const TOO_SMALL_TIME_FOR_WAITING = MINIMAL_WORKER_TIME - TESTCAFE_CLOSING_TIMEOUT;
1616

1717
const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
18-
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
18+
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
19+
20+
const PROXY_AUTH_RE = /^([^:]*):(.*)$/;
1921

2022
const BROWSERSTACK_API_PATHS = {
2123
browserList: {
@@ -38,64 +40,56 @@ const BROWSERSTACK_API_PATHS = {
3840
})
3941
};
4042

43+
const identity = x => x;
44+
45+
const capitalize = str => str[0].toUpperCase() + str.slice(1);
46+
4147
function delay (ms) {
4248
return new Promise(resolve => setTimeout(resolve, ms));
4349
}
4450

45-
function getProxyOptions (environmentVariable) {
46-
var browserstackProxy = process.env[environmentVariable];
47-
var proxyOptions;
48-
49-
if (browserstackProxy) {
50-
var proxyURL = nodeUrl.parse('http://' + browserstackProxy);
51-
52-
if (proxyURL) {
53-
proxyOptions = {};
54-
55-
if (proxyURL.auth) {
56-
var auth = proxyURL.auth.split(':');
51+
function copyOptions (source, destination, transfromFunc = identity) {
52+
Object
53+
.keys(source)
54+
.forEach(key => source[key] && (destination[transfromFunc(key)] = source[key]));
55+
}
5756

58-
proxyOptions.user = auth[0];
59-
proxyOptions.password = auth.length === 2 ? auth[1] : '';
60-
}
57+
function getProxyOptions (proxyConfig) {
58+
try {
59+
var { hostname, port, auth } = nodeUrl.parse('http://' + proxyConfig);
60+
var parsedAuth = auth.match(PROXY_AUTH_RE);
6161

62-
if (proxyURL.host) {
63-
proxyOptions.host = proxyURL.host.hostname;
64-
proxyOptions.port = proxyURL.host.port;
65-
}
66-
}
62+
return {
63+
host: hostname,
64+
port: port,
65+
user: parsedAuth && parsedAuth[1],
66+
pass: parsedAuth && parsedAuth[2]
67+
};
68+
}
69+
catch (e) {
70+
return {};
6771
}
68-
69-
return proxyOptions;
7072
}
7173

7274
function createBrowserStackConnector (accessKey) {
7375
return new Promise((resolve, reject) => {
7476
var connector = new BrowserstackConnector();
75-
var proxyOptions = getProxyOptions('BROWSERSTACK_PROXY');
76-
var localProxyOptions = getProxyOptions('BROWSERSTACK_LOCAL_PROXY');
7777

7878
var opts = {
79-
'key': accessKey,
80-
'logfile': OS.win ? 'NUL' : '/dev/null',
81-
'enable-logging-for-api': true,
82-
'forceLocal': process.env['BROWSERSTACK_FORCE_LOCAL'] || false,
83-
'forceProxy': process.env['BROWSERSTACK_FORCE_PROXY'] || false
79+
key: accessKey,
80+
logfile: OS.win ? 'NUL' : '/dev/null',
81+
forceLocal: !!process.env['BROWSERSTACK_FORCE_LOCAL'],
82+
forceProxy: !!process.env['BROWSERSTACK_FORCE_PROXY'],
83+
84+
//NOTE: additional args use different format
85+
'enable-logging-for-api': true
8486
};
8587

86-
if (proxyOptions) {
87-
opts.proxyUser = proxyOptions.user;
88-
opts.proxyPass = proxyOptions.password;
89-
opts.proxyHost = proxyOptions.host;
90-
opts.proxyPort = proxyOptions.port;
91-
}
88+
var proxyOptions = getProxyOptions(process.env['BROWSERSTACK_PROXY']);
89+
var localProxyOptions = getProxyOptions(process.env['BROWSERSTACK_LOCAL_PROXY']);
9290

93-
if (localProxyOptions) {
94-
opts['local-proxy-user'] = localProxyOptions.user;
95-
opts['local-proxy-pass'] = localProxyOptions.password;
96-
opts['local-proxy-host'] = localProxyOptions.host;
97-
opts['local-proxy-port'] = localProxyOptions.port;
98-
}
91+
copyOptions(proxyOptions, opts, key => 'proxy' + capitalize(key));
92+
copyOptions(localProxyOptions, opts, key => 'local-proxy-' + key);
9993

10094
connector.start(opts, err => {
10195
if (err) {

0 commit comments

Comments
 (0)