Skip to content

Commit 69d9ab2

Browse files
committed
switch to mocha for tests
1 parent 2368393 commit 69d9ab2

File tree

5 files changed

+97
-200
lines changed

5 files changed

+97
-200
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
test:
3-
@./node_modules/.bin/expresso --serial
3+
@./node_modules/.bin/mocha \
4+
--reporter spec
45

56
.PHONY: test

index.js

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
/*!
2-
* Express - Contrib - namespace
3-
* Copyright(c) 2010 TJ Holowaychuk <[email protected]>
4-
* MIT Licensed
5-
*/
61

72
/**
83
* Module dependencies.
94
*/
105

116
var express = require('express')
12-
, join = require('path').join
7+
, methods = require('methods').concat('del')
138
, app = express.application
149
? express.application
1510
: express.HTTPServer.prototype;
@@ -24,61 +19,41 @@ var express = require('express')
2419
* @api public
2520
*/
2621

27-
exports.namespace = function(){
28-
var args = Array.apply(null, arguments)
22+
app.namespace = function(){
23+
var args = Array.prototype.slice.call(arguments)
2924
, path = args.shift()
3025
, fn = args.pop()
3126
, self = this;
3227

33-
if (args.length) self.all(path + '*', args);
28+
if (args.length) self.all(path, args);
29+
if (args.length) self.all(path + '/*', args);
3430
(this._ns = this._ns || []).push(path);
3531
fn.call(this);
3632
this._ns.pop();
3733
return this;
3834
};
3935

40-
/**
41-
* Return the current namespace.
42-
*
43-
* @return {String}
44-
* @api public
45-
*/
46-
47-
exports.__defineGetter__('currentNamespace', function(){
48-
return join.apply(this, this._ns).replace(/\\/g, '/').replace(/\/$/, '') || '/';
49-
});
50-
5136
/**
5237
* Proxy HTTP methods to provide namespacing support.
5338
*/
5439

55-
(express.router || express.Router).methods.concat('del').forEach(function(method){
40+
methods.forEach(function(method){
5641
var orig = app[method];
57-
exports[method] = function(){
42+
app[method] = function(val){
43+
var len = arguments.length;
44+
if ('get' == method && 1 == len) return orig.call(this, val);
45+
5846
var args = Array.prototype.slice.call(arguments)
5947
, path = args.shift()
60-
, fn = args.pop()
6148
, self = this;
6249

6350
this.namespace(path, function(){
64-
var curr = this.currentNamespace;
51+
path = this._ns.join('/').replace(/\/\//g, '/').replace(/\/$/, '');
6552
args.forEach(function(fn){
66-
fn.namespace = curr;
67-
orig.call(self, curr, fn);
53+
orig.call(self, path, fn);
6854
});
69-
70-
fn.namespace = curr;
71-
orig.call(self, curr, fn);
7255
});
7356

7457
return this;
7558
};
7659
});
77-
78-
// merge
79-
80-
for (var key in exports) {
81-
var desc = Object.getOwnPropertyDescriptor(exports, key);
82-
Object.defineProperty(app, key, desc);
83-
Object.defineProperty(app, key, desc);
84-
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
, "author": "TJ Holowaychuk <[email protected]>"
55
, "keywords": ["express"]
66
, "main": "index"
7+
, "dependencies": {
8+
"methods": "0.0.1"
9+
}
710
, "devDependencies": {
811
"express": "3.x"
9-
, "ejs": ">= 0.0.1"
10-
, "expresso": ">= 0.0.1"
12+
, "ejs": "*"
13+
, "mocha": "*"
14+
, "supertest": "*"
1115
}
1216
}

test/namespace.test.js

Lines changed: 70 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,199 +1,109 @@
1+
12
/**
23
* Module dependencies.
34
*/
45

56
var express = require('express')
67
, assert = require('assert')
7-
, namespace = require('../');
8+
, request = require('supertest')
9+
, namespace = require('..')
10+
, pending = require('./support/pending');
811

9-
module.exports = {
10-
'test app.namespace(str, fn)': function(){
11-
var app = express()
12-
, id;
12+
describe('app.namespace(path, fn)', function(){
13+
it('should not prefix root-level paths', function(done){
14+
var app = express();
15+
done = pending(2, done);
1316

1417
app.get('/one', function(req, res){
1518
res.send('GET one');
1619
});
1720

18-
assert.equal(app.namespace('/user', function(){}), app);
19-
20-
app.namespace('/user', function(){
21-
app.all('/:id', function(req, res, next){
22-
id = req.params.id;
23-
next();
24-
});
25-
26-
app.get('/:id', function(req, res){
27-
res.send('GET user ' + id);
28-
});
29-
30-
app.del('/:id', function(req, res){
31-
res.send('DELETE user ' + id);
32-
});
33-
});
34-
35-
app.get('/two', function(req, res){
21+
app.get('/some/two', function(req, res){
3622
res.send('GET two');
3723
});
3824

39-
assert.response(app,
40-
{ url: '/user/12' },
41-
{ body: 'GET user 12' });
42-
43-
assert.response(app,
44-
{ url: '/user/12', method: 'DELETE' },
45-
{ body: 'DELETE user 12' });
46-
47-
assert.response(app,
48-
{ url: '/one' },
49-
{ body: 'GET one' });
50-
51-
assert.response(app,
52-
{ url: '/two' },
53-
{ body: 'GET two' });
54-
},
55-
56-
'test app.namespace(str, fn) nesting': function(done){
57-
var pending = 6
58-
, calls = 0
59-
, app = express();
60-
61-
function finished() {
62-
--pending || function(){
63-
assert.equal(2, calls);
64-
done();
65-
}();
66-
}
25+
request(app)
26+
.get('/one')
27+
.expect('GET one', done);
6728

68-
function middleware(req, res, next) {
69-
++calls;
70-
next();
71-
}
29+
request(app)
30+
.get('/some/two')
31+
.expect('GET two', done);
32+
})
33+
34+
it('should prefix within .namespace()', function(done){
35+
var app = express();
36+
done = pending(4, done);
7237

7338
app.get('/one', function(req, res){
7439
res.send('GET one');
7540
});
7641

77-
app.namespace('/forum/:id', function(){
42+
app.namespace('/foo', function(){
7843
app.get('/', function(req, res){
79-
res.send('GET forum ' + req.params.id);
80-
});
81-
82-
app.get('/edit', function(req, res){
83-
res.send('GET forum ' + req.params.id + ' edit page');
44+
res.send('foo');
8445
});
8546

86-
app.namespace('/thread', function(){
87-
app.get('/:tid', middleware, middleware, function(req, res){
88-
res.send('GET forum ' + req.params.id + ' thread ' + req.params.tid);
47+
app.namespace('/baz', function(){
48+
app.get('/', function(req, res){
49+
res.send('GET baz');
8950
});
90-
});
9151

92-
app.del('/', function(req, res){
93-
res.send('DELETE forum ' + req.params.id);
52+
app.del('/all', function(req, res){
53+
res.send('DELETE all baz');
54+
});
55+
})
56+
57+
app.get('/bar', function(req, res){
58+
res.send('bar');
9459
});
95-
});
96-
97-
app.get('/two', function(req, res){
60+
})
61+
62+
app.get('/some/two', function(req, res){
9863
res.send('GET two');
9964
});
10065

101-
assert.response(app,
102-
{ url: '/forum/1' },
103-
{ body: 'GET forum 1' }
104-
, finished);
105-
106-
assert.response(app,
107-
{ url: '/forum/1/edit' },
108-
{ body: 'GET forum 1 edit page' }
109-
, finished);
110-
111-
assert.response(app,
112-
{ url: '/forum/1/thread/50' },
113-
{ body: 'GET forum 1 thread 50' }
114-
, finished);
115-
116-
assert.response(app,
117-
{ url: '/forum/2', method: 'DELETE' },
118-
{ body: 'DELETE forum 2' }
119-
, finished);
120-
121-
assert.response(app,
122-
{ url: '/one' },
123-
{ body: 'GET one' }
124-
, finished);
125-
126-
assert.response(app,
127-
{ url: '/two' },
128-
{ body: 'GET two' }
129-
, finished);
130-
},
131-
132-
'test fn.route': function(){
133-
var app = express();
66+
request(app)
67+
.get('/foo/baz')
68+
.expect('GET baz', done);
13469

135-
app.namespace('/user/:id', function(){
136-
app.get('/', function handler(req, res){
137-
assert.equal('/user/:id', handler.namespace);
138-
res.send(200);
139-
});
140-
});
70+
request(app)
71+
.del('/foo/baz/all')
72+
.expect('DELETE all baz', done);
14173

142-
assert.response(app,
143-
{ url: '/user/12' },
144-
{ body: 'OK' });
145-
},
146-
'test app.namespace(str, middleware, fn)': function(done){
147-
var app = express(),
148-
calledA = 0,
149-
calledB = 0;
150-
151-
function middlewareA(req,res,next){
152-
calledA++;
153-
next();
154-
}
74+
request(app)
75+
.get('/one')
76+
.expect('GET one', done);
15577

156-
function middlewareB(req,res,next){
157-
calledB++;
158-
next();
159-
}
78+
request(app)
79+
.get('/some/two')
80+
.expect('GET two', done);
16081

161-
app.namespace('/user/:id', middlewareA, function(){
162-
app.get('/', function(req,res){
163-
res.send('got Home');
164-
});
82+
request(app)
83+
.get('/foo')
84+
.expect('foo', done);
16585

166-
app.get('/other', function(req,res){
167-
res.send('got Other');
168-
});
86+
request(app)
87+
.get('/foo/bar')
88+
.expect('bar', done);
89+
})
16990

170-
app.namespace('/nest', middlewareB, function(req,res){
171-
app.get('/', function(req,res){
172-
res.send('got Nest');
173-
});
174-
});
175-
});
91+
it('should support middleware', function(done){
92+
var app = express();
17693

177-
var pending = 3;
178-
function finished() {
179-
--pending || function(){
180-
assert.equal(3, calledA);
181-
assert.equal(1, calledB);
182-
done();
183-
}();
94+
function load(req, res, next) {
95+
req.forum = { id: req.params.id };
96+
next();
18497
}
18598

186-
assert.response(app,
187-
{ url: '/user/12' },
188-
{ body: 'got Home' },
189-
finished);
190-
assert.response(app,
191-
{ url: '/user/12/other' },
192-
{ body: 'got Other' },
193-
finished);
194-
assert.response(app,
195-
{ url: '/user/12/nest' },
196-
{ body: 'got Nest' },
197-
finished);
198-
}
199-
};
99+
app.namespace('/forum/:id', load, function(){
100+
app.get('/', function(req, res){
101+
res.send('' + req.forum.id);
102+
});
103+
});
104+
105+
request(app)
106+
.get('/forum/23')
107+
.expect('23', done);
108+
})
109+
})

test/support/pending.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
module.exports = function(n, fn){
3+
return function(err){
4+
if (err) return fn(err);
5+
--n || fn();
6+
}
7+
};

0 commit comments

Comments
 (0)