Skip to content

Commit

Permalink
tests: add more tests for app.request & app.response
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 2, 2022
1 parent 141914e commit 00ad5be
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 15 deletions.
119 changes: 119 additions & 0 deletions test/app.request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var after = require('after')
var express = require('../')
, request = require('supertest');

Expand All @@ -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)
})
})
})
128 changes: 113 additions & 15 deletions test/app.response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var after = require('after')
var express = require('../')
, request = require('supertest');

Expand All @@ -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)
})
})
})

0 comments on commit 00ad5be

Please sign in to comment.