Skip to content

Commit ee2e4c1

Browse files
committed
test: verify middleware req.body/query/params/headers modifications reach handler
1 parent 6489613 commit ee2e4c1

File tree

3 files changed

+69
-38
lines changed

3 files changed

+69
-38
lines changed

packages/express-wrapper/src/middleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export async function runMiddlewareChainIgnoringResults<
202202
Input,
203203
Chain extends MiddlewareChain,
204204
>(
205-
input: Input,
205+
_input: Input,
206206
chain: Chain,
207207
req: express.Request,
208208
res: express.Response,
@@ -216,11 +216,11 @@ export async function runMiddlewareChainIgnoringResults<
216216
}
217217
});
218218
}
219-
return {
220-
...(req as any).decoded,
221-
...(req.body || {}),
222-
...(req.query || {}),
219+
return {
220+
...(req as any).decoded,
221+
...(req.body || {}),
222+
...(req.query || {}),
223223
...(req.params || {}),
224-
...(req.headers || {})
224+
...(req.headers || {}),
225225
};
226226
}

packages/express-wrapper/test/middleware.test.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,6 @@ test('should handle errors passed to next()', async () => {
4040
);
4141
});
4242

43-
test('middleware that modifies req should pass changes through chain', async () => {
44-
const testReq = { body: {}, headers: {} } as express.Request;
45-
const testRes = {} as express.Response;
46-
47-
const modifyReqMiddleware: express.RequestHandler = (req, _res, next) => {
48-
(req as any).userId = 123;
49-
next();
50-
};
51-
52-
const checkReqMiddleware: express.RequestHandler = (req, _res, next) => {
53-
assert.equal(
54-
(req as any).userId,
55-
123,
56-
'userId should still be on req in second middleware',
57-
);
58-
next();
59-
};
60-
61-
await runMiddlewareChain(
62-
{ foo: 'test' },
63-
[modifyReqMiddleware, noopMiddleware, checkReqMiddleware],
64-
testReq,
65-
testRes,
66-
);
67-
68-
assert.equal(
69-
(testReq as any).userId,
70-
123,
71-
'req should still have userId after chain',
72-
);
73-
});
74-
7543
test('should work with middleware that return values', async () => {
7644
const result = await runMiddlewareChain(
7745
{ foo: 'test' },

packages/express-wrapper/test/server.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,66 @@ test('should return a 400 when request fails to decode', async () => {
276276

277277
assert(response.body.error.startsWith('Invalid value undefined supplied to'));
278278
});
279+
280+
test('middleware that modifies req.body should reach handler even without routeHandler()', async () => {
281+
const PostWithData = httpRoute({
282+
path: '/data',
283+
method: 'POST',
284+
request: httpRequest({
285+
body: {
286+
originalField: t.string,
287+
addedByMiddleware: optional(t.string),
288+
},
289+
}),
290+
response: {
291+
200: t.type({
292+
originalField: t.string,
293+
addedByMiddleware: optional(t.string),
294+
}),
295+
},
296+
});
297+
298+
const testApiSpec = apiSpec({
299+
'test.route': {
300+
post: PostWithData,
301+
},
302+
});
303+
304+
const modifyBodyMiddleware: express.RequestHandler = (req, _res, next) => {
305+
req.body.addedByMiddleware = 'ADDED';
306+
next();
307+
};
308+
309+
const handler = async (params: {
310+
originalField: string;
311+
addedByMiddleware?: string;
312+
}) => {
313+
return {
314+
type: 200,
315+
payload: {
316+
originalField: params.originalField,
317+
addedByMiddleware: params.addedByMiddleware,
318+
},
319+
} as const;
320+
};
321+
322+
const app = createServer(testApiSpec, (app: express.Application) => {
323+
app.use(express.json());
324+
return {
325+
'test.route': {
326+
post: { middleware: [modifyBodyMiddleware], handler },
327+
},
328+
};
329+
});
330+
331+
const response = await supertest(app)
332+
.post('/data')
333+
.send({ originalField: 'test' })
334+
.expect(200);
335+
336+
assert.equal(
337+
response.body.addedByMiddleware,
338+
'ADDED',
339+
'addedByMiddleware should be present because req.body is part of req.decoded',
340+
);
341+
});

0 commit comments

Comments
 (0)