Permalink
Browse files

add 'etag' option

  • Loading branch information...
1 parent a4e93c0 commit 6f6eec7d8d929112a37de61c6b78a50bd9f46a73 @kavu kavu committed with tj Dec 6, 2012
Showing with 72 additions and 10 deletions.
  1. +1 −0 lib/application.js
  2. +13 −10 lib/response.js
  3. +58 −0 test/res.send.js
View
@@ -47,6 +47,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');
debug('booting in %s mode', this.get('env'));
View
@@ -82,6 +82,9 @@ res.send = function(body){
, head = 'HEAD' == req.method
, len;
+ // settings
+ var app = this.app;
+
// allow status / body
if (2 == arguments.length) {
// res.send(body, status) backwards compat
@@ -128,7 +131,7 @@ res.send = function(body){
// ETag support
// TODO: W/ support
- if (len > 1024) {
+ if (app.settings.etag && len > 1024) {
if (!this.get('ETag')) {
this.set('ETag', etag(body));
}
@@ -241,7 +244,7 @@ res.jsonp = function(obj){
/**
* Transfer the file at the given `path`.
- *
+ *
* Automatically sets the _Content-Type_ response header field.
* The callback `fn(err)` is invoked when the transfer is complete
* or when an error occurs. Be sure to check `res.sentHeader`
@@ -263,7 +266,7 @@ res.jsonp = function(obj){
* app.get('/user/:uid/photos/:file', function(req, res){
* var uid = req.params.uid
* , file = req.params.file;
- *
+ *
* req.user.mayViewFilesFrom(uid, function(yes){
* if (yes) {
* res.sendfile('/uploads/' + uid + '/' + file);
@@ -408,11 +411,11 @@ res.type = function(type){
* 'text/plain': function(){
* res.send('hey');
* },
- *
+ *
* 'text/html': function(){
* res.send('<p>hey</p>');
* },
- *
+ *
* 'appliation/json': function(){
* res.send({ message: 'hey' });
* }
@@ -425,11 +428,11 @@ res.type = function(type){
* text: function(){
* res.send('hey');
* },
- *
+ *
* html: function(){
* res.send('<p>hey</p>');
* },
- *
+ *
* json: function(){
* res.send({ message: 'hey' });
* }
@@ -498,15 +501,15 @@ res.attachment = function(filename){
* res.set('Accept', 'application/json');
* res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
*
- * Aliased as `res.header()`.
+ * Aliased as `res.header()`.
*
* @param {String|Object} field
* @param {String} val
* @return {ServerResponse} for chaining
* @api public
*/
-res.set =
+res.set =
res.header = function(field, val){
if (2 == arguments.length) {
this.setHeader(field, '' + val);
@@ -602,7 +605,7 @@ res.cookie = function(name, val, options){
* Mounting:
*
* When an application is mounted, and `res.redirect()`
- * is given a path that does _not_ lead with "/". For
+ * is given a path that does _not_ lead with "/". For
* example suppose a "blog" app is mounted at "/blog",
* the following redirect would result in "/blog/login":
*
View
@@ -298,4 +298,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();
+ });
+ });
+ });
+ })
})

0 comments on commit 6f6eec7

Please sign in to comment.