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