stream-client.notify.js
2.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
'use strict';
var test = require('tape');
var tinyJsonRpc = require('../');
var StreamClient = tinyJsonRpc.StreamClient;
var Client = tinyJsonRpc.Client;
var Server = tinyJsonRpc.Server;
var EventEmitter = require('events').EventEmitter;
var sinon = require('sinon');
test('StreamClient.notify', function (t) {
var server = new Server();
server.provide(function echo (what) {
return what;
});
function expectValidNotification(t, request) {
t.equal(request.jsonrpc, '2.0');
t.equal(request.id, void undefined);
}
t.test('upon a valid notification, sends a valid notification to the server',
function (t) {
var stream = new EventEmitter();
stream.write = sinon.stub().returns(true);
var client = new StreamClient({
server: stream
});
client.notify('echo', 'marco');
sinon.assert.calledOnce(stream.write);
t.equal(stream.write.firstCall.args.length, 1);
var request = JSON.parse(stream.write.firstCall.args[0]);
expectValidNotification(t, request);
t.equal(request.method, 'echo');
t.deepEqual(request.params, ['marco']);
stream.write.reset();
client.notify({
method: 'echo',
params: ['marco']
});
sinon.assert.calledOnce(stream.write);
t.equal(stream.write.firstCall.args.length, 1);
var request = JSON.parse(stream.write.firstCall.args[0]);
expectValidNotification(t, request);
t.equal(request.method, 'echo');
t.deepEqual(request.params, ['marco']);
t.end();
});
t.test('respects backoff signals when writing', function (t) {
var stream = new EventEmitter();
stream.write = sinon.stub().returns(true);
var client = new StreamClient({
server: stream
});
stream.write = sinon.stub();
stream.write.returns(false);
client.notify('echo', 'marco');
sinon.assert.calledOnce(stream.write);
sinon.assert.calledWith(stream.write, JSON.stringify({
jsonrpc: '2.0',
method: 'echo',
params: ['marco']
}));
stream.write.reset();
client.notify('echo', 'marco');
client.notify('echo', 'marco');
sinon.assert.notCalled(stream.write);
stream.write.returns(true);
stream.emit('drain');
sinon.assert.calledTwice(stream.write);
t.ok(stream.write.firstCall.calledWith(JSON.stringify({
jsonrpc: '2.0',
method: 'echo',
params: ['marco']
})));
t.ok(stream.write.secondCall.calledWith(JSON.stringify({
jsonrpc: '2.0',
method: 'echo',
params: ['marco']
})));
t.end();
});
t.end();
});