From 053c40b7501eb1df332e4652cea50e098dc38839 Mon Sep 17 00:00:00 2001 From: Manuel Goerlich Date: Wed, 19 Sep 2012 02:17:27 +0200 Subject: [PATCH] made soda use http.request() istead of the deprecated http.createClient() --- lib/soda/client.js | 63 ++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/lib/soda/client.js b/lib/soda/client.js index 52fc76b..4c0cd4a 100755 --- a/lib/soda/client.js +++ b/lib/soda/client.js @@ -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,34 +83,33 @@ 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; }); @@ -118,7 +117,7 @@ Client.prototype.command = function(cmd, args, fn){ 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