Add in guards on bad data from web API

This commit is contained in:
James Allen 2016-11-29 17:13:16 +00:00
parent 546d9e9cee
commit efab68e6b2
2 changed files with 21 additions and 1 deletions

View file

@ -35,6 +35,10 @@ module.exports = PersistenceManager =
body = JSON.parse body
catch e
return callback(e)
if !body.lines?
return callback(new Error("web API response had no doc lines"))
if !body.version? or not body.version instanceof Number
return callback(new Error("web API response had no valid doc version"))
return callback null, body.lines, body.version
else if res.statusCode == 404
return callback(new Errors.NotFoundError("doc not not found: #{url}"))

View file

@ -84,5 +84,21 @@ describe "PersistenceManager.getDoc", ->
it "should time the execution", ->
@Metrics.Timer::done.called.should.equal true
describe "when request returns an doc without lines", ->
beforeEach ->
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(version: @version))
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
it "should return and error", ->
@callback.calledWith(new Error("web API response had no doc lines")).should.equal true
describe "when request returns an doc without a version", ->
beforeEach ->
@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines))
@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
it "should return and error", ->
@callback.calledWith(new Error("web API response had no valid doc version")).should.equal true