Skip to content

Commit

Permalink
Merge pull request #3 from carlosypunto/master
Browse files Browse the repository at this point in the history
fs.existsSync now is used instead of path.existsSync, that it was deprecated since node 0.8.x
  • Loading branch information
jochen-testingbot committed Dec 1, 2012
2 parents 90c4ba1 + eff3b98 commit 626e531
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
79 changes: 42 additions & 37 deletions lib/soda/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -83,42 +83,47 @@ 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);
var postData = path.replace('/selenium-server/driver/?', "");

// HTTP client request options
var options = {
host: this.host,
port: this.port,
method: "POST",
path: path,
headers: {
Host: this.host + ( this.port ? ':'+this.port : '' ),
'Content-Length': postData.length,
'Content-Type': 'application/x-www-form-urlencoded'
}
};

var req;

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);

req.on('response', 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;
fn(new Error(err), res.body, res);
} else {
if (res.body.indexOf('OK') === 0) {
res.body = res.body.replace(/^OK,?/, '');
var req = http.request(options, 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;
fn(new Error(err), res.body, res);
} else {
if (res.body.indexOf('OK') === 0) {
res.body = res.body.replace(/^OK,?/, '');
}
fn(null, res.body, res);
}
fn(null, res.body, res);
}
});
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});

req.write(postData);
req.end();
return this;
};
Expand Down Expand Up @@ -253,7 +258,7 @@ exports.createClient = function(options){

/**
* Command names.
*
*
* @type Array
*/

Expand Down Expand Up @@ -353,7 +358,7 @@ exports.commands = [

/**
* Accessor names.
*
*
* @type Array
*/

Expand Down Expand Up @@ -420,7 +425,7 @@ exports.accessors = [

/**
* Generate commands via accessors.
*
*
* All accessors get prefixed with:
*
* - get
Expand Down
21 changes: 10 additions & 11 deletions lib/soda/testingbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
var http = require('http')
, qs = require('querystring')
, EventEmitter = require('events').EventEmitter
, fs = require('fs')
, path = require('path');
, fs = require('fs');
var Client = require('./client');

/**
* Initialize a TestingBot client with the given `options`. A suite of environment
* variables are also supported in place of the options described below.
*
*
* Options:
*
*
* - `client_key` TestingBot client key
* - `client_secret` TestingBot client secret
* - `os` Operating system ex "Linux"
* - `browser` Browser name, ex "firefox"
* - `browser-version` Browser version, ex "3.0.", "7."
* - `max-duration` Maximum test duration in seconds, ex 300 (5 minutes)
*
*
* @params {Object} options
* @api public
*/
Expand All @@ -42,8 +41,8 @@ var TestingBotClient = exports = module.exports = function TestingBotClient(opti
this.options = options;
this.browser = options.browser || 'firefox';
this.url = options.url;
if (path.existsSync(process.env.HOME + "/.testingbot")) {

if (fs.existsSync(process.env.HOME + "/.testingbot")) {
var data = fs.readFileSync(process.env.HOME + "/.testingbot", "utf-8");
var parts = data.split(':');
this.client_key = parts[0];
Expand Down Expand Up @@ -117,9 +116,9 @@ TestingBotClient.prototype.__defineGetter__('testUrl', function(){
exports.createClient = function(options){
var obj = new TestingBotClient(options);
obj.end = function(fn) {
TestingBotClient.prototype.end.call(this, function(err) {
TestingBotClient.prototype.end.call(this, function(err) {
// original callback
fn();
fn();
// send results to TestingBot
var postData = qs.stringify({
client_key: obj.client_key,
Expand All @@ -128,7 +127,7 @@ exports.createClient = function(options){
success: err === null,
kind: 10
});

// An object of options to indicate where to post to
var post_options = {
host: 'testingbot.com',
Expand All @@ -152,4 +151,4 @@ exports.createClient = function(options){
});
};
return obj;
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.6",
"author": "Jochen Delabie <info@testingbot.com>",
"main": "./lib/soda/index.js",
"engines": { "node": ">= 0.2.0" },
"engines": { "node": ">= 0.8.0" },
"repository": {
"type": "git",
"url": "git://github.com/testingbot/soda.git"
Expand Down

0 comments on commit 626e531

Please sign in to comment.