MockResponse.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import sinon from 'sinon'
  2. import Path from 'node:path'
  3. import contentDisposition from 'content-disposition'
  4. class MockResponse {
  5. static initClass() {
  6. // Added via ExpressLocals.
  7. this.prototype.setContentDisposition = sinon.stub() // FIXME: should be reset between each test
  8. }
  9. constructor() {
  10. this.rendered = false
  11. this.redirected = false
  12. this.returned = false
  13. this.headers = {}
  14. this.locals = {}
  15. sinon.spy(this, 'contentType')
  16. sinon.spy(this, 'header')
  17. sinon.spy(this, 'json')
  18. sinon.spy(this, 'send')
  19. sinon.spy(this, 'sendStatus')
  20. sinon.spy(this, 'status')
  21. sinon.spy(this, 'render')
  22. sinon.spy(this, 'redirect')
  23. }
  24. header(field, val) {
  25. this.headers[field] = val
  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. writeHead(status) {
  64. this.statusCode = status
  65. }
  66. send(status, body) {
  67. if (arguments.length < 2) {
  68. if (typeof status !== 'number') {
  69. body = status
  70. status = this.statusCode || 200
  71. }
  72. }
  73. this.statusCode = status
  74. this.returned = true
  75. if (status >= 200 && status < 300) {
  76. this.success = true
  77. } else {
  78. this.success = false
  79. }
  80. if (body) {
  81. this.body = body
  82. }
  83. if (this.callback != null) {
  84. return this.callback()
  85. }
  86. }
  87. json(status, body) {
  88. if (arguments.length < 2) {
  89. if (typeof status !== 'number') {
  90. body = status
  91. status = this.statusCode || 200
  92. }
  93. }
  94. this.statusCode = status
  95. this.returned = true
  96. this.contentType('application/json')
  97. if (status >= 200 && status < 300) {
  98. this.success = true
  99. } else {
  100. this.success = false
  101. }
  102. if (body) {
  103. this.body = JSON.stringify(body)
  104. }
  105. if (this.callback != null) {
  106. return this.callback()
  107. }
  108. }
  109. status(status) {
  110. this.statusCode = status
  111. return this
  112. }
  113. setHeader(header, value) {
  114. this.header(header, value)
  115. }
  116. appendHeader(header, value) {
  117. if (this.headers[header]) {
  118. this.headers[header] += `, ${value}`
  119. } else {
  120. this.headers[header] = value
  121. }
  122. }
  123. setTimeout(timout) {
  124. this.timout = timout
  125. }
  126. end(data, encoding) {
  127. if (this.callback) {
  128. return this.callback()
  129. }
  130. }
  131. attachment(filename) {
  132. switch (Path.extname(filename)) {
  133. case '.csv':
  134. this.contentType('text/csv; charset=utf8')
  135. break
  136. case '.zip':
  137. this.contentType('application/zip')
  138. break
  139. default:
  140. throw new Error('unexpected extension')
  141. }
  142. this.header('ContentDisposition', contentDisposition(filename))
  143. return this
  144. }
  145. contentType(type) {
  146. this.header('ContentType', type)
  147. this.type = type
  148. return this
  149. }
  150. type(type) {
  151. return this.contentType(type)
  152. }
  153. }
  154. MockResponse.initClass()
  155. export default MockResponse