This repository was archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.test.js
More file actions
208 lines (173 loc) · 5.69 KB
/
index.test.js
File metadata and controls
208 lines (173 loc) · 5.69 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
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
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const assert = require('assert');
const request = require('request');
const _ = require('lodash');
const { Service } = require('feathers-commons/lib/test-fixture');
const primus = require('../lib');
const methodTests = require('./methods.js');
const eventTests = require('./events');
describe('@feathersjs/primus', () => {
let options = {
socketParams: {
user: { name: 'David' },
provider: 'primus'
}
};
before(done => {
const errorHook = function (hook) {
if (hook.params.query.hookError) {
throw new Error(`Error from ${hook.method}, ${hook.type} hook`);
}
};
const app = options.app = feathers()
.configure(primus({
transformer: 'uws'
}, function (primus) {
primus.authorize(function (req, done) {
req.feathers.user = { name: 'David' };
const { channel } = req.query;
if (channel) {
req.feathers.channel = channel;
}
done();
});
options.primus = primus;
options.socket = new primus.Socket('http://localhost:7888');
}))
.use('todo', Service);
app.service('todo').hooks({
before: { get: errorHook }
});
options.server = app.listen(7888, function () {
app.use('tasks', Service);
app.service('tasks').hooks({
before: { get: errorHook }
});
done();
});
});
after(done => {
options.socket.socket.close();
options.server.close(done);
});
it('exports default and SOCKET_KEY', () => {
assert.ok(primus.SOCKET_KEY);
assert.equal(primus, primus.default);
});
it('is CommonJS compatible', () => {
assert.equal(typeof require('../lib'), 'function');
});
it('throws an error when using an incompatible version of Feathers', () => {
const oldFeathers = require('feathers');
try {
oldFeathers().configure(primus());
assert.ok(false, 'Should never get here');
} catch (e) {
assert.equal(e.message, '@feathersjs/primus is not compatible with this version of Feathers. Use the latest at @feathersjs/feathers.');
}
});
it('runs primus before setup (#131)', done => {
let counter = 0;
const app = feathers()
.configure(primus({
transformer: 'uws'
}, function () {
assert.equal(counter, 0);
counter++;
}))
.use('/todos', {
find () {
return Promise.resolve([]);
},
setup (app) {
assert.ok(app.primus);
assert.equal(counter, 1, 'Primus configuration ran first');
}
});
const srv = app.listen(9119);
srv.on('listening', () => srv.close(done));
});
it('expressified app works', done => {
const data = { message: 'Hello world' };
const app = express(feathers())
.configure(primus({
transformer: 'uws'
}))
.use('/test', (req, res) => res.json(data));
const srv = app.listen(8992).on('listening', () => {
const url = 'http://localhost:8992/test';
request({ url, json: true }, (err, res) => {
assert.ok(!err);
assert.deepEqual(res.body, data);
srv.close(done);
});
});
});
it('Passes handshake as service parameters.', function (done) {
const service = options.app.service('todo');
const old = {
find: service.find,
create: service.create,
update: service.update,
remove: service.remove
};
service.find = function (params) {
assert.deepEqual(_.omit(params, 'query', 'route', 'connection'), options.socketParams,
'Handshake parameters passed on proper position');
return old.find.apply(this, arguments);
};
service.create = function (data, params) {
assert.deepEqual(_.omit(params, 'query', 'route', 'connection'), options.socketParams,
'Passed handshake parameters');
return old.create.apply(this, arguments);
};
service.update = function (id, data, params) {
assert.deepEqual(params, _.extend({
connection: options.socketParams,
route: {},
query: {
test: 'param'
}
}, options.socketParams), 'Passed handshake parameters as query');
return old.update.apply(this, arguments);
};
options.socket.send('create', 'todo', {}, {}, error => {
assert.ok(!error);
options.socket.send('update', 'todo', 1, {}, { test: 'param' }, () => {
assert.ok(!error);
_.extend(service, old);
done();
});
});
});
it('Missing parameters in socket call works. (#88)', function (done) {
const service = options.app.service('todo');
const old = {
find: service.find
};
service.find = function (params) {
assert.deepEqual(_.omit(params, 'query', 'route', 'connection'), options.socketParams,
'Handshake parameters passed on proper position');
return old.find.apply(this, arguments);
};
options.socket.send('find', 'todo', function () {
_.extend(service, old);
done();
});
});
describe('Service method calls', () => {
describe('(\'method\', \'service\') event format', () => {
describe('Service', () => methodTests('todo', options));
describe('Dynamic Service', () => methodTests('todo', options));
});
describe('(\'service::method\') legacy event format', () => {
describe('Service', () => methodTests('tasks', options, true));
describe('Dynamic Service', () => methodTests('tasks', options, true));
});
});
describe('Service events', () => {
describe('Service', () => eventTests('todo', options));
describe('Dynamic Service', () => eventTests('tasks', options));
});
});