forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterceptor-test.js
510 lines (497 loc) · 15.7 KB
/
interceptor-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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Copyright 2013-2015 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
(function (buster, define) {
'use strict';
var assert, refute, fail, failOnThrow, undef;
assert = buster.assertions.assert;
refute = buster.assertions.refute;
fail = buster.assertions.fail;
failOnThrow = buster.assertions.failOnThrow;
define('rest-test/interceptor-test', function (require) {
var interceptor, rest, when;
interceptor = require('rest/interceptor');
rest = require('rest');
when = require('when');
function defaultClient(request) {
return { request: request, id: 'default' };
}
function otherClient(request) {
return { request: request, id: 'other' };
}
function errorClient(request) {
return when.reject({ request: request, id: 'error' });
}
function unresponsiveClient(request) {
request.id = 'unresponsive';
return when.defer().promise;
}
buster.testCase('rest/interceptor', {
'should set the originator client on the request for the, but do not overwrite': function () {
var theInterceptor, client;
theInterceptor = interceptor();
client = theInterceptor(defaultClient).wrap(theInterceptor);
return client().then(function (response) {
assert.same('default', response.id);
assert.same(client, response.request.originator);
})['catch'](fail);
},
'should use the client configured into the interceptor by default': function () {
var theInterceptor, client;
theInterceptor = interceptor({
client: defaultClient
});
client = theInterceptor();
return client().then(function (response) {
assert.same('default', response.id);
assert.same(client, response.request.originator);
})['catch'](fail);
},
'should override the client configured into the interceptor by default': function () {
var theInterceptor, client;
theInterceptor = interceptor({
client: defaultClient
});
client = theInterceptor(otherClient);
return client().then(function (response) {
assert.same('other', response.id);
assert.same(client, response.request.originator);
})['catch'](fail);
},
'should intercept the request phase': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
request.phase = 'request';
return request;
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('request', response.request.phase);
})['catch'](fail);
},
'should intercept the request phase and handle a promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
return when().delay(5).then(function () {
request.phase = 'request';
return request;
});
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('default', response.id);
assert.same('request', response.request.phase);
})['catch'](fail);
},
'should intercept the request phase and handle a rejected promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
request.phase = 'request';
return when.reject('rejected request');
}
});
client = theInterceptor(defaultClient);
return client().then(
fail,
failOnThrow(function (response) {
// request never makes it to the root client
refute(response.id);
assert.same('request', response.request.phase);
assert.same('rejected request', response.error);
})
);
},
'should intercept the response phase': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
response.phase = 'response';
return response;
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('response', response.phase);
})['catch'](fail);
},
'should intercept the response phase and handle a promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
return when().delay(5).then(function () {
response.phase = 'response';
return response;
});
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('response', response.phase);
})['catch'](fail);
},
'should intercept the response phase and handle a rejceted promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
response.phase = 'response';
return when.reject(response);
}
});
client = theInterceptor(defaultClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('response', response.phase);
})
);
},
'should intercept the response phase for an error': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
response.phase = 'response';
return response;
}
});
client = theInterceptor(errorClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('response', response.phase);
})
);
},
'should intercept the response phase for an error and handle a promise maintaining the error': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
response.phase = 'response';
return when(response);
}
});
client = theInterceptor(errorClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('response', response.phase);
})
);
},
'should intercept the response phase for an error and handle a rejected promise maintaining the error': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: function (response) {
response.phase = 'response';
return when.reject(response);
}
});
client = theInterceptor(errorClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('response', response.phase);
})
);
},
'should intercept the success phase': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
success: function (response) {
response.phase = 'success';
return response;
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('success', response.phase);
})['catch'](fail);
},
'should intercept the success phase and handle a promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
success: function (response) {
return when().delay(5).then(function () {
response.phase = 'success';
return response;
});
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same('success', response.phase);
})['catch'](fail);
},
'should intercept the success phase and handle a rejceted promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
success: function (response) {
response.phase = 'success';
return when.reject(response);
}
});
client = theInterceptor(defaultClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('success', response.phase);
})
);
},
'should intercept the error phase': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
error: function (response) {
response.phase = 'error';
return response;
}
});
client = theInterceptor(errorClient);
return client().then(function (response) {
assert.same('error', response.phase);
})['catch'](fail);
},
'should intercept the error phase and handle a promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
error: function (response) {
response.phase = 'error';
return when(response);
}
});
client = theInterceptor(errorClient);
return client().then(function (response) {
assert.same('error', response.phase);
})['catch'](fail);
},
'should intercept the error phase and handle a rejceted promise': function () {
var theInterceptor, client;
theInterceptor = interceptor({
response: fail,
error: function (response) {
response.phase = 'error';
return when.reject(response);
}
});
client = theInterceptor(errorClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('error', response.phase);
})
);
},
'should pass interceptor config to handlers': function () {
var theInterceptor, client, theConfig;
theConfig = { foo: 'bar' };
theInterceptor = interceptor({
request: function (request, config) {
request.phase = 'request';
assert.same(theConfig, config);
return request;
},
response: function (response, config) {
response.phase = 'response';
assert.same(theConfig, config);
return response;
}
});
client = theInterceptor(defaultClient, theConfig);
return client().then(function (response) {
assert.same('request', response.request.phase);
assert.same('response', response.phase);
})['catch'](fail);
},
'should share context between handlers that is unique per request': function () {
var theInterceptor, client, count, counted;
count = 0;
counted = [];
theInterceptor = interceptor({
request: function (request) {
count += 1;
if (count % 2) {
this.count = count;
}
return request;
},
response: function (response) {
counted.push(this.count);
return response;
}
});
client = theInterceptor(defaultClient);
return when.all([client(), client(), client()]).then(function () {
assert.same(3, counted.length);
assert(counted.indexOf(1) >= 0);
// if 'this' was shared between requests, we'd have 1 twice and no undef
assert(counted.indexOf(2) === -1);
assert(counted.indexOf(undef) >= 0);
assert(counted.indexOf(3) >= 0);
})['catch'](fail);
},
'should use the client provided by a ComplexRequest': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
return new interceptor.ComplexRequest({
request: request,
client: defaultClient
});
}
});
client = theInterceptor();
return client().then(function (response) {
assert.same('default', response.id);
assert.same(client, response.request.originator);
})['catch'](fail);
},
'should use the repsponse provided by a ComplexRequest': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
return new interceptor.ComplexRequest({
response: { request: request, id: 'complex-response' }
});
}
});
client = theInterceptor();
return client().then(function (response) {
assert.same('complex-response', response.id);
assert.same(client, response.request.originator);
})['catch'](fail);
},
'should cancel requests with the abort trigger provided by a ComplexRequest': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request) {
refute.same('unresponsive', request.id);
return new interceptor.ComplexRequest({
request: request,
abort: when.reject({ request: request, id: 'abort' })
});
}
});
client = theInterceptor(unresponsiveClient);
return client().then(
fail,
failOnThrow(function (response) {
assert.same('abort', response.id);
assert.same('unresponsive', response.request.id);
})
);
},
'should have access to the client for subsequent requests': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request, config, meta) {
request.client = meta.client;
return request;
},
response: function (response, config, meta) {
response.client = meta.client;
return response;
}
});
client = theInterceptor(defaultClient);
return client().then(function (response) {
assert.same(client, response.client);
assert.same(client, response.request.client);
assert.same('default', response.id);
})['catch'](fail);
},
'should have access to the invocation args': function () {
var theInterceptor, client;
theInterceptor = interceptor({
request: function (request, config, meta) {
request['arguments'] = meta['arguments'];
return request;
},
response: function (response, config, meta) {
response['arguments'] = meta['arguments'];
return response;
}
});
client = theInterceptor(defaultClient);
return client('foo', 'bar').then(function (response) {
assert.same('foo', response['arguments'][0]);
assert.same('bar', response['arguments'][1]);
assert.same(response['arguments'], response.request['arguments']);
assert.same('default', response.id);
})['catch'](fail);
},
'should initialize the config object, modifying the provided object': function () {
var theConfig, theInterceptor, client;
theConfig = { foo: 'bar' };
theInterceptor = interceptor({
init: function (config) {
assert.same(theConfig, config);
config.bleep = 'bloop';
return config;
},
request: function (request, config) {
assert.same(theConfig, config);
request.phase = 'request';
return request;
},
response: function (response, config) {
assert.same(theConfig, config);
response.phase = 'response';
return response;
}
});
refute('bleep' in theConfig);
client = theInterceptor(defaultClient, theConfig);
assert.same('bloop', theConfig.bleep);
return client().then(function (response) {
assert.same('request', response.request.phase);
assert.same('response', response.phase);
assert.same('default', response.id);
})['catch'](fail);
},
'should normalize a string to a request object': function () {
var theInterceptor, client;
theInterceptor = interceptor();
client = theInterceptor(defaultClient);
return client('/').then(function (response) {
assert.same('/', response.request.path);
})['catch'](fail);
},
'should have the default client as the parent by default': function () {
var theInterceptor = interceptor();
assert.same(rest, theInterceptor().skip());
},
'should support interceptor wrapping': function () {
var theInterceptor = interceptor();
assert(typeof theInterceptor().wrap === 'function');
},
'should return a ResponsePromise from intercepted clients': function () {
var theInterceptor, client;
theInterceptor = interceptor();
client = theInterceptor(defaultClient);
assert.isFunction(client().entity);
}
});
});
}(
this.buster || require('buster'),
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));