Skip to content

Commit 082d6d1

Browse files
authored
test: add discarded middleware test (#5819)
1 parent 94546a3 commit 082d6d1

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

test/app.router.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,56 @@ describe('app.router', function(){
11561156
var app = express();
11571157
assert.strictEqual(app.get('/', function () {}), app)
11581158
})
1159+
1160+
it('should should not use disposed router/middleware', function(done){
1161+
// more context: https://github.com/expressjs/express/issues/5743#issuecomment-2277148412
1162+
1163+
var app = express();
1164+
var router = new express.Router();
1165+
1166+
router.use(function(req, res, next){
1167+
res.setHeader('old', 'foo');
1168+
next();
1169+
});
1170+
1171+
app.use(function (req, res, next) {
1172+
return router.handle(req, res, next);
1173+
});
1174+
1175+
app.get('/', function(req, res, next){
1176+
res.send('yee');
1177+
next();
1178+
});
1179+
1180+
request(app)
1181+
.get('/')
1182+
.expect('old', 'foo')
1183+
.expect(function(res) {
1184+
if (typeof res.headers['new'] !== 'undefined') {
1185+
throw new Error('`new` header should not be present');
1186+
}
1187+
})
1188+
.expect(200, 'yee', function(err, res) {
1189+
if (err) return done(err);
1190+
1191+
router = new express.Router();
1192+
1193+
router.use(function(req, res, next){
1194+
res.setHeader('new', 'bar');
1195+
next();
1196+
});
1197+
1198+
request(app)
1199+
.get('/')
1200+
.expect('new', 'bar')
1201+
.expect(function(res) {
1202+
if (typeof res.headers['old'] !== 'undefined') {
1203+
throw new Error('`old` header should not be present');
1204+
}
1205+
})
1206+
.expect(200, 'yee', done);
1207+
});
1208+
})
11591209
})
11601210

11611211
function supportsRegexp(source) {

0 commit comments

Comments
 (0)