MockResponse.coffee 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. sinon = require "sinon"
  2. class MockResponse
  3. constructor: ->
  4. @rendered = false
  5. @redirected = false
  6. @returned = false
  7. @headers = {}
  8. render: (template, variables) ->
  9. @success = true
  10. @rendered = true
  11. @returned = true
  12. @renderedTemplate = template
  13. @renderedVariables = variables
  14. @callback() if @callback?
  15. redirect: (url) ->
  16. @success = true
  17. @redirected = true
  18. @returned = true
  19. @redirectedTo = url
  20. @callback() if @callback?
  21. sendStatus: (status) ->
  22. if arguments.length < 2
  23. if typeof status != "number"
  24. body = status
  25. status = 200
  26. @statusCode = status
  27. @returned = true
  28. if 200 <= status < 300
  29. @success = true
  30. else
  31. @success = false
  32. @callback() if @callback?
  33. send: (status, body) ->
  34. if arguments.length < 2
  35. if typeof status != "number"
  36. body = status
  37. status = 200
  38. @statusCode = status
  39. @returned = true
  40. if 200 <= status < 300
  41. @success = true
  42. else
  43. @success = false
  44. @body = body if body
  45. @callback() if @callback?
  46. json: (status, body) ->
  47. if arguments.length < 2
  48. if typeof status != "number"
  49. body = status
  50. status = 200
  51. @statusCode = status
  52. @returned = true
  53. if 200 <= status < 300
  54. @success = true
  55. else
  56. @success = false
  57. @body = body if body
  58. @callback() if @callback?
  59. status: (@statusCode)->
  60. return @
  61. setHeader: (header, value) ->
  62. @headers[header] = value
  63. setTimeout: (@timout)->
  64. header: sinon.stub()
  65. contentType: sinon.stub()
  66. end: (data, encoding) ->
  67. @callback() if @callback
  68. type: (type) -> @type = type
  69. module.exports = MockResponse