Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ app.handle = function handle(req, res, callback) {
req.res = res;
res.req = req;

// restore the prototypes for an express caller
if (callback) {
var reqProto = Object.getPrototypeOf(req);
var resProto = Object.getPrototypeOf(res);

if (reqProto.app) {
done = function (err) {
Object.setPrototypeOf(req, reqProto)
Object.setPrototypeOf(res, resProto)
callback(err);
};
}
}

// alter the prototypes
Object.setPrototypeOf(req, this.request)
Object.setPrototypeOf(res, this.response)
Expand Down
76 changes: 76 additions & 0 deletions test/app.use.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,82 @@ describe('app', function(){
.expect(200, 'forum', cb)
})

it('should restore the prototypes when mounted on a router', function(done){
var blog = express()
, app = express()
, router = express.Router();

blog.get('/', function(req, res, next){
next();
});

router.use('/blog', blog);
app.use(router);

app.use(function(req, res){
var restored = Object.getPrototypeOf(req) === app.request
&& Object.getPrototypeOf(res) === app.response;
res.end(restored ? 'yes' : 'no');
});

request(app)
.get('/blog')
.expect(200, 'yes', done);
})

it('should restore the prototypes when a router-mounted app calls next(err)', function(done){
var blog = express()
, app = express()
, router = express.Router();

blog.get('/', function(req, res, next){
next(new Error('boom'));
});

router.use('/blog', blog);
app.use(router);

app.use(function(err, req, res, next){
var restored = Object.getPrototypeOf(req) === app.request
&& Object.getPrototypeOf(res) === app.response;
res.status(500).end(restored ? 'yes' : 'no');
});

request(app)
.get('/blog')
.expect(500, 'yes', done);
})

it('should not restore the prototypes when a bare router mounts an app', function(done){
var child = express()
, router = express.Router();

child.use(function(req, res, next){
next();
});

router.use(child);

router.use(function(req, res){
var keptChildProto = Object.getPrototypeOf(req) === child.request
&& Object.getPrototypeOf(res) === child.response;
res.end(keptChildProto ? 'yes' : 'no');
});

var server = function(req, res){
router.handle(req, res, function(err){
if (err) {
res.statusCode = 500;
res.end(err.message);
}
});
};

request(server)
.get('/')
.expect(200, 'yes', done);
})

it('should set the child\'s .parent', function(){
var blog = express()
, app = express();
Expand Down