-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
304 lines (243 loc) · 8.45 KB
/
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* istanbul ignore next */
describe('fortress maximus', function () {
'use strict';
var emit = require('primus-emit')
, assume = require('assume')
, Primus = require('primus')
, fortress = require('./');
var port = 1024
, primus
, http;
beforeEach(function each(next) {
http = require('http').createServer();
primus = new Primus(http, {
transformer: 'websockets'
});
primus.plugin('fortess maximus', fortress);
primus.plugin('emit', emit);
http.port = port++;
http.url = 'http://localhost:'+ http.port;
http.listen(http.port, next);
});
afterEach(function each(next) {
primus.destroy(next);
});
it('adds a validate function', function () {
assume(primus.validate).to.be.a('function');
});
it('adds invalid as reserved event', function () {
assume(primus.reserved('invalid')).to.be.true();
});
it('emits an invalid event when the event to emit is reserved', function (next) {
var primus = new Primus(http, { fortress: 'primus' });
primus.plugin('fortess maximus', fortress);
primus.plugin('emit', emit);
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('reserved');
assume(args).to.be.a('array');
primus.destroy(next);
});
var client = new primus.Socket(http.url);
client.emit('connection');
});
it('emits an invalid event when there are no listeners', function (next) {
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('Missing listener');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('emits an invalid event when there are no listeners (write)', function (next) {
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('Missing listener');
assume(args).is.a('array');
next();
});
var client = new primus.Socket(http.url);
client.write('data');
});
it('emits an invalid event when there are no validators', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function () {
throw new Error('I should have failed');
});
});
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('Missing validator');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
describe('.validate', function () {
it('returns this', function () {
assume(primus.validate('data', function (msg, callback) {})).to.equal(primus);
});
it('adds a new event listener', function () {
assume(primus.listeners('fortress:maximus::data')).to.have.length(0);
primus.validate('data', function (msg, callback) {});
assume(primus.listeners('fortress:maximus::data')).to.have.length(1);
});
it('adds it as a private event', function () {
assume(primus.reserved('fortress:maximus::ihavenolife')).to.equal(false);
primus.validate('ihavenolife', function (data, callback) {});
assume(primus.reserved('fortress:maximus::ihavenolife')).to.equal(true);
});
it('emits the event when the validator does not return an error', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', next);
});
primus.on('invalid', function () {
throw new Error('Fucked');
});
primus.validate('custom', function (next) {
assume(next).to.be.a('function');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('does not emit the event when the validator returns an error', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function () {
throw new Error('I should have failed');
});
});
primus.validate('custom', function (validates) {
assume(validates).to.be.a('function');
validates(new Error('failed'));
});
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('failed');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('emits an invalid event when args are missing', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function () {
throw new Error('I should have failed');
});
});
primus.validate('custom', function (foo, validates) {
assume(validates).to.be.a('function');
validates();
});
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('Missing arguments');
assume(err.event).to.equal('custom');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('uses a custom error instance when the validator returns false', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function () {
throw new Error('I should have failed');
});
});
primus.validate('custom', function (validates) {
validates(false);
});
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('custom');
assume(err.event).to.equal('custom');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('uses a proper error instance when the validator returns a string', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function () {
throw new Error('I should have failed');
});
});
primus.validate('custom', function (validates) {
validates('a string is not an error');
});
primus.on('invalid', function (err, args) {
assume(err.message).to.contain('string is not an error');
assume(err.event).to.equal('custom');
assume(args).to.be.a('array');
next();
});
var client = new primus.Socket(http.url);
client.emit('custom');
});
it('receives the emitted args', function (next) {
primus.on('connection', function (spark) {
spark.on('custom', function (foo, bar) {
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
next();
});
});
primus.validate('custom', function (foo, bar, validates) {
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
validates();
});
var client = new primus.Socket(http.url);
client.emit('custom', 'foo', 'bar');
});
it('is called with the right context (spark)', function (next) {
var validates = 0
, sparky;
primus.on('connection', function (spark) {
sparky = spark;
spark.foo = 'bar';
spark.on('custom', function (foo, bar) {
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
assume(validates).to.equal(1);
next();
});
});
primus.validate('custom', function (foo, bar, valid) {
assume(this.foo).equals('bar');
assume(this).equals(sparky);
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
validates++;
valid();
});
var client = new primus.Socket(http.url);
client.emit('custom', 'foo', 'bar');
});
it('is called with the right context (primus)', function (next) {
var primus = new Primus(http, { fortress: 'primus' })
, validates = 0
, sparky;
primus.plugin('fortess maximus', fortress);
primus.plugin('emit', require('primus-emit/broadcast'));
primus.on('connection', function (spark) {
sparky = spark;
});
primus.on('custom', function (spark, foo, bar) {
assume(spark).equals(sparky);
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
assume(validates).to.equal(1);
primus.destroy(next);
});
primus.validate('custom', function (foo, bar, valid) {
assume(this).equals(primus);
assume(foo).to.equal('foo');
assume(bar).to.equal('bar');
validates++;
valid();
});
var client = new primus.Socket(http.url);
client.emit('custom', 'foo', 'bar');
});
});
});