diff --git a/lib/routes/routeBackbeat.js b/lib/routes/routeBackbeat.js index 8f0158e9e7..eea6c4dda4 100644 --- a/lib/routes/routeBackbeat.js +++ b/lib/routes/routeBackbeat.js @@ -1310,38 +1310,37 @@ const indexingSchema = joi.array().items(indexEntrySchema).min(1); function respondToRequest(err, response, log, callback) { responseJSONBody(err, null, response, log); - return callback(err); + // The callback is optional, as it is only used for testing purposes + // but value may be set to non-undefined or null due to the arsenal + // routes implementation + if (callback && typeof callback === 'function') { + return callback(err); + } + return undefined; } function routeIndexingAPIs(request, response, userInfo, log, callback) { const route = backbeatRoutes[request.method][request.resourceType]; if (!['GET', 'POST'].includes(request.method)) { - responseJSONBody(errors.MethodNotAllowed, null, response, log); - return callback(errors.MethodNotAllowed); + return respondToRequest(errors.MethodNotAllowed, response, log, callback); } if (request.method === 'GET') { - return route(request, response, userInfo, log, err => { - if (err) { - responseJSONBody(err, null, response, log); - } - return callback(err); - }); + return route(request, response, userInfo, log, err => + respondToRequest(err, response, log, callback)); } const op = request.query.operation; if (!op || typeof route[op] !== 'function') { log.error('Invalid operataion parameter', { operation: op }); - responseJSONBody(errors.BadRequest, null, response, log); - return callback(errors.BadRequest); + return respondToRequest(errors.BadRequest, response, log, callback); } return _getRequestPayload(request, (err, payload) => { if (err) { - responseJSONBody(err, null, response, log); - return callback(err); + return respondToRequest(err, response, log, callback); } let parsedIndex; @@ -1350,16 +1349,11 @@ function routeIndexingAPIs(request, response, userInfo, log, callback) { parsedIndex = joi.attempt(JSON.parse(payload), indexingSchema, 'invalid payload'); } catch (err) { log.error('Unable to parse index request body', { error: err }); - responseJSONBody(errors.BadRequest, null, response, log); - return callback(errors.BadRequest); + return respondToRequest(errors.BadRequest, response, log, callback); } - return route[op](parsedIndex, request, response, userInfo, log, err => { - if (err) { - responseJSONBody(err, null, response, log); - } - return callback(err); - }); + return route[op](parsedIndex, request, response, userInfo, log, err => + respondToRequest(err, response, log, callback)); }); } @@ -1477,8 +1471,7 @@ function routeBackbeat(clientIP, request, response, log, callback) { resourceType: request.resourceType, query: request.query, }); - responseJSONBody(errors.MethodNotAllowed, null, response, log); - return callback(errors.MethodNotAllowed); + return respondToRequest(errors.MethodNotAllowed, response, log, callback); } return async.waterfall([ diff --git a/tests/unit/routeBackbeat.js b/tests/unit/routeBackbeat.js index 42e57c6909..8dc881f258 100644 --- a/tests/unit/routeBackbeat.js +++ b/tests/unit/routeBackbeat.js @@ -23,8 +23,6 @@ const testBucket = { namespace, headers: { 'host': `${bucketName}.s3.amazonaws.com`, - // set versioning - }, url: `/${bucketName}`, actionImplicitDenies: false, @@ -47,7 +45,6 @@ describe('routeBackbeat', () => { let response; beforeEach(() => { - // Mock backbeatRoutes sinon.stub(backbeatRoutes, 'PUT').returns({ data: sinon.stub(), metadata: sinon.stub(), @@ -86,7 +83,6 @@ describe('routeBackbeat', () => { index: sinon.stub(), }); - // Mock request and response request = new DummyRequest( { method: 'GET', @@ -131,7 +127,6 @@ describe('routeBackbeat', () => { target: `${bucketName}/${objectName}`, operation: null, versionId: false, - // not sending any body here, so expect error expect: errors.MalformedPOSTRequest, }, {