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

Commit

Permalink
handle empty operation responses in mockResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
winniehell committed Mar 4, 2016
1 parent 60377ba commit 2fe55a5
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions lib/mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 2fe55a5

Please sign in to comment.