client.request.js
3.56 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
var test = require('tape');
var sinon = require('sinon');
var tinyJsonRpc = require('../');
var Client = tinyJsonRpc.Client;
var Server = tinyJsonRpc.Server;
test('Client.request', function (t) {
var server = new Server();
server.provide(function echo (what) {
return what;
});
function expectValidRequest(t, request) {
t.equal(request.jsonrpc, '2.0', 'version is "2.0"');
t.equal(typeof request.id, 'number', '`id` is a number');
}
t.test('throws an error when', function (t) {
var client = new Client({
server: server
});
t.throws(function () {
client.request();
}, /Method must be a string/, 'config is required');
t.throws(function () {
client.request({});
}, /Method must be a string/, 'config.method is required');
[null, {}, [], 23, false].forEach(function (arg) {
t.throws(function () {
client.request({
method: arg
});
}, /Method must be a string/, 'config.method must be a string');
});
var circular = {};
circular.link = circular;
t.throws(
function () {
client.request('echo', circular);
},
/Could not serialize request to JSON/,
'throws if request parameters cannot be serialized'
);
t.throws(
function () {
client.request({
method: 'echo',
params: [ circular ]
});
},
/Could not serialize request to JSON/,
'throws if request parameters cannot be serialized'
);
['', false, true, null, 0, 42].forEach(function (arg) {
t.throws(
function () {
client.request({
method: 'echo',
params: arg
});
},
/Params must be an object or array/,
'config.params must be an object or array'
);
});
t.end();
});
t.test('upon a valid notification', function (t) {
var client = new Client({
server: server
});
var request;
test('on failure, calls the passed callback with the error', function (t) {
server.provide(function fail () {
throw new Error('DOH!');
});
var callback = sinon.spy();
client.request('fail', 'marco', callback);
sinon.assert.calledOnce(callback);
t.equal(callback.firstCall.args.length, 2, 'passed two arguments');
t.equal(callback.firstCall.args[0].message, 'DOH!');
t.equal(callback.firstCall.args[1], null, 'second argument is null');
callback.reset();
client.request({
method: 'fail',
params: ['marco'],
callback: callback
});
sinon.assert.calledOnce(callback);
t.equal(callback.firstCall.args.length, 2, 'passed two arguments');
t.equal(callback.firstCall.args[0].message, 'DOH!');
t.equal(callback.firstCall.args[1], null, 'second argument is null');
t.end();
});
t.test('calls the provided callback with the result', function (t) {
var callback = sinon.spy();
client.request('echo', 'marco', callback);
sinon.assert.calledOnce(callback);
t.equal(callback.firstCall.args.length, 2);
t.equal(callback.firstCall.args[0], null);
t.equal(callback.firstCall.args[1], 'marco');
callback.reset();
client.request({
method: 'echo',
params: ['marco'],
callback: callback
});
sinon.assert.calledOnce(callback);
t.equal(callback.firstCall.args.length, 2);
t.equal(callback.firstCall.args[0], null);
t.equal(callback.firstCall.args[1], 'marco');
t.end();
});
t.end();
});
t.end();
});