-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
73 lines (60 loc) · 2.69 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var assert = require('assert');
var express = require('express');
var app = express();
var pathFinder = require('./index.js');
describe('path-finder', function(){
before(function(){
pathFinder.extend(app);
});
it('should include deprecated methods in express app', function(){
assert(app.addPath);
assert(app.p);
});
it('should add a view helper', function(){
assert(app.locals.p);
});
it('should proxy standard routing methods', function(){
app.get('/hello', function(req, res){ return res.end('world'); });
assert.equal('/hello', app.routes.get[0].path);
});
it('should add named routes when a name is passed', function(){
app.get('/user', 'user', function(req, res){ return res.end('david'); });
assert.equal('/user', pathFinder.path('user'));
});
it('should work with app.all', function(){
app.all('/pages', 'pages', function(req, res){ return res.end('pages'); });
assert.equal('/pages', pathFinder.path('pages'));
assert.equal('/pages', app.routes.unsubscribe[0].path);
})
it('should accept parameter keys in routes', function(){
app.post('/users/:userId/comments/:commentId/edit', 'edit_comment', function(req, res){
return res.end('edit');
});
assert.equal('/users/10/comments/11/edit', pathFinder.path('edit_comment', {userId: 10, commentId: 11}));
});
it('should pass additional keys into query string', function(){
app.put('/search', 'search', function(req, res){ return res.end('search'); });
assert.equal('/search?q=david%20cornu', pathFinder.path('search', {q: 'david cornu'}));
assert.equal('/search?q=poodles&p=1', pathFinder.path('search', {q: 'poodles', p: 1}));
});
it('should accept both param and query string options', function(){
app.delete('/posts/:id', 'post', function(req, res){ return res.end('post'); });
assert.equal('/posts/21?p=2', pathFinder.path('post', {id: 21, p: 2}));
});
it('should return identical results using the view helper', function(){
assert.equal('/posts/21?p=2', app.locals.p('post', {id: 21, p: 2}));
});
it('should allow ad-hoc paths to be added', function(){
pathFinder.addPath('directions', '/directions/:from/:to');
assert.equal('/directions/paris/berlin', pathFinder.path('directions', {from: 'paris', to: 'berlin'}));
});
it('should throw for undefined paths', function(){
assert.throws(function(){ pathFinder.path('booyah'); });
});
it('should not overwrite any of express\' internal methods', function(){
var cleanApp = express();
assert(!cleanApp._paths, 'does not overwrite app._paths');
assert(!cleanApp.addPath, 'does not overwrite app.addPath');
assert(!cleanApp.p, 'does not overwrite app.p');
});
});