diff --git a/History.md b/History.md index 485eb136a9..5323220bcd 100644 --- a/History.md +++ b/History.md @@ -7,6 +7,7 @@ unreleased * cleanup: remove unnecessary require for global Buffer * perf: use loop for acceptParams * Replace `methods` dependency with standard library +* refactor: prefix built-in node module imports 5.0.1 / 2024-10-08 ========== diff --git a/examples/auth/index.js b/examples/auth/index.js index 2884ca4e17..40b73e6de1 100644 --- a/examples/auth/index.js +++ b/examples/auth/index.js @@ -6,7 +6,7 @@ var express = require('../..'); var hash = require('pbkdf2-password')() -var path = require('path'); +var path = require('node:path'); var session = require('express-session'); var app = module.exports = express(); diff --git a/examples/downloads/index.js b/examples/downloads/index.js index c47dddd738..ddc549ffec 100644 --- a/examples/downloads/index.js +++ b/examples/downloads/index.js @@ -5,7 +5,7 @@ */ var express = require('../../'); -var path = require('path'); +var path = require('node:path'); var app = module.exports = express(); diff --git a/examples/ejs/index.js b/examples/ejs/index.js index a39d805a16..0940d0624f 100644 --- a/examples/ejs/index.js +++ b/examples/ejs/index.js @@ -5,7 +5,7 @@ */ var express = require('../../'); -var path = require('path'); +var path = require('node:path'); var app = module.exports = express(); diff --git a/examples/error-pages/index.js b/examples/error-pages/index.js index efa815c474..0863120bc8 100644 --- a/examples/error-pages/index.js +++ b/examples/error-pages/index.js @@ -5,7 +5,7 @@ */ var express = require('../../'); -var path = require('path'); +var path = require('node:path'); var app = module.exports = express(); var logger = require('morgan'); var silent = process.env.NODE_ENV === 'test' diff --git a/examples/markdown/index.js b/examples/markdown/index.js index 23d645e66b..53e40ac38e 100644 --- a/examples/markdown/index.js +++ b/examples/markdown/index.js @@ -6,9 +6,9 @@ var escapeHtml = require('escape-html'); var express = require('../..'); -var fs = require('fs'); +var fs = require('node:fs'); var marked = require('marked'); -var path = require('path'); +var path = require('node:path'); var app = module.exports = express(); diff --git a/examples/mvc/index.js b/examples/mvc/index.js index da4727b282..1d8aa0e3c3 100644 --- a/examples/mvc/index.js +++ b/examples/mvc/index.js @@ -6,7 +6,7 @@ var express = require('../..'); var logger = require('morgan'); -var path = require('path'); +var path = require('node:path'); var session = require('express-session'); var methodOverride = require('method-override'); diff --git a/examples/mvc/lib/boot.js b/examples/mvc/lib/boot.js index 0216e5d76d..fc2ab0fad9 100644 --- a/examples/mvc/lib/boot.js +++ b/examples/mvc/lib/boot.js @@ -5,8 +5,8 @@ */ var express = require('../../..'); -var fs = require('fs'); -var path = require('path'); +var fs = require('node:fs'); +var path = require('node:path'); module.exports = function(parent, options){ var dir = path.join(__dirname, '..', 'controllers'); diff --git a/examples/route-separation/index.js b/examples/route-separation/index.js index a471a4b091..0a29c9421a 100644 --- a/examples/route-separation/index.js +++ b/examples/route-separation/index.js @@ -5,7 +5,7 @@ */ var express = require('../..'); -var path = require('path'); +var path = require('node:path'); var app = express(); var logger = require('morgan'); var cookieParser = require('cookie-parser'); diff --git a/examples/search/index.js b/examples/search/index.js index 4b57168987..951e0d440a 100644 --- a/examples/search/index.js +++ b/examples/search/index.js @@ -12,7 +12,7 @@ */ var express = require('../..'); -var path = require('path'); +var path = require('node:path'); var redis = require('redis'); var db = redis.createClient(); diff --git a/examples/static-files/index.js b/examples/static-files/index.js index 609c546b47..b7c697a2f9 100644 --- a/examples/static-files/index.js +++ b/examples/static-files/index.js @@ -6,7 +6,7 @@ var express = require('../..'); var logger = require('morgan'); -var path = require('path'); +var path = require('node:path'); var app = express(); // log requests diff --git a/examples/view-constructor/github-view.js b/examples/view-constructor/github-view.js index 43d29336ca..eabfb2d0c1 100644 --- a/examples/view-constructor/github-view.js +++ b/examples/view-constructor/github-view.js @@ -4,8 +4,8 @@ * Module dependencies. */ -var https = require('https'); -var path = require('path'); +var https = require('node:https'); +var path = require('node:path'); var extname = path.extname; /** diff --git a/examples/view-locals/index.js b/examples/view-locals/index.js index a2af24f355..e6355602d4 100644 --- a/examples/view-locals/index.js +++ b/examples/view-locals/index.js @@ -5,7 +5,7 @@ */ var express = require('../..'); -var path = require('path'); +var path = require('node:path'); var User = require('./user'); var app = express(); diff --git a/lib/application.js b/lib/application.js index bd319532bd..cf6d78c741 100644 --- a/lib/application.js +++ b/lib/application.js @@ -16,12 +16,12 @@ var finalhandler = require('finalhandler'); var debug = require('debug')('express:application'); var View = require('./view'); -var http = require('http'); +var http = require('node:http'); var methods = require('./utils').methods; var compileETag = require('./utils').compileETag; var compileQueryParser = require('./utils').compileQueryParser; var compileTrust = require('./utils').compileTrust; -var resolve = require('path').resolve; +var resolve = require('node:path').resolve; var once = require('once') var Router = require('router'); @@ -468,8 +468,8 @@ app.disable = function disable(setting) { * Delegate `.VERB(...)` calls to `router.VERB(...)`. */ -methods.forEach(function(method){ - app[method] = function(path){ +methods.forEach(function (method) { + app[method] = function (path) { if (method === 'get' && arguments.length === 1) { // app.get(setting) return this.set(path); @@ -583,8 +583,8 @@ app.render = function render(name, options, callback) { * and HTTPS server you may do so with the "http" * and "https" modules as shown here: * - * var http = require('http') - * , https = require('https') + * var http = require('node:http') + * , https = require('node:https') * , express = require('express') * , app = express(); * @@ -595,7 +595,7 @@ app.render = function render(name, options, callback) { * @public */ -app.listen = function listen () { +app.listen = function listen() { var server = http.createServer(this) var args = Array.prototype.slice.call(arguments) if (typeof args[args.length - 1] === 'function') { diff --git a/lib/express.js b/lib/express.js index b4ef299636..2d502eb54e 100644 --- a/lib/express.js +++ b/lib/express.js @@ -13,7 +13,7 @@ */ var bodyParser = require('body-parser') -var EventEmitter = require('events').EventEmitter; +var EventEmitter = require('node:events').EventEmitter; var mixin = require('merge-descriptors'); var proto = require('./application'); var Router = require('router'); diff --git a/lib/request.js b/lib/request.js index 372a9915e9..d8e5263078 100644 --- a/lib/request.js +++ b/lib/request.js @@ -14,9 +14,9 @@ */ var accepts = require('accepts'); -var isIP = require('net').isIP; +var isIP = require('node:net').isIP; var typeis = require('type-is'); -var http = require('http'); +var http = require('node:http'); var fresh = require('fresh'); var parseRange = require('range-parser'); var parse = require('parseurl'); diff --git a/lib/response.js b/lib/response.js index c5cf78d84f..f6f5740d2d 100644 --- a/lib/response.js +++ b/lib/response.js @@ -16,11 +16,11 @@ var contentDisposition = require('content-disposition'); var createError = require('http-errors') var encodeUrl = require('encodeurl'); var escapeHtml = require('escape-html'); -var http = require('http'); +var http = require('node:http'); var onFinished = require('on-finished'); var mime = require('mime-types') -var path = require('path'); -var pathIsAbsolute = require('path').isAbsolute; +var path = require('node:path'); +var pathIsAbsolute = require('node:path').isAbsolute; var statuses = require('statuses') var sign = require('cookie-signature').sign; var normalizeType = require('./utils').normalizeType; diff --git a/lib/view.js b/lib/view.js index 6beffca6e2..d66b4a2d89 100644 --- a/lib/view.js +++ b/lib/view.js @@ -14,8 +14,8 @@ */ var debug = require('debug')('express:view'); -var path = require('path'); -var fs = require('fs'); +var path = require('node:path'); +var fs = require('node:fs'); /** * Module variables. diff --git a/test/Route.js b/test/Route.js index a748ab7476..e4b73c7e6e 100644 --- a/test/Route.js +++ b/test/Route.js @@ -1,7 +1,7 @@ 'use strict' var after = require('after'); -var assert = require('assert') +var assert = require('node:assert') var express = require('../') , Route = express.Route , methods = require('../lib/utils').methods diff --git a/test/Router.js b/test/Router.js index fcd48ab367..7bac7159b0 100644 --- a/test/Router.js +++ b/test/Router.js @@ -4,10 +4,10 @@ var after = require('after'); var express = require('../') , Router = express.Router , methods = require('../lib/utils').methods - , assert = require('assert'); + , assert = require('node:assert'); -describe('Router', function(){ - it('should return a function with router methods', function() { +describe('Router', function () { + it('should return a function with router methods', function () { var router = new Router(); assert(typeof router === 'function') @@ -16,32 +16,32 @@ describe('Router', function(){ assert(typeof router.use === 'function') }); - it('should support .use of other routers', function(done){ + it('should support .use of other routers', function (done) { var router = new Router(); var another = new Router(); - another.get('/bar', function(req, res){ + another.get('/bar', function (req, res) { res.end(); }); router.use('/foo', another); - router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }, function(){}); + router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }, function () { }); }); - it('should support dynamic routes', function(done){ + it('should support dynamic routes', function (done) { var router = new Router(); var another = new Router(); - another.get('/:bar', function(req, res){ + another.get('/:bar', function (req, res) { assert.strictEqual(req.params.bar, 'route') res.end(); }); router.use('/:foo', another); - router.handle({ url: '/test/route', method: 'GET' }, { end: done }, function(){}); + router.handle({ url: '/test/route', method: 'GET' }, { end: done }, function () { }); }); - it('should handle blank URL', function(done){ + it('should handle blank URL', function (done) { var router = new Router(); router.use(function (req, res) { @@ -88,10 +88,10 @@ describe('Router', function(){ }) }) - it('should not stack overflow with many registered routes', function(done){ + it('should not stack overflow with many registered routes', function (done) { this.timeout(5000) // long-running test - var handler = function(req, res){ res.end(new Error('wrong handler')) }; + var handler = function (req, res) { res.end(new Error('wrong handler')) }; var router = new Router(); for (var i = 0; i < 6000; i++) { @@ -102,7 +102,7 @@ describe('Router', function(){ res.end(); }); - router.handle({ url: '/', method: 'GET' }, { end: done }, function(){}); + router.handle({ url: '/', method: 'GET' }, { end: done }, function () { }); }); it('should not stack overflow with a large sync route stack', function (done) { @@ -159,69 +159,69 @@ describe('Router', function(){ }) }) - describe('.handle', function(){ - it('should dispatch', function(done){ + describe('.handle', function () { + it('should dispatch', function (done) { var router = new Router(); - router.route('/foo').get(function(req, res){ + router.route('/foo').get(function (req, res) { res.send('foo'); }); var res = { - send: function(val) { + send: function (val) { assert.strictEqual(val, 'foo') done(); } } - router.handle({ url: '/foo', method: 'GET' }, res, function(){}); + router.handle({ url: '/foo', method: 'GET' }, res, function () { }); }) }) - describe('.multiple callbacks', function(){ - it('should throw if a callback is null', function(){ + describe('.multiple callbacks', function () { + it('should throw if a callback is null', function () { assert.throws(function () { var router = new Router(); router.route('/foo').all(null); }) }) - it('should throw if a callback is undefined', function(){ + it('should throw if a callback is undefined', function () { assert.throws(function () { var router = new Router(); router.route('/foo').all(undefined); }) }) - it('should throw if a callback is not a function', function(){ + it('should throw if a callback is not a function', function () { assert.throws(function () { var router = new Router(); router.route('/foo').all('not a function'); }) }) - it('should not throw if all callbacks are functions', function(){ + it('should not throw if all callbacks are functions', function () { var router = new Router(); - router.route('/foo').all(function(){}).all(function(){}); + router.route('/foo').all(function () { }).all(function () { }); }) }) - describe('error', function(){ - it('should skip non error middleware', function(done){ + describe('error', function () { + it('should skip non error middleware', function (done) { var router = new Router(); - router.get('/foo', function(req, res, next){ + router.get('/foo', function (req, res, next) { next(new Error('foo')); }); - router.get('/bar', function(req, res, next){ + router.get('/bar', function (req, res, next) { next(new Error('bar')); }); - router.use(function(req, res, next){ + router.use(function (req, res, next) { assert(false); }); - router.use(function(err, req, res, next){ + router.use(function (err, req, res, next) { assert.equal(err.message, 'foo'); done(); }); @@ -229,59 +229,59 @@ describe('Router', function(){ router.handle({ url: '/foo', method: 'GET' }, {}, done); }); - it('should handle throwing inside routes with params', function(done) { + it('should handle throwing inside routes with params', function (done) { var router = new Router(); router.get('/foo/:id', function () { throw new Error('foo'); }); - router.use(function(req, res, next){ + router.use(function (req, res, next) { assert(false); }); - router.use(function(err, req, res, next){ + router.use(function (err, req, res, next) { assert.equal(err.message, 'foo'); done(); }); - router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {}); + router.handle({ url: '/foo/2', method: 'GET' }, {}, function () { }); }); - it('should handle throwing in handler after async param', function(done) { + it('should handle throwing in handler after async param', function (done) { var router = new Router(); - router.param('user', function(req, res, next, val){ - process.nextTick(function(){ + router.param('user', function (req, res, next, val) { + process.nextTick(function () { req.user = val; next(); }); }); - router.use('/:user', function(req, res, next){ + router.use('/:user', function (req, res, next) { throw new Error('oh no!'); }); - router.use(function(err, req, res, next){ + router.use(function (err, req, res, next) { assert.equal(err.message, 'oh no!'); done(); }); - router.handle({ url: '/bob', method: 'GET' }, {}, function() {}); + router.handle({ url: '/bob', method: 'GET' }, {}, function () { }); }); - it('should handle throwing inside error handlers', function(done) { + it('should handle throwing inside error handlers', function (done) { var router = new Router(); - router.use(function(req, res, next){ + router.use(function (req, res, next) { throw new Error('boom!'); }); - router.use(function(err, req, res, next){ + router.use(function (err, req, res, next) { throw new Error('oops'); }); - router.use(function(err, req, res, next){ + router.use(function (err, req, res, next) { assert.equal(err.message, 'oops'); done(); }); @@ -412,17 +412,17 @@ describe('Router', function(){ }); }) - describe('.all', function() { - it('should support using .all to capture all http verbs', function(done){ + describe('.all', function () { + it('should support using .all to capture all http verbs', function (done) { var router = new Router(); var count = 0; - router.all('/foo', function(){ count++; }); + router.all('/foo', function () { count++; }); var url = '/foo?bar=baz'; methods.forEach(function testMethod(method) { - router.handle({ url: url, method: method }, {}, function() {}); + router.handle({ url: url, method: method }, {}, function () { }); }); assert.equal(count, methods.length); @@ -430,7 +430,7 @@ describe('Router', function(){ }) }) - describe('.use', function() { + describe('.use', function () { it('should require middleware', function () { var router = new Router() assert.throws(function () { router.use('/') }, /argument handler is required/) @@ -460,7 +460,7 @@ describe('Router', function(){ var cb = after(4, done) var router = new Router() - function no () { + function no() { throw new Error('should not be called') } @@ -474,30 +474,30 @@ describe('Router', function(){ router.handle({ url: '*', method: 'GET' }, { end: cb }, no) }) - it('should accept array of middleware', function(done){ + it('should accept array of middleware', function (done) { var count = 0; var router = new Router(); - function fn1(req, res, next){ + function fn1(req, res, next) { assert.equal(++count, 1); next(); } - function fn2(req, res, next){ + function fn2(req, res, next) { assert.equal(++count, 2); next(); } - router.use([fn1, fn2], function(req, res){ + router.use([fn1, fn2], function (req, res) { assert.equal(++count, 3); done(); }); - router.handle({ url: '/foo', method: 'GET' }, {}, function(){}); + router.handle({ url: '/foo', method: 'GET' }, {}, function () { }); }) }) - describe('.param', function() { + describe('.param', function () { it('should require function', function () { var router = new Router(); assert.throws(router.param.bind(router, 'id'), /argument fn is required/); @@ -508,15 +508,15 @@ describe('Router', function(){ assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/); }); - it('should call param function when routing VERBS', function(done) { + it('should call param function when routing VERBS', function (done) { var router = new Router(); - router.param('id', function(req, res, next, id) { + router.param('id', function (req, res, next, id) { assert.equal(id, '123'); next(); }); - router.get('/foo/:id/bar', function(req, res, next) { + router.get('/foo/:id/bar', function (req, res, next) { assert.equal(req.params.id, '123'); next(); }); @@ -524,15 +524,15 @@ describe('Router', function(){ router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done); }); - it('should call param function when routing middleware', function(done) { + it('should call param function when routing middleware', function (done) { var router = new Router(); - router.param('id', function(req, res, next, id) { + router.param('id', function (req, res, next, id) { assert.equal(id, '123'); next(); }); - router.use('/foo/:id/bar', function(req, res, next) { + router.use('/foo/:id/bar', function (req, res, next) { assert.equal(req.params.id, '123'); assert.equal(req.url, '/baz'); next(); @@ -541,17 +541,17 @@ describe('Router', function(){ router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done); }); - it('should only call once per request', function(done) { + it('should only call once per request', function (done) { var count = 0; var req = { url: '/foo/bob/bar', method: 'get' }; var router = new Router(); var sub = new Router(); - sub.get('/bar', function(req, res, next) { + sub.get('/bar', function (req, res, next) { next(); }); - router.param('user', function(req, res, next, user) { + router.param('user', function (req, res, next, user) { count++; req.user = user; next(); @@ -560,7 +560,7 @@ describe('Router', function(){ router.use('/foo/:user/', new Router()); router.use('/foo/:user/', sub); - router.handle(req, {}, function(err) { + router.handle(req, {}, function (err) { if (err) return done(err); assert.equal(count, 1); assert.equal(req.user, 'bob'); @@ -568,17 +568,17 @@ describe('Router', function(){ }); }); - it('should call when values differ', function(done) { + it('should call when values differ', function (done) { var count = 0; var req = { url: '/foo/bob/bar', method: 'get' }; var router = new Router(); var sub = new Router(); - sub.get('/bar', function(req, res, next) { + sub.get('/bar', function (req, res, next) { next(); }); - router.param('user', function(req, res, next, user) { + router.param('user', function (req, res, next, user) { count++; req.user = user; next(); @@ -587,7 +587,7 @@ describe('Router', function(){ router.use('/foo/:user/', new Router()); router.use('/:user/bob/', sub); - router.handle(req, {}, function(err) { + router.handle(req, {}, function (err) { if (err) return done(err); assert.equal(count, 2); assert.equal(req.user, 'foo'); @@ -596,8 +596,8 @@ describe('Router', function(){ }); }); - describe('parallel requests', function() { - it('should not mix requests', function(done) { + describe('parallel requests', function () { + it('should not mix requests', function (done) { var req1 = { url: '/foo/50/bar', method: 'get' }; var req2 = { url: '/foo/10/bar', method: 'get' }; var router = new Router(); @@ -605,11 +605,11 @@ describe('Router', function(){ var cb = after(2, done) - sub.get('/bar', function(req, res, next) { + sub.get('/bar', function (req, res, next) { next(); }); - router.param('ms', function(req, res, next, ms) { + router.param('ms', function (req, res, next, ms) { ms = parseInt(ms, 10); req.ms = ms; setTimeout(next, ms); @@ -618,14 +618,14 @@ describe('Router', function(){ router.use('/foo/:ms/', new Router()); router.use('/foo/:ms/', sub); - router.handle(req1, {}, function(err) { + router.handle(req1, {}, function (err) { assert.ifError(err); assert.equal(req1.ms, 50); assert.equal(req1.originalUrl, '/foo/50/bar'); cb() }); - router.handle(req2, {}, function(err) { + router.handle(req2, {}, function (err) { assert.ifError(err); assert.equal(req2.ms, 10); assert.equal(req2.originalUrl, '/foo/10/bar'); diff --git a/test/app.engine.js b/test/app.engine.js index 214510a94c..b0553aa247 100644 --- a/test/app.engine.js +++ b/test/app.engine.js @@ -1,9 +1,9 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../') - , fs = require('fs'); -var path = require('path') + , fs = require('node:fs'); +var path = require('node:path') function render(path, options, fn) { fs.readFile(path, 'utf8', function(err, str){ diff --git a/test/app.head.js b/test/app.head.js index fabb98795a..0207caaeda 100644 --- a/test/app.head.js +++ b/test/app.head.js @@ -2,7 +2,7 @@ var express = require('../'); var request = require('supertest'); -var assert = require('assert'); +var assert = require('node:assert'); describe('HEAD', function(){ it('should default to GET', function(done){ diff --git a/test/app.js b/test/app.js index fe7d4c2758..c1e815a052 100644 --- a/test/app.js +++ b/test/app.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('..') var request = require('supertest') diff --git a/test/app.listen.js b/test/app.listen.js index 7e7e731a3b..180162a0fa 100644 --- a/test/app.listen.js +++ b/test/app.listen.js @@ -1,7 +1,7 @@ 'use strict' var express = require('../') -var assert = require('assert') +var assert = require('node:assert') describe('app.listen()', function(){ it('should wrap with an HTTP server', function(done){ diff --git a/test/app.locals.js b/test/app.locals.js index a4f804fe2a..3963762fe2 100644 --- a/test/app.locals.js +++ b/test/app.locals.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../') describe('app', function(){ diff --git a/test/app.render.js b/test/app.render.js index 9d202acfdd..ca15e761d3 100644 --- a/test/app.render.js +++ b/test/app.render.js @@ -1,8 +1,8 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('..'); -var path = require('path') +var path = require('node:path') var tmpl = require('./support/tmpl'); describe('app', function(){ diff --git a/test/app.request.js b/test/app.request.js index 4930af84c2..b6c00f5baa 100644 --- a/test/app.request.js +++ b/test/app.request.js @@ -10,7 +10,7 @@ describe('app', function(){ var app = express(); app.request.querystring = function(){ - return require('url').parse(this.url).query; + return require('node:url').parse(this.url).query; }; app.use(function(req, res){ diff --git a/test/app.router.js b/test/app.router.js index 61b48eb4a3..017f4f4ef4 100644 --- a/test/app.router.js +++ b/test/app.router.js @@ -3,26 +3,26 @@ var after = require('after'); var express = require('../') , request = require('supertest') - , assert = require('assert') + , assert = require('node:assert') , methods = require('../lib/utils').methods; var shouldSkipQuery = require('./support/utils').shouldSkipQuery -describe('app.router', function(){ - it('should restore req.params after leaving router', function(done){ +describe('app.router', function () { + it('should restore req.params after leaving router', function (done) { var app = express(); var router = new express.Router(); - function handler1(req, res, next){ + function handler1(req, res, next) { res.setHeader('x-user-id', String(req.params.id)); next() } - function handler2(req, res){ + function handler2(req, res) { res.send(req.params.id); } - router.use(function(req, res, next){ + router.use(function (req, res, next) { res.setHeader('x-router', String(req.params.id)); next(); }); @@ -30,32 +30,32 @@ describe('app.router', function(){ app.get('/user/:id', handler1, router, handler2); request(app) - .get('/user/1') - .expect('x-router', 'undefined') - .expect('x-user-id', '1') - .expect(200, '1', done); + .get('/user/1') + .expect('x-router', 'undefined') + .expect('x-user-id', '1') + .expect(200, '1', done); }) - describe('methods', function(){ - methods.forEach(function(method){ + describe('methods', function () { + methods.forEach(function (method) { if (method === 'connect') return; - it('should include ' + method.toUpperCase(), function(done){ + it('should include ' + method.toUpperCase(), function (done) { if (method === 'query' && shouldSkipQuery(process.versions.node)) { this.skip() } var app = express(); - app[method]('/foo', function(req, res){ + app[method]('/foo', function (req, res) { res.send(method) }); request(app) [method]('/foo') - .expect(200, done) + .expect(200, done) }) - it('should reject numbers for app.' + method, function(){ + it('should reject numbers for app.' + method, function () { var app = express(); assert.throws(app[method].bind(app, '/', 3), /argument handler must be a function/); }) @@ -77,22 +77,22 @@ describe('app.router', function(){ }); request(app) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(app) - .delete('/') - .expect(200, 'deleted everything', cb); + .delete('/') + .expect(200, 'deleted everything', cb); request(app) - .post('/') - .expect('X-Method-Altered', '1') - .expect(200, 'deleted everything', cb); + .post('/') + .expect('X-Method-Altered', '1') + .expect(200, 'deleted everything', cb); }); }) describe('decode params', function () { - it('should decode correct params', function(done){ + it('should decode correct params', function (done) { var app = express(); app.get('/:name', function (req, res) { @@ -100,11 +100,11 @@ describe('app.router', function(){ }); request(app) - .get('/foo%2Fbar') - .expect('foo/bar', done); + .get('/foo%2Fbar') + .expect('foo/bar', done); }) - it('should not accept params in malformed paths', function(done) { + it('should not accept params in malformed paths', function (done) { var app = express(); app.get('/:name', function (req, res) { @@ -112,11 +112,11 @@ describe('app.router', function(){ }); request(app) - .get('/%foobar') - .expect(400, done); + .get('/%foobar') + .expect(400, done); }) - it('should not decode spaces', function(done) { + it('should not decode spaces', function (done) { var app = express(); app.get('/:name', function (req, res) { @@ -124,11 +124,11 @@ describe('app.router', function(){ }); request(app) - .get('/foo+bar') - .expect('foo+bar', done); + .get('/foo+bar') + .expect('foo+bar', done); }) - it('should work with unicode', function(done) { + it('should work with unicode', function (done) { var app = express(); app.get('/:name', function (req, res) { @@ -136,77 +136,77 @@ describe('app.router', function(){ }); request(app) - .get('/%ce%b1') - .expect('\u03b1', done); + .get('/%ce%b1') + .expect('\u03b1', done); }) }) - it('should be .use()able', function(done){ + it('should be .use()able', function (done) { var app = express(); var calls = []; - app.use(function(req, res, next){ + app.use(function (req, res, next) { calls.push('before'); next(); }); - app.get('/', function(req, res, next){ + app.get('/', function (req, res, next) { calls.push('GET /') next(); }); - app.use(function(req, res, next){ + app.use(function (req, res, next) { calls.push('after'); res.json(calls) }); request(app) - .get('/') - .expect(200, ['before', 'GET /', 'after'], done) + .get('/') + .expect(200, ['before', 'GET /', 'after'], done) }) - describe('when given a regexp', function(){ - it('should match the pathname only', function(done){ + describe('when given a regexp', function () { + it('should match the pathname only', function (done) { var app = express(); - app.get(/^\/user\/[0-9]+$/, function(req, res){ + app.get(/^\/user\/[0-9]+$/, function (req, res) { res.end('user'); }); request(app) - .get('/user/12?foo=bar') - .expect('user', done); + .get('/user/12?foo=bar') + .expect('user', done); }) - it('should populate req.params with the captures', function(done){ + it('should populate req.params with the captures', function (done) { var app = express(); - app.get(/^\/user\/([0-9]+)\/(view|edit)?$/, function(req, res){ + app.get(/^\/user\/([0-9]+)\/(view|edit)?$/, function (req, res) { var id = req.params[0] , op = req.params[1]; res.end(op + 'ing user ' + id); }); request(app) - .get('/user/10/edit') - .expect('editing user 10', done); + .get('/user/10/edit') + .expect('editing user 10', done); }) if (supportsRegexp('(?.*)')) { - it('should populate req.params with named captures', function(done){ + it('should populate req.params with named captures', function (done) { var app = express(); var re = new RegExp('^/user/(?[0-9]+)/(view|edit)?$'); - app.get(re, function(req, res){ + app.get(re, function (req, res) { var id = req.params.userId , op = req.params[0]; res.end(op + 'ing user ' + id); }); request(app) - .get('/user/10/edit') - .expect('editing user 10', done); + .get('/user/10/edit') + .expect('editing user 10', done); }) } @@ -240,153 +240,153 @@ describe('app.router', function(){ }) }) - describe('case sensitivity', function(){ - it('should be disabled by default', function(done){ + describe('case sensitivity', function () { + it('should be disabled by default', function (done) { var app = express(); - app.get('/user', function(req, res){ + app.get('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/USER') - .expect('tj', done); + .get('/USER') + .expect('tj', done); }) - describe('when "case sensitive routing" is enabled', function(){ - it('should match identical casing', function(done){ + describe('when "case sensitive routing" is enabled', function () { + it('should match identical casing', function (done) { var app = express(); app.enable('case sensitive routing'); - app.get('/uSer', function(req, res){ + app.get('/uSer', function (req, res) { res.end('tj'); }); request(app) - .get('/uSer') - .expect('tj', done); + .get('/uSer') + .expect('tj', done); }) - it('should not match otherwise', function(done){ + it('should not match otherwise', function (done) { var app = express(); app.enable('case sensitive routing'); - app.get('/uSer', function(req, res){ + app.get('/uSer', function (req, res) { res.end('tj'); }); request(app) - .get('/user') - .expect(404, done); + .get('/user') + .expect(404, done); }) }) }) - describe('params', function(){ - it('should overwrite existing req.params by default', function(done){ + describe('params', function () { + it('should overwrite existing req.params by default', function (done) { var app = express(); var router = new express.Router(); - router.get('/:action', function(req, res){ + router.get('/:action', function (req, res) { res.send(req.params); }); app.use('/user/:user', router); request(app) - .get('/user/1/get') - .expect(200, '{"action":"get"}', done); + .get('/user/1/get') + .expect(200, '{"action":"get"}', done); }) - it('should allow merging existing req.params', function(done){ + it('should allow merging existing req.params', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); - router.get('/:action', function(req, res){ + router.get('/:action', function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use('/user/:user', router); request(app) - .get('/user/tj/get') - .expect(200, '[["action","get"],["user","tj"]]', done); + .get('/user/tj/get') + .expect(200, '[["action","get"],["user","tj"]]', done); }) - it('should use params from router', function(done){ + it('should use params from router', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); - router.get('/:thing', function(req, res){ + router.get('/:thing', function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use('/user/:thing', router); request(app) - .get('/user/tj/get') - .expect(200, '[["thing","get"]]', done); + .get('/user/tj/get') + .expect(200, '[["thing","get"]]', done); }) - it('should merge numeric indices req.params', function(done){ + it('should merge numeric indices req.params', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); router.get(/^\/(.*)\.(.*)/, function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use(/^\/user\/id:(\d+)/, router); request(app) - .get('/user/id:10/profile.json') - .expect(200, '[["0","10"],["1","profile"],["2","json"]]', done); + .get('/user/id:10/profile.json') + .expect(200, '[["0","10"],["1","profile"],["2","json"]]', done); }) - it('should merge numeric indices req.params when more in parent', function(done){ + it('should merge numeric indices req.params when more in parent', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); router.get(/\/(.*)/, function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use(/^\/user\/id:(\d+)\/name:(\w+)/, router); request(app) - .get('/user/id:10/name:tj/profile') - .expect(200, '[["0","10"],["1","tj"],["2","profile"]]', done); + .get('/user/id:10/name:tj/profile') + .expect(200, '[["0","10"],["1","tj"],["2","profile"]]', done); }) - it('should merge numeric indices req.params when parent has same number', function(done){ + it('should merge numeric indices req.params when parent has same number', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); - router.get(/\/name:(\w+)/, function(req, res){ + router.get(/\/name:(\w+)/, function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use(/\/user\/id:(\d+)/, router); request(app) - .get('/user/id:10/name:tj') - .expect(200, '[["0","10"],["1","tj"]]', done); + .get('/user/id:10/name:tj') + .expect(200, '[["0","10"],["1","tj"]]', done); }) - it('should ignore invalid incoming req.params', function(done){ + it('should ignore invalid incoming req.params', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); - router.get('/:name', function(req, res){ + router.get('/:name', function (req, res) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); app.use('/user/', function (req, res, next) { @@ -395,11 +395,11 @@ describe('app.router', function(){ }); request(app) - .get('/user/tj') - .expect(200, '[["name","tj"]]', done); + .get('/user/tj') + .expect(200, '[["name","tj"]]', done); }) - it('should restore req.params', function(done){ + it('should restore req.params', function (done) { var app = express(); var router = new express.Router({ mergeParams: true }); @@ -410,45 +410,45 @@ describe('app.router', function(){ app.use(/\/user\/id:(\d+)/, function (req, res, next) { router(req, res, function (err) { var keys = Object.keys(req.params).sort(); - res.send(keys.map(function(k){ return [k, req.params[k]] })); + res.send(keys.map(function (k) { return [k, req.params[k]] })); }); }); request(app) - .get('/user/id:42/user:tj/profile') - .expect(200, '[["0","42"]]', done); + .get('/user/id:42/user:tj/profile') + .expect(200, '[["0","42"]]', done); }) }) - describe('trailing slashes', function(){ - it('should be optional by default', function(done){ + describe('trailing slashes', function () { + it('should be optional by default', function (done) { var app = express(); - app.get('/user', function(req, res){ + app.get('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/user/') - .expect('tj', done); + .get('/user/') + .expect('tj', done); }) - describe('when "strict routing" is enabled', function(){ - it('should match trailing slashes', function(done){ + describe('when "strict routing" is enabled', function () { + it('should match trailing slashes', function (done) { var app = express(); app.enable('strict routing'); - app.get('/user/', function(req, res){ + app.get('/user/', function (req, res) { res.end('tj'); }); request(app) - .get('/user/') - .expect('tj', done); + .get('/user/') + .expect('tj', done); }) - it('should pass-though middleware', function(done){ + it('should pass-though middleware', function (done) { var app = express(); app.enable('strict routing'); @@ -458,17 +458,17 @@ describe('app.router', function(){ next(); }); - app.get('/user/', function(req, res){ + app.get('/user/', function (req, res) { res.end('tj'); }); request(app) - .get('/user/') - .expect('x-middleware', 'true') - .expect(200, 'tj', done); + .get('/user/') + .expect('x-middleware', 'true') + .expect(200, 'tj', done); }) - it('should pass-though mounted middleware', function(done){ + it('should pass-though mounted middleware', function (done) { var app = express(); app.enable('strict routing'); @@ -478,106 +478,106 @@ describe('app.router', function(){ next(); }); - app.get('/user/test/', function(req, res){ + app.get('/user/test/', function (req, res) { res.end('tj'); }); request(app) - .get('/user/test/') - .expect('x-middleware', 'true') - .expect(200, 'tj', done); + .get('/user/test/') + .expect('x-middleware', 'true') + .expect(200, 'tj', done); }) - it('should match no slashes', function(done){ + it('should match no slashes', function (done) { var app = express(); app.enable('strict routing'); - app.get('/user', function(req, res){ + app.get('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/user') - .expect('tj', done); + .get('/user') + .expect('tj', done); }) - it('should match middleware when omitting the trailing slash', function(done){ + it('should match middleware when omitting the trailing slash', function (done) { var app = express(); app.enable('strict routing'); - app.use('/user/', function(req, res){ + app.use('/user/', function (req, res) { res.end('tj'); }); request(app) - .get('/user') - .expect(200, 'tj', done); + .get('/user') + .expect(200, 'tj', done); }) - it('should match middleware', function(done){ + it('should match middleware', function (done) { var app = express(); app.enable('strict routing'); - app.use('/user', function(req, res){ + app.use('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/user') - .expect(200, 'tj', done); + .get('/user') + .expect(200, 'tj', done); }) - it('should match middleware when adding the trailing slash', function(done){ + it('should match middleware when adding the trailing slash', function (done) { var app = express(); app.enable('strict routing'); - app.use('/user', function(req, res){ + app.use('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/user/') - .expect(200, 'tj', done); + .get('/user/') + .expect(200, 'tj', done); }) - it('should fail when omitting the trailing slash', function(done){ + it('should fail when omitting the trailing slash', function (done) { var app = express(); app.enable('strict routing'); - app.get('/user/', function(req, res){ + app.get('/user/', function (req, res) { res.end('tj'); }); request(app) - .get('/user') - .expect(404, done); + .get('/user') + .expect(404, done); }) - it('should fail when adding the trailing slash', function(done){ + it('should fail when adding the trailing slash', function (done) { var app = express(); app.enable('strict routing'); - app.get('/user', function(req, res){ + app.get('/user', function (req, res) { res.end('tj'); }); request(app) - .get('/user/') - .expect(404, done); + .get('/user/') + .expect(404, done); }) }) }) - it('should allow literal "."', function(done){ + it('should allow literal "."', function (done) { var app = express(); - app.get('/api/users/:from..:to', function(req, res){ + app.get('/api/users/:from..:to', function (req, res) { var from = req.params.from , to = req.params.to; @@ -585,119 +585,119 @@ describe('app.router', function(){ }); request(app) - .get('/api/users/1..50') - .expect('users from 1 to 50', done); + .get('/api/users/1..50') + .expect('users from 1 to 50', done); }) - describe(':name', function(){ - it('should denote a capture group', function(done){ + describe(':name', function () { + it('should denote a capture group', function (done) { var app = express(); - app.get('/user/:user', function(req, res){ + app.get('/user/:user', function (req, res) { res.end(req.params.user); }); request(app) - .get('/user/tj') - .expect('tj', done); + .get('/user/tj') + .expect('tj', done); }) - it('should match a single segment only', function(done){ + it('should match a single segment only', function (done) { var app = express(); - app.get('/user/:user', function(req, res){ + app.get('/user/:user', function (req, res) { res.end(req.params.user); }); request(app) - .get('/user/tj/edit') - .expect(404, done); + .get('/user/tj/edit') + .expect(404, done); }) - it('should allow several capture groups', function(done){ + it('should allow several capture groups', function (done) { var app = express(); - app.get('/user/:user/:op', function(req, res){ + app.get('/user/:user/:op', function (req, res) { res.end(req.params.op + 'ing ' + req.params.user); }); request(app) - .get('/user/tj/edit') - .expect('editing tj', done); + .get('/user/tj/edit') + .expect('editing tj', done); }) - it('should work following a partial capture group', function(done){ + it('should work following a partial capture group', function (done) { var app = express(); var cb = after(2, done); - app.get('/user{s}/:user/:op', function(req, res){ + app.get('/user{s}/:user/:op', function (req, res) { res.end(req.params.op + 'ing ' + req.params.user + (req.url.startsWith('/users') ? ' (old)' : '')); }); request(app) - .get('/user/tj/edit') - .expect('editing tj', cb); + .get('/user/tj/edit') + .expect('editing tj', cb); request(app) - .get('/users/tj/edit') - .expect('editing tj (old)', cb); + .get('/users/tj/edit') + .expect('editing tj (old)', cb); }) - it('should work inside literal parenthesis', function(done){ + it('should work inside literal parenthesis', function (done) { var app = express(); - app.get('/:user\\(:op\\)', function(req, res){ + app.get('/:user\\(:op\\)', function (req, res) { res.end(req.params.op + 'ing ' + req.params.user); }); request(app) - .get('/tj(edit)') - .expect('editing tj', done); + .get('/tj(edit)') + .expect('editing tj', done); }) - it('should work in array of paths', function(done){ + it('should work in array of paths', function (done) { var app = express(); var cb = after(2, done); - app.get(['/user/:user/poke', '/user/:user/pokes'], function(req, res){ + app.get(['/user/:user/poke', '/user/:user/pokes'], function (req, res) { res.end('poking ' + req.params.user); }); request(app) - .get('/user/tj/poke') - .expect('poking tj', cb); + .get('/user/tj/poke') + .expect('poking tj', cb); request(app) - .get('/user/tj/pokes') - .expect('poking tj', cb); + .get('/user/tj/pokes') + .expect('poking tj', cb); }) }) - describe(':name?', function(){ - it('should denote an optional capture group', function(done){ + describe(':name?', function () { + it('should denote an optional capture group', function (done) { var app = express(); - app.get('/user/:user{/:op}', function(req, res){ + app.get('/user/:user{/:op}', function (req, res) { var op = req.params.op || 'view'; res.end(op + 'ing ' + req.params.user); }); request(app) - .get('/user/tj') - .expect('viewing tj', done); + .get('/user/tj') + .expect('viewing tj', done); }) - it('should populate the capture group', function(done){ + it('should populate the capture group', function (done) { var app = express(); - app.get('/user/:user{/:op}', function(req, res){ + app.get('/user/:user{/:op}', function (req, res) { var op = req.params.op || 'view'; res.end(op + 'ing ' + req.params.user); }); request(app) - .get('/user/tj/edit') - .expect('editing tj', done); + .get('/user/tj/edit') + .expect('editing tj', done); }) }) @@ -777,12 +777,12 @@ describe('app.router', function(){ }) }) - describe('.:name', function(){ - it('should denote a format', function(done){ + describe('.:name', function () { + it('should denote a format', function (done) { var app = express(); var cb = after(2, done) - app.get('/:name.:format', function(req, res){ + app.get('/:name.:format', function (req, res) { res.end(req.params.name + ' as ' + req.params.format); }); @@ -796,12 +796,12 @@ describe('app.router', function(){ }) }) - describe('.:name?', function(){ - it('should denote an optional format', function(done){ + describe('.:name?', function () { + it('should denote an optional format', function (done) { var app = express(); var cb = after(2, done) - app.get('/:name{.:format}', function(req, res){ + app.get('/:name{.:format}', function (req, res) { res.end(req.params.name + ' as ' + (req.params.format || 'html')); }); @@ -815,12 +815,12 @@ describe('app.router', function(){ }) }) - describe('when next() is called', function(){ - it('should continue lookup', function(done){ + describe('when next() is called', function () { + it('should continue lookup', function (done) { var app = express() , calls = []; - app.get('/foo{/:bar}', function(req, res, next){ + app.get('/foo{/:bar}', function (req, res, next) { calls.push('/foo/:bar?'); next(); }); @@ -829,7 +829,7 @@ describe('app.router', function(){ assert(0); }); - app.get('/foo', function(req, res, next){ + app.get('/foo', function (req, res, next) { calls.push('/foo'); next(); }); @@ -840,16 +840,16 @@ describe('app.router', function(){ }); request(app) - .get('/foo') - .expect(200, ['/foo/:bar?', '/foo', '/foo 2'], done) + .get('/foo') + .expect(200, ['/foo/:bar?', '/foo', '/foo 2'], done) }) }) - describe('when next("route") is called', function(){ - it('should jump to next route', function(done){ + describe('when next("route") is called', function () { + it('should jump to next route', function (done) { var app = express() - function fn(req, res, next){ + function fn(req, res, next) { res.set('X-Hit', '1') next('route') } @@ -858,14 +858,14 @@ describe('app.router', function(){ res.end('failure') }); - app.get('/foo', function(req, res){ + app.get('/foo', function (req, res) { res.end('success') }) request(app) - .get('/foo') - .expect('X-Hit', '1') - .expect(200, 'success', done) + .get('/foo') + .expect('X-Hit', '1') + .expect(200, 'success', done) }) }) @@ -874,7 +874,7 @@ describe('app.router', function(){ var app = express() var router = express.Router() - function fn (req, res, next) { + function fn(req, res, next) { res.set('X-Hit', '1') next('router') } @@ -894,18 +894,18 @@ describe('app.router', function(){ }) request(app) - .get('/foo') - .expect('X-Hit', '1') - .expect(200, 'success', done) + .get('/foo') + .expect('X-Hit', '1') + .expect(200, 'success', done) }) }) - describe('when next(err) is called', function(){ - it('should break out of app.router', function(done){ + describe('when next(err) is called', function () { + it('should break out of app.router', function (done) { var app = express() , calls = []; - app.get('/foo{/:bar}', function(req, res, next){ + app.get('/foo{/:bar}', function (req, res, next) { calls.push('/foo/:bar?'); next(); }); @@ -914,7 +914,7 @@ describe('app.router', function(){ assert(0); }); - app.get('/foo', function(req, res, next){ + app.get('/foo', function (req, res, next) { calls.push('/foo'); next(new Error('fail')); }); @@ -923,7 +923,7 @@ describe('app.router', function(){ assert(0); }); - app.use(function(err, req, res, next){ + app.use(function (err, req, res, next) { res.json({ calls: calls, error: err.message @@ -931,11 +931,11 @@ describe('app.router', function(){ }) request(app) - .get('/foo') - .expect(200, { calls: ['/foo/:bar?', '/foo'], error: 'fail' }, done) + .get('/foo') + .expect(200, { calls: ['/foo/:bar?', '/foo'], error: 'fail' }, done) }) - it('should call handler in same route, if exists', function(done){ + it('should call handler in same route, if exists', function (done) { var app = express(); function fn1(req, res, next) { @@ -957,8 +957,8 @@ describe('app.router', function(){ }) request(app) - .get('/foo') - .expect('route go boom!', done) + .get('/foo') + .expect('route go boom!', done) }) }) @@ -967,45 +967,45 @@ describe('app.router', function(){ var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { return Promise.reject(new Error('boom!')) }) - router.use(function sawError (err, req, res, next) { + router.use(function sawError(err, req, res, next) { res.send('saw ' + err.name + ': ' + err.message) }) app.use(router) request(app) - .get('/') - .expect(200, 'saw Error: boom!', done) + .get('/') + .expect(200, 'saw Error: boom!', done) }) it('should pass rejected promise without value', function (done) { var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { return Promise.reject() }) - router.use(function sawError (err, req, res, next) { + router.use(function sawError(err, req, res, next) { res.send('saw ' + err.name + ': ' + err.message) }) app.use(router) request(app) - .get('/') - .expect(200, 'saw Error: Rejected promise', done) + .get('/') + .expect(200, 'saw Error: Rejected promise', done) }) it('should ignore resolved promise', function (done) { var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { res.send('saw GET /foo') return Promise.resolve('foo') }) @@ -1017,8 +1017,8 @@ describe('app.router', function(){ app.use(router) request(app) - .get('/foo') - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect(200, 'saw GET /foo', done) }) describe('error handling', function () { @@ -1026,57 +1026,57 @@ describe('app.router', function(){ var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { return Promise.reject(new Error('boom!')) }) - router.use(function handleError (err, req, res, next) { + router.use(function handleError(err, req, res, next) { return Promise.reject(new Error('caught: ' + err.message)) }) - router.use(function sawError (err, req, res, next) { + router.use(function sawError(err, req, res, next) { res.send('saw ' + err.name + ': ' + err.message) }) app.use(router) request(app) - .get('/') - .expect(200, 'saw Error: caught: boom!', done) + .get('/') + .expect(200, 'saw Error: caught: boom!', done) }) it('should pass rejected promise without value', function (done) { var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { return Promise.reject() }) - router.use(function handleError (err, req, res, next) { + router.use(function handleError(err, req, res, next) { return Promise.reject(new Error('caught: ' + err.message)) }) - router.use(function sawError (err, req, res, next) { + router.use(function sawError(err, req, res, next) { res.send('saw ' + err.name + ': ' + err.message) }) app.use(router) request(app) - .get('/') - .expect(200, 'saw Error: caught: Rejected promise', done) + .get('/') + .expect(200, 'saw Error: caught: Rejected promise', done) }) it('should ignore resolved promise', function (done) { var app = express() var router = new express.Router() - router.use(function createError (req, res, next) { + router.use(function createError(req, res, next) { return Promise.reject(new Error('boom!')) }) - router.use(function handleError (err, req, res, next) { + router.use(function handleError(err, req, res, next) { res.send('saw ' + err.name + ': ' + err.message) return Promise.resolve('foo') }) @@ -1088,31 +1088,31 @@ describe('app.router', function(){ app.use(router) request(app) - .get('/foo') - .expect(200, 'saw Error: boom!', done) + .get('/foo') + .expect(200, 'saw Error: boom!', done) }) }) }) - it('should allow rewriting of the url', function(done){ + it('should allow rewriting of the url', function (done) { var app = express(); - app.get('/account/edit', function(req, res, next){ + app.get('/account/edit', function (req, res, next) { req.user = { id: 12 }; // faux authenticated user req.url = '/user/' + req.user.id + '/edit'; next(); }); - app.get('/user/:id/edit', function(req, res){ + app.get('/user/:id/edit', function (req, res) { res.send('editing user ' + req.params.id); }); request(app) - .get('/account/edit') - .expect('editing user 12', done); + .get('/account/edit') + .expect('editing user 12', done); }) - it('should run in order added', function(done){ + it('should run in order added', function (done) { var app = express(); var path = []; @@ -1121,17 +1121,17 @@ describe('app.router', function(){ next(); }); - app.get('/user/:id', function(req, res, next){ + app.get('/user/:id', function (req, res, next) { path.push(1); next(); }); - app.use(function(req, res, next){ + app.use(function (req, res, next) { path.push(2); next(); }); - app.all('/user/:id', function(req, res, next){ + app.all('/user/:id', function (req, res, next) { path.push(3); next(); }); @@ -1141,28 +1141,28 @@ describe('app.router', function(){ next(); }); - app.use(function(req, res, next){ + app.use(function (req, res, next) { path.push(5); res.end(path.join(',')) }); request(app) - .get('/user/1') - .expect(200, '0,1,2,3,4,5', done); + .get('/user/1') + .expect(200, '0,1,2,3,4,5', done); }) - it('should be chainable', function(){ + it('should be chainable', function () { var app = express(); - assert.strictEqual(app.get('/', function () {}), app) + assert.strictEqual(app.get('/', function () { }), app) }) - it('should should not use disposed router/middleware', function(done){ + it('should should not use disposed router/middleware', function (done) { // more context: https://github.com/expressjs/express/issues/5743#issuecomment-2277148412 var app = express(); var router = new express.Router(); - router.use(function(req, res, next){ + router.use(function (req, res, next) { res.setHeader('old', 'foo'); next(); }); @@ -1171,39 +1171,39 @@ describe('app.router', function(){ return router.handle(req, res, next); }); - app.get('/', function(req, res, next){ + app.get('/', function (req, res, next) { res.send('yee'); next(); }); request(app) - .get('/') - .expect('old', 'foo') - .expect(function(res) { - if (typeof res.headers['new'] !== 'undefined') { - throw new Error('`new` header should not be present'); - } - }) - .expect(200, 'yee', function(err, res) { - if (err) return done(err); - - router = new express.Router(); - - router.use(function(req, res, next){ - res.setHeader('new', 'bar'); - next(); - }); - - request(app) .get('/') - .expect('new', 'bar') - .expect(function(res) { - if (typeof res.headers['old'] !== 'undefined') { - throw new Error('`old` header should not be present'); + .expect('old', 'foo') + .expect(function (res) { + if (typeof res.headers['new'] !== 'undefined') { + throw new Error('`new` header should not be present'); } }) - .expect(200, 'yee', done); - }); + .expect(200, 'yee', function (err, res) { + if (err) return done(err); + + router = new express.Router(); + + router.use(function (req, res, next) { + res.setHeader('new', 'bar'); + next(); + }); + + request(app) + .get('/') + .expect('new', 'bar') + .expect(function (res) { + if (typeof res.headers['old'] !== 'undefined') { + throw new Error('`old` header should not be present'); + } + }) + .expect(200, 'yee', done); + }); }) }) diff --git a/test/app.routes.error.js b/test/app.routes.error.js index efc0108b0f..ed53c7857b 100644 --- a/test/app.routes.error.js +++ b/test/app.routes.error.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../') , request = require('supertest'); diff --git a/test/app.use.js b/test/app.use.js index a88a2f2c8e..1d56aa3b02 100644 --- a/test/app.use.js +++ b/test/app.use.js @@ -1,7 +1,7 @@ 'use strict' var after = require('after'); -var assert = require('assert') +var assert = require('node:assert') var express = require('..'); var request = require('supertest'); diff --git a/test/config.js b/test/config.js index b04367fdbf..d004de03ea 100644 --- a/test/config.js +++ b/test/config.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert'); +var assert = require('node:assert'); var express = require('..'); describe('config', function () { diff --git a/test/exports.js b/test/exports.js index dc635d1dbc..fc7836c159 100644 --- a/test/exports.js +++ b/test/exports.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../'); var request = require('supertest'); diff --git a/test/express.json.js b/test/express.json.js index 2c394922c8..6b91734ed3 100644 --- a/test/express.json.js +++ b/test/express.json.js @@ -1,7 +1,7 @@ 'use strict' -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('..') var request = require('supertest') diff --git a/test/express.raw.js b/test/express.raw.js index 53995bf829..362fccb1e3 100644 --- a/test/express.raw.js +++ b/test/express.raw.js @@ -1,7 +1,7 @@ 'use strict' -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('..') var request = require('supertest') diff --git a/test/express.static.js b/test/express.static.js index 65a3718a60..16a8ec0516 100644 --- a/test/express.static.js +++ b/test/express.static.js @@ -1,8 +1,8 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('..') -var path = require('path') +var path = require('node:path') var request = require('supertest') var utils = require('./support/utils') diff --git a/test/express.text.js b/test/express.text.js index 95fcbf8657..1d22929e3b 100644 --- a/test/express.text.js +++ b/test/express.text.js @@ -1,7 +1,7 @@ 'use strict' -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('..') var request = require('supertest') diff --git a/test/express.urlencoded.js b/test/express.urlencoded.js index 50c3e93bba..b2df949f9f 100644 --- a/test/express.urlencoded.js +++ b/test/express.urlencoded.js @@ -1,7 +1,7 @@ 'use strict' -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('..') var request = require('supertest') diff --git a/test/middleware.basic.js b/test/middleware.basic.js index 19f00d9a29..1f1ed17571 100644 --- a/test/middleware.basic.js +++ b/test/middleware.basic.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../'); var request = require('supertest'); diff --git a/test/req.get.js b/test/req.get.js index 16589b3f05..e73d109c84 100644 --- a/test/req.get.js +++ b/test/req.get.js @@ -2,7 +2,7 @@ var express = require('../') , request = require('supertest') - , assert = require('assert'); + , assert = require('node:assert'); describe('req', function(){ describe('.get(field)', function(){ diff --git a/test/req.query.js b/test/req.query.js index bc76d4106b..c0d3c8376e 100644 --- a/test/req.query.js +++ b/test/req.query.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('../') , request = require('supertest'); diff --git a/test/res.append.js b/test/res.append.js index 8f72598bf5..325dd4d12e 100644 --- a/test/res.append.js +++ b/test/res.append.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('..') var request = require('supertest') diff --git a/test/res.download.js b/test/res.download.js index 5718ff4409..1bd7663c54 100644 --- a/test/res.download.js +++ b/test/res.download.js @@ -1,11 +1,11 @@ 'use strict' var after = require('after'); -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('..'); -var path = require('path') +var path = require('node:path') var request = require('supertest'); var utils = require('./support/utils') diff --git a/test/res.format.js b/test/res.format.js index 59205bfaf4..be42730957 100644 --- a/test/res.format.js +++ b/test/res.format.js @@ -3,7 +3,7 @@ var after = require('after') var express = require('../') , request = require('supertest') - , assert = require('assert'); + , assert = require('node:assert'); var app1 = express(); diff --git a/test/res.json.js b/test/res.json.js index bef8adafd5..ffd547e95b 100644 --- a/test/res.json.js +++ b/test/res.json.js @@ -2,7 +2,7 @@ var express = require('../') , request = require('supertest') - , assert = require('assert'); + , assert = require('node:assert'); describe('res', function(){ describe('.json(object)', function(){ diff --git a/test/res.jsonp.js b/test/res.jsonp.js index e9cc08bc05..c1f90f1109 100644 --- a/test/res.jsonp.js +++ b/test/res.jsonp.js @@ -2,7 +2,7 @@ var express = require('../') , request = require('supertest') - , assert = require('assert'); + , assert = require('node:assert'); var utils = require('./support/utils'); describe('res', function(){ diff --git a/test/res.location.js b/test/res.location.js index fb03221d7a..b81c6f07d8 100644 --- a/test/res.location.js +++ b/test/res.location.js @@ -2,8 +2,8 @@ var express = require('../') , request = require('supertest') - , assert = require('assert') - , url = require('url'); + , assert = require('node:assert') + , url = require('node:url'); describe('res', function(){ describe('.location(url)', function(){ diff --git a/test/res.render.js b/test/res.render.js index 50f0b0a742..114b398e0b 100644 --- a/test/res.render.js +++ b/test/res.render.js @@ -1,7 +1,7 @@ 'use strict' var express = require('..'); -var path = require('path') +var path = require('node:path') var request = require('supertest'); var tmpl = require('./support/tmpl'); diff --git a/test/res.send.js b/test/res.send.js index b061dfcc51..860607c49b 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert') +var assert = require('node:assert') var express = require('..'); var methods = require('../lib/utils').methods; var request = require('supertest'); diff --git a/test/res.sendFile.js b/test/res.sendFile.js index ec90adf5d3..505f8d114d 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -1,13 +1,13 @@ 'use strict' var after = require('after'); -var assert = require('assert') -var AsyncLocalStorage = require('async_hooks').AsyncLocalStorage +var assert = require('node:assert') +var AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage var express = require('../') , request = require('supertest') var onFinished = require('on-finished'); -var path = require('path'); +var path = require('node:path'); var fixtures = path.join(__dirname, 'fixtures'); var utils = require('./support/utils'); diff --git a/test/support/tmpl.js b/test/support/tmpl.js index bab65669d3..e24b6fe773 100644 --- a/test/support/tmpl.js +++ b/test/support/tmpl.js @@ -1,4 +1,4 @@ -var fs = require('fs'); +var fs = require('node:fs'); var variableRegExp = /\$([0-9a-zA-Z\.]+)/g; diff --git a/test/support/utils.js b/test/support/utils.js index c74f8efe0c..25022528ef 100644 --- a/test/support/utils.js +++ b/test/support/utils.js @@ -4,7 +4,7 @@ * @private */ -var assert = require('assert'); +var assert = require('node:assert'); /** * Module exports. diff --git a/test/utils.js b/test/utils.js index ae73988b01..d1142266ac 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,6 +1,6 @@ 'use strict' -var assert = require('assert'); +var assert = require('node:assert'); var utils = require('../lib/utils'); describe('utils.etag(body, encoding)', function(){