Skip to content

Commit 6f6eec7

Browse files
kavutj
authored andcommitted
add 'etag' option
1 parent a4e93c0 commit 6f6eec7

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

lib/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ app.init = function(){
4747
app.defaultConfiguration = function(){
4848
// default settings
4949
this.enable('x-powered-by');
50+
this.enable('etag');
5051
this.set('env', process.env.NODE_ENV || 'development');
5152
debug('booting in %s mode', this.get('env'));
5253

lib/response.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ res.send = function(body){
8282
, head = 'HEAD' == req.method
8383
, len;
8484

85+
// settings
86+
var app = this.app;
87+
8588
// allow status / body
8689
if (2 == arguments.length) {
8790
// res.send(body, status) backwards compat
@@ -128,7 +131,7 @@ res.send = function(body){
128131

129132
// ETag support
130133
// TODO: W/ support
131-
if (len > 1024) {
134+
if (app.settings.etag && len > 1024) {
132135
if (!this.get('ETag')) {
133136
this.set('ETag', etag(body));
134137
}
@@ -241,7 +244,7 @@ res.jsonp = function(obj){
241244

242245
/**
243246
* Transfer the file at the given `path`.
244-
*
247+
*
245248
* Automatically sets the _Content-Type_ response header field.
246249
* The callback `fn(err)` is invoked when the transfer is complete
247250
* or when an error occurs. Be sure to check `res.sentHeader`
@@ -263,7 +266,7 @@ res.jsonp = function(obj){
263266
* app.get('/user/:uid/photos/:file', function(req, res){
264267
* var uid = req.params.uid
265268
* , file = req.params.file;
266-
*
269+
*
267270
* req.user.mayViewFilesFrom(uid, function(yes){
268271
* if (yes) {
269272
* res.sendfile('/uploads/' + uid + '/' + file);
@@ -408,11 +411,11 @@ res.type = function(type){
408411
* 'text/plain': function(){
409412
* res.send('hey');
410413
* },
411-
*
414+
*
412415
* 'text/html': function(){
413416
* res.send('<p>hey</p>');
414417
* },
415-
*
418+
*
416419
* 'appliation/json': function(){
417420
* res.send({ message: 'hey' });
418421
* }
@@ -425,11 +428,11 @@ res.type = function(type){
425428
* text: function(){
426429
* res.send('hey');
427430
* },
428-
*
431+
*
429432
* html: function(){
430433
* res.send('<p>hey</p>');
431434
* },
432-
*
435+
*
433436
* json: function(){
434437
* res.send({ message: 'hey' });
435438
* }
@@ -498,15 +501,15 @@ res.attachment = function(filename){
498501
* res.set('Accept', 'application/json');
499502
* res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
500503
*
501-
* Aliased as `res.header()`.
504+
* Aliased as `res.header()`.
502505
*
503506
* @param {String|Object} field
504507
* @param {String} val
505508
* @return {ServerResponse} for chaining
506509
* @api public
507510
*/
508511

509-
res.set =
512+
res.set =
510513
res.header = function(field, val){
511514
if (2 == arguments.length) {
512515
this.setHeader(field, '' + val);
@@ -602,7 +605,7 @@ res.cookie = function(name, val, options){
602605
* Mounting:
603606
*
604607
* 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 "/". For
606609
* example suppose a "blog" app is mounted at "/blog",
607610
* the following redirect would result in "/blog/login":
608611
*

test/res.send.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,62 @@ describe('res', function(){
298298
.get('/?callback=foo')
299299
.expect('{"foo":"bar"}', done);
300300
})
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+
})
301359
})

0 commit comments

Comments
 (0)