-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
190 lines (162 loc) · 4.62 KB
/
index.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const Koa = require('koa')
const request = require('supertest')
const should = require('should')
const koaJsonp = require('koa-safe-jsonp')
const koaViews = require('koa-views')
const responseHandler = require('.')
describe('koa-response-handler', () => {
it('have a statusCode method', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
should(ctx.statusCode).be.ok()
should(ctx.statusCode).be.a.Function()
should(ctx.response.statusCode).be.equal(ctx.statusCode)
})
await request(app.listen())
.get('/')
.expect(404)
})
it('have a sendStatus method', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
should(ctx.sendStatus).be.ok()
should(ctx.sendStatus).be.a.Function()
should(ctx.response.sendStatus).be.equal(ctx.sendStatus)
})
await request(app.listen())
.get('/')
.expect(404)
})
it('have a send method', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
should(ctx.send).be.ok()
should(ctx.send).be.a.Function()
should(ctx.response.send).be.equal(ctx.send)
})
await request(app.listen())
.get('/')
.expect(404)
})
it('have a json method', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
should(ctx.json).be.ok()
should(ctx.json).be.a.Function()
should(ctx.response.json).be.equal(ctx.json)
})
await request(app.listen())
.get('/')
.expect(404)
})
it('should sendStatus method work', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
ctx
.statusCode(200)
.sendStatus()
})
await request(app.listen())
.get('/')
.expect('Content-Type', /text/)
.expect(/OK/)
.expect(200)
})
it('should send method work', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
ctx
.statusCode(200)
.send('hello world')
})
await request(app.listen())
.get('/')
.expect('Content-Type', /text/)
.expect(/hello world/)
.expect(200)
})
it('should json method work', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
ctx
.statusCode(200)
.json({ msg: 'hello world' })
})
await request(app.listen())
.get('/')
.expect('Content-Type', /json/)
.expect(/hello world/)
.expect(200)
})
it('should throw when pass bad option --is-json', async () => {
const app = new Koa()
.use(responseHandler({
isJSON: 'this is string not function'
}))
.use(ctx => {
ctx
.statusCode(200)
.json({ msg: 'hello world' })
})
await request(app.listen())
.get('/')
.expect(500)
})
it('should throw when pass bad json --is-json', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => {
ctx
.statusCode(200)
.json('{ x: 10, , y: 10 }')
})
await request(app.listen())
.get('/')
.expect(500)
})
it('koa-safe-jsonp: should find jsonp function', async () => {
const app = new Koa()
koaJsonp(app)
app
.use(responseHandler())
.use(ctx => {
should(ctx.jsonp).be.ok()
should(ctx.jsonp).be.a.Function()
should(ctx.response.jsonp).be.equal(ctx.jsonp)
should(!ctx.jsonp.toString().includes('defaultNotImplMethods')).be.equal(true)
})
await request(app.listen()).get('/')
})
it('koa-safe-jsonp: should throw when do not find the jsonp function', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => { should.throws(() => { ctx.render() }) })
await request(app.listen()).get('/')
})
it('koa-views: should find render function', async () => {
const app = new Koa()
.use(koaViews())
.use(responseHandler())
.use(ctx => {
should(ctx.render).be.ok()
should(ctx.render).be.a.Function()
should(ctx.response.render).be.equal(ctx.render)
// should use `koa-views`.
should(!ctx.render.toString().includes('defaultNotImplMethods')).be.equal(true)
})
await request(app.listen()).get('/')
})
it('koa-views: should throw when do not find the render function', async () => {
const app = new Koa()
.use(responseHandler())
.use(ctx => { should.throws(() => { ctx.render() }) })
await request(app.listen()).get('/')
})
})