Files
express/test/app.js

132 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-11-24 12:10:08 -08:00
var assert = require('assert')
var express = require('..')
var request = require('supertest')
2011-11-24 12:10:08 -08:00
describe('app', function(){
it('should inherit from event emitter', function(done){
var app = express();
app.on('foo', done);
app.emit('foo');
})
it('should be callable', function(){
var app = express();
assert.equal(typeof app, 'function');
})
it('should 404 without routes', function(done){
request(express())
.get('/')
.expect(404, done);
})
2011-11-24 12:10:08 -08:00
})
2011-12-06 15:36:44 -08:00
describe('app.parent', function(){
it('should return the parent when mounted', function(){
var app = express()
, blog = express()
, blogAdmin = express();
app.use('/blog', blog);
blog.use('/admin', blogAdmin);
assert(!app.parent, 'app.parent');
assert.strictEqual(blog.parent, app)
assert.strictEqual(blogAdmin.parent, blog)
2011-12-06 15:36:44 -08:00
})
})
remove app.router and refactor middleware processing This is an overhaul of middleware processing, Router and Route. Connect is no longer used to process the middleware stack. This functionality has been split into two parts: middleware stack and default error response. The entry point for request processing is the `app.handle` method. It sets up the default error response handle (to run in the event of no other error handler) and then triggers the app router (instance of Router) to handle the request. The app router `handle` function contains the middleware dispatch layer previously in the connect codebase. This layer handle the logic for dispatching `.use` calls (stripping paths if needed). The app contains a base router `app._router`. New routes can be created and `.use`d on this router to organize routes into files. Routers now have the following methods `.use`, `.all`, `.param` which are all public. Additionally, Routers have a `.route(path)` method which returns a new instance of Route for the requested path. Route(s) are isolated middleware stacks and contain methods for the HTTP verbs as well as an `.all` method to act similar to middleware. These methods are chainable to easily describe requirements for a route. var route = Router.route('/foo'); // or 'app.route('/foo')' route .all(auth) .get(function(...) {}) .all(more_checks) .post(function(...) {}) Any Route and Router methods which accept handlers also accept error (arity 4) handlers which will also behave as expected. Finally, the `app.router` getter has been removed. Middleware and handlers are run IN THE ORDER they are seen in the file. This means that code which injected the `app.router` and then added error handlers (or other middleware) will need to be updated to move those handlers after any requests added on the app object. The examples have been updated accordingly. This is the largest breaking change to codebases in this commit.
2014-01-25 17:57:25 -05:00
describe('app.mountpath', function(){
2011-12-06 15:36:44 -08:00
it('should return the mounted path', function(){
var admin = express();
var app = express();
var blog = express();
var fallback = express();
2011-12-06 15:36:44 -08:00
app.use('/blog', blog);
app.use(fallback);
blog.use('/admin', admin);
2011-12-06 15:36:44 -08:00
assert.strictEqual(admin.mountpath, '/admin')
assert.strictEqual(app.mountpath, '/')
assert.strictEqual(blog.mountpath, '/blog')
assert.strictEqual(fallback.mountpath, '/')
remove app.router and refactor middleware processing This is an overhaul of middleware processing, Router and Route. Connect is no longer used to process the middleware stack. This functionality has been split into two parts: middleware stack and default error response. The entry point for request processing is the `app.handle` method. It sets up the default error response handle (to run in the event of no other error handler) and then triggers the app router (instance of Router) to handle the request. The app router `handle` function contains the middleware dispatch layer previously in the connect codebase. This layer handle the logic for dispatching `.use` calls (stripping paths if needed). The app contains a base router `app._router`. New routes can be created and `.use`d on this router to organize routes into files. Routers now have the following methods `.use`, `.all`, `.param` which are all public. Additionally, Routers have a `.route(path)` method which returns a new instance of Route for the requested path. Route(s) are isolated middleware stacks and contain methods for the HTTP verbs as well as an `.all` method to act similar to middleware. These methods are chainable to easily describe requirements for a route. var route = Router.route('/foo'); // or 'app.route('/foo')' route .all(auth) .get(function(...) {}) .all(more_checks) .post(function(...) {}) Any Route and Router methods which accept handlers also accept error (arity 4) handlers which will also behave as expected. Finally, the `app.router` getter has been removed. Middleware and handlers are run IN THE ORDER they are seen in the file. This means that code which injected the `app.router` and then added error handlers (or other middleware) will need to be updated to move those handlers after any requests added on the app object. The examples have been updated accordingly. This is the largest breaking change to codebases in this commit.
2014-01-25 17:57:25 -05:00
})
})
describe('app.router', function(){
it('should throw with notice', function(done){
var app = express()
try {
app.router;
} catch(err) {
done();
}
2011-12-06 15:36:44 -08:00
})
})
describe('app.path()', function(){
it('should return the canonical', function(){
var app = express()
, blog = express()
, blogAdmin = express();
app.use('/blog', blog);
blog.use('/admin', blogAdmin);
assert.strictEqual(app.path(), '')
assert.strictEqual(blog.path(), '/blog')
assert.strictEqual(blogAdmin.path(), '/blog/admin')
2011-12-06 15:36:44 -08:00
})
2012-02-26 11:56:49 -08:00
})
describe('in development', function(){
before(function () {
this.env = process.env.NODE_ENV
process.env.NODE_ENV = 'development'
})
after(function () {
process.env.NODE_ENV = this.env
})
2012-02-26 11:56:49 -08:00
it('should disable "view cache"', function(){
var app = express();
assert.ok(!app.enabled('view cache'))
2012-02-26 11:56:49 -08:00
})
})
describe('in production', function(){
before(function () {
this.env = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
})
after(function () {
process.env.NODE_ENV = this.env
})
2012-02-26 11:56:49 -08:00
it('should enable "view cache"', function(){
var app = express();
assert.ok(app.enabled('view cache'))
2012-02-26 11:56:49 -08:00
})
remove app.router and refactor middleware processing This is an overhaul of middleware processing, Router and Route. Connect is no longer used to process the middleware stack. This functionality has been split into two parts: middleware stack and default error response. The entry point for request processing is the `app.handle` method. It sets up the default error response handle (to run in the event of no other error handler) and then triggers the app router (instance of Router) to handle the request. The app router `handle` function contains the middleware dispatch layer previously in the connect codebase. This layer handle the logic for dispatching `.use` calls (stripping paths if needed). The app contains a base router `app._router`. New routes can be created and `.use`d on this router to organize routes into files. Routers now have the following methods `.use`, `.all`, `.param` which are all public. Additionally, Routers have a `.route(path)` method which returns a new instance of Route for the requested path. Route(s) are isolated middleware stacks and contain methods for the HTTP verbs as well as an `.all` method to act similar to middleware. These methods are chainable to easily describe requirements for a route. var route = Router.route('/foo'); // or 'app.route('/foo')' route .all(auth) .get(function(...) {}) .all(more_checks) .post(function(...) {}) Any Route and Router methods which accept handlers also accept error (arity 4) handlers which will also behave as expected. Finally, the `app.router` getter has been removed. Middleware and handlers are run IN THE ORDER they are seen in the file. This means that code which injected the `app.router` and then added error handlers (or other middleware) will need to be updated to move those handlers after any requests added on the app object. The examples have been updated accordingly. This is the largest breaking change to codebases in this commit.
2014-01-25 17:57:25 -05:00
})
2014-05-20 23:25:51 -04:00
describe('without NODE_ENV', function(){
before(function () {
this.env = process.env.NODE_ENV
process.env.NODE_ENV = ''
})
after(function () {
process.env.NODE_ENV = this.env
})
2014-05-20 23:25:51 -04:00
it('should default to development', function(){
var app = express();
assert.strictEqual(app.get('env'), 'development')
2014-05-20 23:25:51 -04:00
})
})