forked from apigee-127/swagger-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
125 lines (102 loc) · 3.67 KB
/
Copy pathhelpers.js
File metadata and controls
125 lines (102 loc) · 3.67 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Apigee Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var _ = require('lodash-compat');
var assert = require('assert');
var cp = require('child_process');
var path = require('path');
var swagger = require('../');
var errorHandler = module.exports.errorHandler = function () {
return function (err, req, res, next) {
if (err) {
if (res.statusCode < 400) {
res.statusCode = 500;
}
// Useful for debugging
// console.log(err);
// console.log(err.stack);
// if (err.results) {
// console.log(JSON.stringify(err.results, null, 2));
// }
res.end(err.message);
return next();
} else {
return next();
}
};
};
module.exports.createServer = function (initArgs, options, callback) {
var app = require('connect')();
var serverInit = function (middleware) {
var handler = options.handler || function (req, res) {
res.end('OK');
};
function register (middleware) {
if (_.isUndefined(options.mountPoint)) {
app.use(middleware);
} else {
app.use(options.mountPoint, middleware);
}
}
register(middleware.swaggerMetadata());
// Conditionally enable security (To avoid having to rewrite all Swagger documents or all tests)
if (Object.keys(options.swaggerSecurityOptions || {}).length > 0) {
register(middleware.swaggerSecurity(options.swaggerSecurityOptions));
}
register(middleware.swaggerValidator(options.swaggerValidatorOptions));
register(middleware.swaggerRouter(options.swaggerRouterOptions));
register(middleware.swaggerUi(options.swaggerUiOptions));
register(handler);
// Error handler middleware to pass errors downstream as JSON
app.use(errorHandler());
callback(app);
};
initArgs.push(serverInit);
swagger.initializeMiddleware.apply(undefined, initArgs);
};
var prepareText = module.exports.prepareText = function (text) {
return text.replace(/ /g, ' ').replace(/\n/g, '');
};
module.exports.expectContent = function (content, done) {
return function (err, res) {
if (err) {
throw err;
}
if (_.isArray(content) || _.isPlainObject(content)) {
assert.deepEqual(JSON.parse(prepareText(res.text)), content);
} else {
assert.equal(prepareText(res.text), content);
}
if (_.isFunction(done)) {
done();
}
};
};
module.exports.executeCLI = function (args, done) {
// Add Node args
args.unshift('node', path.resolve(path.join(__dirname, '..', 'bin', 'swagger-tools')));
cp.exec(args.join(' '), function (err, stdout, stderr) {
done(stderr, stdout);
});
};