app.response.js
This file
Path
test/app.response.js
143 lines · 2.9 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144'use strict'
var after = require('after')var express = require('../') , request = require('supertest');
describe('app', function(){ describe('.response', function(){ it('should extend the response prototype', function(done){ var app = express();
app.response.shout = function(str){ this.send(str.toUpperCase()); };
app.use(function(req, res){ res.shout('hey'); });
request(app) .get('/') .expect('HEY', done); })
it('should only extend for the referenced app', function (done) { var app1 = express() var app2 = express() var cb = after(2, done)
app1.response.shout = function (str) { this.send(str.toUpperCase()) }
app1.get('/', function (req, res) { res.shout('foo') })
app2.get('/', function (req, res) { res.shout('foo') })
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) }) })})