This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.spec.js
101 lines (85 loc) · 3.05 KB
/
router.spec.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
const expect = require('chai').expect;
const injector = require('injectdeps');
const bunyan = require('bunyan');
const logger = require('./test/logger');
const sinon = require('sinon');
require('chai').use(require('sinon-chai'));
const httpMocks = require('node-mocks-http');
function standardBindings() {
return injector.getContainer()
.bindName('container').toContainer()
.bindName('bunyan').toPlainObject(bunyan)
.bindName('logger').toObject(logger)
.bindName('router').toObject(require('./router'));
}
function helloRequestNoSwagger(){
return httpMocks.createRequest({
method: 'GET',
url: '/hello',
});
}
function helloRequestWithSwagger() {
let req = helloRequestNoSwagger();
req.swagger = {
path:{
"x-swagger-router-controller": "helloWorld"
},
operation: {
operationId: 'hello'
}
};
return req;
}
describe('router', () => {
it('should call the handler for a valid path', (done) => {
const controller = { hello: sinon.stub().callsArg(2) };
const container = standardBindings()
.bindName('controller.helloWorld').toPlainObject(controller);
const request = helloRequestWithSwagger();
const router = container.newObject('router')({ prefix: 'controller.' });
router(request, httpMocks.createResponse(), function(err) {
if (err) {
return done(err);
}
expect(controller.hello).to.have.been.calledOnce;
done();
});
});
it('should call next() with an error if the path is not supported', (done) => {
const controller = { hello: sinon.stub().callsArg(2) };
const container = standardBindings()
.bindName('controller.helloWorld').toPlainObject(controller);
const request = helloRequestNoSwagger();
const response = httpMocks.createResponse();
const router = container.newObject('router')({ prefix: 'controller.' });
router(request, response, function(err) {
expect(err).to.be.an('error');
expect(controller.hello).to.not.have.been.called;
expect(err.statusCode).to.equal(404);
done();
});
});
it('should call next() with an error if the controller is not found', (done) => {
const controller = { hello: sinon.stub().callsArg(2) };
const container = standardBindings();
const request = helloRequestWithSwagger();
const response = httpMocks.createResponse();
const router = container.newObject('router')({ prefix: 'controller.' });
router(request, response, function(err) {
expect(err).to.be.an('error');
expect(controller.hello).to.not.have.been.called;
done();
});
});
it('should call next() with an error if the operationId is not supported', (done) => {
const controller = { hello: true };
const container = standardBindings()
.bindName('controller.helloWorld').toPlainObject(controller);
const request = helloRequestWithSwagger();
const router = container.newObject('router')({ prefix: 'controller.' });
router(request, httpMocks.createResponse(), function(err) {
expect(err).to.be.an('error');
done();
});
});
});