diff --git a/test/app.request.js b/test/app.request.js index 728043a5a3..0288f80b09 100644 --- a/test/app.request.js +++ b/test/app.request.js @@ -1,4 +1,5 @@ +var after = require('after') var express = require('../') , request = require('supertest'); @@ -19,5 +20,123 @@ describe('app', function(){ .get('/foo?name=tobi') .expect('name=tobi', done); }) + + it('should only extend for the referenced app', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.request.foobar = function () { + return 'tobi' + } + + app1.get('/', function (req, res) { + res.send(req.foobar()) + }) + + app2.get('/', function (req, res) { + res.send(req.foobar()) + }) + + request(app1) + .get('/') + .expect(200, 'tobi', cb) + + request(app2) + .get('/') + .expect(500, /(?:not a function|has no method)/, cb) + }) + + it('should inherit to sub apps', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.request.foobar = function () { + return 'tobi' + } + + app1.use('/sub', app2) + + app1.get('/', function (req, res) { + res.send(req.foobar()) + }) + + app2.get('/', function (req, res) { + res.send(req.foobar()) + }) + + request(app1) + .get('/') + .expect(200, 'tobi', cb) + + request(app1) + .get('/sub') + .expect(200, 'tobi', cb) + }) + + it('should allow sub app to override', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.request.foobar = function () { + return 'tobi' + } + + app2.request.foobar = function () { + return 'loki' + } + + app1.use('/sub', app2) + + app1.get('/', function (req, res) { + res.send(req.foobar()) + }) + + app2.get('/', function (req, res) { + res.send(req.foobar()) + }) + + request(app1) + .get('/') + .expect(200, 'tobi', cb) + + request(app1) + .get('/sub') + .expect(200, 'loki', cb) + }) + + it('should not pollute parent app', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.request.foobar = function () { + return 'tobi' + } + + app2.request.foobar = function () { + return 'loki' + } + + app1.use('/sub', app2) + + app1.get('/sub/foo', function (req, res) { + res.send(req.foobar()) + }) + + app2.get('/', function (req, res) { + res.send(req.foobar()) + }) + + request(app1) + .get('/sub') + .expect(200, 'loki', cb) + + request(app1) + .get('/sub/foo') + .expect(200, 'tobi', cb) + }) }) }) diff --git a/test/app.response.js b/test/app.response.js index c6ea77c820..ea696b566f 100644 --- a/test/app.response.js +++ b/test/app.response.js @@ -1,4 +1,5 @@ +var after = require('after') var express = require('../') , request = require('supertest'); @@ -20,25 +21,122 @@ describe('app', function(){ .expect('HEY', done); }) - it('should not be influenced by other app protos', function(done){ - var app = express() - , app2 = express(); + it('should only extend for the referenced app', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) - app.response.shout = function(str){ - this.send(str.toUpperCase()); - }; + app1.response.shout = function (str) { + this.send(str.toUpperCase()) + } - app2.response.shout = function(str){ - this.send(str); - }; + app1.get('/', function (req, res) { + res.shout('foo') + }) - app.use(function(req, res){ - res.shout('hey'); - }); + app2.get('/', function (req, res) { + res.shout('foo') + }) - request(app) - .get('/') - .expect('HEY', done); + request(app1) + .get('/') + .expect(200, 'FOO', cb) + + request(app2) + .get('/') + .expect(500, /(?:not a function|has no method)/, cb) + }) + + it('should inherit to sub apps', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.response.shout = function (str) { + this.send(str.toUpperCase()) + } + + app1.use('/sub', app2) + + app1.get('/', function (req, res) { + res.shout('foo') + }) + + app2.get('/', function (req, res) { + res.shout('foo') + }) + + request(app1) + .get('/') + .expect(200, 'FOO', cb) + + request(app1) + .get('/sub') + .expect(200, 'FOO', cb) + }) + + it('should allow sub app to override', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.response.shout = function (str) { + this.send(str.toUpperCase()) + } + + app2.response.shout = function (str) { + this.send(str + '!') + } + + app1.use('/sub', app2) + + app1.get('/', function (req, res) { + res.shout('foo') + }) + + app2.get('/', function (req, res) { + res.shout('foo') + }) + + request(app1) + .get('/') + .expect(200, 'FOO', cb) + + request(app1) + .get('/sub') + .expect(200, 'foo!', cb) + }) + + it('should not pollute parent app', function (done) { + var app1 = express() + var app2 = express() + var cb = after(2, done) + + app1.response.shout = function (str) { + this.send(str.toUpperCase()) + } + + app2.response.shout = function (str) { + this.send(str + '!') + } + + app1.use('/sub', app2) + + app1.get('/sub/foo', function (req, res) { + res.shout('foo') + }) + + app2.get('/', function (req, res) { + res.shout('foo') + }) + + request(app1) + .get('/sub') + .expect(200, 'foo!', cb) + + request(app1) + .get('/sub/foo') + .expect(200, 'FOO', cb) }) }) })