1 | import koa from 'koa'
|
2 | import KoaRouter from 'koa-router'
|
3 | import koaBodyparser from 'koa-bodyparser'
|
4 | import mongoose, {Schema} from 'mongoose'
|
5 | import bluebird from 'bluebird'
|
6 | import supertest from 'supertest'
|
7 |
|
8 | import koaRouter from '../../src/extend/koa-router'
|
9 | import ResourceController from '../../src/koa/controllers/resource-controller'
|
10 | import error from '../../src/koa/middlewares/error'
|
11 |
|
12 | mongoose.Promise = bluebird
|
13 |
|
14 | const app = koa()
|
15 | const Model = mongoose.model('KoaRouterResourceDummyModel', new Schema({
|
16 | test: Number
|
17 | }))
|
18 |
|
19 | app.use(error({debug: true}))
|
20 | app.use(koaBodyparser())
|
21 |
|
22 | describe('Utils: extend KoaRouter', function () {
|
23 | before(function * () {
|
24 | yield mongoose
|
25 | .connect(process.env.MONGODB_URI || 'mongodb://localhost/test')
|
26 | })
|
27 |
|
28 | after(function * () {
|
29 | yield mongoose.connection.db.dropDatabase()
|
30 | yield mongoose.connection.close()
|
31 | })
|
32 |
|
33 | let router = null
|
34 | let request = null
|
35 | before(function () {
|
36 | koaRouter(KoaRouter)
|
37 | router = new KoaRouter()
|
38 | router
|
39 | .scope('/foo', scopeRouter => {
|
40 | scopeRouter.get('/bar', function * () {
|
41 | this.body = "foobar"
|
42 | })
|
43 | })
|
44 | .resource('/dummy', Model, ResourceController, {})
|
45 | app.use(router.routes(), router.allowedMethods())
|
46 | request = supertest(app.listen())
|
47 | })
|
48 |
|
49 | describe('prototype scope', function () {
|
50 | it('should exists and a function', function () {
|
51 | KoaRouter.prototype.should.have.a.property('scope')
|
52 | KoaRouter.prototype.scope.should.be.a.Function()
|
53 | })
|
54 |
|
55 | it('should return 200 scope /foo and GET /bar as /foo/bar', function * () {
|
56 | const resp = yield request.get('/foo/bar')
|
57 | resp.text.should.be.eql('foobar')
|
58 | })
|
59 | })
|
60 |
|
61 | describe('prototype resource', function () {
|
62 | it('should exists and a function', function () {
|
63 | KoaRouter.prototype.should.have.a.property('resource')
|
64 | KoaRouter.prototype.resource.should.be.a.Function()
|
65 | })
|
66 |
|
67 | it('should response 200 for GET /dummy', function * () {
|
68 | const response = yield request.get('/dummy')
|
69 | response.status.should.be.eql(200)
|
70 | response.body.should.be.an.Array()
|
71 | })
|
72 |
|
73 | it('should response 200 for POST /dummy', function * () {
|
74 | const response = yield request.post('/dummy')
|
75 | .send({test: 1})
|
76 | response.status.should.be.eql(200)
|
77 | response.body.should.have.property('_id')
|
78 | response.body.should.have.property('test')
|
79 | response.body.test.should.be.eql(1)
|
80 | })
|
81 |
|
82 | it('should response 400 for POST /dummy', function * () {
|
83 | const response = yield request.post('/dummy')
|
84 | .send({test: 'test'})
|
85 | response.status.should.be.eql(400)
|
86 | })
|
87 |
|
88 | it('should response 200 for GET /dummy/:id', function * () {
|
89 | const doc = yield Model.create({test: 2})
|
90 | const response = yield request.get(`/dummy/${doc._id}`)
|
91 | response.status.should.be.eql(200)
|
92 | response.body.should.have.property('_id')
|
93 | response.body.should.have.property('test')
|
94 | response.body.test.should.be.eql(2)
|
95 | })
|
96 |
|
97 | it('should response 200 for PUT /dummy/:id', function * () {
|
98 | const doc = yield Model.create({test: 3})
|
99 | const response = yield request.put(`/dummy/${doc._id}`)
|
100 | .send({test: 4})
|
101 | response.status.should.be.eql(200)
|
102 | response.body.should.have.property('_id')
|
103 | response.body.should.have.property('test')
|
104 | response.body.test.should.be.eql(4)
|
105 | })
|
106 |
|
107 | it('should response 400 for PUT /dummy/:id', function * () {
|
108 | const doc = yield Model.create({test: 3})
|
109 | const response = yield request.put(`/dummy/${doc._id}`)
|
110 | .send({test: 'test'})
|
111 | response.status.should.be.eql(400)
|
112 | })
|
113 |
|
114 | it('should response 200 for DELETE /dummy/:id', function * () {
|
115 | const doc = yield Model.create({test: 5})
|
116 | const response = yield request.delete(`/dummy/${doc._id}`)
|
117 | response.status.should.be.eql(200)
|
118 | response.body.should.have.property('_id')
|
119 | response.body.should.have.property('test')
|
120 | response.body.test.should.be.eql(5)
|
121 |
|
122 |
|
123 | const refetch = yield request.get(`/dummy/${doc._id}`)
|
124 | refetch.status.should.be.eql(404)
|
125 | })
|
126 | })
|
127 | })
|