added raw endpoint to docstore

This commit is contained in:
Henry Oswald 2014-05-20 13:04:33 +01:00
parent 7cca4f0368
commit 973d9b8643
3 changed files with 39 additions and 3 deletions

View file

@ -17,6 +17,7 @@ app.use Metrics.http.monitor(logger)
app.get '/project/:project_id/doc', HttpController.getAllDocs
app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc
app.get '/project/:project_id/doc/:doc_id/raw', HttpController.getRawDoc
app.post '/project/:project_id/doc/:doc_id', bodyParser.json(limit: "2mb"), HttpController.updateDoc
app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc

View file

@ -13,6 +13,18 @@ module.exports = HttpController =
else
res.json HttpController._buildDocView(doc)
getRawDoc: (req, res, next = (error)->)->
project_id = req.params.project_id
doc_id = req.params.doc_id
logger.log project_id: project_id, doc_id: doc_id, "getting raw doc"
DocManager.getDoc project_id, doc_id, (error, doc) ->
return next(error) if error?
if !doc?
res.send 404
else
res.setHeader('content-type', 'text/plain')
res.send HttpController._buildRawDocView(doc)
getAllDocs: (req, res, next = (error) ->) ->
project_id = req.params.project_id
logger.log project_id: project_id, "getting all docs"
@ -57,4 +69,7 @@ module.exports = HttpController =
_id: doc._id.toString()
lines: doc.lines
rev: doc.rev
}
}
_buildRawDocView: (doc)->
return doc.lines.join("\n")

View file

@ -1,4 +1,5 @@
SandboxedModule = require('sandboxed-module')
assert = require("chai").assert
sinon = require('sinon')
chai = require('chai')
chai.should()
@ -11,14 +12,14 @@ describe "HttpController", ->
@HttpController = SandboxedModule.require modulePath, requires:
"./DocManager": @DocManager = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
@res = { send: sinon.stub(), json: sinon.stub() }
@res = { send: sinon.stub(), json: sinon.stub(), setHeader:sinon.stub() }
@req = {}
@next = sinon.stub()
@project_id = "mock-project-id"
@doc_id = "mock-doc-id"
@doc = {
_id: @doc_id
lines: ["mock", "lines"]
lines: ["mock", "lines", " here", "", "", " spaces "]
version: 42
rev: 5
}
@ -45,6 +46,25 @@ describe "HttpController", ->
})
.should.equal true
describe "getRawDoc", ->
beforeEach ->
@req.params =
project_id: @project_id
doc_id: @doc_id
@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
@HttpController.getRawDoc @req, @res, @next
it "should get the document", ->
@DocManager.getDoc
.calledWith(@project_id, @doc_id)
.should.equal true
it "should set the content type header", ->
@res.setHeader.calledWith('content-type', 'text/plain').should.equal true
it "should send the raw version of the doc", ->
assert.deepEqual @res.send.args[0][0], "#{@doc.lines[0]}\n#{@doc.lines[1]}\n#{@doc.lines[2]}\n#{@doc.lines[3]}\n#{@doc.lines[4]}\n#{@doc.lines[5]}"
describe "getAllDocs", ->
describe "normally", ->
beforeEach ->