Permalink
Browse files

Merge pull request #54 from MaThGo/use-http-request

made soda use http.request() istead of the deprecated http.createClient()
  • Loading branch information...
2 parents 146e29d + 053c40b commit db2fcb8fde3dc7e503a3e13b70f46fcb2aeeefb6 @rauchg rauchg committed Sep 19, 2012
Showing with 33 additions and 30 deletions.
  1. +33 −30 lib/soda/client.js
View
@@ -14,14 +14,14 @@ var http = require('http')
/**
* Initialize a `Client` with the given `options`.
- *
+ *
* Options:
- *
+ *
* - `host` Hostname defaulting to localhost
* - `port` Port number defaulting to 4444
* - `browser` Browser name
* - `url` URL string
- *
+ *
* @params {Object} options
* @api public
*/
@@ -83,42 +83,41 @@ Client.prototype.session = function(fn){
Client.prototype.command = function(cmd, args, fn){
this.emit('command', cmd, args);
- // HTTP client
- var client = http.createClient(this.port, this.host);
-
// Path construction
var path = this.commandPath(cmd, args);
+ // Assemble Request Options as far as possible
+ var reqOptions = {
+ host: this.host,
+ port: this.port,
+ path: path,
+ method: 'GET',
+ headers: { Host: this.host + (this.port ? ':' + this.port : '') }
+ }
+
var req;
-
- // Selenium RC can support POST request: http://svn.openqa.org/fisheye/changelog/selenium-rc/?cs=1898,
+
+ // Selenium RC can support POST request: http://svn.openqa.org/fisheye/changelog/selenium-rc/?cs=1898,
// we need to switch to use POST if the URL's is too long (Below I use the Internet Explorer's limit).
// See also: http://jira.openqa.org/browse/SRC-50
if (path.length > 2048 && (this.host + path ).length > 2083) {
- postData = this.commandPath(cmd, args).replace('/selenium-server/driver/?', "");
- req = client.request('POST'
- , path
- , { Host: this.host + (this.port ? ':' + this.port : '')
- , 'Content-Length': postData.length
- , 'Content-Type': 'application/x-www-form-urlencoded'
- });
-
- req.write(postData);
- } else {
- req = client.request('GET'
- , path
- , { Host: this.host + (this.port ? ':' + this.port : '') });
+ var postData = this.commandPath(cmd, args).replace('/selenium-server/driver/?', "");
+
+ reqOptions.path = path;
+ reqOptions.method = 'POST';
+ reqOptions.headers['Content-Length'] = postData.length;
+ reqOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
-
- req.on('response', function(res){
+
+ req = http.request(reqOptions, function(res) {
res.body = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ res.body += chunk; });
res.on('end', function(){
if (res.body.indexOf('ERROR') === 0 ||
res.body.indexOf('Timed out after ') === 0) {
var err = res.body.replace(/^ERROR: */, '');
- err = cmd + '(' + args.join(', ') + '): ' + err;
+ err = cmd + '(' + args.join(', ') + '): ' + err;
fn(new Error(err), res.body, res);
} else {
if (res.body.indexOf('OK') === 0) {
@@ -127,9 +126,14 @@ Client.prototype.command = function(cmd, args, fn){
fn(null, res.body, res);
}
});
+ return this;
});
+
+ if(postData) {
+ req.write(postData);
+ }
+
req.end();
- return this;
};
/**
@@ -190,7 +194,7 @@ Client.prototype.__defineGetter__('chain', function(){
*/
Client.prototype.end = function(fn){
- this._done = function(){this.queue = []; return fn.apply(this, arguments)};
+ this._done = function(){this.queue = null; return fn.apply(this, arguments)};
this.queue.shift()();
};
@@ -262,7 +266,7 @@ exports.createClient = function(options){
/**
* Command names.
- *
+ *
* @type Array
*/
@@ -362,7 +366,7 @@ exports.commands = [
/**
* Accessor names.
- *
+ *
* @type Array
*/
@@ -423,13 +427,12 @@ exports.accessors = [
, 'PromptPresent'
, 'SomethingSelected'
, 'TextPresent'
- , 'TextNotPresent'
, 'Visible'
];
/**
* Generate commands via accessors.
- *
+ *
* All accessors get prefixed with:
*
* - get

0 comments on commit db2fcb8

Please sign in to comment.