Skip to content

Commit

Permalink
Fix jayson binary client and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeh committed Jun 8, 2012
1 parent 7128c19 commit e669cac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 70 deletions.
4 changes: 2 additions & 2 deletions bin/jayson.js
Expand Up @@ -13,7 +13,7 @@ var eyes = require('eyes');
// initialize program and define arguments
program.version(pkg.version)
.option('-m, --method [name]', 'Method', String)
.option('-p, --params [json]', 'Array or Object to use as parameters', jayson.utils.parse)
.option('-p, --params [json]', 'Array or Object to use as parameters', JSON.parse)
.option('-u, --url [url]', 'URL to server', url.parse)
.option('-q, --quiet', 'Only output the response value and any errors', Boolean)
.option('-s, --socket [path]', 'Path to UNIX socket', parseSocket)
Expand Down Expand Up @@ -56,7 +56,7 @@ client.request(program.method, program.params, function(err, response) {
}

if(!response || program.json) {
std.out('%s', JSON.stringify(response)).replace("\n", ""), true);
std.out('%s', JSON.stringify(response).replace("\n", ""), true);
return process.exit(0);
}

Expand Down
117 changes: 49 additions & 68 deletions test/bin.client.test.js
@@ -1,101 +1,82 @@
var should = require('should');
var jayson = require(__dirname + '/..');
var support = require('./support/client-server');

var exec = require('child_process').exec;
var url = require('url');

var methods = {
add: function(a, b, callback) { callback(null, a + b); }
};
var bin = __dirname + '/../bin/jayson.js';

var binaryPath = __dirname + '/../bin/jayson.js';
describe('jayson binary', function() {

describe('A HTTP server', function() {
var server = jayson.server(methods);
var http = server.http();
var server = jayson.server(support.methods, support.options);

it('should be able to listen to a temporary socket', function(done) {
http.listen(__dirname + '/bin.test.socket', done);
});
var http = null;

after(function(done) {
http.on('close', done);
http.close();
});

});
describe('port server', function() {

describe('A HTTP socket server', function() {
var server = jayson.server(methods);
var path = __dirname + '/bin.test.socket';
var http = server.http();
var hostname = 'localhost';
var port = 1337;
var url = require('url').format({
port: port,
protocol: 'http',
hostname: hostname
});

before(function(done) {
http.listen(path, done);
});
before(function(done) {
http = server.http();
http.listen(port, hostname, done);
});

it('should be callable', function(done) {
var args = formatArgs(binaryPath, {
socket: path,
method: 'add',
params: JSON.stringify([1, 2]),
json: true
var a = 3, b = 4;
var args = formatArgs(bin, {
url: url, method: 'add', json: true,
params: JSON.stringify([a, b])
});
exec(args, function(err, stdout, stderr) {
should.not.exist(err);
var json = {};
(function() {
json = JSON.parse(stdout);
}).should.not.throw();
should.exist(stdout, stderr);
var json = JSON.parse(stdout);
json.should.have.property('id');
json.should.have.property('result');
json.result.should.equal(3);
json.result.should.equal(a + b);
stderr.should.equal('');
done();
});
});

after(function(done) {
http.on('close', done);
http.close();
});
});

describe('A HTTP port server', function() {
var server = jayson.server(methods);
var http = server.http();
var hostname = 'localhost';
var port = 3000;
var urlStr = url.format({
port: port,
protocol: 'http',
hostname: hostname
});
describe('socket server', function() {

before(function(done) {
http.listen(port, hostname, done);
});
var socketPath = __dirname + '/support/bin.test.socket';

it('should be callable', function(done) {
var args = formatArgs(binaryPath, {
url: urlStr,
method: 'add',
params: JSON.stringify([3, 4]),
json: true
before(function(done) {
http = server.http();
http.listen(socketPath, done);
});
exec(args, function(err, stdout, stderr) {
should.not.exist(err);
should.exist(stdout, stderr);
var json = {};
(function() { json = jayson.utils.parse(stdout); }).should.not.throw();
json.should.have.property('id');
json.should.have.property('result');
json.result.should.equal(7);
stderr.should.equal('');
done();

it('should be callable', function(done) {
var a = 1, b = 2;
var args = formatArgs(bin, {
socket: socketPath, method: 'add', json: true,
params: JSON.stringify([a, b])
});
exec(args, function(err, stdout, stderr) {
should.not.exist(err);
var json = JSON.parse(stdout);
json.should.have.property('id');
json.should.have.property('result');
json.result.should.equal(a + b);
stderr.should.equal('');
done();
});
});

});

after(function(done) {
afterEach(function(done) {
if(!http) done();
http.on('close', done);
http.close();
});
Expand Down

0 comments on commit e669cac

Please sign in to comment.