Skip to content

Commit 8db7efa

Browse files
committed
fix: merge req.body modifications into decoded request
Added test case to verify that middleware modifications to the req object persist through the middleware chain. This addresses the issue where normal Express middleware behavior wasn't being tested with a realistic use case.
1 parent c5000de commit 8db7efa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/express-wrapper/src/middleware.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,11 @@ export async function runMiddlewareChainIgnoringResults<
216216
}
217217
});
218218
}
219-
return input;
219+
return {
220+
...(req as any).decoded,
221+
...(req.body || {}),
222+
...(req.query || {}),
223+
...(req.params || {}),
224+
...(req.headers || {})
225+
};
220226
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ 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+
4375
test('should work with middleware that return values', async () => {
4476
const result = await runMiddlewareChain(
4577
{ foo: 'test' },

0 commit comments

Comments
 (0)