add 'etag' option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Max Riveiro · committed by TJ Holowaychuk · parent a4e93c0 · Browse files · 3 files·+72 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+72 added−10 removed
- tests:+58 added−0 removed
- other:+14 added−10 removed
Provenance
- Commit
6f6eec7- Tree
95b5214- Author
Max Riveiro <kavu13@gmail.com>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
a4e93c0
3 files changed+72 added−10 removed
modified:lib/application.js+1 added
45 unmodified lines46 47 app.defaultConfiguration = function(){48 // default settings49 this.enable('x-powered-by');50+ this.enable('etag');51 this.set('env', process.env.NODE_ENV || 'development');52 debug('booting in %s mode', this.get('env'));53 54 // implicit middleware477 unmodified linesmodified:lib/response.js+13 added-10 removed
80 unmodified lines81 var req = this.req82 , head = 'HEAD' == req.method83 , len;84 85+ // settings86+ var app = this.app;87+88 // allow status / body89 if (2 == arguments.length) {90 // res.send(body, status) backwards compat91 if ('number' != typeof body && 'number' == typeof arguments[1]) {38 unmodified lines130 }131 132 // ETag support133 // TODO: W/ support131- if (len > 1024) {134+ if (app.settings.etag && len > 1024) {135 if (!this.get('ETag')) {136 this.set('ETag', etag(body));137 }138 }104 unmodified lines243 };244 245 /**246 * Transfer the file at the given `path`.244- * 247+ *248 * Automatically sets the _Content-Type_ response header field.249 * The callback `fn(err)` is invoked when the transfer is complete250 * or when an error occurs. Be sure to check `res.sentHeader`251 * if you wish to attempt responding, as the header and some data13 unmodified lines265 *266 * app.get('/user/:uid/photos/:file', function(req, res){267 * var uid = req.params.uid268 * , file = req.params.file;266- * 269+ *270 * req.user.mayViewFilesFrom(uid, function(yes){271 * if (yes) {272 * res.sendfile('/uploads/' + uid + '/' + file);273 * } else {136 unmodified lines410 * res.format({411 * 'text/plain': function(){412 * res.send('hey');413 * },411- * 414+ *415 * 'text/html': function(){416 * res.send('<p>hey</p>');417 * },415- * 418+ *419 * 'appliation/json': function(){420 * res.send({ message: 'hey' });421 * }422 * });4 unmodified lines427 * res.format({428 * text: function(){429 * res.send('hey');430 * },428- * 431+ *432 * html: function(){433 * res.send('<p>hey</p>');434 * },432- * 435+ *436 * json: function(){437 * res.send({ message: 'hey' });438 * }439 * });60 unmodified lines500 *501 * res.set('Accept', 'application/json');502 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });503 *501- * Aliased as `res.header()`. 504+ * Aliased as `res.header()`.505 *506 * @param {String|Object} field507 * @param {String} val508 * @return {ServerResponse} for chaining509 * @api public510 */511 509-res.set = 512+res.set =513 res.header = function(field, val){514 if (2 == arguments.length) {515 this.setHeader(field, '' + val);516 } else {87 unmodified lines604 *605 * Mounting:606 *607 * When an application is mounted, and `res.redirect()`605- * is given a path that does _not_ lead with "/". For 608+ * is given a path that does _not_ lead with "/". For609 * example suppose a "blog" app is mounted at "/blog",610 * the following redirect would result in "/blog/login":611 *612 * res.redirect('login');108 unmodified linesmodified:test/res.send.js+58 added
296 unmodified lines297 request(app)298 .get('/?callback=foo')299 .expect('{"foo":"bar"}', done);300 })301+302+ describe('"etag" setting', function(){303+ describe('when enabled', function(){304+ it('should send ETag ', function(done){305+ var app = express();306+307+ app.use(function(req, res){308+ var str = Array(1024 * 2).join('-');309+ res.send(str);310+ });311+312+ request(app)313+ .get('/')314+ .end(function(err, res){315+ res.headers.should.have.property('etag', '"-1498647312"');316+ done();317+ });318+ });319+ });320+321+ describe('when disabled', function(){322+ it('should send no ETag', function(done){323+ var app = express();324+325+ app.use(function(req, res){326+ var str = Array(1024 * 2).join('-');327+ res.send(str);328+ });329+330+ app.disable('etag');331+332+ request(app)333+ .get('/')334+ .end(function(err, res){335+ res.headers.should.not.have.property('etag');336+ done();337+ });338+ });339+340+ it('should send ETag when manually set', function(done){341+ var app = express();342+343+ app.disable('etag');344+345+ app.use(function(req, res){346+ res.set('etag', 1);347+ res.send(200);348+ });349+350+ request(app)351+ .get('/')352+ .end(function(err, res){353+ res.headers.should.have.property('etag');354+ done();355+ });356+ });357+ });358+ })359 })