Skip to content

Commit 560bd65

Browse files
committed
✅ add discarded middleware test
1 parent 21df421 commit 560bd65

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/app.router.js

+50
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,56 @@ describe('app.router', function(){
11301130
var app = express();
11311131
assert.strictEqual(app.get('/', function () {}), app)
11321132
})
1133+
1134+
it('should should not use disposed router/middleware', function(done){
1135+
// more context: https://github.com/expressjs/express/issues/5743#issuecomment-2277148412
1136+
1137+
var app = express();
1138+
var router = new express.Router();
1139+
1140+
router.use(function(req, res, next){
1141+
res.setHeader('old', 'foo');
1142+
next();
1143+
});
1144+
1145+
app.use(function (req, res, next) {
1146+
return router.handle(req, res, next);
1147+
});
1148+
1149+
app.get('/', function(req, res, next){
1150+
res.send('yee');
1151+
next();
1152+
});
1153+
1154+
request(app)
1155+
.get('/')
1156+
.expect('old', 'foo')
1157+
.expect(function(res) {
1158+
if (typeof res.headers['new'] !== 'undefined') {
1159+
throw new Error('`new` header should not be present');
1160+
}
1161+
})
1162+
.expect(200, 'yee', function(err, res) {
1163+
if (err) return done(err);
1164+
1165+
router = new express.Router();
1166+
1167+
router.use(function(req, res, next){
1168+
res.setHeader('new', 'bar');
1169+
next();
1170+
});
1171+
1172+
request(app)
1173+
.get('/')
1174+
.expect('new', 'bar')
1175+
.expect(function(res) {
1176+
if (typeof res.headers['old'] !== 'undefined') {
1177+
throw new Error('`old` header should not be present');
1178+
}
1179+
})
1180+
.expect(200, 'yee', done);
1181+
});
1182+
})
11331183
})
11341184

11351185
function supportsRegexp(source) {

0 commit comments

Comments
 (0)