diff --git a/services/document-updater/app.coffee b/services/document-updater/app.coffee index 6ff06f2f2a..3ca7ef2b6f 100644 --- a/services/document-updater/app.coffee +++ b/services/document-updater/app.coffee @@ -28,7 +28,7 @@ Metrics.event_loop.monitor(logger, 100) app = express() app.use(Metrics.http.monitor(logger)); -app.use bodyParser({limit: (Settings.max_doc_length + 64 * 1024)}) +app.use bodyParser.json({limit: (Settings.max_doc_length + 64 * 1024)}) Metrics.injectMetricsRoute(app) DispatchManager.createAndStartDispatchers(Settings.dispatcherCount || 10) @@ -61,7 +61,7 @@ app.post '/project/:project_id/history/resync', HttpCont app.post '/project/:project_id/flush', HttpController.flushProject app.post '/project/:project_id/doc/:doc_id/change/:change_id/accept', HttpController.acceptChanges app.post '/project/:project_id/doc/:doc_id/change/accept', HttpController.acceptChanges -app.del '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment +app.delete '/project/:project_id/doc/:doc_id/comment/:comment_id', HttpController.deleteComment app.get '/flush_all_projects', HttpController.flushAllProjects app.get '/flush_queued_projects', HttpController.flushQueuedProjects @@ -74,7 +74,7 @@ app.get '/total', (req, res)-> app.get '/status', (req, res)-> if Settings.shuttingDown - res.send 503 # Service unavailable + res.sendStatus 503 # Service unavailable else res.send('document updater is alive') @@ -83,18 +83,18 @@ app.get "/health_check/redis", (req, res, next) -> pubsubClient.healthCheck (error) -> if error? logger.err {err: error}, "failed redis health check" - res.send 500 + res.sendStatus 500 else - res.send 200 + res.sendStatus 200 docUpdaterRedisClient = require("redis-sharelatex").createClient(Settings.redis.documentupdater) app.get "/health_check/redis_cluster", (req, res, next) -> docUpdaterRedisClient.healthCheck (error) -> if error? logger.err {err: error}, "failed redis cluster health check" - res.send 500 + res.sendStatus 500 else - res.send 200 + res.sendStatus 200 app.get "/health_check", (req, res, next) -> async.series [ @@ -115,20 +115,20 @@ app.get "/health_check", (req, res, next) -> cb(error) ] , (error) -> if error? - res.send 500 + res.sendStatus 500 else - res.send 200 + res.sendStatus 200 app.use (error, req, res, next) -> if error instanceof Errors.NotFoundError - res.send 404 + res.sendStatus 404 else if error instanceof Errors.OpRangeNotAvailableError - res.send 422 # Unprocessable Entity + res.sendStatus 422 # Unprocessable Entity else if error.statusCode is 413 - res.send(413, "request entity too large") + res.status(413).send("request entity too large") else logger.error err: error, req: req, "request errored" - res.send(500, "Oops, something went wrong") + res.status(500).send("Oops, something went wrong") shutdownCleanly = (signal) -> return () -> diff --git a/services/document-updater/app/coffee/HttpController.coffee b/services/document-updater/app/coffee/HttpController.coffee index b7d38343d4..67d247ab97 100644 --- a/services/document-updater/app/coffee/HttpController.coffee +++ b/services/document-updater/app/coffee/HttpController.coffee @@ -57,7 +57,7 @@ module.exports = HttpController = ProjectManager.getProjectDocsAndFlushIfOld project_id, projectStateHash, excludeVersions, (error, result) -> timer.done() if error instanceof Errors.ProjectStateChangedError - res.send 409 # conflict + res.sendStatus 409 # conflict else if error? return next(error) else @@ -73,7 +73,7 @@ module.exports = HttpController = if error? return next(error) else - res.send 200 + res.sendStatus 200 setDoc: (req, res, next = (error) ->) -> doc_id = req.params.doc_id @@ -82,14 +82,14 @@ module.exports = HttpController = lineSize = HttpController._getTotalSizeOfLines(lines) if lineSize > TWO_MEGABYTES logger.log {project_id, doc_id, source, lineSize, user_id}, "document too large, returning 406 response" - return res.send 406 + return res.sendStatus 406 logger.log {project_id, doc_id, lines, source, user_id, undoing}, "setting doc via http" timer = new Metrics.Timer("http.setDoc") DocumentManager.setDocWithLock project_id, doc_id, lines, source, user_id, undoing, (error) -> timer.done() return next(error) if error? logger.log project_id: project_id, doc_id: doc_id, "set doc via http" - res.send 204 # No Content + res.sendStatus 204 # No Content flushDocIfLoaded: (req, res, next = (error) ->) -> @@ -101,7 +101,7 @@ module.exports = HttpController = timer.done() return next(error) if error? logger.log project_id: project_id, doc_id: doc_id, "flushed doc via http" - res.send 204 # No Content + res.sendStatus 204 # No Content deleteDoc: (req, res, next = (error) ->) -> doc_id = req.params.doc_id @@ -117,7 +117,7 @@ module.exports = HttpController = return next(error) if error? logger.log project_id: project_id, doc_id: doc_id, "deleted doc via http" - res.send 204 # No Content + res.sendStatus 204 # No Content flushProject: (req, res, next = (error) ->) -> project_id = req.params.project_id @@ -127,7 +127,7 @@ module.exports = HttpController = timer.done() return next(error) if error? logger.log project_id: project_id, "flushed project via http" - res.send 204 # No Content + res.sendStatus 204 # No Content deleteProject: (req, res, next = (error) ->) -> project_id = req.params.project_id @@ -139,14 +139,14 @@ module.exports = HttpController = ProjectManager.queueFlushAndDeleteProject project_id, (error) -> return next(error) if error? logger.log project_id: project_id, "queue delete of project via http" - res.send 204 # No Content + res.sendStatus 204 # No Content else timer = new Metrics.Timer("http.deleteProject") ProjectManager.flushAndDeleteProjectWithLocks project_id, options, (error) -> timer.done() return next(error) if error? logger.log project_id: project_id, "deleted project via http" - res.send 204 # No Content + res.sendStatus 204 # No Content deleteMultipleProjects: (req, res, next = (error) ->) -> project_ids = req.body?.project_ids || [] @@ -156,7 +156,7 @@ module.exports = HttpController = ProjectManager.queueFlushAndDeleteProject project_id, cb , (error) -> return next(error) if error? - res.send 204 # No Content + res.sendStatus 204 # No Content acceptChanges: (req, res, next = (error) ->) -> {project_id, doc_id} = req.params @@ -169,7 +169,7 @@ module.exports = HttpController = timer.done() return next(error) if error? logger.log {project_id, doc_id}, "accepted #{ change_ids.length } changes via http" - res.send 204 # No Content + res.sendStatus 204 # No Content deleteComment: (req, res, next = (error) ->) -> {project_id, doc_id, comment_id} = req.params @@ -179,7 +179,7 @@ module.exports = HttpController = timer.done() return next(error) if error? logger.log {project_id, doc_id, comment_id}, "deleted comment via http" - res.send 204 # No Content + res.sendStatus 204 # No Content updateProject: (req, res, next = (error) ->) -> timer = new Metrics.Timer("http.updateProject") @@ -191,7 +191,7 @@ module.exports = HttpController = timer.done() return next(error) if error? logger.log project_id: project_id, "updated project via http" - res.send 204 # No Content + res.sendStatus 204 # No Content resyncProjectHistory: (req, res, next = (error) ->) -> project_id = req.params.project_id @@ -201,7 +201,7 @@ module.exports = HttpController = HistoryManager.resyncProjectHistory project_id, projectHistoryId, docs, files, (error) -> return next(error) if error? logger.log {project_id}, "queued project history resync via http" - res.send 204 + res.sendStatus 204 flushAllProjects: (req, res, next = (error)-> )-> res.setTimeout(5 * 60 * 1000) @@ -212,7 +212,7 @@ module.exports = HttpController = ProjectFlusher.flushAllProjects options, (err, project_ids)-> if err? logger.err err:err, "error bulk flushing projects" - res.send 500 + res.sendStatus 500 else res.send project_ids @@ -225,7 +225,7 @@ module.exports = HttpController = DeleteQueueManager.flushAndDeleteOldProjects options, (err, flushed)-> if err? logger.err err:err, "error flushing old projects" - res.send 500 + res.sendStatus 500 else logger.log {flushed: flushed}, "flush of queued projects completed" res.send {flushed: flushed} diff --git a/services/document-updater/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee b/services/document-updater/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee index 2a0c8603a4..eb635225da 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee @@ -9,9 +9,9 @@ module.exports = MockProjectHistoryApi = app.post "/project/:project_id/flush", (req, res, next) => @flushProject req.params.project_id, (error) -> if error? - res.send 500 + res.sendStatus 500 else - res.send 204 + res.sendStatus 204 app.listen 3054, (error) -> throw error if error? diff --git a/services/document-updater/test/acceptance/coffee/helpers/MockTrackChangesApi.coffee b/services/document-updater/test/acceptance/coffee/helpers/MockTrackChangesApi.coffee index 95caead368..924937fe39 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/MockTrackChangesApi.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/MockTrackChangesApi.coffee @@ -9,9 +9,9 @@ module.exports = MockTrackChangesApi = app.post "/project/:project_id/doc/:doc_id/flush", (req, res, next) => @flushDoc req.params.doc_id, (error) -> if error? - res.send 500 + res.sendStatus 500 else - res.send 204 + res.sendStatus 204 app.listen 3015, (error) -> throw error if error? diff --git a/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee b/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee index daea1dcf40..19b518f7c6 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee @@ -31,18 +31,18 @@ module.exports = MockWebApi = app.get "/project/:project_id/doc/:doc_id", (req, res, next) => @getDocument req.params.project_id, req.params.doc_id, (error, doc) -> if error? - res.send 500 + res.sendStatus 500 else if doc? res.send JSON.stringify doc else - res.send 404 + res.sendStatus 404 - app.post "/project/:project_id/doc/:doc_id", bodyParser({limit: MAX_REQUEST_SIZE}), (req, res, next) => + app.post "/project/:project_id/doc/:doc_id", bodyParser.json({limit: MAX_REQUEST_SIZE}), (req, res, next) => MockWebApi.setDocument req.params.project_id, req.params.doc_id, req.body.lines, req.body.version, req.body.ranges, req.body.lastUpdatedAt, req.body.lastUpdatedBy, (error) -> if error? - res.send 500 + res.sendStatus 500 else - res.send 204 + res.sendStatus 204 app.listen 3000, (error) -> throw error if error? diff --git a/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee b/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee index 00fd16c088..4f316a12ba 100644 --- a/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee +++ b/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee @@ -24,8 +24,9 @@ describe "HttpController", -> @next = sinon.stub() @res = send: sinon.stub() + sendStatus: sinon.stub() json: sinon.stub() - + describe "getDoc", -> beforeEach -> @lines = ["one", "two", "three"] @@ -119,7 +120,7 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true - + describe "setDoc", -> beforeEach -> @lines = ["one", "two", "three"] @@ -147,7 +148,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -179,7 +180,7 @@ describe "HttpController", -> @HttpController.setDoc(@req, @res, @next) it 'should send back a 406 response', -> - @res.send.calledWith(406).should.equal true + @res.sendStatus.calledWith(406).should.equal true it 'should not call setDocWithLock', -> @DocumentManager.setDocWithLock.callCount.should.equal 0 @@ -201,7 +202,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -222,7 +223,7 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true - + describe "flushDocIfLoaded", -> beforeEach -> @lines = ["one", "two", "three"] @@ -243,7 +244,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -289,7 +290,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -313,7 +314,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send.calledWith(204).should.equal true + @res.sendStatus.calledWith(204).should.equal true describe "when an errors occurs", -> beforeEach -> @@ -329,7 +330,7 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true - + describe "deleteProject", -> beforeEach -> @req = @@ -347,7 +348,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -379,7 +380,7 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true - + describe "acceptChanges", -> beforeEach -> @req = @@ -399,7 +400,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -438,7 +439,7 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true - + describe "deleteComment", -> beforeEach -> @req = @@ -458,7 +459,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -524,7 +525,7 @@ describe "HttpController", -> @HttpController.getProjectDocsAndFlushIfOld(@req, @res, @next) it "should return an HTTP 409 Conflict response", -> - @res.send + @res.sendStatus .calledWith(409) .should.equal true @@ -561,7 +562,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true @@ -601,7 +602,7 @@ describe "HttpController", -> .should.equal true it "should return a successful No Content response", -> - @res.send + @res.sendStatus .calledWith(204) .should.equal true