overleaf/services/web/test/unit/coffee/helpers/MockResponse.coffee
Brian Gough 43899589dc Merge pull request #1514 from sharelatex/bg-avoid-text-html-content-type-in-responses
use explicit json content-type to avoid security issues with text/html

GitOrigin-RevId: 0c8d96a61380231c5f878572ed91b8ab24375f56
2019-03-11 10:58:32 +00:00

87 lines
1.6 KiB
CoffeeScript

sinon = require "sinon"
class MockResponse
constructor: ->
@rendered = false
@redirected = false
@returned = false
@headers = {}
render: (template, variables) ->
@success = true
@rendered = true
@returned = true
@renderedTemplate = template
@renderedVariables = variables
@callback() if @callback?
redirect: (url) ->
@success = true
@redirected = true
@returned = true
@redirectedTo = url
@callback() if @callback?
sendStatus: (status) ->
if arguments.length < 2
if typeof status != "number"
body = status
status = 200
@statusCode = status
@returned = true
if 200 <= status < 300
@success = true
else
@success = false
@callback() if @callback?
send: (status, body) ->
if arguments.length < 2
if typeof status != "number"
body = status
status = 200
@statusCode = status
@returned = true
if 200 <= status < 300
@success = true
else
@success = false
@body = body if body
@callback() if @callback?
json: (status, body) ->
if arguments.length < 2
if typeof status != "number"
body = status
status = 200
@statusCode = status
@returned = true
@type = 'application/json'
if 200 <= status < 300
@success = true
else
@success = false
@body = JSON.stringify(body) if body
@callback() if @callback?
status: (@statusCode)->
return @
setHeader: (header, value) ->
@headers[header] = value
setContentDisposition: sinon.stub()
setTimeout: (@timout)->
header: sinon.stub()
contentType: sinon.stub()
end: (data, encoding) ->
@callback() if @callback
type: (type) -> @type = type
module.exports = MockResponse