Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
filter out empty responses in mockResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
winniehell committed Mar 4, 2016
1 parent dc67f0b commit 745aa7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
11 changes: 5 additions & 6 deletions lib/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,17 @@ exports.getAllowedMethods = function(path) {
* sorted by response code ("default" comes last).
*/
exports.getResponsesBetween = function(operation, min, max) {
return _.map(operation.responses,
function(response, responseCode) {
return _(operation.responses)
.omit(_.isEmpty)
.map(function(response, responseCode) {
return {
code: parseInt(responseCode) || responseCode,
api: response
};
})
.sort(function(a, b) {
.sortBy(function(response) {
// Sort by response code. "default" comes last.
a = _.isNumber(a.code) ? a.code : 999;
b = _.isNumber(b.code) ? b.code : 999;
return a - b;
return _.isNumber(response.code) ? response.code : Number.POSITIVE_INFINITY;
})
.filter(function(response) {
return (response.code >= min && response.code <= max) || _.isString(response.code);
Expand Down
19 changes: 13 additions & 6 deletions lib/mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ function mock(context, router, dataStore) {
}
}
else {
// There is no response with a 2XX or 3XX code, so just use the first one
var keys = Object.keys(req.swagger.operation.responses);
util.debug('Using %s response for %s %s', keys[0], req.method, req.path);
response = req.swagger.operation.responses[keys[0]];
res.status(parseInt(keys[0]));
// There is no response with a 2XX or 3XX code, so just use the first non-empty one
var responseCodes = Object.keys(_.omit(req.swagger.operation.responses, _.isEmpty));

if (responseCodes.length > 0) {
var responseCode = responseCodes[0];
util.debug('Using %s response for %s %s', responseCode, req.method, req.path);
response = req.swagger.operation.responses[responseCode];
res.status(parseInt(responseCode));
} else {
next();
return;
}
}
}

Expand Down Expand Up @@ -162,7 +169,7 @@ function mockResponseHeaders(req, res, next) {
if (!name || !_.isFunction(name.toLowerCase)) {
return;
}

switch (name.toLowerCase()) {
case 'location':
res.location(req.baseUrl + (res.swagger.location || req.path));
Expand Down

0 comments on commit 745aa7d

Please sign in to comment.