Skip to content
Myr

change generation of ETags with res.send() to GET requests only. Closes #1619

Before adoption: this commit came in with the history the project adopted, so no landing records it.

if for some reason this is not ideal for your use-case please let me know and comment in the issue

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent f976625 · Browse files · 2 files+29 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−11 removed

  • tests:+25 added−7 removed
  • other:+4 added−4 removed

2 files+29 added−11 removed

modified:lib/response.js+4 added-4 removed
76 unmodified lines
77 * @api public78 */7980res.send = function(body){81 var req = this.req82 , head = 'HEAD' == req.method83 , len;81 var req = this.req;82 var head = 'HEAD' == req.method;83 var len;8485 // allow status / body86 if (2 == arguments.length) {87 // res.send(body, status) backwards compat
39 unmodified lines
127 }128129 // ETag support130 // TODO: W/ support131 if (len > 1024) {131 if (len > 1024 && 'GET' == req.method) {132 if (!this.get('ETag')) {133 this.set('ETag', etag(body));134 }135 }
622 unmodified lines
modified:test/res.send.js+25 added-7 removed
12var express = require('../')3 , request = require('./support/http');3 , request = require('./support/http')4 , assert = require('assert');56describe('res', function(){7 describe('.send(null)', function(){8 it('should set body to ""', function(done){
7 unmodified lines
16 .get('/')17 .expect('', done);18 })19 })19 2021 describe('.send(undefined)', function(){22 it('should set body to ""', function(done){23 var app = express();24
20 unmodified lines
45 .expect('Created')46 .expect(201, done);47 })48 })48 4950 describe('.send(code, body)', function(){51 it('should set .statusCode and body', function(done){52 var app = express();53
53 unmodified lines
107 .get('/')108 .expect('ETag', '"-1498647312"')109 .end(done);110 })110 111112 it('should not set ETag for non-GET/HEAD', function(done){113 var app = express();114115 app.use(function(req, res){116 var str = Array(1024 * 2).join('-');117 res.send(str);118 });119120 request(app)121 .post('/')122 .end(function(err, res){123 if (err) return done(err);124 assert(!res.header.etag, 'has an ETag');125 done();126 });127 })128129 it('should not override Content-Type', function(done){130 var app = express();131132 app.use(function(req, res){
6 unmodified lines
139 .expect('hey')140 .expect(200, done);141 })142 })125 143144 describe('.send(Buffer)', function(){145 it('should send as octet-stream', function(done){146 var app = express();147
41 unmodified lines
189 done();190 })191 })192 })175 193194 describe('.send(Object)', function(){195 it('should send as application/json', function(done){196 var app = express();197
43 unmodified lines
241 done();242 })243 })244 })227 245246 describe('when .statusCode is 304', function(){247 it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){248 var app = express();249
72 unmodified lines