express-response-middleware 2.1.0 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cjs/index.cjs +17 -20
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +8 -8
- package/dist/esm/index.d.ts +8 -8
- package/dist/esm/index.mjs +17 -20
- package/dist/esm/index.mjs.map +1 -1
- package/package.json +19 -15
- package/.github/workflows/release.yml +0 -42
- package/.github/workflows/runTest.yml +0 -56
- package/.node-version +0 -1
- package/.prettierrc.json +0 -8
- package/CHANGELOG.md +0 -74
- package/docs/MIGRATE_FROM_MUNG.md +0 -153
- package/docs/V1_README.md +0 -115
- package/index.d.ts +0 -69
- package/renovate.json +0 -6
- package/rollup.config.mjs +0 -51
- package/src/endMiddleware.ts +0 -61
- package/src/index.ts +0 -13
- package/src/jsonMiddleware.ts +0 -71
- package/src/jsonpMiddleware.ts +0 -73
- package/src/sendMiddleware.ts +0 -50
- package/src/tests/endMiddleware.test.ts +0 -92
- package/src/tests/jsonMiddleware.test.ts +0 -209
- package/src/tests/jsonpMiddleware.test.ts +0 -191
- package/src/tests/mixAndMatch.test.ts +0 -253
- package/src/tests/sendMiddleware.test.ts +0 -187
- package/src/tests/testHelpers/sleep.ts +0 -1
- package/src/tests/writeMiddleware.test.ts +0 -224
- package/src/utils/asyncLocalStorage.ts +0 -3
- package/src/utils/errorHandler.ts +0 -10
- package/src/writeMiddleware.ts +0 -70
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -14
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import express from 'express'
|
|
2
|
-
import request from 'supertest'
|
|
3
|
-
import { expect } from 'vitest'
|
|
4
|
-
import { endMiddleware, TransformEnd } from '../index.js'
|
|
5
|
-
import { sleep } from './testHelpers/sleep.js'
|
|
6
|
-
|
|
7
|
-
describe('endMiddleware', () => {
|
|
8
|
-
const header: TransformEnd = (_req, res) => {
|
|
9
|
-
res.set('x-inspected-by', 'me')
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const headerAsync: TransformEnd = async (_req, res) => {
|
|
13
|
-
await sleep()
|
|
14
|
-
res.set('x-inspected-by', 'me')
|
|
15
|
-
await sleep()
|
|
16
|
-
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const header2: TransformEnd = (_req, res) => {
|
|
21
|
-
res.set('x-inspected-by-2', 'him')
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const headerAsync2: TransformEnd = async (_req, res) => {
|
|
25
|
-
await sleep()
|
|
26
|
-
res.set('x-inspected-by-2', 'him')
|
|
27
|
-
await sleep()
|
|
28
|
-
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const error: TransformEnd = req => {
|
|
33
|
-
;(req as any).hopefully_fails()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const errorAsync: TransformEnd = async req => {
|
|
37
|
-
await sleep()
|
|
38
|
-
;(req as any).hopefully_fails()
|
|
39
|
-
await sleep()
|
|
40
|
-
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
it.each([header, headerAsync])('should return the headers', async handler => {
|
|
45
|
-
const server = express()
|
|
46
|
-
.use(endMiddleware(handler))
|
|
47
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
48
|
-
const response = await request(server).get('/')
|
|
49
|
-
|
|
50
|
-
expect(response.status).toStrictEqual(200)
|
|
51
|
-
expect(response.body).toStrictEqual({ a: 'a' })
|
|
52
|
-
expect(response.headers).toStrictEqual(
|
|
53
|
-
expect.objectContaining({
|
|
54
|
-
'x-inspected-by': 'me',
|
|
55
|
-
})
|
|
56
|
-
)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it.each([
|
|
60
|
-
[header, header2],
|
|
61
|
-
[header, headerAsync2],
|
|
62
|
-
[headerAsync, header2],
|
|
63
|
-
[headerAsync, headerAsync2],
|
|
64
|
-
])('should work with multiple middleware', async (handler, handler2) => {
|
|
65
|
-
const server = express()
|
|
66
|
-
.use(endMiddleware(handler))
|
|
67
|
-
.use(endMiddleware(handler2))
|
|
68
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
69
|
-
const response = await request(server).get('/')
|
|
70
|
-
|
|
71
|
-
expect(response.status).toStrictEqual(200)
|
|
72
|
-
expect(response.body).toStrictEqual({ a: 'a' })
|
|
73
|
-
expect(response.headers).toStrictEqual(
|
|
74
|
-
expect.objectContaining({
|
|
75
|
-
'x-inspected-by': 'me',
|
|
76
|
-
'x-inspected-by-2': 'him',
|
|
77
|
-
})
|
|
78
|
-
)
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it.each([error, errorAsync])(
|
|
82
|
-
'should 500 on a synchronous exception',
|
|
83
|
-
async handler => {
|
|
84
|
-
const server = express()
|
|
85
|
-
.use(endMiddleware(handler))
|
|
86
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
87
|
-
const response = await request(server).get('/')
|
|
88
|
-
|
|
89
|
-
expect(response.status).toStrictEqual(500)
|
|
90
|
-
}
|
|
91
|
-
)
|
|
92
|
-
})
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
-
import request from 'supertest'
|
|
3
|
-
import { jsonMiddleware, TransformJson } from '../index.js'
|
|
4
|
-
import { sleep } from './testHelpers/sleep.js'
|
|
5
|
-
|
|
6
|
-
describe('jsonMiddleware', () => {
|
|
7
|
-
const noop: TransformJson = () => {}
|
|
8
|
-
|
|
9
|
-
const noopAsync: TransformJson = async json => {
|
|
10
|
-
await sleep()
|
|
11
|
-
return json
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const inspect: TransformJson<any> = json => {
|
|
15
|
-
json.inspected_by = 'me'
|
|
16
|
-
return json
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const inspect2: TransformJson<any> = json => {
|
|
20
|
-
json.inspected_by_2 = 'him'
|
|
21
|
-
return json
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const inspectAsync: TransformJson = async json => {
|
|
25
|
-
await sleep()
|
|
26
|
-
;(json as any).inspected_by = 'me'
|
|
27
|
-
await sleep()
|
|
28
|
-
|
|
29
|
-
return json
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const inspectAsync2: TransformJson = async json => {
|
|
33
|
-
await sleep()
|
|
34
|
-
;(json as any).inspected_by_2 = 'him'
|
|
35
|
-
await sleep()
|
|
36
|
-
|
|
37
|
-
return json
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const error: TransformJson<any> = json => {
|
|
41
|
-
json.foo.bar.hopefully.fails()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const errorAsync: TransformJson<any> = async json => {
|
|
45
|
-
await sleep()
|
|
46
|
-
json.foo.bar.hopefully.fails()
|
|
47
|
-
await sleep()
|
|
48
|
-
|
|
49
|
-
return json
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const noPermission: TransformJson = (_json, _req, res) => {
|
|
53
|
-
res.status(403).send('no permissions')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const noPermissionAsync: TransformJson = async (json, _req, res) => {
|
|
57
|
-
await sleep()
|
|
58
|
-
res.status(403).send('no permissions')
|
|
59
|
-
await sleep()
|
|
60
|
-
|
|
61
|
-
return Promise.resolve(json)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
it.each([inspect, inspectAsync])(
|
|
65
|
-
'should return the JSON result',
|
|
66
|
-
async handler => {
|
|
67
|
-
const server = express()
|
|
68
|
-
.use(jsonMiddleware(handler))
|
|
69
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
70
|
-
const response = await request(server).get('/')
|
|
71
|
-
|
|
72
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
73
|
-
|
|
74
|
-
expect(response.status).toStrictEqual(200)
|
|
75
|
-
expect(response.body).toStrictEqual(expected)
|
|
76
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
-
JSON.stringify(expected).length.toString()
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
it.each([
|
|
83
|
-
[inspect, inspect2],
|
|
84
|
-
[inspect, inspectAsync2],
|
|
85
|
-
[inspectAsync, inspect2],
|
|
86
|
-
[inspectAsync, inspectAsync2],
|
|
87
|
-
])('should work with multiple middleware', async (handler, handler2) => {
|
|
88
|
-
const server = express()
|
|
89
|
-
.use(jsonMiddleware(handler))
|
|
90
|
-
.use(jsonMiddleware(handler2))
|
|
91
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
92
|
-
const response = await request(server).get('/')
|
|
93
|
-
|
|
94
|
-
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
95
|
-
|
|
96
|
-
expect(response.status).toStrictEqual(200)
|
|
97
|
-
expect(response.body).toStrictEqual(expected)
|
|
98
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
-
JSON.stringify(expected).length.toString()
|
|
100
|
-
)
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
it.each([inspect, inspectAsync])(
|
|
104
|
-
'should call callback with an error',
|
|
105
|
-
async handler => {
|
|
106
|
-
const server = express()
|
|
107
|
-
.use(jsonMiddleware(handler))
|
|
108
|
-
.get('/', (_req, res) => res.status(404).json({ a: 'a' }).end())
|
|
109
|
-
const response = await request(server).get('/')
|
|
110
|
-
|
|
111
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
112
|
-
|
|
113
|
-
expect(response.status).toStrictEqual(404)
|
|
114
|
-
expect(response.body).toStrictEqual(expected)
|
|
115
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
-
JSON.stringify(expected).length.toString()
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
it.each([inspect, inspectAsync])(
|
|
122
|
-
'should return the JSON result from a res.send',
|
|
123
|
-
async handler => {
|
|
124
|
-
const server = express()
|
|
125
|
-
.use(jsonMiddleware(handler))
|
|
126
|
-
.get('/', (_req, res) => res.status(200).send({ a: 'a' }).end())
|
|
127
|
-
const response = await request(server).get('/')
|
|
128
|
-
|
|
129
|
-
let expected = { a: 'a', inspected_by: 'me' }
|
|
130
|
-
|
|
131
|
-
expect(response.status).toStrictEqual(200)
|
|
132
|
-
expect(response.body).toStrictEqual(expected)
|
|
133
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
134
|
-
JSON.stringify(expected).length.toString()
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
it.each([noop, noopAsync])(
|
|
140
|
-
'should return a as application/json',
|
|
141
|
-
async handler => {
|
|
142
|
-
const server = express()
|
|
143
|
-
.use(jsonMiddleware(handler))
|
|
144
|
-
.get('/', (_req, res) => res.status(200).json(42).end())
|
|
145
|
-
const response = await request(server).get('/')
|
|
146
|
-
|
|
147
|
-
expect(response.status).toStrictEqual(200)
|
|
148
|
-
expect(response.text).toStrictEqual('42')
|
|
149
|
-
expect(response.headers).toStrictEqual(
|
|
150
|
-
expect.objectContaining({
|
|
151
|
-
'content-type': 'application/json; charset=utf-8',
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
it.each([noPermission, noPermissionAsync])(
|
|
158
|
-
'should abort if a response is sent',
|
|
159
|
-
async handler => {
|
|
160
|
-
const server = express()
|
|
161
|
-
.use(jsonMiddleware(handler))
|
|
162
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
163
|
-
const response = await request(server).get('/')
|
|
164
|
-
|
|
165
|
-
expect(response.status).toStrictEqual(403)
|
|
166
|
-
expect(response.text).toStrictEqual('no permissions')
|
|
167
|
-
expect(response.headers).toStrictEqual(
|
|
168
|
-
expect.objectContaining({
|
|
169
|
-
'content-type': 'text/html; charset=utf-8',
|
|
170
|
-
})
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
it.each([error, errorAsync])(
|
|
176
|
-
'should 500 on a synchronous exception',
|
|
177
|
-
async handler => {
|
|
178
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
179
|
-
res.status(500).send(err.message).end()
|
|
180
|
-
const server = express()
|
|
181
|
-
.use(errorHandler)
|
|
182
|
-
.use(jsonMiddleware(handler))
|
|
183
|
-
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
184
|
-
|
|
185
|
-
const response = await request(server).get('/')
|
|
186
|
-
|
|
187
|
-
expect(response.status).toStrictEqual(500)
|
|
188
|
-
}
|
|
189
|
-
)
|
|
190
|
-
|
|
191
|
-
it.each([error, errorAsync])(
|
|
192
|
-
'should 500 on an asynchronous exception',
|
|
193
|
-
async handler => {
|
|
194
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
195
|
-
res.status(500).send(err.message).end()
|
|
196
|
-
const server = express()
|
|
197
|
-
.use(errorHandler)
|
|
198
|
-
.use(jsonMiddleware(handler))
|
|
199
|
-
.get('/', (_req, res) => {
|
|
200
|
-
process.nextTick(() => {
|
|
201
|
-
res.status(200).json({ a: 'a' }).end()
|
|
202
|
-
})
|
|
203
|
-
})
|
|
204
|
-
const response = await request(server).get('/')
|
|
205
|
-
|
|
206
|
-
expect(response.status).toStrictEqual(500)
|
|
207
|
-
}
|
|
208
|
-
)
|
|
209
|
-
})
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
-
import request from 'supertest'
|
|
3
|
-
import { jsonpMiddleware, TransformJsonp } from '../index.js'
|
|
4
|
-
import { sleep } from './testHelpers/sleep.js'
|
|
5
|
-
|
|
6
|
-
describe('jsonpMiddleware', () => {
|
|
7
|
-
const noop: TransformJsonp = () => {}
|
|
8
|
-
|
|
9
|
-
const noopAsync: TransformJsonp = async json => {
|
|
10
|
-
await sleep()
|
|
11
|
-
return json
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const inspect: TransformJsonp<any> = json => {
|
|
15
|
-
json.inspected_by = 'me'
|
|
16
|
-
return json
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const inspect2: TransformJsonp<any> = json => {
|
|
20
|
-
json.inspected_by_2 = 'him'
|
|
21
|
-
return json
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const inspectAsync: TransformJsonp = async json => {
|
|
25
|
-
await sleep()
|
|
26
|
-
;(json as any).inspected_by = 'me'
|
|
27
|
-
await sleep()
|
|
28
|
-
|
|
29
|
-
return json
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const inspectAsync2: TransformJsonp = async json => {
|
|
33
|
-
await sleep()
|
|
34
|
-
;(json as any).inspected_by_2 = 'him'
|
|
35
|
-
await sleep()
|
|
36
|
-
|
|
37
|
-
return json
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const error: TransformJsonp<any> = json => {
|
|
41
|
-
json.foo.bar.hopefully.fails()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const errorAsync: TransformJsonp<any> = async json => {
|
|
45
|
-
await sleep()
|
|
46
|
-
json.foo.bar.hopefully.fails()
|
|
47
|
-
await sleep()
|
|
48
|
-
|
|
49
|
-
return json
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const noPermission: TransformJsonp = (_json, _req, res) => {
|
|
53
|
-
res.status(403).send('no permissions')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const noPermissionAsync: TransformJsonp = async (json, _req, res) => {
|
|
57
|
-
await sleep()
|
|
58
|
-
res.status(403).send('no permissions')
|
|
59
|
-
await sleep()
|
|
60
|
-
|
|
61
|
-
return Promise.resolve(json)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
it.each([inspect, inspectAsync])(
|
|
65
|
-
'should return the JSON result',
|
|
66
|
-
async handler => {
|
|
67
|
-
const server = express()
|
|
68
|
-
.use(jsonpMiddleware(handler))
|
|
69
|
-
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
70
|
-
const response = await request(server).get('/')
|
|
71
|
-
|
|
72
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
73
|
-
|
|
74
|
-
expect(response.status).toStrictEqual(200)
|
|
75
|
-
expect(response.body).toStrictEqual(expected)
|
|
76
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
77
|
-
JSON.stringify(expected).length.toString()
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
it.each([
|
|
83
|
-
[inspect, inspect2],
|
|
84
|
-
[inspect, inspectAsync2],
|
|
85
|
-
[inspectAsync, inspect2],
|
|
86
|
-
[inspectAsync, inspectAsync2],
|
|
87
|
-
])('should work with multiple middleware', async (handler, handler2) => {
|
|
88
|
-
const server = express()
|
|
89
|
-
.use(jsonpMiddleware(handler))
|
|
90
|
-
.use(jsonpMiddleware(handler2))
|
|
91
|
-
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
92
|
-
const response = await request(server).get('/')
|
|
93
|
-
|
|
94
|
-
const expected = { a: 'a', inspected_by: 'me', inspected_by_2: 'him' }
|
|
95
|
-
|
|
96
|
-
expect(response.status).toStrictEqual(200)
|
|
97
|
-
expect(response.body).toStrictEqual(expected)
|
|
98
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
99
|
-
JSON.stringify(expected).length.toString()
|
|
100
|
-
)
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
it.each([inspect, inspectAsync])(
|
|
104
|
-
'should call callback with an error',
|
|
105
|
-
async handler => {
|
|
106
|
-
const server = express()
|
|
107
|
-
.use(jsonpMiddleware(handler))
|
|
108
|
-
.get('/', (_req, res) => res.status(404).jsonp({ a: 'a' }).end())
|
|
109
|
-
const response = await request(server).get('/')
|
|
110
|
-
|
|
111
|
-
const expected = { a: 'a', inspected_by: 'me' }
|
|
112
|
-
|
|
113
|
-
expect(response.status).toStrictEqual(404)
|
|
114
|
-
expect(response.body).toStrictEqual(expected)
|
|
115
|
-
expect(response.headers['content-length']).toStrictEqual(
|
|
116
|
-
JSON.stringify(expected).length.toString()
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
it.each([noop, noopAsync])(
|
|
122
|
-
'should return a as application/json',
|
|
123
|
-
async handler => {
|
|
124
|
-
const server = express()
|
|
125
|
-
.use(jsonpMiddleware(handler))
|
|
126
|
-
.get('/', (_req, res) => res.status(200).jsonp(42).end())
|
|
127
|
-
const response = await request(server).get('/')
|
|
128
|
-
|
|
129
|
-
expect(response.status).toStrictEqual(200)
|
|
130
|
-
expect(response.text).toStrictEqual('42')
|
|
131
|
-
expect(response.headers).toStrictEqual(
|
|
132
|
-
expect.objectContaining({
|
|
133
|
-
'content-type': 'application/json; charset=utf-8',
|
|
134
|
-
})
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
it.each([noPermission, noPermissionAsync])(
|
|
140
|
-
'should abort if a response is sent',
|
|
141
|
-
async handler => {
|
|
142
|
-
const server = express()
|
|
143
|
-
.use(jsonpMiddleware(handler))
|
|
144
|
-
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
145
|
-
const response = await request(server).get('/')
|
|
146
|
-
|
|
147
|
-
expect(response.status).toStrictEqual(403)
|
|
148
|
-
expect(response.text).toStrictEqual('no permissions')
|
|
149
|
-
expect(response.headers).toStrictEqual(
|
|
150
|
-
expect.objectContaining({
|
|
151
|
-
'content-type': 'text/html; charset=utf-8',
|
|
152
|
-
})
|
|
153
|
-
)
|
|
154
|
-
}
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
it.each([error, errorAsync])(
|
|
158
|
-
'should 500 on a synchronous exception',
|
|
159
|
-
async handler => {
|
|
160
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
161
|
-
res.status(500).send(err.message).end()
|
|
162
|
-
const server = express()
|
|
163
|
-
.use(errorHandler)
|
|
164
|
-
.use(jsonpMiddleware(handler))
|
|
165
|
-
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
166
|
-
|
|
167
|
-
const response = await request(server).get('/')
|
|
168
|
-
|
|
169
|
-
expect(response.status).toStrictEqual(500)
|
|
170
|
-
}
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
it.each([error, errorAsync])(
|
|
174
|
-
'should 500 on an asynchronous exception',
|
|
175
|
-
async handler => {
|
|
176
|
-
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
177
|
-
res.status(500).send(err.message).end()
|
|
178
|
-
const server = express()
|
|
179
|
-
.use(errorHandler)
|
|
180
|
-
.use(jsonpMiddleware(handler))
|
|
181
|
-
.get('/', (_req, res) => {
|
|
182
|
-
process.nextTick(() => {
|
|
183
|
-
res.status(200).jsonp({ a: 'a' }).end()
|
|
184
|
-
})
|
|
185
|
-
})
|
|
186
|
-
const response = await request(server).get('/')
|
|
187
|
-
|
|
188
|
-
expect(response.status).toStrictEqual(500)
|
|
189
|
-
}
|
|
190
|
-
)
|
|
191
|
-
})
|