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
/
tools.spec.js
86 lines (75 loc) · 2.87 KB
/
tools.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
const expect = require('chai').expect;
const injector = require('injectdeps');
const lodash = require('lodash');
const jsYaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const bunyan = require('bunyan');
const logger = require('./test/logger');
const httpMocks = require('node-mocks-http');
const swaggerMetadata = require('swagger-tools/middleware/swagger-metadata');
const swaggerHelpers = require('swagger-tools/lib/helpers');
const swaggerFilepath = path.resolve(__dirname, './test/swagger.yaml');
function standardBindings() {
return injector.getContainer()
.bindName('fs').toPlainObject(fs)
.bindName('path').toPlainObject(path)
.bindName('_').toPlainObject(lodash)
.bindName('js-yaml').toPlainObject(jsYaml)
.bindName('bunyan').toPlainObject(bunyan)
.bindName('logger').toObject(logger)
.bindName('swagger.metadata').toPlainObject(swaggerMetadata)
.bindName('swagger.helpers').toPlainObject(swaggerHelpers)
.bindName('swagger.spec').toObject(require('./yaml-loader'))
.bindName('swagger.tools').toObject(require('./tools'));
}
describe('metadata', () => {
it('should return an appropriate input data coercion middleware', (done) => {
const container = standardBindings()
.bindName('swagger.filePath').toScalarValue(swaggerFilepath);
container
.newObject('swagger.tools')
.catch((err) => {
container.newObject('logger')('test').error(err);
done(err);
})
.then((tools) => {
expect(tools).to.have.a.property('swaggerMetadata');
const request = httpMocks.createRequest({
method: 'GET',
url: '/hello',
query: {
name: 'test',
age: 10
}
});
tools.swaggerMetadata()(request, httpMocks.createResponse(), function() {
expect(request).to.have.a.property('swagger');
expect(request.swagger).to.have.a.property('apiPath').that.equals('/hello');
expect(request.swagger.params.name).to.have.a.property('value').that.equals('test');
expect(request.swagger.params.location).to.have.a.property('value').to.be.undefined;
expect(request.swagger.params).to.not.have.a.property('age');
done();
});
});
});
it('should not add a swagger property to a route not specified in the yaml file', (done) => {
standardBindings()
.bindName('swagger.filePath').toScalarValue(swaggerFilepath)
.newObject('swagger.tools')
.then((tools) => {
const request = httpMocks.createRequest({
method: 'GET',
url: '/hellohello',
query: {
name: 'test',
age: 10
}
});
tools.swaggerMetadata()(request, httpMocks.createResponse(), function() {
expect(request).to.not.have.a.property('swagger');
done();
});
});
});
});