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

123 lines
2.9 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let MockWebApi
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const MAX_REQUEST_SIZE = 2 * (2 * 1024 * 1024 + 64 * 1024)
module.exports = MockWebApi = {
docs: {},
clearDocs() {
return (this.docs = {})
},
insertDoc(project_id, doc_id, doc) {
if (doc.version == null) {
doc.version = 0
}
if (doc.lines == null) {
doc.lines = []
}
doc.pathname = '/a/b/c.tex'
return (this.docs[`${project_id}:${doc_id}`] = doc)
},
setDocument(
project_id,
doc_id,
lines,
version,
ranges,
lastUpdatedAt,
lastUpdatedBy,
callback
) {
if (callback == null) {
callback = function () {}
}
const doc =
this.docs[`${project_id}:${doc_id}`] ||
(this.docs[`${project_id}:${doc_id}`] = {})
doc.lines = lines
doc.version = version
doc.ranges = ranges
doc.pathname = '/a/b/c.tex'
doc.lastUpdatedAt = lastUpdatedAt
doc.lastUpdatedBy = lastUpdatedBy
return callback(null)
},
getDocument(project_id, doc_id, callback) {
if (callback == null) {
callback = function () {}
}
return callback(null, this.docs[`${project_id}:${doc_id}`])
},
run() {
app.get('/project/:project_id/doc/:doc_id', (req, res, next) => {
return this.getDocument(
req.params.project_id,
req.params.doc_id,
(error, doc) => {
if (error != null) {
return res.sendStatus(500)
} else if (doc != null) {
return res.send(JSON.stringify(doc))
} else {
return res.sendStatus(404)
}
}
)
})
app.post(
'/project/:project_id/doc/:doc_id',
bodyParser.json({ limit: MAX_REQUEST_SIZE }),
(req, res, next) => {
return 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,
2021-07-13 11:04:42 +00:00
error => {
if (error != null) {
return res.sendStatus(500)
} else {
return res.json({ rev: '123' })
}
}
)
}
)
return app
2021-07-13 11:04:42 +00:00
.listen(3000, error => {
if (error != null) {
throw error
}
})
2021-07-13 11:04:42 +00:00
.on('error', error => {
console.error('error starting MockWebApi:', error.message)
return process.exit(1)
})
2021-07-13 11:04:42 +00:00
},
}
2014-02-12 10:40:42 +00:00
MockWebApi.run()