-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpromise.js
286 lines (282 loc) · 10.9 KB
/
promise.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
var cfg = require('../dummyserver-config');
var httpinvoke = require('../httpinvoke-node');
function isValidReason(reason) {
'use strict';
return typeof reason === 'object' && reason !== null && reason instanceof Error;
}
function isValidResponse(response, crossDomain) {
'use strict';
return typeof response === 'object' && typeof response.body === 'string' && (typeof response.statusCode === 'number' || !(!crossDomain || httpinvoke.corsStatus)) && typeof response.headers === 'object';
}
function isValidProgress(progress, crossDomain) {
'use strict';
if(typeof progress !== 'object' || typeof progress.type !== 'string') {
return false;
}
if(progress.type === 'upload') {
if(typeof progress.current !== 'number') {
return false;
}
if(typeof progress.total !== 'number') {
return false;
}
if(progress.current > progress.total) {
return false;
}
} else if(progress.type === 'download') {
if(typeof progress.current !== 'number') {
return false;
}
if(typeof progress.total === 'number') {
if(progress.current > progress.total) {
return false;
}
}
} else if(progress.type === 'headers') {
if((!crossDomain || httpinvoke.corsStatus) && typeof progress.statusCode !== 'number') {
return false;
}
if(typeof progress.headers !== 'object') {
return false;
}
if(typeof progress.headers['content-type'] !== 'string') {
return false;
}
} else if(progress.type === 'body') {
if(typeof progress.statusCode !== 'number') {
return false;
}
if(typeof progress.headers !== 'object') {
return false;
}
if(typeof progress.headers['content-type'] !== 'string') {
return false;
}
if(typeof progress.body !== 'string') {
return false;
}
} else {
return false;
}
return true;
}
describe('promise', function() {
'use strict';
this.timeout(10000);
cfg.eachBase(function(postfix, url, crossDomain) {
it('supports Promises/A' + postfix, function(done) {
var promise = httpinvoke(url).then(function(response) {
if(!done) {
return;
}
if(isValidResponse(response, crossDomain)) {
done();
} else {
done(new Error('invalid response ' + JSON.stringify(response)));
}
}, function(error) {
if(!done) {
return;
}
done(error);
}, function(progress) {
if(!done) {
return;
}
if(!isValidProgress(progress, crossDomain)) {
done(new Error('invalid progress ' + JSON.stringify(progress)));
done = null;
}
});
if(typeof promise.then !== 'function') {
done(new Error('then() did not return a promise'));
done = null;
}
});
it('supports skipping onresolved' + postfix, function(done) {
httpinvoke(url, {
finished: done
}).then(null, function() {
}, function() {
});
});
it('supports skipping onrejected' + postfix, function(done) {
httpinvoke('foo:bar', {
finished: function(err) {
if(!err) {
return done(new Error('expected error'));
}
done();
}
}).then(function() {
}, null, function() {
});
});
it('supports Promises/A+ requirement, that callbacks must be called as functions' + postfix, function(done) {
httpinvoke(url).then(function() {
if(!done) {
return;
}
if(this !== global && this !== null) {
done(new Error('onResolved was not called as a function'));
done = null;
return;
}
httpinvoke(url, 'ERROR').then(function() {
}, function() {
if(!done) {
return;
}
if(this !== global && this !== null) {
done(new Error('onRejected was not called as a function'));
done = null;
return;
}
done();
});
}, function() {
}, function() {
if(!done) {
return;
}
if(this !== global && this !== null) {
done(new Error('onProgress was not called as a function'));
done = null;
}
});
});
it('supports Promises/A+ requirement, that If onResolved is not a function, it must be ignored' + postfix, function(done) {
httpinvoke(url, 'ERROR').then(null, function(err) {
if(!isValidReason(err)) {
return done(new Error('invalid reason'));
}
done();
});
});
it('supports Promises/A+ requirement, that If onRejected is not a function, it must be ignored' + postfix, function(done) {
httpinvoke(url).then(function(response) {
if(!isValidResponse(response, crossDomain)) {
done(new Error('invalid response ' + JSON.stringify(response)));
}
done();
}, null);
});
it('supports Promises/A+ requirement, that If onResolved returns a value x, returned promise must be resolved using x' + postfix, function(done) {
var value = {};
httpinvoke(url).then(function() {
return value;
}).then(function(v) {
if(value !== v) {
done(new Error('was not resolved with same value'));
}
done();
}, function() {
done(new Error('was rejected'));
});
});
it('supports Promises/A+ requirement, that If onResolved throws an exception e, returned promise must be rejected with e as the reason' + postfix, function(done) {
var e = {};
httpinvoke(url).then(function() {
throw e;
}).then(function() {
done(new Error('was not rejected'));
}, function(err) {
if(err !== e) {
done(new Error('was not rejected with same reason'));
}
done();
});
});
it('supports Promises/A+ requirement, that If onRejected returns a value x, returned promise must be resolved using x' + postfix, function(done) {
var value = {};
httpinvoke(url, 'ERROR').then(null, function() {
return value;
}).then(function(v) {
if(value !== v) {
done(new Error('was not resolved with the same value'));
}
done();
}, function() {
done(new Error('was rejected'));
});
});
it('supports Promises/A+ requirement, that If onRejected throws an exception e, returned promise must be rejected with e as the reason' + postfix, function(done) {
var e = {};
httpinvoke(url, 'ERROR').then(null, function() {
throw e;
}).then(function() {
done(new Error('was not rejected'));
}, function(err) {
if(err !== e) {
done(new Error('was not rejected with same reason'));
}
done();
});
});
it('supports a requirement, that If onNotify throws an exception e, returned promise must be rejected with e as the reason' + postfix, function(done) {
var e = {};
httpinvoke(url).then(null, null, function() {
throw e;
}).then(function() {
done(new Error('was not rejected'));
}, function(err) {
if(err !== e) {
done(new Error('was not rejected with same reason'));
}
done();
});
});
it('supports a requirement, that If onResolved is not a function and the original promise is resolved, the returned promise must be resolved with the same value' + postfix, function(done) {
var output;
httpinvoke(url, function(err, _output) {
output = _output;
}).then().then(function(res) {
if(output !== res.body) {
return done(new Error('the passed value is not the same'));
}
done();
});
});
it('supports a requirement, that If onRejected is not a function and the original promise is rejected, the returned promise must be rejected with the same reason' + postfix, function(done) {
var err;
httpinvoke(url, 'ERROR', function(_err) {
err = _err;
}).then().then(null, function(_err) {
if(err !== _err) {
return done(new Error('the passed reason is not the same'));
}
done();
});
});
['chunked', 'joined'].forEach(function(partial) {
it('supports .partial in "download" progress event, when option "partialOutputMode" is set to "' + partial + '"' + postfix, function(done) {
httpinvoke(url, {
partialOutputMode: partial
}).then(function() {
if(!done) {
return;
}
done();
done = null;
}, function(err) {
if(!done) {
return;
}
done(err);
done = null;
}, function(progress) {
if(!isValidProgress(progress, crossDomain)) {
done(new Error('invalid progress'));
done = null;
return;
}
if(progress.type === 'download' && typeof progress.partial !== 'string' && typeof progress.partial !== 'object') {
done(new Error('progress.partial is neither string, nor object'));
done = null;
return;
}
});
});
});
});
});