MockResponse.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS206: Consider reworking classes to avoid initClass
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. class MockResponse {
  16. static initClass() {
  17. this.prototype.setContentDisposition = sinon.stub()
  18. this.prototype.header = sinon.stub()
  19. this.prototype.contentType = sinon.stub()
  20. }
  21. constructor() {
  22. this.rendered = false
  23. this.redirected = false
  24. this.returned = false
  25. this.headers = {}
  26. }
  27. render(template, variables) {
  28. this.success = true
  29. this.rendered = true
  30. this.returned = true
  31. this.renderedTemplate = template
  32. this.renderedVariables = variables
  33. if (this.callback != null) {
  34. return this.callback()
  35. }
  36. }
  37. redirect(url) {
  38. this.success = true
  39. this.redirected = true
  40. this.returned = true
  41. this.redirectedTo = url
  42. if (this.callback != null) {
  43. return this.callback()
  44. }
  45. }
  46. sendStatus(status) {
  47. if (arguments.length < 2) {
  48. if (typeof status !== 'number') {
  49. status = 200
  50. }
  51. }
  52. this.statusCode = status
  53. this.returned = true
  54. if (status >= 200 && status < 300) {
  55. this.success = true
  56. } else {
  57. this.success = false
  58. }
  59. if (this.callback != null) {
  60. return this.callback()
  61. }
  62. }
  63. send(status, body) {
  64. if (arguments.length < 2) {
  65. if (typeof status !== 'number') {
  66. body = status
  67. status = this.statusCode || 200
  68. }
  69. }
  70. this.statusCode = status
  71. this.returned = true
  72. if (status >= 200 && status < 300) {
  73. this.success = true
  74. } else {
  75. this.success = false
  76. }
  77. if (body) {
  78. this.body = body
  79. }
  80. if (this.callback != null) {
  81. return this.callback()
  82. }
  83. }
  84. json(status, body) {
  85. if (arguments.length < 2) {
  86. if (typeof status !== 'number') {
  87. body = status
  88. status = this.statusCode || 200
  89. }
  90. }
  91. this.statusCode = status
  92. this.returned = true
  93. this.type = 'application/json'
  94. if (status >= 200 && status < 300) {
  95. this.success = true
  96. } else {
  97. this.success = false
  98. }
  99. if (body) {
  100. this.body = JSON.stringify(body)
  101. }
  102. if (this.callback != null) {
  103. return this.callback()
  104. }
  105. }
  106. status(status) {
  107. this.statusCode = status
  108. return this
  109. }
  110. setHeader(header, value) {
  111. return (this.headers[header] = value)
  112. }
  113. setTimeout(timout) {
  114. this.timout = timout
  115. }
  116. end(data, encoding) {
  117. if (this.callback) {
  118. return this.callback()
  119. }
  120. }
  121. type(type) {
  122. return (this.type = type)
  123. }
  124. }
  125. MockResponse.initClass()
  126. module.exports = MockResponse