MockResponse.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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()
  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. send(status, body) {
  77. if (arguments.length < 2) {
  78. if (typeof status !== 'number') {
  79. body = status
  80. status = this.statusCode || 200
  81. }
  82. }
  83. this.statusCode = status
  84. this.returned = true
  85. if (status >= 200 && status < 300) {
  86. this.success = true
  87. } else {
  88. this.success = false
  89. }
  90. if (body) {
  91. this.body = body
  92. }
  93. if (this.callback != null) {
  94. return this.callback()
  95. }
  96. }
  97. json(status, body) {
  98. if (arguments.length < 2) {
  99. if (typeof status !== 'number') {
  100. body = status
  101. status = this.statusCode || 200
  102. }
  103. }
  104. this.statusCode = status
  105. this.returned = true
  106. this.contentType('application/json')
  107. if (status >= 200 && status < 300) {
  108. this.success = true
  109. } else {
  110. this.success = false
  111. }
  112. if (body) {
  113. this.body = JSON.stringify(body)
  114. }
  115. if (this.callback != null) {
  116. return this.callback()
  117. }
  118. }
  119. status(status) {
  120. this.statusCode = status
  121. return this
  122. }
  123. setHeader(header, value) {
  124. this.header(header, value)
  125. }
  126. setTimeout(timout) {
  127. this.timout = timout
  128. }
  129. end(data, encoding) {
  130. if (this.callback) {
  131. return this.callback()
  132. }
  133. }
  134. attachment(filename) {
  135. switch (Path.extname(filename)) {
  136. case '.csv':
  137. this.contentType('text/csv; charset=utf-8')
  138. break
  139. case '.zip':
  140. this.contentType('application/zip')
  141. break
  142. default:
  143. throw new Error('unexpected extension')
  144. }
  145. this.header('Content-Disposition', contentDisposition(filename))
  146. return this
  147. }
  148. contentType(type) {
  149. this.header('Content-Type', type)
  150. this.type = type
  151. return this
  152. }
  153. type(type) {
  154. return this.contentType(type)
  155. }
  156. }
  157. MockResponse.initClass()
  158. module.exports = MockResponse