Skip to content
Myr

Generate ETags for all request responses

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

closes #2546

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent e1057bd · Browse files · 4 files+125 added−100 removed
This commit

State

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

Diff stats

4 files+125 added−100 removed

  • tests:+100 added−91 removed
  • other:+25 added−9 removed

4 files+125 added−100 removed

modified:lib/response.js+21 added-9 removed
1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */78/**9 * Module dependencies.10 * @api private11 */1213var contentDisposition = require('content-disposition');14var deprecate = require('depd')('express');
78 unmodified lines
93 */9495res.send = function(body){96 var req = this.req;89 var head = 'HEAD' == req.method;97 var type;98 var encoding;99 var len;100
58 unmodified lines
159 : Buffer.byteLength(body, encoding);160 this.set('Content-Length', len);161 }162156 // ETag support157 var etag = len !== undefined && app.get('etag fn');158 if (etag && ('GET' === req.method || 'HEAD' === req.method)) {159 if (!this.get('ETag')) {160 etag = etag(body, encoding);161 etag && this.set('ETag', etag);163 // populate ETag164 var etag;165 var generateETag = len !== undefined && app.get('etag fn');166 if (typeof generateETag === 'function' && !this.get('ETag')) {167 if ((etag = generateETag(body, encoding))) {168 this.set('ETag', etag);169 }170 }171172 // freshness
6 unmodified lines
179 this.removeHeader('Transfer-Encoding');180 body = '';181 }182183 if (req.method === 'HEAD') {184 // skip body for HEAD185 this.end();186 } else {176 // respond177 this.end((head ? null : body), encoding);187 // respond188 this.end(body, encoding);189 }190191 return this;192};193
653 unmodified lines
modified:test/res.send.js+100 added-91 removed
12var express = require('../')3 , request = require('supertest')4 , assert = require('assert');2var assert = require('assert');3var express = require('..');4var methods = require('methods');5var request = require('supertest');67describe('res', function(){8 describe('.send(null)', function(){9 it('should set body to ""', function(done){
100 unmodified lines
110 done();111 })112 })113113 it('should set ETag', function(done){114 it('should set ETag', function (done) {115 var app = express();116116 app.use(function(req, res){117 app.use(function (req, res) {118 var str = Array(1024 * 2).join('-');119 res.send(str);120 });121122 request(app)123 .get('/')124 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')124 .end(done);125 .expect(200, done);126 })127127 it('should not set ETag for non-GET/HEAD', function(done){128 var app = express();129130 app.use(function(req, res){131 var str = Array(1024 * 2).join('-');132 res.send(str);133 });134135 request(app)136 .post('/')137 .end(function(err, res){138 if (err) return done(err);139 assert(!res.header.etag, 'has an ETag');140 done();141 });142 })143128 it('should not override Content-Type', function(done){129 var app = express();130131 app.use(function(req, res){
50 unmodified lines
182 done();183 })184 })185202 it('should set ETag', function(done){186 it('should set ETag', function (done) {187 var app = express();188205 app.use(function(req, res){189 app.use(function (req, res) {190 var str = Array(1024 * 2).join('-');191 res.send(new Buffer(str));192 });193194 request(app)195 .get('/')196 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')213 .end(done);197 .expect(200, done);198 })199200 it('should not override Content-Type', function(done){201 var app = express();
139 unmodified lines
341 .get('/?callback=foo')342 .expect('{"foo":"bar"}', done);343 })344361 describe('"etag" setting', function(){362 describe('when enabled', function(){363 it('should send ETag', function(done){345 describe('"etag" setting', function () {346 describe('when enabled', function () {347 it('should send ETag', function (done) {348 var app = express();349366 app.use(function(req, res){350 app.use(function (req, res) {351 res.send('kajdslfkasdf');352 });353354 app.enable('etag');355356 request(app)357 .get('/')374 .expect('etag', 'W/"c-5aee35d8"', done)375 })358 .expect('ETag', 'W/"c-5aee35d8"')359 .expect(200, done);360 });361377 it('should send ETag for empty string response', function(done){378 var app = express()362 methods.forEach(function (method) {363 if (method === 'connect') return;364365 it('should send ETag in response to ' + method.toUpperCase() + ' request', function (done) {366 var app = express();367368 app[method]('/', function (req, res) {369 res.send('kajdslfkasdf');370 });371372 request(app)373 [method]('/')374 .expect('ETag', 'W/"c-5aee35d8"')375 .expect(200, done);376 })377 });378379 it('should send ETag for empty string response', function (done) {380 var app = express();381380 app.use(function(req, res){381 res.send('')382 app.use(function (req, res) {383 res.send('');384 });385384 app.enable('etag')386 app.enable('etag');387388 request(app)389 .get('/')388 .expect('etag', 'W/"0-0"', done)390 .expect('ETag', 'W/"0-0"')391 .expect(200, done);392 })393391 it('should send ETag for long response', function(done){394 it('should send ETag for long response', function (done) {395 var app = express();396394 app.use(function(req, res){397 app.use(function (req, res) {398 var str = Array(1024 * 2).join('-');399 res.send(str);400 });401402 app.enable('etag');403404 request(app)405 .get('/')403 .expect('etag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="', done)406 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')407 .expect(200, done);408 });409406 it('should not override ETag when manually set', function(done){410 it('should not override ETag when manually set', function (done) {411 var app = express();412409 app.use(function(req, res){413 app.use(function (req, res) {414 res.set('etag', '"asdf"');415 res.send(200);416 });417418 app.enable('etag');419420 request(app)421 .get('/')418 .expect('etag', '"asdf"', done)422 .expect('ETag', '"asdf"')423 .expect(200, done);424 });425421 it('should not send ETag for res.send()', function(done){422 var app = express()426 it('should not send ETag for res.send()', function (done) {427 var app = express();428424 app.use(function(req, res){425 res.send()429 app.use(function (req, res) {430 res.send();431 });432428 app.enable('etag')433 app.enable('etag');434435 request(app)436 .get('/')432 .end(function(err, res){433 res.headers.should.not.have.property('etag');434 done();435 })437 .expect(shouldNotHaveHeader('ETag'))438 .expect(200, done);439 })440 });441439 describe('when disabled', function(){440 it('should send no ETag', function(done){442 describe('when disabled', function () {443 it('should send no ETag', function (done) {444 var app = express();445443 app.use(function(req, res){446 app.use(function (req, res) {447 var str = Array(1024 * 2).join('-');448 res.send(str);449 });450451 app.disable('etag');452453 request(app)454 .get('/')452 .end(function(err, res){453 res.headers.should.not.have.property('etag');454 done();455 });455 .expect(shouldNotHaveHeader('ETag'))456 .expect(200, done);457 });458458 it('should send ETag when manually set', function(done){459 it('should send ETag when manually set', function (done) {460 var app = express();461462 app.disable('etag');463463 app.use(function(req, res){464 app.use(function (req, res) {465 res.set('etag', '"asdf"');466 res.send(200);467 });468469 request(app)470 .get('/')470 .expect('etag', '"asdf"', done)471 .expect('ETag', '"asdf"')472 .expect(200, done);473 });474 });475474 describe('when "strong"', function(){475 it('should send strong ETag', function(done){476 var app = express()476 describe('when "strong"', function () {477 it('should send strong ETag', function (done) {478 var app = express();479480 app.set('etag', 'strong');481480 app.use(function(req, res){482 app.use(function (req, res) {483 res.send('hello, world!');484 });485486 request(app)487 .get('/')486 .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)488 .expect('ETag', '"Otu60XkfuuPskIiUxJY4cA=="')489 .expect(200, done);490 })491 })492490 describe('when "weak"', function(){491 it('should send weak ETag', function(done){492 var app = express()493 describe('when "weak"', function () {494 it('should send weak ETag', function (done) {495 var app = express();496497 app.set('etag', 'weak');498496 app.use(function(req, res){499 app.use(function (req, res) {500 res.send('hello, world!');501 });502503 request(app)504 .get('/')502 .expect('etag', 'W/"d-58988d13"', done)505 .expect('ETag', 'W/"d-58988d13"')506 .expect(200, done)507 })508 })509506 describe('when a function', function(){507 it('should send custom ETag', function(done){508 var app = express()510 describe('when a function', function () {511 it('should send custom ETag', function (done) {512 var app = express();513510 app.set('etag', function(body, encoding){511 body.should.equal('hello, world!')512 encoding.should.equal('utf8')513 return '"custom"'514 app.set('etag', function (body, encoding) {515 body.should.equal('hello, world!');516 encoding.should.equal('utf8');517 return '"custom"';518 });519516 app.use(function(req, res){520 app.use(function (req, res) {521 res.send('hello, world!');522 });523524 request(app)525 .get('/')522 .expect('etag', '"custom"', done)526 .expect('ETag', '"custom"')527 .expect(200, done);528 })529525 it('should not send falsy ETag', function(done){526 var app = express()530 it('should not send falsy ETag', function (done) {531 var app = express();532528 app.set('etag', function(body, encoding){529 return undefined533 app.set('etag', function (body, encoding) {534 return undefined;535 });536532 app.use(function(req, res){537 app.use(function (req, res) {538 res.send('hello, world!');539 });540541 request(app)542 .get('/')538 .end(function(err, res){539 res.headers.should.not.have.property('etag')540 done();541 })543 .expect(shouldNotHaveHeader('ETag'))544 .expect(200, done);545 })546 })547 })548})549550function shouldNotHaveHeader(header) {551 return function (res) {552 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)553 }554}
modified:History.md+2 added
13.x2===34 * Generate `ETag`s for all request responses5 - No longer restricted to only responses for `GET` and `HEAD` requests6 * Use `content-type` to parse `Content-Type` headers7 * deps: connect@2.29.08 - Use `content-type` to parse `Content-Type` headers9 - deps: body-parser@~1.12.0
1981 unmodified lines
modified:LICENSE+2 added
1(The MIT License)23Copyright (c) 2009-2013 TJ Holowaychuk <tj@vision-media.ca>4Copyright (c) 2013 Roman Shtylman <shtylman+expressjs@gmail.com>5Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>67Permission is hereby granted, free of charge, to any person obtaining8a copy of this software and associated documentation files (the9'Software'), to deal in the Software without restriction, including
15 unmodified lines