/
bin.client.test.js
96 lines (78 loc) · 2.19 KB
/
bin.client.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var should = require('should');
var jayson = require(__dirname + '/..');
var support = require('./support/client-server');
var exec = require('child_process').exec;
var bin = __dirname + '/../bin/jayson.js';
describe('jayson binary', function() {
var server = jayson.server(support.methods, support.options);
var http = null;
describe('port server', function() {
var hostname = 'localhost';
var port = 1337;
var url = require('url').format({
port: port,
protocol: 'http',
hostname: hostname
});
before(function(done) {
http = server.http();
http.listen(port, hostname, done);
});
it('should be callable', function(done) {
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);
should.exist(stdout, stderr);
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();
});
});
});
describe('socket server', function() {
var socketPath = __dirname + '/support/bin.test.socket';
before(function(done) {
http = server.http();
http.listen(socketPath, 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();
});
});
});
afterEach(function(done) {
if(!http) done();
http.on('close', done);
http.close();
});
});
function formatArgs(binary, args) {
var buf = binary;
for(var arg in args) {
var value = args[arg];
buf += ' --' + arg;
if(typeof(value) === 'string') {
buf += ' "' + value + '"';
}
}
return buf;
}