Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,18 @@ methods.forEach(function (method) {
return this.set(path);
}

if (method === 'head' && this.router && Array.isArray(this.router.stack)) {
var hasPrecedingGet = this.router.stack.some(function (layer) {
return layer.route && layer.route.path === path && layer.route.methods && layer.route.methods.get;
});
if (hasPrecedingGet) {
process.emitWarning(
'HEAD route for "' + path + '" declared after GET route will be shadowed. Declare HEAD routes before GET routes to ensure they execute.',
'ExpressWarning'
);
}
}

var route = this.route(path);
route[method].apply(route, slice.call(arguments, 1));
return this;
Expand Down
22 changes: 22 additions & 0 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ exports.application = proto;
exports.request = req;
exports.response = res;

/**
* Wrap Router.prototype.head to warn if declared after a GET route on the same path.
*/

if (Router.prototype && typeof Router.prototype.head === 'function') {
var origRouterHead = Router.prototype.head;
Router.prototype.head = function head(path) {
if (Array.isArray(this.stack)) {
var hasPrecedingGet = this.stack.some(function (layer) {
return layer.route && layer.route.path === path && layer.route.methods && layer.route.methods.get;
});
if (hasPrecedingGet) {
process.emitWarning(
'HEAD route for "' + path + '" declared after GET route will be shadowed. Declare HEAD routes before GET routes to ensure they execute.',
'ExpressWarning'
);
}
}
return origRouterHead.apply(this, arguments);
};
}

/**
* Expose constructors.
*/
Expand Down
54 changes: 54 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,58 @@ describe('Router', function () {
});
});
});

describe('.head()', function () {
it('should emit a warning when HEAD is declared after GET on the same path', function (done) {
var router = new Router()
var warnings = []

function onWarning(warning) {
warnings.push(warning)
}

process.on('warning', onWarning)

router.get('/tobi', function (req, res) {
res.end('tobi')
})

router.head('/tobi', function (req, res) {
res.end()
})

process.nextTick(function () {
process.removeListener('warning', onWarning)
assert.strictEqual(warnings.length, 1)
assert.strictEqual(warnings[0].name, 'ExpressWarning')
assert.ok(warnings[0].message.includes('HEAD route for "/tobi" declared after GET route will be shadowed'))
done()
})
})

it('should not emit a warning when HEAD is declared before GET on the same path', function (done) {
var router = new Router()
var warnings = []

function onWarning(warning) {
warnings.push(warning)
}

process.on('warning', onWarning)

router.head('/tobi', function (req, res) {
res.end()
})

router.get('/tobi', function (req, res) {
res.end('tobi')
})

process.nextTick(function () {
process.removeListener('warning', onWarning)
assert.strictEqual(warnings.length, 0)
done()
})
})
})
})
77 changes: 77 additions & 0 deletions test/app.head.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,81 @@ describe('app.head()', function(){
.expect('x-method', 'head')
.expect(200, done)
})

it('should emit a warning when HEAD is declared after GET on the same path', function (done) {
var app = express()
var warnings = []

function onWarning(warning) {
warnings.push(warning)
}

process.on('warning', onWarning)

app.get('/tobi', function (req, res) {
res.send('tobi')
})

app.head('/tobi', function (req, res) {
res.end()
})

process.nextTick(function () {
process.removeListener('warning', onWarning)
assert.strictEqual(warnings.length, 1)
assert.strictEqual(warnings[0].name, 'ExpressWarning')
assert.ok(warnings[0].message.includes('HEAD route for "/tobi" declared after GET route will be shadowed'))
done()
})
})

it('should not emit a warning when HEAD is declared before GET on the same path', function (done) {
var app = express()
var warnings = []

function onWarning(warning) {
warnings.push(warning)
}

process.on('warning', onWarning)

app.head('/tobi', function (req, res) {
res.end()
})

app.get('/tobi', function (req, res) {
res.send('tobi')
})

process.nextTick(function () {
process.removeListener('warning', onWarning)
assert.strictEqual(warnings.length, 0)
done()
})
})

it('should not emit a warning when HEAD and GET are declared on different paths', function (done) {
var app = express()
var warnings = []

function onWarning(warning) {
warnings.push(warning)
}

process.on('warning', onWarning)

app.get('/foo', function (req, res) {
res.send('foo')
})

app.head('/bar', function (req, res) {
res.end()
})

process.nextTick(function () {
process.removeListener('warning', onWarning)
assert.strictEqual(warnings.length, 0)
done()
})
})
})