Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Commit 8d8fa44

Browse files
committed
Adding tests and fix for websockets in sub-apps
1 parent eeeb0a0 commit 8d8fa44

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

src/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,25 @@ export default function(config, configurer) {
1010
return function() {
1111
const app = this;
1212

13-
app.configure(socket);
13+
app.configure(socket('primus'));
1414

1515
// Monkey patch app.setup(server)
1616
Proto.mixin({
1717
setup(server) {
1818
debug('Setting up Primus');
1919

20-
const primus = this.primus = new Primus(server, config);
20+
let primus = this.primus;
2121

22-
primus.use('emitter', Emitter);
22+
if(!primus) {
23+
primus = this.primus = new Primus(server, config);
2324

24-
primus.before('feathers', function(req, res, next) {
25-
req.feathers = { provider: 'primus' };
26-
next();
27-
}, 0);
25+
primus.use('emitter', Emitter);
26+
27+
primus.before('feathers', function(req, res, next) {
28+
req.feathers = { provider: 'primus' };
29+
next();
30+
}, 0);
31+
}
2832

2933
if (typeof configurer === 'function') {
3034
debug('Calling Primus configuration function');

test/sub-app.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import feathers from 'feathers';
2+
import primus from '../src';
3+
import memory from 'feathers-memory';
4+
5+
function todoService() {
6+
return memory().extend({
7+
get: function(id, params, callback) {
8+
if(params.query.error) {
9+
return callback(new Error('Something went wrong'));
10+
}
11+
12+
return this._super(id, params).then(
13+
data => Object.assign({ query: params.query }, data)
14+
);
15+
}
16+
});
17+
}
18+
19+
export default function(callback) {
20+
const options = {
21+
transformer: 'websockets'
22+
};
23+
const app = feathers().configure(primus(options, primus => callback(primus)));
24+
const v1 = feathers().configure(primus(options)).use('/todos', todoService());
25+
const v2 = feathers().configure(primus(options)).use('/todos', todoService());
26+
27+
app.use('/api/v1', v1);
28+
app.use('/api/v2', v2);
29+
30+
v1.service('todos').create({ text: 'some todo', complete: false }, {}, function() {});
31+
v2.service('todos').create({ text: 'some todo', complete: false }, {}, function() {});
32+
33+
return app;
34+
}

test/sub-app.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import feathers from 'feathers/client';
2+
import baseTests from 'feathers-commons/lib/test/client';
3+
4+
import server from './sub-app';
5+
import primus from '../client';
6+
7+
describe('Sub Apps', function() {
8+
const app = feathers().configure(primus({}, { timeout: 500 }));
9+
const v1Service = app.service('api/v1/todos');
10+
const v2Service = app.service('api/v2/todos');
11+
12+
before(function(done) {
13+
this.server = server(primus => {
14+
const connection = this.socket = new primus.Socket('http://localhost:14014');
15+
v1Service.connection = connection;
16+
v2Service.connection = connection;
17+
}).listen(14014);
18+
19+
this.server.on('listening', done);
20+
});
21+
22+
after(function(done) {
23+
this.socket.socket.close();
24+
this.server.close(done);
25+
});
26+
27+
describe('First Sub App', () => {
28+
baseTests(v1Service);
29+
});
30+
31+
describe('Second Sub App', () => {
32+
baseTests(v2Service);
33+
});
34+
});

0 commit comments

Comments
 (0)