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
176 lines (150 loc) · 4.45 KB
/
index.test.js
File metadata and controls
176 lines (150 loc) · 4.45 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
import assert from 'assert';
import feathers from 'feathers';
import hooks from 'feathers-hooks';
import _ from 'lodash';
import { Service as todoService, verify } from 'feathers-commons/lib/test-fixture';
import services from './service.test.js';
import primus from '../src';
describe('feathers-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(hooks())
.configure(primus({
transformer: 'uws'
}, function (primus) {
options.socket = new primus.Socket('http://localhost:7888');
primus.authorize(function (req, done) {
req.feathers.user = { name: 'David' };
done();
});
}))
.use('todo', todoService);
app.service('todo').before({
get: errorHook
});
options.server = app.listen(7888, function () {
app.use('tasks', todoService);
app.service('tasks').before({
get: errorHook
});
done();
});
});
after(done => {
options.socket.socket.close();
options.server.close(done);
});
it('is CommonJS compatible', () => {
assert.equal(typeof require('../lib'), 'function');
});
it('runs primus before setup (#131)', function (done) {
var counter = 0;
var app = feathers()
.configure(primus({
transformer: 'uws'
}, function () {
assert.equal(counter, 0);
counter++;
}))
.use('/todos', {
find: function (params, callback) {
callback(null, []);
},
setup: function (app) {
assert.ok(app.primus);
assert.equal(counter, 1, 'SocketIO configuration ran first');
}
});
var srv = app.listen(9119);
srv.on('listening', function () {
srv.close(done);
});
});
it('Passes handshake as service parameters.', function (done) {
var service = options.app.service('todo');
var old = {
find: service.find,
create: service.create,
update: service.update,
remove: service.remove
};
service.find = function (params) {
assert.deepEqual(_.omit(params, 'query'), options.socketParams,
'Handshake parameters passed on proper position');
old.find.apply(this, arguments);
};
service.create = function (data, params) {
assert.deepEqual(_.omit(params, 'query'), options.socketParams,
'Passed handshake parameters');
old.create.apply(this, arguments);
};
service.update = function (id, data, params) {
assert.deepEqual(params, _.extend({
query: {
test: 'param'
}
}, options.socketParams), 'Passed handshake parameters as query');
old.update.apply(this, arguments);
};
options.socket.send('todo::create', {}, {}, function () {
options.socket.send('todo::update', 1, {}, { test: 'param' }, function () {
_.extend(service, old);
done();
});
});
});
it('Missing parameters in socket call works. (#88)', function (done) {
var service = options.app.service('todo');
var old = {
find: service.find
};
service.find = function (params) {
assert.deepEqual(_.omit(params, 'query'), options.socketParams,
'Handshake parameters passed on proper position');
old.find.apply(this, arguments);
};
options.socket.send('todo::find', function () {
_.extend(service, old);
done();
});
});
it('uses mountpath for sub-apps and calls their setup', done => {
let server;
const sub = feathers()
.configure(primus({
transformer: 'uws'
}, function (primus) {
const socket = new primus.Socket('http://localhost:9876');
const original = {
name: 'creating'
};
socket.once('v1/todo created', data => {
verify.create(original, data);
socket.socket.close();
server.close(done);
});
socket.send('v1/todo::create', original);
}))
.use('/todo', todoService);
const main = feathers()
.use('/v1', sub);
server = main.listen(9876);
});
describe('Services', function () {
services('todo', options);
});
describe('Dynamic services', function () {
services('tasks', options);
});
});