Create HttpController for HTTP requestS

This commit is contained in:
James Allen 2014-01-27 18:09:37 +00:00
parent 5dea123b13
commit d4295c2023
3 changed files with 53 additions and 12 deletions

View file

@ -1,19 +1,12 @@
Settings = require "settings-sharelatex" Settings = require "settings-sharelatex"
express = require "express"
app = express()
HistoryManager = require "./app/js/HistoryManager"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
logger.initialize("history") logger.initialize("history")
app.post "/doc/:doc_id/history", express.bodyParser(), (req, res, next) -> HttpController = require "./app/js/HttpController"
doc_id = req.params.doc_id express = require "express"
docOps = req.body.docOps app = express()
version = req.body.version
logger.log doc_id: doc_id, version: version, "compressing doc history" app.post "/doc/:doc_id/history", express.bodyParser(), HttpController.appendUpdates
HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) ->
return next(error) if error?
res.send 204
app.use (error, req, res, next) -> app.use (error, req, res, next) ->
logger.error err: error, "an internal error occured" logger.error err: error, "an internal error occured"

View file

@ -0,0 +1,12 @@
HistoryManager = require "./HistoryManager"
logger = require "logger-sharelatex"
module.exports = HttpController =
appendUpdates: (req, res, next = (error) ->) ->
doc_id = req.params.doc_id
docOps = req.body.docOps
version = req.body.version
logger.log doc_id: doc_id, version: version, "compressing doc history"
HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) ->
return next(error) if error?
res.send 204

View file

@ -0,0 +1,36 @@
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/HttpController.js"
SandboxedModule = require('sandboxed-module')
describe "HttpController", ->
beforeEach ->
@HttpController = SandboxedModule.require modulePath, requires:
"logger-sharelatex": { log: sinon.stub() }
"./HistoryManager": @HistoryManager = {}
@doc_id = "doc-id-123"
@version = 42
@next = sinon.stub()
describe "appendUpdates", ->
beforeEach ->
@req =
params:
doc_id: @doc_id
body:
docOps: @docOps = ["mock-ops"]
version: @version
@res =
send: sinon.stub()
@HistoryManager.compressAndSaveRawUpdates = sinon.stub().callsArg(2)
@HttpController.appendUpdates @req, @res, @next
it "should append the updates", ->
@HistoryManager.compressAndSaveRawUpdates
.calledWith(@doc_id, @docOps)
.should.equal true
it "should return a success code", ->
@res.send.calledWith(204).should.equal true