MockResponse.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const Path = require('path')
  16. const contentDisposition = require('content-disposition')
  17. class MockResponse {
  18. static initClass() {
  19. // Added via ExpressLocals.
  20. this.prototype.setContentDisposition = sinon.stub() // FIXME: should be reset between each test
  21. }
  22. constructor() {
  23. this.rendered = false
  24. this.redirected = false
  25. this.returned = false
  26. this.headers = {}
  27. this.locals = {}
  28. sinon.spy(this, 'contentType')
  29. sinon.spy(this, 'header')
  30. sinon.spy(this, 'json')
  31. sinon.spy(this, 'send')
  32. sinon.spy(this, 'sendStatus')
  33. sinon.spy(this, 'status')
  34. sinon.spy(this, 'render')
  35. sinon.spy(this, 'redirect')
  36. }
  37. header(field, val) {
  38. this.headers[field] = val
  39. }
  40. render(template, variables) {
  41. this.success = true
  42. this.rendered = true
  43. this.returned = true
  44. this.renderedTemplate = template
  45. this.renderedVariables = variables
  46. if (this.callback != null) {
  47. return this.callback()
  48. }
  49. }
  50. redirect(url) {
  51. this.success = true
  52. this.redirected = true
  53. this.returned = true
  54. this.redirectedTo = url
  55. if (this.callback != null) {
  56. return this.callback()
  57. }
  58. }
  59. sendStatus(status) {
  60. if (arguments.length < 2) {
  61. if (typeof status !== 'number') {
  62. status = 200
  63. }
  64. }
  65. this.statusCode = status
  66. this.returned = true
  67. if (status >= 200 && status < 300) {
  68. this.success = true
  69. } else {
  70. this.success = false
  71. }
  72. if (this.callback != null) {
  73. return this.callback()
  74. }
  75. }
  76. writeHead(status) {
  77. this.statusCode = status
  78. }
  79. send(status, body) {
  80. if (arguments.length < 2) {
  81. if (typeof status !== 'number') {
  82. body = status
  83. status = this.statusCode || 200
  84. }
  85. }
  86. this.statusCode = status
  87. this.returned = true
  88. if (status >= 200 && status < 300) {
  89. this.success = true
  90. } else {
  91. this.success = false
  92. }
  93. if (body) {
  94. this.body = body
  95. }
  96. if (this.callback != null) {
  97. return this.callback()
  98. }
  99. }
  100. json(status, body) {
  101. if (arguments.length < 2) {
  102. if (typeof status !== 'number') {
  103. body = status
  104. status = this.statusCode || 200
  105. }
  106. }
  107. this.statusCode = status
  108. this.returned = true
  109. this.contentType('application/json')
  110. if (status >= 200 && status < 300) {
  111. this.success = true
  112. } else {
  113. this.success = false
  114. }
  115. if (body) {
  116. this.body = JSON.stringify(body)
  117. }
  118. if (this.callback != null) {
  119. return this.callback()
  120. }
  121. }
  122. status(status) {
  123. this.statusCode = status
  124. return this
  125. }
  126. setHeader(header, value) {
  127. this.header(header, value)
  128. }
  129. setTimeout(timout) {
  130. this.timout = timout
  131. }
  132. end(data, encoding) {
  133. if (this.callback) {
  134. return this.callback()
  135. }
  136. }
  137. attachment(filename) {
  138. switch (Path.extname(filename)) {
  139. case '.csv':
  140. this.contentType('text/csv; charset=utf-8')
  141. break
  142. case '.zip':
  143. this.contentType('application/zip')
  144. break
  145. default:
  146. throw new Error('unexpected extension')
  147. }
  148. this.header('Content-Disposition', contentDisposition(filename))
  149. return this
  150. }
  151. contentType(type) {
  152. this.header('Content-Type', type)
  153. this.type = type
  154. return this
  155. }
  156. type(type) {
  157. return this.contentType(type)
  158. }
  159. }
  160. MockResponse.initClass()
  161. module.exports = MockResponse