-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
130 lines (112 loc) · 2.85 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var koa = require('koa');
var request = require('supertest');
var route = require('koa-route');
var paramify = require('./');
var assert = require('assert');
describe('paramify', function(){
route = paramify(route);
var param = route.param;
var get = route.get;
var app = koa();
param('user', function*(id, next){
if (id == '1') {
this.user = { name: 'one' };
yield next;
} else {
this.status = 404;
}
});
param('user', function*(id, next){
this.pre = 'lol: ';
yield next;
});
app.use(get('/:user', function*(){
this.body = this.pre + this.user.name;
}));
it('should work without params', function(done){
var app = koa();
var _ = paramify(route);
app.use(_.get('/', function*(user){
this.body = 'home';
}));
request(app.listen())
.get('/')
.expect('home', done);
});
it('should work without param fns', function(done){
var app = koa();
var _ = paramify(route);
app.use(_.get('/:user', function*(user){
this.body = user;
}));
request(app.listen())
.get('/julian')
.expect('julian', done);
});
it('should work with param fns', function(done){
var app = koa();
var _ = paramify(route);
_.param('user', function*(id, next){
this.user = 'julian';
yield next;
});
app.use(_.get('/:user', function*(){
this.body = this.user;
}));
request(app.listen())
.get('/julian')
.expect('julian', done);
});
it('should work with multiple params', function(done){
var app = koa();
var _ = paramify(route);
_.param('user', function*(id, next){
this.user = 'julian';
yield next;
});
_.param('repo', function*(id, next){
this.repo = 'repo';
yield next;
});
app.use(_.get('/:user/:repo', function*(){
this.body = this.user + ': ' + this.repo;
}));
request(app.listen())
.get('/julian/repo')
.expect('julian: repo', done);
});
it('should abort inside param fns', function(done){
var app = koa();
var _ = paramify(route);
_.param('user', function*(id, next){
this.status = 404;
});
app.use(_.get('/:user', function*(){
this.body = this.user;
}));
request(app.listen())
.get('/julian')
.expect(404, done);
});
it('should accept middleware', function(done){
var app = koa();
var _ = paramify(route);
_.param('user', function*(id, next){
this.user = 'julian';
yield next;
});
_.param('user', function*(next){
this.user = this.user.toUpperCase();
yield next;
});
app.use(_.get('/:user', function*(){
this.body = this.user;
}));
request(app.listen())
.get('/julian')
.expect('JULIAN', done);
});
it('should clone', function(){
assert(route != paramify(route));
});
})