express-response-middleware 1.0.0 → 2.0.1
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/.github/workflows/release.yml +17 -3
- package/CHANGELOG.md +50 -0
- package/README.md +121 -57
- package/dist/cjs/index.cjs +199 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +24 -0
- package/dist/esm/index.d.ts +24 -0
- package/dist/esm/index.mjs +194 -0
- package/dist/esm/index.mjs.map +1 -0
- package/docs/MIGRATE_FROM_MUNG.md +153 -0
- package/docs/V1_README.md +115 -0
- package/package.json +26 -14
- package/renovate.json +6 -0
- package/rollup.config.mjs +49 -0
- package/src/endMiddleware.ts +61 -0
- package/src/index.ts +13 -0
- package/src/jsonMiddleware.ts +71 -0
- package/src/jsonpMiddleware.ts +73 -0
- package/src/sendMiddleware.ts +50 -0
- package/src/tests/endMiddleware.test.ts +92 -0
- package/src/tests/jsonMiddleware.test.ts +209 -0
- package/src/tests/jsonpMiddleware.test.ts +191 -0
- package/src/tests/mixAndMatch.test.ts +253 -0
- package/src/tests/sendMiddleware.test.ts +187 -0
- package/src/tests/testHelpers/sleep.ts +1 -0
- package/src/tests/writeMiddleware.test.ts +224 -0
- package/src/utils/asyncLocalStorage.ts +3 -0
- package/src/utils/errorHandler.ts +10 -0
- package/src/writeMiddleware.ts +70 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +14 -0
- package/index.js +0 -203
- package/test/headers.js +0 -66
- package/test/json.js +0 -192
- package/test/jsonAsync.js +0 -170
- package/test/write.js +0 -160
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import { expect } from 'vitest'
|
|
4
|
+
import {
|
|
5
|
+
endMiddleware,
|
|
6
|
+
jsonMiddleware,
|
|
7
|
+
jsonpMiddleware,
|
|
8
|
+
TransformChunk,
|
|
9
|
+
TransformEnd,
|
|
10
|
+
TransformJson,
|
|
11
|
+
writeMiddleware,
|
|
12
|
+
} from '../index'
|
|
13
|
+
import { sleep } from './testHelpers/sleep'
|
|
14
|
+
import { sendMiddleware, TransformSend } from '../sendMiddleware'
|
|
15
|
+
|
|
16
|
+
describe('mix and match', () => {
|
|
17
|
+
const header: TransformEnd = (_req, res) => {
|
|
18
|
+
res.set('x-inspected-by', 'me')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const headerAsync: TransformEnd = async (_req, res) => {
|
|
22
|
+
await sleep()
|
|
23
|
+
res.set('x-inspected-by', 'me')
|
|
24
|
+
await sleep()
|
|
25
|
+
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const inspect: TransformJson<any> = json => {
|
|
30
|
+
json.inspected_by = 'me'
|
|
31
|
+
return json
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const inspectAsync: TransformJson = async json => {
|
|
35
|
+
await sleep()
|
|
36
|
+
;(json as any).inspected_by = 'me'
|
|
37
|
+
await sleep()
|
|
38
|
+
|
|
39
|
+
return json
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const appendText: TransformChunk = chunk => {
|
|
43
|
+
return chunk + ' with more content'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const appendTextAsync: TransformChunk = async chunk => {
|
|
47
|
+
await sleep()
|
|
48
|
+
return chunk + ' with more content'
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const modifyText: TransformSend = data => {
|
|
52
|
+
return data + ' with more content'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const modifyTextAsync: TransformSend = async data => {
|
|
56
|
+
await sleep()
|
|
57
|
+
return data + ' with more content'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
it.each([
|
|
61
|
+
[inspect, header],
|
|
62
|
+
[inspectAsync, header],
|
|
63
|
+
[inspect, headerAsync],
|
|
64
|
+
[inspectAsync, headerAsync],
|
|
65
|
+
])('end should work with json', async (jsonHandler, endHandler) => {
|
|
66
|
+
const server = express()
|
|
67
|
+
.use(jsonMiddleware(jsonHandler))
|
|
68
|
+
.use(endMiddleware(endHandler))
|
|
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).toStrictEqual(
|
|
77
|
+
expect.objectContaining({
|
|
78
|
+
'x-inspected-by': 'me',
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it.each([
|
|
84
|
+
[inspect, header],
|
|
85
|
+
[inspectAsync, header],
|
|
86
|
+
[inspect, headerAsync],
|
|
87
|
+
[inspectAsync, headerAsync],
|
|
88
|
+
])('end should work with json reverse', async (jsonHandler, endHandler) => {
|
|
89
|
+
const server = express()
|
|
90
|
+
.use(endMiddleware(endHandler))
|
|
91
|
+
.use(jsonMiddleware(jsonHandler))
|
|
92
|
+
.get('/', (_req, res) => res.status(200).json({ a: 'a' }).end())
|
|
93
|
+
const response = await request(server).get('/')
|
|
94
|
+
|
|
95
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
96
|
+
|
|
97
|
+
expect(response.status).toStrictEqual(200)
|
|
98
|
+
expect(response.body).toStrictEqual(expected)
|
|
99
|
+
expect(response.headers).toStrictEqual(
|
|
100
|
+
expect.objectContaining({
|
|
101
|
+
'x-inspected-by': 'me',
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it.each([
|
|
107
|
+
[inspect, header],
|
|
108
|
+
[inspectAsync, header],
|
|
109
|
+
[inspect, headerAsync],
|
|
110
|
+
[inspectAsync, headerAsync],
|
|
111
|
+
])('end should work with jsonp', async (jsonHandler, endHandler) => {
|
|
112
|
+
const server = express()
|
|
113
|
+
.use(jsonpMiddleware(jsonHandler))
|
|
114
|
+
.use(endMiddleware(endHandler))
|
|
115
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
116
|
+
const response = await request(server).get('/')
|
|
117
|
+
|
|
118
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
119
|
+
|
|
120
|
+
expect(response.status).toStrictEqual(200)
|
|
121
|
+
expect(response.body).toStrictEqual(expected)
|
|
122
|
+
expect(response.headers).toStrictEqual(
|
|
123
|
+
expect.objectContaining({
|
|
124
|
+
'x-inspected-by': 'me',
|
|
125
|
+
})
|
|
126
|
+
)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it.each([
|
|
130
|
+
[inspect, header],
|
|
131
|
+
[inspectAsync, header],
|
|
132
|
+
[inspect, headerAsync],
|
|
133
|
+
[inspectAsync, headerAsync],
|
|
134
|
+
])('end should work with json reverse', async (jsonHandler, endHandler) => {
|
|
135
|
+
const server = express()
|
|
136
|
+
.use(endMiddleware(endHandler))
|
|
137
|
+
.use(jsonpMiddleware(jsonHandler))
|
|
138
|
+
.get('/', (_req, res) => res.status(200).jsonp({ a: 'a' }).end())
|
|
139
|
+
const response = await request(server).get('/')
|
|
140
|
+
|
|
141
|
+
const expected = { a: 'a', inspected_by: 'me' }
|
|
142
|
+
|
|
143
|
+
expect(response.status).toStrictEqual(200)
|
|
144
|
+
expect(response.body).toStrictEqual(expected)
|
|
145
|
+
expect(response.headers).toStrictEqual(
|
|
146
|
+
expect.objectContaining({
|
|
147
|
+
'x-inspected-by': 'me',
|
|
148
|
+
})
|
|
149
|
+
)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it.each([
|
|
153
|
+
[modifyText, header],
|
|
154
|
+
[modifyTextAsync, header],
|
|
155
|
+
[modifyText, headerAsync],
|
|
156
|
+
[modifyTextAsync, headerAsync],
|
|
157
|
+
])('end should work with send', async (sendHandler, endHandler) => {
|
|
158
|
+
const server = express()
|
|
159
|
+
.use(sendMiddleware(sendHandler))
|
|
160
|
+
.use(endMiddleware(endHandler))
|
|
161
|
+
.get('/', (_req, res) => {
|
|
162
|
+
res.send('This is the response body')
|
|
163
|
+
})
|
|
164
|
+
const response = await request(server).get('/')
|
|
165
|
+
|
|
166
|
+
expect(response.status).toStrictEqual(200)
|
|
167
|
+
expect(response.text).toStrictEqual(
|
|
168
|
+
'This is the response body with more content'
|
|
169
|
+
)
|
|
170
|
+
expect(response.headers).toStrictEqual(
|
|
171
|
+
expect.objectContaining({
|
|
172
|
+
'x-inspected-by': 'me',
|
|
173
|
+
})
|
|
174
|
+
)
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it.each([
|
|
178
|
+
[modifyText, header],
|
|
179
|
+
[modifyTextAsync, header],
|
|
180
|
+
[modifyText, headerAsync],
|
|
181
|
+
[modifyTextAsync, headerAsync],
|
|
182
|
+
])('end should work with send', async (sendHandler, endHandler) => {
|
|
183
|
+
const server = express()
|
|
184
|
+
.use(endMiddleware(endHandler))
|
|
185
|
+
.use(sendMiddleware(sendHandler))
|
|
186
|
+
.get('/', (_req, res) => {
|
|
187
|
+
res.send('This is the response body')
|
|
188
|
+
})
|
|
189
|
+
const response = await request(server).get('/')
|
|
190
|
+
|
|
191
|
+
expect(response.status).toStrictEqual(200)
|
|
192
|
+
expect(response.text).toStrictEqual(
|
|
193
|
+
'This is the response body with more content'
|
|
194
|
+
)
|
|
195
|
+
expect(response.headers).toStrictEqual(
|
|
196
|
+
expect.objectContaining({
|
|
197
|
+
'x-inspected-by': 'me',
|
|
198
|
+
})
|
|
199
|
+
)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it.each([
|
|
203
|
+
[appendText, header],
|
|
204
|
+
[appendTextAsync, header],
|
|
205
|
+
[appendText, headerAsync],
|
|
206
|
+
[appendTextAsync, headerAsync],
|
|
207
|
+
])('end should work with write', async (writeHandler, endHandler) => {
|
|
208
|
+
const server = express()
|
|
209
|
+
.use(endMiddleware(endHandler))
|
|
210
|
+
.use(writeMiddleware(writeHandler))
|
|
211
|
+
.get('/', (_req, res) => {
|
|
212
|
+
res.status(200).write('This is the response body')
|
|
213
|
+
res.end()
|
|
214
|
+
})
|
|
215
|
+
const response = await request(server).get('/')
|
|
216
|
+
|
|
217
|
+
expect(response.status).toStrictEqual(200)
|
|
218
|
+
expect(response.text).toStrictEqual(
|
|
219
|
+
'This is the response body with more content'
|
|
220
|
+
)
|
|
221
|
+
expect(response.headers).toStrictEqual(
|
|
222
|
+
expect.not.objectContaining({
|
|
223
|
+
'x-inspected-by': 'me',
|
|
224
|
+
})
|
|
225
|
+
)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it.each([
|
|
229
|
+
[appendText, header],
|
|
230
|
+
[appendTextAsync, header],
|
|
231
|
+
[appendText, headerAsync],
|
|
232
|
+
[appendTextAsync, headerAsync],
|
|
233
|
+
])('end should work with write reverse', async (writeHandler, endHandler) => {
|
|
234
|
+
const server = express()
|
|
235
|
+
.use(endMiddleware(endHandler))
|
|
236
|
+
.use(writeMiddleware(writeHandler))
|
|
237
|
+
.get('/', (_req, res) => {
|
|
238
|
+
res.status(200).write('This is the response body')
|
|
239
|
+
res.end()
|
|
240
|
+
})
|
|
241
|
+
const response = await request(server).get('/')
|
|
242
|
+
|
|
243
|
+
expect(response.status).toStrictEqual(200)
|
|
244
|
+
expect(response.text).toStrictEqual(
|
|
245
|
+
'This is the response body with more content'
|
|
246
|
+
)
|
|
247
|
+
expect(response.headers).toStrictEqual(
|
|
248
|
+
expect.not.objectContaining({
|
|
249
|
+
'x-inspected-by': 'me',
|
|
250
|
+
})
|
|
251
|
+
)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { sendMiddleware, TransformSend } from '../sendMiddleware'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import express from 'express'
|
|
4
|
+
import { expect } from 'vitest'
|
|
5
|
+
import { sleep } from './testHelpers/sleep'
|
|
6
|
+
|
|
7
|
+
describe('sendMiddleware', () => {
|
|
8
|
+
const modifyText: TransformSend = data => {
|
|
9
|
+
return data + ' with more content'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const modifyTextAsync: TransformSend = async data => {
|
|
13
|
+
await sleep()
|
|
14
|
+
return data + ' with more content'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const modifyText2: TransformSend = data => {
|
|
18
|
+
return data + ' with more content 2'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const modifyTextAsync2: TransformSend = async data => {
|
|
22
|
+
await sleep()
|
|
23
|
+
return data + ' with more content 2'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const modifyJson: TransformSend = data => {
|
|
27
|
+
if (typeof data === 'object') {
|
|
28
|
+
;(data as any).b = 'b'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return data
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const modifyJsonAsync: TransformSend = async data => {
|
|
35
|
+
await sleep()
|
|
36
|
+
|
|
37
|
+
if (typeof data === 'object') {
|
|
38
|
+
;(data as any).b = 'b'
|
|
39
|
+
}
|
|
40
|
+
await sleep()
|
|
41
|
+
|
|
42
|
+
return data
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const error: TransformSend = data => {
|
|
46
|
+
;(data as any).foo.bar.hopefully.fails()
|
|
47
|
+
return data
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const errorAsync: TransformSend = async data => {
|
|
51
|
+
await sleep()
|
|
52
|
+
;(data as any).foo.bar.hopefully.fails()
|
|
53
|
+
await sleep()
|
|
54
|
+
return data
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const error403: TransformSend = (data, _req, res) => {
|
|
58
|
+
res.status(403).end()
|
|
59
|
+
return data
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const error403Async: TransformSend = async (data, _req, res) => {
|
|
63
|
+
await sleep()
|
|
64
|
+
res.status(403).end()
|
|
65
|
+
await sleep()
|
|
66
|
+
|
|
67
|
+
return data
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
it.each([modifyText, modifyTextAsync])(
|
|
71
|
+
'should return the modified text result',
|
|
72
|
+
async handler => {
|
|
73
|
+
const server = express()
|
|
74
|
+
.use(sendMiddleware(handler))
|
|
75
|
+
.get('/', (_req, res) => {
|
|
76
|
+
res.send('This is the response body')
|
|
77
|
+
})
|
|
78
|
+
const response = await request(server).get('/')
|
|
79
|
+
|
|
80
|
+
expect(response.text).toStrictEqual(
|
|
81
|
+
'This is the response body with more content'
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
it.each([
|
|
87
|
+
[modifyText, modifyText2],
|
|
88
|
+
[modifyText, modifyTextAsync2],
|
|
89
|
+
[modifyTextAsync, modifyText2],
|
|
90
|
+
[modifyTextAsync, modifyTextAsync2],
|
|
91
|
+
])('should work with multiple middleware', async (handler, handler2) => {
|
|
92
|
+
const server = express()
|
|
93
|
+
.use(sendMiddleware(handler))
|
|
94
|
+
.use(sendMiddleware(handler2))
|
|
95
|
+
.get('/', (_req, res) => res.send('This is the response body'))
|
|
96
|
+
const response = await request(server).get('/')
|
|
97
|
+
|
|
98
|
+
expect(response.text).toStrictEqual(
|
|
99
|
+
'This is the response body with more content 2 with more content'
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it.each([modifyJson, modifyJsonAsync])(
|
|
104
|
+
'should return a modified json body ',
|
|
105
|
+
async handler => {
|
|
106
|
+
const server = express()
|
|
107
|
+
.use(sendMiddleware(handler))
|
|
108
|
+
.get('/', (_req, res) => {
|
|
109
|
+
res.send({
|
|
110
|
+
a: 'a',
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
const response = await request(server).get('/')
|
|
114
|
+
|
|
115
|
+
expect(response.status).toStrictEqual(200)
|
|
116
|
+
expect(response.body).toStrictEqual({
|
|
117
|
+
a: 'a',
|
|
118
|
+
b: 'b',
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
it.each([modifyText, modifyTextAsync])(
|
|
124
|
+
'should send an error response',
|
|
125
|
+
async handler => {
|
|
126
|
+
const server = express()
|
|
127
|
+
.use(sendMiddleware(handler))
|
|
128
|
+
.get('/', (_req, res) => {
|
|
129
|
+
res.status(404).send('This is the response body')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const response = await request(server).get('/')
|
|
133
|
+
|
|
134
|
+
expect(response.status).toStrictEqual(404)
|
|
135
|
+
|
|
136
|
+
expect(response.text).toStrictEqual(
|
|
137
|
+
'This is the response body with more content'
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
it.each([error403, error403Async])(
|
|
143
|
+
'should abort if a response is sent',
|
|
144
|
+
async handler => {
|
|
145
|
+
const server = express()
|
|
146
|
+
.use(sendMiddleware(handler))
|
|
147
|
+
.get('/', (_req, res) => {
|
|
148
|
+
res.status(200).send('This is the response body')
|
|
149
|
+
})
|
|
150
|
+
const response = await request(server).get('/')
|
|
151
|
+
|
|
152
|
+
expect(response.status).toStrictEqual(403)
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
it.each([error, errorAsync])(
|
|
157
|
+
'should 500 on a synchronous exception',
|
|
158
|
+
async handler => {
|
|
159
|
+
const server = express()
|
|
160
|
+
.use(sendMiddleware(handler))
|
|
161
|
+
.get('/', (_req, res) => {
|
|
162
|
+
res.status(200).send('This is the response body')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const response = await request(server).get('/')
|
|
166
|
+
|
|
167
|
+
expect(response.status).toStrictEqual(500)
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
it.each([error, errorAsync])(
|
|
172
|
+
'should 500 on an asynchronous exception',
|
|
173
|
+
async handler => {
|
|
174
|
+
const server = express()
|
|
175
|
+
.use(sendMiddleware(handler))
|
|
176
|
+
.get('/', (_req, res) => {
|
|
177
|
+
process.nextTick(() => {
|
|
178
|
+
res.status(200).send('This is the response body')
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const response = await request(server).get('/')
|
|
183
|
+
|
|
184
|
+
expect(response.status).toStrictEqual(500)
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const sleep = () => new Promise(resolve => setTimeout(resolve, 100))
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import express, { ErrorRequestHandler } from 'express'
|
|
2
|
+
import request from 'supertest'
|
|
3
|
+
import { TransformChunk, writeMiddleware } from '../index'
|
|
4
|
+
import { expect } from 'vitest'
|
|
5
|
+
import { sleep } from './testHelpers/sleep'
|
|
6
|
+
|
|
7
|
+
describe('write', () => {
|
|
8
|
+
const appendText: TransformChunk = chunk => {
|
|
9
|
+
return chunk + ' with more content'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const appendTextAsync: TransformChunk = async chunk => {
|
|
13
|
+
await sleep()
|
|
14
|
+
return chunk + ' with more content'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const appendText2: TransformChunk = chunk => {
|
|
18
|
+
return chunk + ' with more content2'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const appendTextAsync2: TransformChunk = async chunk => {
|
|
22
|
+
await sleep()
|
|
23
|
+
return chunk + ' with more content2'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const inspectJson: TransformChunk = chunk => {
|
|
27
|
+
try {
|
|
28
|
+
const json = JSON.parse(String(chunk))
|
|
29
|
+
json.inspected_by = 'me'
|
|
30
|
+
return JSON.stringify(json)
|
|
31
|
+
} catch (e) {
|
|
32
|
+
console.log('JSON parse error')
|
|
33
|
+
throw e
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const inspectJsonAsync: TransformChunk = chunk => {
|
|
38
|
+
try {
|
|
39
|
+
sleep()
|
|
40
|
+
const json = JSON.parse(String(chunk))
|
|
41
|
+
json.inspected_by = 'me'
|
|
42
|
+
sleep()
|
|
43
|
+
return JSON.stringify(json)
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.log('JSON parse error')
|
|
46
|
+
throw e
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const error: TransformChunk = chunk => {
|
|
51
|
+
;(chunk as any).foo.bar.hopefully.fails()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const errorAsync: TransformChunk = chunk => {
|
|
55
|
+
;(chunk as any).foo.bar.hopefully.fails()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const error403: TransformChunk = (_chunk, _encoding, _req, res) => {
|
|
59
|
+
res.status(403).json({ foo: 'bar ' })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const error403Async: TransformChunk = (_chunk, _encoding, _req, res) => {
|
|
63
|
+
res.status(403).json({ foo: 'bar ' })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
it.each([appendText, appendTextAsync])(
|
|
67
|
+
'should return the text result',
|
|
68
|
+
async handler => {
|
|
69
|
+
const server = express()
|
|
70
|
+
.use(writeMiddleware(handler))
|
|
71
|
+
.get('/', (_req, res) => {
|
|
72
|
+
res.status(200).write('This is the response body')
|
|
73
|
+
res.end()
|
|
74
|
+
})
|
|
75
|
+
const response = await request(server).get('/')
|
|
76
|
+
|
|
77
|
+
expect(response.status).toStrictEqual(200)
|
|
78
|
+
expect(response.text).toStrictEqual(
|
|
79
|
+
'This is the response body with more content'
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
it.each([
|
|
85
|
+
[
|
|
86
|
+
appendText,
|
|
87
|
+
appendText2,
|
|
88
|
+
'This is the response body with more content2 with more content',
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
appendText,
|
|
92
|
+
appendTextAsync2,
|
|
93
|
+
'This is the response body with more content2 with more content',
|
|
94
|
+
],
|
|
95
|
+
[
|
|
96
|
+
appendTextAsync,
|
|
97
|
+
appendText2,
|
|
98
|
+
'This is the response body with more content2 with more content',
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
appendTextAsync,
|
|
102
|
+
appendTextAsync2,
|
|
103
|
+
'This is the response body with more content2 with more content',
|
|
104
|
+
],
|
|
105
|
+
])(
|
|
106
|
+
'should work with multiple middleware',
|
|
107
|
+
async (handler, handler2, expectStr) => {
|
|
108
|
+
const server = express()
|
|
109
|
+
.use(writeMiddleware(handler))
|
|
110
|
+
.use(writeMiddleware(handler2))
|
|
111
|
+
.get('/', (_req, res) => {
|
|
112
|
+
res.status(200).write('This is the response body')
|
|
113
|
+
res.end()
|
|
114
|
+
})
|
|
115
|
+
const response = await request(server).get('/')
|
|
116
|
+
|
|
117
|
+
expect(response.status).toStrictEqual(200)
|
|
118
|
+
expect(response.text).toStrictEqual(expectStr)
|
|
119
|
+
}
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
it.each([inspectJson, inspectJsonAsync])(
|
|
123
|
+
'should return a `body` when the content type is application/json',
|
|
124
|
+
async handler => {
|
|
125
|
+
const server = express()
|
|
126
|
+
.use(writeMiddleware(handler))
|
|
127
|
+
.get('/', (_req, res) => {
|
|
128
|
+
res
|
|
129
|
+
.set('Content-Type', 'application/json')
|
|
130
|
+
.status(200)
|
|
131
|
+
.write(
|
|
132
|
+
JSON.stringify({
|
|
133
|
+
a: 'a',
|
|
134
|
+
})
|
|
135
|
+
)
|
|
136
|
+
res.end()
|
|
137
|
+
})
|
|
138
|
+
const response = await request(server).get('/')
|
|
139
|
+
|
|
140
|
+
expect(response.status).toStrictEqual(200)
|
|
141
|
+
expect(response.body).toStrictEqual({ a: 'a', inspected_by: 'me' })
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
it.each([appendText, appendTextAsync])(
|
|
146
|
+
'should call callback an error response',
|
|
147
|
+
async handler => {
|
|
148
|
+
const server = express()
|
|
149
|
+
.use(writeMiddleware(handler))
|
|
150
|
+
.get('/', (_req, res) => {
|
|
151
|
+
res.status(404).write('This is the response body')
|
|
152
|
+
res.end()
|
|
153
|
+
})
|
|
154
|
+
const response = await request(server).get('/')
|
|
155
|
+
|
|
156
|
+
expect(response.status).toStrictEqual(404)
|
|
157
|
+
expect(response.text).toStrictEqual(
|
|
158
|
+
'This is the response body with more content'
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
it.each([error403, error403Async])(
|
|
164
|
+
'should abort if a response is sent',
|
|
165
|
+
async handler => {
|
|
166
|
+
const server = express()
|
|
167
|
+
.use(writeMiddleware(handler))
|
|
168
|
+
.get('/', (_req, res) => {
|
|
169
|
+
res
|
|
170
|
+
.set('Content-Type', 'application/json')
|
|
171
|
+
.status(200)
|
|
172
|
+
.write('This is the response body')
|
|
173
|
+
res.end()
|
|
174
|
+
})
|
|
175
|
+
const response = await request(server).get('/')
|
|
176
|
+
|
|
177
|
+
expect(response.status).to.equal(403)
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
it.each([error, errorAsync])(
|
|
182
|
+
'should 500 on a synchronous exception',
|
|
183
|
+
async handler => {
|
|
184
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
185
|
+
res.status(500).send(err.message).end()
|
|
186
|
+
const server = express()
|
|
187
|
+
.use(errorHandler)
|
|
188
|
+
.use(writeMiddleware(handler))
|
|
189
|
+
.get('/', (_req, res) => {
|
|
190
|
+
res
|
|
191
|
+
.set('Content-Type', 'application/json')
|
|
192
|
+
.status(200)
|
|
193
|
+
.write('This is the response body')
|
|
194
|
+
res.end()
|
|
195
|
+
})
|
|
196
|
+
const response = await request(server).get('/')
|
|
197
|
+
|
|
198
|
+
expect(response.status).toStrictEqual(500)
|
|
199
|
+
}
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
it.each([error, errorAsync])(
|
|
203
|
+
'should 500 on an asynchronous exception',
|
|
204
|
+
async handler => {
|
|
205
|
+
const errorHandler: ErrorRequestHandler = (err, _req, res, _next) =>
|
|
206
|
+
res.status(500).send(err.message).end()
|
|
207
|
+
const server = express()
|
|
208
|
+
.use(errorHandler)
|
|
209
|
+
.use(writeMiddleware(handler))
|
|
210
|
+
.get('/', (_req, res) => {
|
|
211
|
+
process.nextTick(() => {
|
|
212
|
+
res
|
|
213
|
+
.set('Content-Type', 'application/json')
|
|
214
|
+
.status(200)
|
|
215
|
+
.write('This is the response body')
|
|
216
|
+
res.end()
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
const response = await request(server).get('/')
|
|
220
|
+
|
|
221
|
+
expect(response.status).toStrictEqual(500)
|
|
222
|
+
}
|
|
223
|
+
)
|
|
224
|
+
})
|