Merge tag '3.20.0'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parents ca480d7, 85755e3 · Browse files · 10 files·+307 added−145 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+307 added−145 removed
- tests:+190 added−119 removed
- other:+117 added−26 removed
Provenance
- Commit
531f024- Tree
d1d9092- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent 1
ca480d7- Parent 2
85755e3
10 files changed+307 added−145 removed
modified:lib/application.js+38 added-2 removed
1+/*!2+ * express3+ * Copyright(c) 2009-2013 TJ Holowaychuk4+ * Copyright(c) 2013 Roman Shtylman5+ * Copyright(c) 2014-2015 Douglas Christopher Wilson6+ * MIT Licensed7+ */8+9 /**10 * Module dependencies.11+ * @api private12 */13 14 var finalhandler = require('finalhandler');15 var flatten = require('./utils').flatten;18 unmodified lines34 35 var app = exports = module.exports = {};36 37 /**38+ * Variable for trust proxy inheritance back-compat39+ * @api private40+ */41+42+var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';43+44+/**45 * Initialize the server.46 *47 * - setup default configuration48 * - setup default middleware24 unmodified lines73 this.set('query parser', 'extended');74 this.set('subdomain offset', 2);75 this.set('trust proxy', false);76 77+ // trust proxy inherit back-compat78+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {79+ configurable: true,80+ value: true81+ });82+83 debug('booting in %s mode', env);84 63- // inherit protos64- this.on('mount', function(parent){85+ this.on('mount', function onmount(parent) {86+ // inherit trust proxy87+ if (this.settings[trustProxyDefaultSymbol] === true88+ && typeof parent.settings['trust proxy fn'] === 'function') {89+ delete this.settings['trust proxy'];90+ delete this.settings['trust proxy fn'];91+ }92+93+ // inherit protos94 this.request.__proto__ = parent.request;95 this.response.__proto__ = parent.response;96 this.engines.__proto__ = parent.engines;97 this.settings.__proto__ = parent.settings;257 unmodified lines355 break;356 case 'trust proxy':357 debug('compile trust proxy %s', val);358 this.set('trust proxy fn', compileTrust(val));359+360+ // trust proxy inherit back-compat361+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {362+ configurable: true,363+ value: false364+ });365+366 break;367 }368 369 return this;238 unmodified linesmodified:lib/response.js+15 added-10 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');151 unmodified lines166 len = chunk.length;167 this.set('Content-Length', len);168 }169 162- // method check163- var isHead = req.method === 'HEAD';164-165- // ETag support166- if (len !== undefined && (isHead || req.method === 'GET')) {167- var etag = app.get('etag fn');168- if (etag && !this.get('ETag')) {169- etag = etag(chunk, encoding);170- etag && this.set('ETag', etag);170+ // populate ETag171+ var etag;172+ var generateETag = len !== undefined && app.get('etag fn');173+ if (typeof generateETag === 'function' && !this.get('ETag')) {174+ if ((etag = generateETag(chunk, encoding))) {175+ this.set('ETag', etag);176 }177 }178 179 // freshness6 unmodified lines186 this.removeHeader('Transfer-Encoding');187 chunk = '';188 }189 185- if (isHead) {190+ if (req.method === 'HEAD') {191 // skip body for HEAD192 this.end();193 } else {194 // respond835 unmodified linesmodified:lib/utils.js+15 added-5 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 contentType = require('content-type');15 var deprecate = require('depd')('express');16 var mime = require('send').mime;17 var basename = require('path').basename;18 var etag = require('etag');19 var proxyaddr = require('proxy-addr');20 var qs = require('qs');21 var querystring = require('querystring');13-var typer = require('media-typer');22 23 /**24 * Return strong ETag for `body`.25 *239 unmodified lines265 * @return {String}266 * @api private267 */268 261-exports.setCharset = function(type, charset){262- if (!type || !charset) return type;269+exports.setCharset = function setCharset(type, charset) {270+ if (!type || !charset) {271+ return type;272+ }273 274 // parse type265- var parsed = typer.parse(type);275+ var parsed = contentType.parse(type);276 277 // set charset278 parsed.parameters.charset = charset;279 280 // format type271- return typer.format(parsed);281+ return contentType.format(parsed);282 };283 284 /**285 * Return new empty object.8 unmodified linesmodified:test/config.js+74 added-29 removed
1232345675678991011121213141415161718192021222324192025262728293031322733343536373839404136424344454647484944505152535455505657585960555661626364656066676268696465707172737475767177787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221237980124125126127128129130131878813213313413513613713813995140141142143144145101146147148149150151152108153154155156157158114159160161162
var express = require('../') , assert = require('assert');var assert = require('assert');var express = require('..');
describe('config', function(){ describe('.set()', function(){ it('should set a value', function(){describe('config', function () { describe('.set()', function () { it('should set a value', function () { var app = express(); app.set('foo', 'bar').should.equal(app); app.set('foo', 'bar'); assert.equal(app.get('foo'), 'bar'); })
it('should return the app when undefined', function(){ it('should return the app', function () { var app = express(); app.set('foo', undefined).should.equal(app); assert.equal(app.set('foo', 'bar'), app); })
it('should return the app when undefined', function () { var app = express(); assert.equal(app.set('foo', undefined), app); })
describe('"etag"', function(){ it('should throw on bad value', function(){ var app = express() app.set.bind(app, 'etag', 42).should.throw(/unknown value/) var app = express(); assert.throws(app.set.bind(app, 'etag', 42), /unknown value/); })
it('should set "etag fn"', function(){ var app = express() var fn = function(){} app.set('etag', fn) app.get('etag fn').should.equal(fn) assert.equal(app.get('etag fn'), fn) }) })
describe('"trust proxy"', function(){ it('should set "trust proxy fn"', function(){ var app = express() var fn = function(){} app.set('trust proxy', fn) app.get('trust proxy fn').should.equal(fn) assert.equal(app.get('trust proxy fn'), fn) }) }) })
describe('.get()', function(){ it('should return undefined when unset', function(){ var app = express(); assert(undefined === app.get('foo')); assert.strictEqual(app.get('foo'), undefined); }) it('should otherwise return the value', function(){ var app = express(); app.set('foo', 'bar'); app.get('foo').should.equal('bar'); assert.equal(app.get('foo'), 'bar'); })
describe('when mounted', function(){ it('should default to the parent app', function(){ var app = express() , blog = express(); var app = express(); var blog = express();
app.set('title', 'Express'); app.use(blog); blog.get('title').should.equal('Express'); assert.equal(blog.get('title'), 'Express'); })
it('should given precedence to the child', function(){ var app = express() , blog = express(); var app = express(); var blog = express();
app.use(blog); app.set('title', 'Express'); blog.set('title', 'Some Blog');
blog.get('title').should.equal('Some Blog'); assert.equal(blog.get('title'), 'Some Blog'); })
it('should inherit "trust proxy" setting', function () { var app = express(); var blog = express();
function fn() { return false }
app.set('trust proxy', fn); assert.equal(app.get('trust proxy'), fn); assert.equal(app.get('trust proxy fn'), fn);
app.use(blog);
assert.equal(blog.get('trust proxy'), fn); assert.equal(blog.get('trust proxy fn'), fn); })
it('should prefer child "trust proxy" setting', function () { var app = express(); var blog = express();
function fn1() { return false } function fn2() { return true }
app.set('trust proxy', fn1); assert.equal(app.get('trust proxy'), fn1); assert.equal(app.get('trust proxy fn'), fn1);
blog.set('trust proxy', fn2); assert.equal(blog.get('trust proxy'), fn2); assert.equal(blog.get('trust proxy fn'), fn2);
app.use(blog);
assert.equal(app.get('trust proxy'), fn1); assert.equal(app.get('trust proxy fn'), fn1); assert.equal(blog.get('trust proxy'), fn2); assert.equal(blog.get('trust proxy fn'), fn2); }) }) })
describe('.enable()', function(){ it('should set the value to true', function(){ var app = express(); app.enable('tobi').should.equal(app); app.get('tobi').should.be.true; assert.equal(app.enable('tobi'), app); assert.strictEqual(app.get('tobi'), true); }) }) describe('.disable()', function(){ it('should set the value to false', function(){ var app = express(); app.disable('tobi').should.equal(app); app.get('tobi').should.be.false; assert.equal(app.disable('tobi'), app); assert.strictEqual(app.get('tobi'), false); }) }) describe('.enabled()', function(){ it('should default to false', function(){ var app = express(); app.enabled('foo').should.be.false; assert.strictEqual(app.enabled('foo'), false); }) it('should return true when set', function(){ var app = express(); app.set('foo', 'bar'); app.enabled('foo').should.be.true; assert.strictEqual(app.enabled('foo'), true); }) }) describe('.disabled()', function(){ it('should default to true', function(){ var app = express(); app.disabled('foo').should.be.true; assert.strictEqual(app.disabled('foo'), true); }) it('should return false when set', function(){ var app = express(); app.set('foo', 'bar'); app.disabled('foo').should.be.false; assert.strictEqual(app.disabled('foo'), false); }) })})modified:test/req.ip.js+17 added
33 unmodified lines34 .get('/')35 .set('X-Forwarded-For', 'client, p1, p2')36 .expect('p1', done);37 })38+39+ it('should return the addr after trusted proxy, from sub app', function (done) {40+ var app = express();41+ var sub = express();42+43+ app.set('trust proxy', 2);44+ app.use(sub);45+46+ sub.use(function (req, res, next) {47+ res.send(req.ip);48+ });49+50+ request(app)51+ .get('/')52+ .set('X-Forwarded-For', 'client, p1, p2')53+ .expect(200, 'p1', done);54+ })55 })56 57 describe('when "trust proxy" is disabled', function(){58 it('should return the remote address', function(done){39 unmodified linesmodified:test/res.send.js+99 added-90 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){104 unmodified lines114 done();115 })116 })117 117- it('should set ETag', function(done){118+ it('should set ETag', function (done) {119 var app = express();120 120- app.use(function(req, res){121+ app.use(function (req, res) {122 var str = Array(1000).join('-');123 res.send(str);124 });125 126 request(app)127 .get('/')128 .expect('ETag', 'W/"3e7-8084ccd1"')128- .end(done);129+ .expect(200, done);130 })131 131- it('should not set ETag for non-GET/HEAD', function(done){132- var app = express();133-134- app.use(function(req, res){135- var str = Array(1000).join('-');136- res.send(str);137- });138-139- request(app)140- .post('/')141- .end(function(err, res){142- if (err) return done(err);143- assert(!res.header.etag, 'has an ETag');144- done();145- });146- })147-132 it('should not override Content-Type', function(done){133 var app = express();134 135 app.use(function(req, res){50 unmodified lines186 done();187 })188 })189 206- it('should set ETag', function(done){190+ it('should set ETag', function (done) {191 var app = express();192 209- app.use(function(req, res){193+ app.use(function (req, res) {194 var str = Array(1000).join('-');195 res.send(new Buffer(str));196 });197 198 request(app)199 .get('/')200 .expect('ETag', 'W/"3e7-8084ccd1"')217- .end(done);201+ .expect(200, done);202 })203 204 it('should not override Content-Type', function(done){205 var app = express();141 unmodified lines347 .get('/?callback=foo')348 .expect('{"foo":"bar"}', done);349 })350 367- describe('"etag" setting', function(){368- describe('when enabled', function(){369- it('should send ETag', function(done){351+ describe('"etag" setting', function () {352+ describe('when enabled', function () {353+ it('should send ETag', function (done) {354 var app = express();355 372- app.use(function(req, res){356+ app.use(function (req, res) {357 res.send('kajdslfkasdf');358 });359 360 app.enable('etag');361 362 request(app)363 .get('/')380- .expect('etag', 'W/"c-5aee35d8"', done)381- })364+ .expect('ETag', 'W/"c-5aee35d8"')365+ .expect(200, done);366+ });367 383- it('should send ETag for empty string response', function(done){384- var app = express()368+ methods.forEach(function (method) {369+ if (method === 'connect') return;370 371+ it('should send ETag in response to ' + method.toUpperCase() + ' request', function (done) {372+ var app = express();373+374+ app[method]('/', function (req, res) {375+ res.send('kajdslfkasdf');376+ });377+378+ request(app)379+ [method]('/')380+ .expect('ETag', 'W/"c-5aee35d8"')381+ .expect(200, done);382+ })383+ });384+385+ it('should send ETag for empty string response', function (done) {386+ var app = express();387+386- app.use(function(req, res){387- res.send('')388+ app.use(function (req, res) {389+ res.send('');390 });391 390- app.enable('etag')392+ app.enable('etag');393 394 request(app)395 .get('/')394- .expect('etag', 'W/"0-0"', done)396+ .expect('ETag', 'W/"0-0"')397+ .expect(200, done);398 })399 397- it('should send ETag for long response', function(done){400+ it('should send ETag for long response', function (done) {401 var app = express();402 400- app.use(function(req, res){403+ app.use(function (req, res) {404 var str = Array(1000).join('-');405 res.send(str);406 });407 408 app.enable('etag');409 410 request(app)411 .get('/')409- .expect('etag', 'W/"3e7-8084ccd1"', done)412+ .expect('ETag', 'W/"3e7-8084ccd1"')413+ .expect(200, done);414 });415 412- it('should not override ETag when manually set', function(done){416+ it('should not override ETag when manually set', function (done) {417 var app = express();418 415- app.use(function(req, res){419+ app.use(function (req, res) {420 res.set('etag', '"asdf"');421 res.send(200);422 });423 424 app.enable('etag');425 426 request(app)427 .get('/')424- .expect('etag', '"asdf"', done)428+ .expect('ETag', '"asdf"')429+ .expect(200, done);430 });431 427- it('should not send ETag for res.send()', function(done){428- var app = express()432+ it('should not send ETag for res.send()', function (done) {433+ var app = express();434 430- app.use(function(req, res){431- res.send()435+ app.use(function (req, res) {436+ res.send();437 });438 434- app.enable('etag')439+ app.enable('etag');440 441 request(app)442 .get('/')438- .end(function(err, res){439- res.headers.should.not.have.property('etag');440- done();441- })443+ .expect(shouldNotHaveHeader('ETag'))444+ .expect(200, done);445 })446 });447 445- describe('when disabled', function(){446- it('should send no ETag', function(done){448+ describe('when disabled', function () {449+ it('should send no ETag', function (done) {450 var app = express();451 449- app.use(function(req, res){452+ app.use(function (req, res) {453 var str = Array(1000).join('-');454 res.send(str);455 });456 457 app.disable('etag');458 459 request(app)460 .get('/')458- .end(function(err, res){459- res.headers.should.not.have.property('etag');460- done();461- });461+ .expect(shouldNotHaveHeader('ETag'))462+ .expect(200, done);463 });464 464- it('should send ETag when manually set', function(done){465+ it('should send ETag when manually set', function (done) {466 var app = express();467 468 app.disable('etag');469 469- app.use(function(req, res){470+ app.use(function (req, res) {471 res.set('etag', '"asdf"');472 res.send(200);473 });474 475 request(app)476 .get('/')476- .expect('etag', '"asdf"', done)477+ .expect('ETag', '"asdf"')478+ .expect(200, done);479 });480 });481 480- describe('when "strong"', function(){481- it('should send strong ETag', function(done){482- var app = express()482+ describe('when "strong"', function () {483+ it('should send strong ETag', function (done) {484+ var app = express();485 486 app.set('etag', 'strong');487 486- app.use(function(req, res){488+ app.use(function (req, res) {489 res.send('hello, world!');490 });491 492 request(app)493 .get('/')492- .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)494+ .expect('ETag', '"Otu60XkfuuPskIiUxJY4cA=="')495+ .expect(200, done);496 })497 })498 496- describe('when "weak"', function(){497- it('should send weak ETag', function(done){498- var app = express()499+ describe('when "weak"', function () {500+ it('should send weak ETag', function (done) {501+ var app = express();502 503 app.set('etag', 'weak');504 502- app.use(function(req, res){505+ app.use(function (req, res) {506 res.send('hello, world!');507 });508 509 request(app)510 .get('/')508- .expect('etag', 'W/"d-58988d13"', done)511+ .expect('ETag', 'W/"d-58988d13"')512+ .expect(200, done)513 })514 })515 512- describe('when a function', function(){513- it('should send custom ETag', function(done){514- var app = express()516+ describe('when a function', function () {517+ it('should send custom ETag', function (done) {518+ var app = express();519 516- app.set('etag', function(body, encoding){520+ app.set('etag', function (body, encoding) {521 var chunk = !Buffer.isBuffer(body)522 ? new Buffer(body, encoding)523 : body;520- chunk.toString().should.equal('hello, world!')521- return '"custom"'524+ chunk.toString().should.equal('hello, world!');525+ return '"custom"';526 });527 524- app.use(function(req, res){528+ app.use(function (req, res) {529 res.send('hello, world!');530 });531 532 request(app)533 .get('/')530- .expect('etag', '"custom"', done)534+ .expect('ETag', '"custom"')535+ .expect(200, done);536 })537 533- it('should not send falsy ETag', function(done){534- var app = express()538+ it('should not send falsy ETag', function (done) {539+ var app = express();540 536- app.set('etag', function(body, encoding){537- return undefined541+ app.set('etag', function (body, encoding) {542+ return undefined;543 });544 540- app.use(function(req, res){545+ app.use(function (req, res) {546 res.send('hello, world!');547 });548 549 request(app)550 .get('/')546- .end(function(err, res){547- res.headers.should.not.have.property('etag')548- done();549- })551+ .expect(shouldNotHaveHeader('ETag'))552+ .expect(200, done);553 })554 })555 })556 })557+558+function shouldNotHaveHeader(header) {559+ return function (res) {560+ assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)561+ }562+}modified:.travis.yml+2 added-5 removed
123456784567language: node_jsnode_js: - "0.10" - "0.11"matrix: allow_failures: - node_js: "0.11" fast_finish: true - "0.12"sudo: falsescript: "npm run-script test-travis"after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"modified:History.md+41 added
1+unreleased2+==========3+4+ * Fix `"trust proxy"` setting to inherit when app is mounted5+ * Generate `ETag`s for all request responses6+ - No longer restricted to only responses for `GET` and `HEAD` requests7+ * Use `content-type` to parse `Content-Type` headers8+ * deps: cookie-signature@1.0.69+ * deps: send@0.12.110+ - Always read the stat size from the file11+ - Fix mutating passed-in `options`12+ - deps: mime@1.3.413+14 4.11.2 / 2015-02-0115 ===================16 17 * Fix `res.redirect` double-calling `res.end` for `HEAD` requests672 unmodified lines690 - `app.router()` - returns the app Router instance691 - `app.route()` - Proxy to the app's `Router#route()` method to create a new route692 - Router & Route - public API693 694+3.20.0 / 2015-02-18695+===================696+697+ * Fix `"trust proxy"` setting to inherit when app is mounted698+ * Generate `ETag`s for all request responses699+ - No longer restricted to only responses for `GET` and `HEAD` requests700+ * Use `content-type` to parse `Content-Type` headers701+ * deps: connect@2.29.0702+ - Use `content-type` to parse `Content-Type` headers703+ - deps: body-parser@~1.12.0704+ - deps: compression@~1.4.1705+ - deps: connect-timeout@~1.6.0706+ - deps: cookie-parser@~1.3.4707+ - deps: cookie-signature@1.0.6708+ - deps: csurf@~1.7.0709+ - deps: errorhandler@~1.3.4710+ - deps: express-session@~1.10.3711+ - deps: http-errors@~1.3.1712+ - deps: response-time@~2.3.0713+ - deps: serve-index@~1.6.2714+ - deps: serve-static@~1.9.1715+ - deps: type-is@~1.6.0716+ * deps: cookie-signature@1.0.6717+ * deps: send@0.12.1718+ - Always read the stat size from the file719+ - Fix mutating passed-in `options`720+ - deps: mime@1.3.4721+722 3.19.2 / 2015-02-01723 ===================724 725 * deps: connect@2.28.31960 unmodified linesmodified:LICENSE+2 added
1 (The MIT License)2 3 Copyright (c) 2009-2014 TJ Holowaychuk <tj@vision-media.ca>4+Copyright (c) 2013-2014 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 linesmodified:package.json+4 added-4 removed
27 unmodified lines28 ],29 "dependencies": {30 "accepts": "~1.2.3",31 "content-disposition": "0.5.0",32+ "content-type": "~1.0.1",32- "cookie-signature": "1.0.5",33+ "cookie-signature": "1.0.6",34 "debug": "~2.1.1",35 "depd": "~1.0.0",36 "escape-html": "1.0.1",37 "etag": "~1.5.1",38 "finalhandler": "0.3.3",39 "fresh": "0.2.4",39- "media-typer": "0.3.0",40 "methods": "~1.1.1",41 "on-finished": "~2.2.0",42 "parseurl": "~1.3.0",43 "path-to-regexp": "0.1.3",44 "proxy-addr": "~1.0.6",45 "qs": "2.3.3",46 "range-parser": "~1.0.2",47- "send": "0.11.1",47+ "send": "0.12.1",48 "serve-static": "~1.8.1",49 "type-is": "~1.5.6",50 "vary": "~1.0.0",51 "cookie": "0.1.2",5 unmodified lines57 "ejs": "2.1.4",58 "istanbul": "0.3.5",59 "marked": "0.3.3",60 "mocha": "~2.1.0",61- "should": "~4.6.2",61+ "should": "~5.0.0",62 "supertest": "~0.15.0",63 "hjs": "~0.0.6",64 "body-parser": "~1.11.0",65 "connect-redis": "~2.2.0",25 unmodified lines