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. const body = status
  50. status = 200
  51. }
  52. }
  53. this.statusCode = status
  54. this.returned = true
  55. if (status >= 200 && status < 300) {
  56. this.success = true
  57. } else {
  58. this.success = false
  59. }
  60. if (this.callback != null) {
  61. return this.callback()
  62. }
  63. }
  64. send(status, body) {
  65. if (arguments.length < 2) {
  66. if (typeof status !== 'number') {
  67. body = status
  68. status = 200
  69. }
  70. }
  71. this.statusCode = status
  72. this.returned = true
  73. if (status >= 200 && status < 300) {
  74. this.success = true
  75. } else {
  76. this.success = false
  77. }
  78. if (body) {
  79. this.body = body
  80. }
  81. if (this.callback != null) {
  82. return this.callback()
  83. }
  84. }
  85. json(status, body) {
  86. if (arguments.length < 2) {
  87. if (typeof status !== 'number') {
  88. body = status
  89. status = this.statusCode || 200
  90. }
  91. }
  92. this.statusCode = status
  93. this.returned = true
  94. this.type = 'application/json'
  95. if (status >= 200 && status < 300) {
  96. this.success = true
  97. } else {
  98. this.success = false
  99. }
  100. if (body) {
  101. this.body = JSON.stringify(body)
  102. }
  103. if (this.callback != null) {
  104. return this.callback()
  105. }
  106. }
  107. status(status) {
  108. this.statusCode = status
  109. return this
  110. }
  111. setHeader(header, value) {
  112. return (this.headers[header] = value)
  113. }
  114. setTimeout(timout) {
  115. this.timout = timout
  116. }
  117. end(data, encoding) {
  118. if (this.callback) {
  119. return this.callback()
  120. }
  121. }
  122. type(type) {
  123. return (this.type = type)
  124. }
  125. }
  126. MockResponse.initClass()
  127. module.exports = MockResponse