1 |
|
2 | describe('ware', function () {
|
3 |
|
4 | var assert = require('assert');
|
5 | var noop = function(){};
|
6 | var ware = require('..');
|
7 |
|
8 | describe('#use', function () {
|
9 | it('should be chainable', function () {
|
10 | var w = ware();
|
11 | assert(w.use(noop) == w);
|
12 | });
|
13 |
|
14 | it('should add a middleware to fns', function () {
|
15 | var w = ware().use(noop);
|
16 | assert(1 == w.fns.length);
|
17 | });
|
18 |
|
19 | it('should accept an array of middleware', function () {
|
20 | var w = ware().use([noop, noop]);
|
21 | assert(2 == w.fns.length);
|
22 | });
|
23 |
|
24 | it('should accept a Ware instance', function () {
|
25 | var o = ware().use(noop).use(noop);
|
26 | var w = ware().use(o);
|
27 | assert(2 == w.fns.length);
|
28 | });
|
29 |
|
30 | it('should accept an array of Ware instances', function() {
|
31 | var a = ware().use(noop).use(noop);
|
32 | var b = ware().use(noop).use(noop);
|
33 | var w = ware([a, b]);
|
34 | assert(4 == w.fns.length);
|
35 | })
|
36 |
|
37 | it('should accept middleware on construct', function () {
|
38 | var w = ware(noop);
|
39 | assert(1 == w.fns.length);
|
40 | });
|
41 | });
|
42 |
|
43 | describe('#run', function () {
|
44 | describe('async', function() {
|
45 | it('should receive an error', function (done) {
|
46 | var error = new Error();
|
47 | ware()
|
48 | .use(function (next) { next(error); })
|
49 | .run(function (err) {
|
50 | assert(err == error);
|
51 | done();
|
52 | });
|
53 | });
|
54 |
|
55 | it('should receive initial arguments', function (done) {
|
56 | ware()
|
57 | .use(function (req, res, next) { next(); })
|
58 | .run('req', 'res', function (err, req, res) {
|
59 | assert(!err);
|
60 | assert('req' == req);
|
61 | assert('res' == res);
|
62 | done();
|
63 | });
|
64 | });
|
65 |
|
66 | it('should take any number of arguments', function (done) {
|
67 | ware()
|
68 | .use(function (a, b, c, next) { next(); })
|
69 | .run('a', 'b', 'c', function (err, a, b, c) {
|
70 | assert(!err);
|
71 | assert('a' == a);
|
72 | assert('b' == b);
|
73 | assert('c' == c);
|
74 | done();
|
75 | });
|
76 | });
|
77 |
|
78 | it('should let middleware manipulate the same input objects', function (done) {
|
79 | ware()
|
80 | .use(function (obj, next) {
|
81 | obj.value = obj.value * 2;
|
82 | next();
|
83 | })
|
84 | .use(function (obj, next) {
|
85 | obj.value = obj.value.toString();
|
86 | next();
|
87 | })
|
88 | .run({ value: 21 }, function (err, obj) {
|
89 | assert('42' == obj.value);
|
90 | done();
|
91 | });
|
92 | });
|
93 |
|
94 | it('should jump to done on error', function (done) {
|
95 | var errors = 0;
|
96 | ware()
|
97 | .use(function (next) { next(new Error()); })
|
98 | .use(function (next) { errors++; next(err); })
|
99 | .use(function (next) { errors++; next(err); })
|
100 | .run(function (err) {
|
101 | assert(err);
|
102 | assert(0 == errors);
|
103 | done();
|
104 | });
|
105 | });
|
106 |
|
107 | it('should not require a callback', function (done) {
|
108 | ware()
|
109 | .use(function (obj, next) { assert(obj); next(); })
|
110 | .use(function (obj, next) { done(); })
|
111 | .run('obj');
|
112 | });
|
113 | });
|
114 |
|
115 | describe('sync', function() {
|
116 | it('should receive an error', function (done) {
|
117 | var error = new Error();
|
118 | ware()
|
119 | .use(function () { return error; })
|
120 | .run(function (err) {
|
121 | assert(err == error);
|
122 | done();
|
123 | });
|
124 | });
|
125 |
|
126 | it('should catch an error', function (done) {
|
127 | var error = new Error();
|
128 | ware()
|
129 | .use(function () { throw error; })
|
130 | .run(function (err) {
|
131 | assert(err === error);
|
132 | done();
|
133 | });
|
134 | });
|
135 |
|
136 | it('should receive initial arguments', function (done) {
|
137 | ware()
|
138 | .use(function (req, res) { return; })
|
139 | .run('req', 'res', function (err, req, res) {
|
140 | assert(!err);
|
141 | assert('req' == req);
|
142 | assert('res' == res);
|
143 | done();
|
144 | });
|
145 | });
|
146 |
|
147 | it('should take any number of arguments', function (done) {
|
148 | ware()
|
149 | .use(function (a, b, c) { })
|
150 | .run('a', 'b', 'c', function (err, a, b, c) {
|
151 | assert(!err);
|
152 | assert('a' == a);
|
153 | assert('b' == b);
|
154 | assert('c' == c);
|
155 | done();
|
156 | });
|
157 | });
|
158 |
|
159 | it('should let middleware manipulate the same input objects', function (done) {
|
160 | ware()
|
161 | .use(function (obj) {
|
162 | obj.value = obj.value * 2;
|
163 | })
|
164 | .use(function (obj) {
|
165 | obj.value = obj.value.toString();
|
166 | })
|
167 | .run({ value: 21 }, function (err, obj) {
|
168 | assert(!err);
|
169 | assert('42' == obj.value);
|
170 | done();
|
171 | });
|
172 | });
|
173 |
|
174 |
|
175 | it('should skip middleware on error', function (done) {
|
176 | var errors = 0;
|
177 | ware()
|
178 | .use(function () { return new Error(); })
|
179 | .use(function (next) { errors++; next(err); })
|
180 | .use(function (next) { errors++; next(err); })
|
181 | .run(function (err) {
|
182 | assert(err);
|
183 | assert(0 == errors);
|
184 | done();
|
185 | });
|
186 | });
|
187 |
|
188 | it('should not require a callback', function (done) {
|
189 | ware()
|
190 | .use(function (obj) { assert(obj); })
|
191 | .use(function (obj) { done(); })
|
192 | .run('obj');
|
193 | });
|
194 |
|
195 | it('should support promises', function (done) {
|
196 | ware()
|
197 | .use(function () {
|
198 | return {
|
199 | then: function (resolve) { resolve(10); }
|
200 | };
|
201 | })
|
202 | .run(done);
|
203 | });
|
204 |
|
205 | it('should skip middleware on promise error', function (done) {
|
206 | var errors = 0;
|
207 | ware()
|
208 | .use(function () {
|
209 | return {
|
210 | then: function (resolve, reject) { reject(new Error()); }
|
211 | };
|
212 | })
|
213 | .use(function (next) { errors++; next(err); })
|
214 | .use(function (next) { errors++; next(err); })
|
215 | .run(function (err) {
|
216 | assert(err);
|
217 | assert(0 == errors);
|
218 | done();
|
219 | });
|
220 | });
|
221 | })
|
222 |
|
223 | describe('generator', function() {
|
224 | it('should receive an error', function (done) {
|
225 | var error = new Error();
|
226 | ware()
|
227 | .use(function *() { throw error; })
|
228 | .run(function (err) {
|
229 | assert(err == error);
|
230 | done();
|
231 | });
|
232 | });
|
233 |
|
234 | it('should receive initial arguments', function (done) {
|
235 | ware()
|
236 | .use(function *(req, res) { })
|
237 | .run('req', 'res', function (err, req, res) {
|
238 | assert(!err);
|
239 | assert('req' == req);
|
240 | assert('res' == res);
|
241 | done();
|
242 | });
|
243 | });
|
244 |
|
245 | it('should take any number of arguments', function (done) {
|
246 | ware()
|
247 | .use(function *(a, b, c) { })
|
248 | .run('a', 'b', 'c', function (err, a, b, c) {
|
249 | assert(!err);
|
250 | assert('a' == a);
|
251 | assert('b' == b);
|
252 | assert('c' == c);
|
253 | done();
|
254 | });
|
255 | });
|
256 |
|
257 | it('should let middleware manipulate the same input objects', function (done) {
|
258 | ware()
|
259 | .use(function *(obj) {
|
260 | obj.value = obj.value * 2;
|
261 | })
|
262 | .use(function *(obj) {
|
263 | obj.value = obj.value.toString();
|
264 | })
|
265 | .run({ value: 21 }, function (err, obj) {
|
266 | assert('42' == obj.value);
|
267 | done();
|
268 | });
|
269 | });
|
270 |
|
271 | it('should wait for the gen to finish', function(done) {
|
272 | ware()
|
273 | .use(function *(a, b, c) {
|
274 | yield wait(100);
|
275 | })
|
276 | .run('a', 'b', 'c', function (err, a, b, c) {
|
277 | assert(!err);
|
278 | assert('a' == a);
|
279 | assert('b' == b);
|
280 | assert('c' == c);
|
281 | done();
|
282 | });
|
283 | })
|
284 |
|
285 | it('should jump to done on error', function (done) {
|
286 | var errors = 0;
|
287 | ware()
|
288 | .use(function *() { throw new Error(); })
|
289 | .use(function *() { errors++; })
|
290 | .use(function *() { errors++; })
|
291 | .run(function (err) {
|
292 | assert(err);
|
293 | assert(0 == errors);
|
294 | done();
|
295 | });
|
296 | });
|
297 |
|
298 | it('should not require a callback', function (done) {
|
299 | ware()
|
300 | .use(function *(obj) { assert(obj); })
|
301 | .use(function *(obj) { done(); })
|
302 | .run('obj');
|
303 | });
|
304 | });
|
305 | });
|
306 | });
|
307 |
|
308 | function wait(ms) {
|
309 | return function(fn) {
|
310 | setTimeout(fn, ms);
|
311 | }
|
312 | }
|