-
Notifications
You must be signed in to change notification settings - Fork 1
/
restful-express-es6.spec.js
137 lines (103 loc) · 3.23 KB
/
restful-express-es6.spec.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
131
132
133
134
135
136
137
import chai from 'chai';
import {get, post, put, endPoint, process, runFunctionBefore} from './restful-express';
const expect = chai.expect;
class Router {
constructor() {
this.routes = {};
}
get(url, ...methods) {
this.routes[`GET:${url}`] = methods;
}
post(url, ...methods) {
this.routes[`POST:${url}`] = methods;
}
put(url, ...methods) {
this.routes[`PUT:${url}`] = methods;
}
}
describe('HttpMethods', function() {
var router;
beforeEach(() => {
router = new Router();
});
it('should define a GET endpoint', function() {
class Test {
@get('/test')
method() {
return true;
}
}
process(router, Test);
expect(router.routes['GET:/test']).to.exist;
expect(router.routes['GET:/test'][0]()).to.be.true;
});
it('should define a POST endpoint', function() {
class Test {
@post('/test')
method() {
return true;
}
}
process(router, Test);
expect(router.routes['POST:/test']).to.exist;
expect(router.routes['POST:/test'][0]()).to.be.true;
});
it('should define a PUT endpoint', function() {
class Test {
@put('/test')
method() {
return true;
}
}
process(router, Test);
expect(router.routes['PUT:/test']).to.exist;
expect(router.routes['PUT:/test'][0]()).to.be.true;
});
});
describe('Middleware', function() {
var router;
beforeEach(() => {
router = new Router();
});
it('should define functions to run before the method endpoint', function() {
function first(config) {
runFunctionBefore(config, (req, res) => 'first');
}
function second(config) {
runFunctionBefore(config, (req, res) => 'second');
}
class Test {
@get('/test', first, second)
method() {
return 'original';
}
}
process(router, Test);
expect(router.routes['GET:/test'][0]()).to.equal('first');
expect(router.routes['GET:/test'][1]()).to.equal('second');
expect(router.routes['GET:/test'][2]()).to.equal('original');
});
it('should support both functions on class and methods', function() {
function first(config) {
runFunctionBefore(config, (req, res) => 'first');
}
function second(config) {
runFunctionBefore(config, (req, res) => 'second');
}
function third(config) {
runFunctionBefore(config, (req, res) => 'third');
}
@endPoint(first, second)
class Test {
@get('/test', third)
method() {
return 'original';
}
}
process(router, Test);
expect(router.routes['GET:/test'][0]()).to.equal('first');
expect(router.routes['GET:/test'][1]()).to.equal('second');
expect(router.routes['GET:/test'][2]()).to.equal('third');
expect(router.routes['GET:/test'][3]()).to.equal('original');
});
});