test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* global describe, it */
  2. 'use strict'
  3. const co = require('co')
  4. const Koa = require('koa')
  5. const KoaV1 = require('koa-v1')
  6. const assert = require('assert')
  7. const convert = require('./index')
  8. const request = require('supertest')
  9. describe('convert()', () => {
  10. it('should work', () => {
  11. let call = []
  12. let ctx = {}
  13. let mw = convert(function * (next) {
  14. assert.ok(ctx === this)
  15. call.push(1)
  16. })
  17. return mw(ctx, function () {
  18. call.push(2)
  19. }).then(function () {
  20. assert.deepEqual(call, [1])
  21. })
  22. })
  23. it('should inherit the original middleware name', () => {
  24. let mw = convert(function * testing (next) {})
  25. assert.strictEqual(mw._name, 'testing')
  26. })
  27. it('should work with `yield next`', () => {
  28. let call = []
  29. let ctx = {}
  30. let mw = convert(function * (next) {
  31. assert.ok(ctx === this)
  32. call.push(1)
  33. yield next
  34. call.push(3)
  35. })
  36. return mw(ctx, function () {
  37. call.push(2)
  38. return Promise.resolve()
  39. }).then(function () {
  40. assert.deepEqual(call, [1, 2, 3])
  41. })
  42. })
  43. it('should work with `yield* next`', () => {
  44. let call = []
  45. let ctx = {}
  46. let mw = convert(function * (next) {
  47. assert.ok(ctx === this)
  48. call.push(1)
  49. yield* next
  50. call.push(3)
  51. })
  52. return mw(ctx, function () {
  53. call.push(2)
  54. return Promise.resolve()
  55. }).then(function () {
  56. assert.deepEqual(call, [1, 2, 3])
  57. })
  58. })
  59. })
  60. describe('convert.compose()', () => {
  61. it('should work', () => {
  62. let call = []
  63. let context = {}
  64. let _context
  65. let mw = convert.compose([
  66. function * name (next) {
  67. call.push(1)
  68. yield next
  69. call.push(11)
  70. },
  71. (ctx, next) => {
  72. call.push(2)
  73. return next().then(() => {
  74. call.push(10)
  75. })
  76. },
  77. function * (next) {
  78. call.push(3)
  79. yield* next
  80. call.push(9)
  81. },
  82. co.wrap(function * (ctx, next) {
  83. call.push(4)
  84. yield next()
  85. call.push(8)
  86. }),
  87. function * (next) {
  88. try {
  89. call.push(5)
  90. yield next
  91. } catch (e) {
  92. call.push(7)
  93. }
  94. },
  95. (ctx, next) => {
  96. _context = ctx
  97. call.push(6)
  98. throw new Error()
  99. }
  100. ])
  101. return mw(context).then(() => {
  102. assert.equal(context, _context)
  103. assert.deepEqual(call, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  104. })
  105. })
  106. it('should work too', () => {
  107. let call = []
  108. let context = {}
  109. let _context
  110. let mw = convert.compose(
  111. (ctx, next) => {
  112. call.push(1)
  113. return next().catch(() => {
  114. call.push(4)
  115. })
  116. },
  117. function * (next) {
  118. call.push(2)
  119. yield next
  120. call.push(-1) // should not call this
  121. },
  122. function * (next) {
  123. call.push(3)
  124. yield* next
  125. call.push(-1) // should not call this
  126. },
  127. (ctx, next) => {
  128. _context = ctx
  129. return Promise.reject(new Error())
  130. }
  131. )
  132. return mw(context).then(() => {
  133. assert.equal(context, _context)
  134. assert.deepEqual(call, [1, 2, 3, 4])
  135. })
  136. })
  137. })
  138. describe('convert.back()', () => {
  139. it('should work with koa 1', done => {
  140. let app = new KoaV1()
  141. app.use(function * (next) {
  142. this.body = [1]
  143. yield next
  144. this.body.push(6)
  145. })
  146. app.use(convert.back((ctx, next) => {
  147. ctx.body.push(2)
  148. return next().then(() => {
  149. ctx.body.push(5)
  150. })
  151. }))
  152. app.use(convert.back(co.wrap(function * (ctx, next) {
  153. ctx.body.push(3)
  154. yield next()
  155. ctx.body.push(4)
  156. })))
  157. request(app.callback())
  158. .get('/')
  159. .expect(200, [1, 2, 3, 4, 5, 6])
  160. .end(done)
  161. })
  162. it('should guard multiple calls', done => {
  163. let app = new KoaV1()
  164. app.use(function * (next) {
  165. try {
  166. this.body = [1]
  167. yield next
  168. } catch (e) {
  169. this.body.push(e.message)
  170. }
  171. })
  172. app.use(convert.back(co.wrap(function * (ctx, next) {
  173. ctx.body.push(2)
  174. yield next()
  175. yield next() // this should throw new Error('next() called multiple times')
  176. })))
  177. request(app.callback())
  178. .get('/')
  179. .expect(200, [1, 2, 'next() called multiple times'])
  180. .end(done)
  181. })
  182. it('should inherit the original middleware name', () => {
  183. let mw = convert.back(function testing (ctx, next) {})
  184. assert.strictEqual(mw._name, 'testing')
  185. })
  186. })
  187. describe('migration snippet', () => {
  188. let app = new Koa()
  189. // snippet
  190. const _use = app.use
  191. app.use = x => _use.call(app, convert(x))
  192. // end
  193. app.use((ctx, next) => {
  194. ctx.body = [1]
  195. return next().then(() => {
  196. ctx.body.push(9)
  197. })
  198. })
  199. app.use(function * (next) {
  200. this.body.push(2)
  201. yield next
  202. this.body.push(8)
  203. })
  204. app.use(function * (next) {
  205. this.body.push(3)
  206. yield* next
  207. this.body.push(7)
  208. })
  209. app.use(co.wrap(function * (ctx, next) {
  210. ctx.body.push(4)
  211. yield next()
  212. ctx.body.push(6)
  213. }))
  214. app.use(ctx => {
  215. ctx.body.push(5)
  216. })
  217. it('should work', done => {
  218. request(app.callback())
  219. .get('/')
  220. .expect(200, [1, 2, 3, 4, 5, 6, 7, 8, 9])
  221. .end(done)
  222. })
  223. })