Skip to content

Commit

Permalink
Merge pull request #1694 from kavu/add_disable_etag
Browse files Browse the repository at this point in the history
Add application setting to disable ETag (again)
  • Loading branch information
tj committed Aug 2, 2013
2 parents 6942070 + 54a192a commit 610e172
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/application.js
Expand Up @@ -46,6 +46,7 @@ app.init = function(){
app.defaultConfiguration = function(){
// default settings
this.enable('x-powered-by');
this.enable('etag');
this.set('env', process.env.NODE_ENV || 'development');
this.set('subdomain offset', 2);
debug('booting in %s mode', this.get('env'));
Expand Down
5 changes: 4 additions & 1 deletion lib/response.js
Expand Up @@ -82,6 +82,9 @@ res.send = function(body){
var head = 'HEAD' == req.method;
var len;

// settings
var app = this.app;

// allow status / body
if (2 == arguments.length) {
// res.send(body, status) backwards compat
Expand Down Expand Up @@ -128,7 +131,7 @@ res.send = function(body){

// ETag support
// TODO: W/ support
if (len > 1024 && 'GET' == req.method) {
if (app.settings['etag'] && len > 1024 && 'GET' == req.method) {
if (!this.get('ETag')) {
this.set('ETag', etag(body));
}
Expand Down
58 changes: 58 additions & 0 deletions test/res.send.js
Expand Up @@ -318,4 +318,62 @@ describe('res', function(){
.get('/?callback=foo')
.expect('{"foo":"bar"}', done);
})

describe('"etag" setting', function(){
describe('when enabled', function(){
it('should send ETag ', function(done){
var app = express();

app.use(function(req, res){
var str = Array(1024 * 2).join('-');
res.send(str);
});

request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('etag', '"-1498647312"');
done();
});
});
});

describe('when disabled', function(){
it('should send no ETag', function(done){
var app = express();

app.use(function(req, res){
var str = Array(1024 * 2).join('-');
res.send(str);
});

app.disable('etag');

request(app)
.get('/')
.end(function(err, res){
res.headers.should.not.have.property('etag');
done();
});
});

it('should send ETag when manually set', function(done){
var app = express();

app.disable('etag');

app.use(function(req, res){
res.set('etag', 1);
res.send(200);
});

request(app)
.get('/')
.end(function(err, res){
res.headers.should.have.property('etag');
done();
});
});
});
})
})

1 comment on commit 610e172

@SameerSiddiqui
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Please sign in to comment.