-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnested-router-error.test.js
More file actions
197 lines (164 loc) · 5.9 KB
/
Copy pathnested-router-error.test.js
File metadata and controls
197 lines (164 loc) · 5.9 KB
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
191
192
193
194
195
196
197
/* global describe, it */
const expect = require('chai').expect
const request = require('supertest')
const sequential = require('../lib/router/sequential')
const zero = require('../index')
describe('0http - Nested Router Error Handling', () => {
describe('req.url restoration on nested router errors', () => {
it('should restore req.url when a nested handler throws synchronously', (done) => {
let capturedUrl = null
let capturedOriginalUrl = null
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
capturedUrl = req.url
capturedOriginalUrl = req.originalUrl
res.statusCode = 500
res.end('error')
}
const parent = sequential({ errorHandler })
const child = sequential()
child.get('/crash', (req, res) => {
throw new Error('boom')
})
parent.use('/api', child)
const req = { method: 'GET', url: '/api/crash', headers: {} }
const res = { statusCode: 200, finished: false, setHeader: () => {}, end: () => { res.finished = true } }
parent.lookup(req, res)
expect(capturedUrl).to.equal('/api/crash')
expect(capturedOriginalUrl).to.equal('/api/crash')
expect(req.url).to.equal('/api/crash')
expect(req.preRouterUrl).to.equal(undefined)
expect(res.statusCode).to.equal(500)
done()
})
it('should restore req.url when a nested handler calls next(err)', (done) => {
let capturedUrl = null
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
capturedUrl = req.url
res.statusCode = 500
res.end('error')
}
const parent = sequential({ errorHandler })
const child = sequential()
child.get('/next-error', (req, res, next) => {
next(new Error('next error'))
})
parent.use('/api', child)
const req = { method: 'GET', url: '/api/next-error', headers: {} }
const res = { statusCode: 200, finished: false, setHeader: () => {}, end: () => { res.finished = true } }
parent.lookup(req, res)
expect(capturedUrl).to.equal('/api/next-error')
expect(req.url).to.equal('/api/next-error')
expect(req.preRouterUrl).to.equal(undefined)
done()
})
it('should restore req.url when a nested async handler rejects', (done) => {
let capturedUrl = null
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
capturedUrl = req.url
res.statusCode = 500
res.end('error')
}
const parent = sequential({ errorHandler })
const child = sequential()
child.get('/async-crash', async (req, res) => {
throw new Error('async boom')
})
parent.use('/api', child)
const req = { method: 'GET', url: '/api/async-crash', headers: {} }
const res = { statusCode: 200, finished: false, setHeader: () => {}, end: () => { res.finished = true } }
parent.lookup(req, res)
setImmediate(() => {
expect(capturedUrl).to.equal('/api/async-crash')
expect(req.url).to.equal('/api/async-crash')
expect(req.preRouterUrl).to.equal(undefined)
done()
})
})
it('should invoke the parent error handler, not the nested router default', (done) => {
let parentHandlerCalled = false
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
parentHandlerCalled = true
res.statusCode = 500
res.end('parent handled')
}
const parent = sequential({ errorHandler })
const child = sequential() // uses default error handler
child.get('/crash', (req, res) => {
throw new Error('boom')
})
parent.use('/api', child)
const req = { method: 'GET', url: '/api/crash', headers: {} }
const res = { statusCode: 200, finished: false, setHeader: () => {}, end: (body) => { res._body = body; res.finished = true } }
parent.lookup(req, res)
expect(parentHandlerCalled).to.equal(true)
expect(res._body).to.equal('parent handled')
done()
})
})
describe('full server integration', () => {
it('should return parent error response for nested router errors', (done) => {
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
res.statusCode = 500
res.end('parent-handled')
}
const { router, server } = zero({
router: sequential({ errorHandler })
})
const child = sequential()
child.get('/crash', (req, res) => {
throw new Error('boom')
})
router.use('/api', child)
server.listen(0, () => {
const port = server.address().port
request(`http://127.0.0.1:${port}`)
.get('/api/crash')
.expect(500)
.end((err, res) => {
if (err) {
server.close(() => done(err))
return
}
expect(res.text).to.equal('parent-handled')
server.close(done)
})
})
})
it('should not corrupt req.url in parent error handler logs', (done) => {
const loggedUrls = []
const errorHandler = (err, req, res) => {
expect(err).to.be.an('error')
loggedUrls.push(req.url)
res.statusCode = 500
res.end('error')
}
const { router, server } = zero({
router: sequential({ errorHandler })
})
const child = sequential()
child.get('/crash', (req, res) => {
throw new Error('boom')
})
router.use('/api', child)
server.listen(0, () => {
const port = server.address().port
request(`http://127.0.0.1:${port}`)
.get('/api/crash')
.expect(500)
.end((err) => {
if (err) {
server.close(() => done(err))
return
}
expect(loggedUrls).to.deep.equal(['/api/crash'])
server.close(done)
})
})
})
})
})