Skip to content

Commit

Permalink
Some refactoring; Bump version to 1.1.0 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBelym authored Apr 14, 2017
1 parent 73af46e commit a2f56c9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ Tip: you can skip version (`@53.0`) or/and OS name (`:Windows 10`).
## Browserstack Proxy Options
Proxy options can be passed via envrionment variables.

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

## Author
Developer Express Inc. (https://devexpress.com)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testcafe-browser-provider-browserstack",
"version": "1.0.0",
"version": "1.1.0",
"description": "browserstack TestCafe browser provider plugin.",
"repository": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
"homepage": "https://github.com/DevExpress/testcafe-browser-provider-browserstack",
Expand Down
78 changes: 36 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const TESTCAFE_CLOSING_TIMEOUT = 10000;
const TOO_SMALL_TIME_FOR_WAITING = MINIMAL_WORKER_TIME - TESTCAFE_CLOSING_TIMEOUT;

const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' +
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.';

const PROXY_AUTH_RE = /^([^:]*):(.*)$/;

const BROWSERSTACK_API_PATHS = {
browserList: {
Expand All @@ -38,64 +40,56 @@ const BROWSERSTACK_API_PATHS = {
})
};

const identity = x => x;

const capitalize = str => str[0].toUpperCase() + str.slice(1);

function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function getProxyOptions (environmentVariable) {
var browserstackProxy = process.env[environmentVariable];
var proxyOptions;

if (browserstackProxy) {
var proxyURL = nodeUrl.parse('http://' + browserstackProxy);

if (proxyURL) {
proxyOptions = {};

if (proxyURL.auth) {
var auth = proxyURL.auth.split(':');
function copyOptions (source, destination, transfromFunc = identity) {
Object
.keys(source)
.forEach(key => source[key] && (destination[transfromFunc(key)] = source[key]));
}

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

if (proxyURL.host) {
proxyOptions.host = proxyURL.host.hostname;
proxyOptions.port = proxyURL.host.port;
}
}
return {
host: hostname,
port: port,
user: parsedAuth && parsedAuth[1],
pass: parsedAuth && parsedAuth[2]
};
}
catch (e) {
return {};
}

return proxyOptions;
}

function createBrowserStackConnector (accessKey) {
return new Promise((resolve, reject) => {
var connector = new BrowserstackConnector();
var proxyOptions = getProxyOptions('BROWSERSTACK_PROXY');
var localProxyOptions = getProxyOptions('BROWSERSTACK_LOCAL_PROXY');

var opts = {
'key': accessKey,
'logfile': OS.win ? 'NUL' : '/dev/null',
'enable-logging-for-api': true,
'forceLocal': process.env['BROWSERSTACK_FORCE_LOCAL'] || false,
'forceProxy': process.env['BROWSERSTACK_FORCE_PROXY'] || false
key: accessKey,
logfile: OS.win ? 'NUL' : '/dev/null',
forceLocal: !!process.env['BROWSERSTACK_FORCE_LOCAL'],
forceProxy: !!process.env['BROWSERSTACK_FORCE_PROXY'],

//NOTE: additional args use different format
'enable-logging-for-api': true
};

if (proxyOptions) {
opts.proxyUser = proxyOptions.user;
opts.proxyPass = proxyOptions.password;
opts.proxyHost = proxyOptions.host;
opts.proxyPort = proxyOptions.port;
}
var proxyOptions = getProxyOptions(process.env['BROWSERSTACK_PROXY']);
var localProxyOptions = getProxyOptions(process.env['BROWSERSTACK_LOCAL_PROXY']);

if (localProxyOptions) {
opts['local-proxy-user'] = localProxyOptions.user;
opts['local-proxy-pass'] = localProxyOptions.password;
opts['local-proxy-host'] = localProxyOptions.host;
opts['local-proxy-port'] = localProxyOptions.port;
}
copyOptions(proxyOptions, opts, key => 'proxy' + capitalize(key));
copyOptions(localProxyOptions, opts, key => 'local-proxy-' + key);

connector.start(opts, err => {
if (err) {
Expand Down

0 comments on commit a2f56c9

Please sign in to comment.