From 745aa7d409b09aab615e28907ce254e9c8010fa4 Mon Sep 17 00:00:00 2001 From: winniehell Date: Fri, 4 Mar 2016 22:57:01 +0100 Subject: [PATCH] filter out empty responses in mockResponse --- lib/helpers/util.js | 11 +++++------ lib/mock/index.js | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/helpers/util.js b/lib/helpers/util.js index fb27d60..039d587 100644 --- a/lib/helpers/util.js +++ b/lib/helpers/util.js @@ -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); diff --git a/lib/mock/index.js b/lib/mock/index.js index 0c2f0bf..2d74db4 100644 --- a/lib/mock/index.js +++ b/lib/mock/index.js @@ -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; + } } } @@ -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));