overleaf/services/document-updater/test/acceptance/coffee/helpers/MockWebApi.coffee

55 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
express = require("express")
2020-03-19 11:20:32 -04:00
bodyParser = require("body-parser")
2014-02-12 05:40:42 -05:00
app = express()
2019-11-14 11:32:59 -05:00
MAX_REQUEST_SIZE = 2*(2*1024*1024 + 64*1024)
2014-02-12 05:40:42 -05:00
module.exports = MockWebApi =
docs: {}
clearDocs: () -> @docs = {}
insertDoc: (project_id, doc_id, doc) ->
doc.version ?= 0
doc.lines ?= []
2017-10-03 10:34:07 -04:00
doc.pathname = '/a/b/c.tex'
2014-02-12 05:40:42 -05:00
@docs["#{project_id}:#{doc_id}"] = doc
setDocument: (project_id, doc_id, lines, version, ranges, lastUpdatedAt, lastUpdatedBy, callback = (error) ->) ->
doc = @docs["#{project_id}:#{doc_id}"] ||= {}
doc.lines = lines
doc.version = version
doc.ranges = ranges
2017-10-03 10:34:07 -04:00
doc.pathname = '/a/b/c.tex'
doc.lastUpdatedAt = lastUpdatedAt
doc.lastUpdatedBy = lastUpdatedBy
2014-02-12 05:40:42 -05:00
callback null
getDocument: (project_id, doc_id, callback = (error, doc) ->) ->
callback null, @docs["#{project_id}:#{doc_id}"]
run: () ->
app.get "/project/:project_id/doc/:doc_id", (req, res, next) =>
@getDocument req.params.project_id, req.params.doc_id, (error, doc) ->
if error?
2020-03-19 11:39:57 -04:00
res.sendStatus 500
2014-02-12 05:40:42 -05:00
else if doc?
res.send JSON.stringify doc
else
2020-03-19 11:39:57 -04:00
res.sendStatus 404
2014-02-12 05:40:42 -05:00
2020-03-19 11:39:57 -04:00
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?
2020-03-19 11:39:57 -04:00
res.sendStatus 500
else
2020-03-19 11:39:57 -04:00
res.sendStatus 204
2014-02-12 05:40:42 -05:00
app.listen 3000, (error) ->
throw error if error?
2017-10-20 09:56:12 -04:00
.on "error", (error) ->
console.error "error starting MockWebApi:", error.message
process.exit(1)
2014-02-12 05:40:42 -05:00
MockWebApi.run()