Skip to content

Commit

Permalink
Merge branch 'releases/v0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
beatfactor committed Dec 13, 2016
2 parents dea01d0 + f3bc41c commit bc761f2
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 22 deletions.
41 changes: 30 additions & 11 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ var format = util.format;

module.exports = (function() {
var Settings = {
selenium_host : 'localhost',
selenium_port : 4444,
default_path : '/wd/hub',
credentials : null,
use_ssl : false,
proxy : null
selenium_host : 'localhost',
selenium_port : 4444,
default_path : '/wd/hub',
credentials : null,
use_ssl : false,
proxy : null,
timeout : 60000,
retry_attempts : 0
};

var DO_NOT_LOG_ERRORS = [
Expand All @@ -29,11 +31,13 @@ module.exports = (function() {
util.inherits(HttpRequest, events.EventEmitter);

HttpRequest.prototype.setOptions = function(options) {
this.data = options.data && jsonStringify(options.data) || '';
this.contentLength = this.data.length;
this.reqOptions = this.createOptions(options);
this.hostname = formatHostname(this.reqOptions.host, this.reqOptions.port);
this.request = null;
this.data = options.data && jsonStringify(options.data) || '';
this.contentLength = this.data.length;
this.reqOptions = this.createOptions(options);
this.hostname = formatHostname(this.reqOptions.host, this.reqOptions.port);
this.request = null;
this.timeout = Settings.timeout;
this.retryAttempts = Settings.retry_attempts;

return this;
};
Expand Down Expand Up @@ -165,6 +169,15 @@ module.exports = (function() {
self.emit('error', {}, response);
});

this.request.setTimeout(this.timeout, function() {
self.request.abort();

if (self.retryAttempts) {
self.retryAttempts = self.retryAttempts - 1;
self.send();
}
});

Logger.info('Request: ' + this.reqOptions.method + ' ' + this.hostname + this.reqOptions.path,
'\n - data: ', this.data, '\n - headers: ', JSON.stringify(this.reqOptions.headers));

Expand Down Expand Up @@ -205,6 +218,12 @@ module.exports = (function() {
HttpRequest.setDefaultPathPrefix = function(path) {
Settings.default_path = path;
};
HttpRequest.setTimeout = function(timeout) {
Settings.timeout = timeout;
};
HttpRequest.setRetryAttempts = function(retryAttempts) {
Settings.retry_attempts = retryAttempts;
};

///////////////////////////////////////////////////////////
// Helpers
Expand Down
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ Nightwatch.prototype.setOptions = function(options) {

this.api.options.log_screenshot_data = this.options.log_screenshot_data ||
(typeof this.options.log_screenshot_data == 'undefined');
var seleniumPort = this.options.seleniumPort || this.options.selenium_port;
var seleniumHost = this.options.seleniumHost || this.options.selenium_host;
var useSSL = this.options.useSsl || this.options.use_ssl;
var proxy = this.options.proxy;
var seleniumPort = this.options.seleniumPort || this.options.selenium_port;
var seleniumHost = this.options.seleniumHost || this.options.selenium_host;
var useSSL = this.options.useSsl || this.options.use_ssl;
var proxy = this.options.proxy;
var timeoutOptions = this.options.request_timeout_options || {};

if (seleniumPort) {
HttpRequest.setSeleniumPort(seleniumPort);
Expand All @@ -130,6 +131,12 @@ Nightwatch.prototype.setOptions = function(options) {
if (proxy) {
HttpRequest.setProxy(proxy);
}
if (typeof timeoutOptions.timeout != 'undefined') {
HttpRequest.setTimeout(timeoutOptions.timeout);
}
if (typeof timeoutOptions.retry_attempts != 'undefined') {
HttpRequest.setRetryAttempts(timeoutOptions.retry_attempts);
}

if (typeof this.options.default_path_prefix == 'string') {
HttpRequest.setDefaultPathPrefix(this.options.default_path_prefix);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"mocha-lcov-reporter": "^1.2.0",
"mock-spawn": "^0.2.1",
"mockery": "~1.4.0",
"nock": "~0.45.0",
"nodeunit": "latest"
"nock": "~0.45.0"
},
"bin": {
"nightwatch": "./bin/nightwatch"
Expand Down
23 changes: 18 additions & 5 deletions test/lib/mockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function MockServer(options, callback) {
req.on('data', function(chunk) {
postdata += chunk;
});

req.on('end', function() {
var item = nextInLine(req, postdata);
var responsedata = '';
Expand All @@ -80,11 +81,23 @@ function MockServer(options, callback) {
options.finishedCallback(req, res);
}

res.end(responsedata, function() {
if (item && item.__once) {
removeMock(item);
}
});
if (item && item.__once) {
removeMock(item);
}

if (item && item.socketDelay) {
var timeoutId = setTimeout(function() {
res.end(responsedata);
}, item.socketDelay);

req.on('close', function(err) {
clearTimeout(timeoutId);
});

return;
}

res.end(responsedata);
});
});

Expand Down
4 changes: 4 additions & 0 deletions test/src/http/testRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = {
},

afterEach: function () {
HttpRequest.setTimeout(60000) // back to default after these tests
HttpRequest.setRetryAttempts(0);

mockery.disable();
},

Expand Down Expand Up @@ -206,4 +209,5 @@ module.exports = {

}
}

};
89 changes: 89 additions & 0 deletions test/src/http/testRequestTimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var common = require('../../common.js');
var HttpRequest = common.require('http/request.js');
var MockServer = require('../../lib/mockserver.js');
var Logger = common.require('util/logger');
var assert = require('assert');

module.exports = {
'test HttpRequestTimeout' : {
beforeEach : function(done) {
this.server = MockServer.init();
Logger.disable();
this.server.on('listening', function() {
done();
});
},

afterEach : function(done) {
this.server.close(function() {
done();
});
},

testRequestTimeout: function (done) {
MockServer.addMock({
url : '/wd/hub/123456/element',
response : '',
method: 'GET',
socketDelay : 400
}, true);

var options = {
path: '/:sessionId/element',
selenium_port: 10195,
method: 'GET',
sessionId: '123456'
};

HttpRequest.setTimeout(50);

var request = new HttpRequest(options);

request.on('error', function (err, res) {
assert.ok(res instanceof Error);
done();
}).on('success', function(result, response) {
done(new Error('Request should have timed out.'));
}).send();

},

testRetryAttempts: function (done) {
MockServer.addMock({
url : '/wd/hub/10000000/element',
response : '',
method: 'GET',
socketDelay : 200
}, true).addMock({
url : '/wd/hub/10000000/element',
response : '',
method: 'GET'
}, true);

var options = {
path: '/:sessionId/element',
selenium_port: 10195,
method: 'GET',
sessionId: '10000000'
};

HttpRequest.setTimeout(50);
HttpRequest.setRetryAttempts(1);

var request = new HttpRequest(options);
assert.equal(request.retryAttempts, 1);

request
.on('error', function(res, err) {
assert.strictEqual(request.retryAttempts, 0);
assert.ok(err instanceof Error); // the 'socket hang up' error, caused by aborting the request
})
.on('success', function (result) {
assert.strictEqual(request.retryAttempts, 0);
done();
})
.send();
}
}

};
20 changes: 20 additions & 0 deletions test/src/index/testNightwatchIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,26 @@ module.exports = {
eq(client.options.end_session_on_fail, false);
},

testSetRequestTimeoutOptions: function () {
var client = Nightwatch.createClient({
request_timeout_options: {
timeout : 10000,
retry_attempts : 3
}
});

assert.deepEqual(client.options.request_timeout_options, {
timeout : 10000,
retry_attempts : 3
});

var common = require('../../common.js');
var HttpRequest = common.require('http/request.js');
var request = new HttpRequest({});
assert.equal(request.timeout, 10000);
assert.equal(request.retryAttempts, 3);
},

'test session response with status success and no sessionId': function (done) {
MockServer.addMock({
url : '/wd/hub/session',
Expand Down

0 comments on commit bc761f2

Please sign in to comment.