diff --git a/lib/mock/index.js b/lib/mock/index.js index d6e3931..a0fe48d 100644 --- a/lib/mock/index.js +++ b/lib/mock/index.js @@ -51,52 +51,58 @@ function mock(context, router, dataStore) { * Determines the best Response object for this request and sets `res.swagger` to a {@link SemanticRequest} object. * Also sets `res.statusCode` if it isn't already set. */ - function mockResponse(req, res, next) { - if (util.isSwaggerRequest(req) && !isDisabled()) { - var response; - - // Is there already a statusCode? (perhaps set by third-party middleware) - if (res.statusCode && req.swagger.operation.responses[res.statusCode]) { - util.debug('Using %s response for %s %s', res.statusCode, req.method, req.path); - response = req.swagger.operation.responses[res.statusCode]; - } - else { - // Use the first response with a 2XX or 3XX code (or "default") - var responses = util.getResponsesBetween(req.swagger.operation, 200, 399); - if (responses.length > 0) { - response = responses[0].api; - - // Set the response status code - if (_.isNumber(responses[0].code)) { - util.debug('Using %s response for %s %s', responses[0].code, req.method, req.path); - res.status(responses[0].code); + function mockResponse (req, res, next) { + if (!util.isSwaggerRequest(req) || + isDisabled() || + _.isEmpty(req.swagger.operation.responses) + ) { + next(); + return; + } + + var response; + + // Is there already a statusCode? (perhaps set by third-party middleware) + if (res.statusCode && req.swagger.operation.responses[res.statusCode]) { + util.debug('Using %s response for %s %s', res.statusCode, req.method, req.path); + response = req.swagger.operation.responses[res.statusCode]; + } + else { + // Use the first response with a 2XX or 3XX code (or "default") + var responses = util.getResponsesBetween(req.swagger.operation, 200, 399); + if (responses.length > 0) { + response = responses[0].api; + + // Set the response status code + if (_.isNumber(responses[0].code)) { + util.debug('Using %s response for %s %s', responses[0].code, req.method, req.path); + res.status(responses[0].code); + } + else { + if (req.method === 'POST' || req.method === 'PUT') { + res.status(201); + } + else if (req.method === 'DELETE' && !responses[0].api.schema) { + res.status(204); } else { - if (req.method === 'POST' || req.method === 'PUT') { - res.status(201); - } - else if (req.method === 'DELETE' && !responses[0].api.schema) { - res.status(204); - } - else { - res.status(200); - } - util.debug('Using %s (%d) response for %s %s', responses[0].code, res.statusCode, req.method, req.path); + res.status(200); } - } - 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])); + util.debug('Using %s (%d) response for %s %s', responses[0].code, res.statusCode, req.method, req.path); } } - - // The rest of the Mock middleware will use this ResponseMetadata object - res.swagger = new SemanticResponse(response, req.swagger.path); + 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])); + } } + // The rest of the Mock middleware will use this ResponseMetadata object + res.swagger = new SemanticResponse(response, req.swagger.path); + next(); }