-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathagent.js
86 lines (79 loc) · 2.86 KB
/
agent.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
var Backend = require('../lib/backend');
var logger = require('../lib/logger');
var sinon = require('sinon');
var StreamSocket = require('../lib/stream-socket');
var expect = require('chai').expect;
var ACTIONS = require('../lib/message-actions').ACTIONS;
var Connection = require('../lib/client/connection');
var protocol = require('../lib/protocol');
var LegacyConnection = require('sharedb-legacy/lib/client').Connection;
describe('Agent', function() {
var backend;
beforeEach(function() {
backend = new Backend();
});
afterEach(function(done) {
backend.close(done);
});
describe('handshake', function() {
it('warns when messages are sent before the handshake', function(done) {
var socket = new StreamSocket();
var stream = socket.stream;
backend.listen(stream);
sinon.spy(logger, 'warn');
socket.send(JSON.stringify({a: ACTIONS.subscribe, c: 'dogs', d: 'fido'}));
var connection = new Connection(socket);
socket._open();
connection.once('connected', function() {
expect(logger.warn).to.have.been.calledOnceWithExactly(
'Unexpected message received before handshake',
{a: ACTIONS.subscribe, c: 'dogs', d: 'fido'}
);
done();
});
});
it('does not warn when messages are sent after the handshake', function(done) {
var socket = new StreamSocket();
var stream = socket.stream;
var agent = backend.listen(stream);
sinon.spy(logger, 'warn');
var connection = new Connection(socket);
socket._open();
connection.once('connected', function() {
socket.send(JSON.stringify({a: ACTIONS.subscribe, c: 'dogs', d: 'fido'}));
expect(logger.warn).not.to.have.been.called;
expect(agent._firstReceivedMessage).to.be.null;
done();
});
});
it('does not warn for clients on protocol v1.0', function(done) {
backend.use('receive', function(request, next) {
var error = null;
if (request.data.a === ACTIONS.handshake) error = new Error('Unexpected handshake');
next(error);
});
var socket = new StreamSocket();
var stream = socket.stream;
backend.listen(stream);
sinon.spy(logger, 'warn');
socket.send(JSON.stringify({a: ACTIONS.subscribe, c: 'dogs', d: 'fido'}));
var connection = new LegacyConnection(socket);
socket._open();
connection.get('dogs', 'fido').fetch(function(error) {
if (error) return done(error);
expect(logger.warn).not.to.have.been.called;
done();
});
});
it('records the client protocol on the agent', function(done) {
var connection = backend.connect();
connection.once('connected', function() {
expect(connection.agent.protocol).to.eql({
major: protocol.major,
minor: protocol.minor
});
done();
});
});
});
});