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
Provenance
- Commit
eaf3318- Tree
0fbdbb8- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
e1057bd
4 files changed+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+ */7+8 /**9 * Module dependencies.10+ * @api private11 */12 13 var contentDisposition = require('content-disposition');14 var deprecate = require('depd')('express');78 unmodified lines93 */94 95 res.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 lines159 : Buffer.byteLength(body, encoding);160 this.set('Content-Length', len);161 }162 156- // 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 }171 172 // freshness6 unmodified lines179 this.removeHeader('Transfer-Encoding');180 body = '';181 }182 183+ 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+ }190 191 return this;192 };193 653 unmodified linesmodified:test/res.send.js+100 added-91 removed
1 2-var express = require('../')3- , request = require('supertest')4- , assert = require('assert');2+var assert = require('assert');3+var express = require('..');4+var methods = require('methods');5+var request = require('supertest');6 7 describe('res', function(){8 describe('.send(null)', function(){9 it('should set body to ""', function(done){100 unmodified lines110 done();111 })112 })113 113- it('should set ETag', function(done){114+ it('should set ETag', function (done) {115 var app = express();116 116- app.use(function(req, res){117+ app.use(function (req, res) {118 var str = Array(1024 * 2).join('-');119 res.send(str);120 });121 122 request(app)123 .get('/')124 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')124- .end(done);125+ .expect(200, done);126 })127 127- it('should not set ETag for non-GET/HEAD', function(done){128- var app = express();129-130- app.use(function(req, res){131- var str = Array(1024 * 2).join('-');132- res.send(str);133- });134-135- 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- })143-128 it('should not override Content-Type', function(done){129 var app = express();130 131 app.use(function(req, res){50 unmodified lines182 done();183 })184 })185 202- it('should set ETag', function(done){186+ it('should set ETag', function (done) {187 var app = express();188 205- 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 });193 194 request(app)195 .get('/')196 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')213- .end(done);197+ .expect(200, done);198 })199 200 it('should not override Content-Type', function(done){201 var app = express();139 unmodified lines341 .get('/?callback=foo')342 .expect('{"foo":"bar"}', done);343 })344 361- 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();349 366- app.use(function(req, res){350+ app.use(function (req, res) {351 res.send('kajdslfkasdf');352 });353 354 app.enable('etag');355 356 request(app)357 .get('/')374- .expect('etag', 'W/"c-5aee35d8"', done)375- })358+ .expect('ETag', 'W/"c-5aee35d8"')359+ .expect(200, done);360+ });361 377- it('should send ETag for empty string response', function(done){378- var app = express()362+ methods.forEach(function (method) {363+ if (method === 'connect') return;364 365+ it('should send ETag in response to ' + method.toUpperCase() + ' request', function (done) {366+ var app = express();367+368+ app[method]('/', function (req, res) {369+ res.send('kajdslfkasdf');370+ });371+372+ request(app)373+ [method]('/')374+ .expect('ETag', 'W/"c-5aee35d8"')375+ .expect(200, done);376+ })377+ });378+379+ it('should send ETag for empty string response', function (done) {380+ var app = express();381+380- app.use(function(req, res){381- res.send('')382+ app.use(function (req, res) {383+ res.send('');384 });385 384- app.enable('etag')386+ app.enable('etag');387 388 request(app)389 .get('/')388- .expect('etag', 'W/"0-0"', done)390+ .expect('ETag', 'W/"0-0"')391+ .expect(200, done);392 })393 391- it('should send ETag for long response', function(done){394+ it('should send ETag for long response', function (done) {395 var app = express();396 394- app.use(function(req, res){397+ app.use(function (req, res) {398 var str = Array(1024 * 2).join('-');399 res.send(str);400 });401 402 app.enable('etag');403 404 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 });409 406- 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();412 409- app.use(function(req, res){413+ app.use(function (req, res) {414 res.set('etag', '"asdf"');415 res.send(200);416 });417 418 app.enable('etag');419 420 request(app)421 .get('/')418- .expect('etag', '"asdf"', done)422+ .expect('ETag', '"asdf"')423+ .expect(200, done);424 });425 421- 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();428 424- app.use(function(req, res){425- res.send()429+ app.use(function (req, res) {430+ res.send();431 });432 428- app.enable('etag')433+ app.enable('etag');434 435 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 });441 439- 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();445 443- app.use(function(req, res){446+ app.use(function (req, res) {447 var str = Array(1024 * 2).join('-');448 res.send(str);449 });450 451 app.disable('etag');452 453 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 });458 458- it('should send ETag when manually set', function(done){459+ it('should send ETag when manually set', function (done) {460 var app = express();461 462 app.disable('etag');463 463- app.use(function(req, res){464+ app.use(function (req, res) {465 res.set('etag', '"asdf"');466 res.send(200);467 });468 469 request(app)470 .get('/')470- .expect('etag', '"asdf"', done)471+ .expect('ETag', '"asdf"')472+ .expect(200, done);473 });474 });475 474- 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();479 480 app.set('etag', 'strong');481 480- app.use(function(req, res){482+ app.use(function (req, res) {483 res.send('hello, world!');484 });485 486 request(app)487 .get('/')486- .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)488+ .expect('ETag', '"Otu60XkfuuPskIiUxJY4cA=="')489+ .expect(200, done);490 })491 })492 490- 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();496 497 app.set('etag', 'weak');498 496- app.use(function(req, res){499+ app.use(function (req, res) {500 res.send('hello, world!');501 });502 503 request(app)504 .get('/')502- .expect('etag', 'W/"d-58988d13"', done)505+ .expect('ETag', 'W/"d-58988d13"')506+ .expect(200, done)507 })508 })509 506- 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();513 510- 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 });519 516- app.use(function(req, res){520+ app.use(function (req, res) {521 res.send('hello, world!');522 });523 524 request(app)525 .get('/')522- .expect('etag', '"custom"', done)526+ .expect('ETag', '"custom"')527+ .expect(200, done);528 })529 525- 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();532 528- app.set('etag', function(body, encoding){529- return undefined533+ app.set('etag', function (body, encoding) {534+ return undefined;535 });536 532- app.use(function(req, res){537+ app.use(function (req, res) {538 res.send('hello, world!');539 });540 541 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 })549+550+function 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
1 3.x2 ===3 4+ * 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.01981 unmodified linesmodified:LICENSE+2 added
1 (The MIT License)2 3 Copyright (c) 2009-2013 TJ Holowaychuk <tj@vision-media.ca>4+Copyright (c) 2013 Roman Shtylman <shtylman+expressjs@gmail.com>5+Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>6 7 Permission is hereby granted, free of charge, to any person obtaining8 a copy of this software and associated documentation files (the9 'Software'), to deal in the Software without restriction, including15 unmodified lines