OLD | NEW |
1 part of angular.mock; | 1 library angular.mock.http_backend; |
2 | 2 |
| 3 import 'dart:async' as dart_async; |
| 4 import 'dart:convert' show JSON; |
| 5 import 'dart:html'; |
| 6 |
| 7 import 'package:angular/angular.dart'; |
| 8 import 'package:angular/utils.dart' as utils; |
| 9 |
| 10 |
3 class _MockXhr { | 11 class _MockXhr { |
4 var $$method, $$url, $$async, $$reqHeaders, $$respHeaders; | 12 var method, url, async, reqHeaders, respHeaders; |
5 | 13 |
6 void open(method, url, async) { | 14 void open(method, url, async) { |
7 $$method = method; | 15 this.method = method; |
8 $$url = url; | 16 this.url = url; |
9 $$async = async; | 17 this.async = async; |
10 $$reqHeaders = {}; | 18 reqHeaders = {}; |
11 $$respHeaders = {}; | 19 respHeaders = {}; |
12 } | 20 } |
13 | 21 |
14 var $$data; | 22 var data; |
15 | 23 |
16 void send(data) { | 24 void send(data) { |
17 $$data = data; | 25 data = data; |
18 } | 26 } |
19 | 27 |
20 void setRequestHeader(key, value) { | 28 void setRequestHeader(key, value) { |
21 $$reqHeaders[key] = value; | 29 reqHeaders[key] = value; |
22 } | 30 } |
23 | 31 |
24 String getResponseHeader(name) { | 32 String getResponseHeader(name) { |
25 // the lookup must be case insensitive, that's why we try two quick | 33 // the lookup must be case insensitive, that's why we try two quick |
26 // lookups and full scan at last | 34 // lookups and full scan at last |
27 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; | 35 if (respHeaders.containsKey(name)) return respHeaders[name]; |
28 | 36 |
29 name = name.toLowerCase(); | 37 name = name.toLowerCase(); |
30 if ($$respHeaders.containsKey(name)) return $$respHeaders[name]; | 38 if (respHeaders.containsKey(name)) return respHeaders[name]; |
31 | 39 |
32 String header = null; | 40 String header = null; |
33 $$respHeaders.forEach((headerName, headerVal) { | 41 respHeaders.forEach((headerName, headerVal) { |
34 if (header != null) return; | 42 if (header != null) return; |
35 if (headerName.toLowerCase()) header = headerVal; | 43 if (headerName.toLowerCase()) header = headerVal; |
36 }); | 44 }); |
37 return header; | 45 return header; |
38 } | 46 } |
39 | 47 |
40 getAllResponseHeaders() { | 48 getAllResponseHeaders() { |
41 if ($$respHeaders == null) return ''; | 49 if (respHeaders == null) return ''; |
42 | 50 |
43 var lines = []; | 51 var lines = []; |
44 | 52 |
45 $$respHeaders.forEach((key, value) { | 53 respHeaders.forEach((key, value) { |
46 lines.add("$key: $value"); | 54 lines.add("$key: $value"); |
47 }); | 55 }); |
48 return lines.join('\n'); | 56 return lines.join('\n'); |
49 } | 57 } |
50 | 58 |
51 // noop | 59 // noop |
52 abort() {} | 60 abort() {} |
53 } | 61 } |
54 | 62 |
55 /** | 63 /** |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 170 |
163 var prettyPrint = (data) { | 171 var prettyPrint = (data) { |
164 return (data is String || data is Function || data is RegExp) | 172 return (data is String || data is Function || data is RegExp) |
165 ? data | 173 ? data |
166 : JSON.encode(data); | 174 : JSON.encode(data); |
167 }; | 175 }; |
168 | 176 |
169 var wrapResponse = (wrapped) { | 177 var wrapResponse = (wrapped) { |
170 var handleResponse = () { | 178 var handleResponse = () { |
171 var response = wrapped.response(method, url, data, headers); | 179 var response = wrapped.response(method, url, data, headers); |
172 xhr.$$respHeaders = response[2]; | 180 xhr.respHeaders = response[2]; |
173 utils.relaxFnApply(callback, [response[0], response[1], | 181 utils.relaxFnApply(callback, [response[0], response[1], |
174 xhr.getAllResponseHeaders()]); | 182 xhr.getAllResponseHeaders()]); |
175 }; | 183 }; |
176 | 184 |
177 var handleTimeout = () { | 185 var handleTimeout = () { |
178 for (var i = 0; i < responses.length; i++) { | 186 for (var i = 0; i < responses.length; i++) { |
179 if (identical(responses[i], handleResponse)) { | 187 if (identical(responses[i], handleResponse)) { |
180 responses.removeAt(i); | 188 responses.removeAt(i); |
181 callback(-1, null, ''); | 189 callback(-1, null, ''); |
182 break; | 190 break; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 chain = new _Chain(respond: (status, data, headers) { | 253 chain = new _Chain(respond: (status, data, headers) { |
246 definition.response = _createResponse(status, data, headers); | 254 definition.response = _createResponse(status, data, headers); |
247 }); | 255 }); |
248 | 256 |
249 definitions.add(definition); | 257 definitions.add(definition); |
250 return chain; | 258 return chain; |
251 } | 259 } |
252 | 260 |
253 /** | 261 /** |
254 * @ngdoc method | 262 * @ngdoc method |
255 * @name ngMock.$httpBackend#whenGET | 263 * @name ngMock.httpBackend#whenGET |
256 * @methodOf ngMock.$httpBackend | 264 * @methodOf ngMock.httpBackend |
257 * @description | 265 * @description |
258 * Creates a new backend definition for GET requests. For more info see `when(
)`. | 266 * Creates a new backend definition for GET requests. For more info see `when(
)`. |
259 * | 267 * |
260 * @param {string|RegExp} url HTTP url. | 268 * @param {string|RegExp} url HTTP url. |
261 * @param {(Object|function(Object))=} headers HTTP headers. | 269 * @param {(Object|function(Object))=} headers HTTP headers. |
262 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 270 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
263 * request is handled. | 271 * request is handled. |
264 */ | 272 */ |
265 _Chain whenGET(url, [headers]) => when('GET', url, null, headers); | 273 _Chain whenGET(url, [headers]) => when('GET', url, null, headers); |
266 | 274 |
267 /** | 275 /** |
268 * @ngdoc method | 276 * @ngdoc method |
269 * @name ngMock.$httpBackend#whenDELETE | 277 * @name ngMock.httpBackend#whenDELETE |
270 * @methodOf ngMock.$httpBackend | 278 * @methodOf ngMock.httpBackend |
271 * @description | 279 * @description |
272 * Creates a new backend definition for DELETE requests. For more info see `wh
en()`. | 280 * Creates a new backend definition for DELETE requests. For more info see `wh
en()`. |
273 * | 281 * |
274 * @param {string|RegExp} url HTTP url. | 282 * @param {string|RegExp} url HTTP url. |
275 * @param {(Object|function(Object))=} headers HTTP headers. | 283 * @param {(Object|function(Object))=} headers HTTP headers. |
276 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 284 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
277 * request is handled. | 285 * request is handled. |
278 */ | 286 */ |
279 _Chain whenDELETE(url, [headers]) => when('DELETE', url, null, headers); | 287 _Chain whenDELETE(url, [headers]) => when('DELETE', url, null, headers); |
280 | 288 |
281 /** | 289 /** |
282 * @ngdoc method | 290 * @ngdoc method |
283 * @name ngMock.$httpBackend#whenJSONP | 291 * @name ngMock.httpBackend#whenJSONP |
284 * @methodOf ngMock.$httpBackend | 292 * @methodOf ngMock.httpBackend |
285 * @description | 293 * @description |
286 * Creates a new backend definition for JSONP requests. For more info see `whe
n()`. | 294 * Creates a new backend definition for JSONP requests. For more info see `whe
n()`. |
287 * | 295 * |
288 * @param {string|RegExp} url HTTP url. | 296 * @param {string|RegExp} url HTTP url. |
289 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 297 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
290 * request is handled. | 298 * request is handled. |
291 */ | 299 */ |
292 _Chain whenJSONP(url, [headers]) => when('JSONP', url, null, headers); | 300 _Chain whenJSONP(url, [headers]) => when('JSONP', url, null, headers); |
293 | 301 |
294 /** | 302 /** |
295 * @ngdoc method | 303 * @ngdoc method |
296 * @name ngMock.$httpBackend#whenPUT | 304 * @name ngMock.httpBackend#whenPUT |
297 * @methodOf ngMock.$httpBackend | 305 * @methodOf ngMock.httpBackend |
298 * @description | 306 * @description |
299 * Creates a new backend definition for PUT requests. For more info see `when
()`. | 307 * Creates a new backend definition for PUT requests. For more info see `when
()`. |
300 * | 308 * |
301 * @param {string|RegExp} url HTTP url. | 309 * @param {string|RegExp} url HTTP url. |
302 * @param {(string|RegExp)=} data HTTP request body. | 310 * @param {(string|RegExp)=} data HTTP request body. |
303 * @param {(Object|function(Object))=} headers HTTP headers. | 311 * @param {(Object|function(Object))=} headers HTTP headers. |
304 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 312 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
305 * request is handled. | 313 * request is handled. |
306 */ | 314 */ |
307 _Chain whenPUT(url, [data, headers]) => when('PUT', url, data, headers); | 315 _Chain whenPUT(url, [data, headers]) => when('PUT', url, data, headers); |
308 | 316 |
309 /** | 317 /** |
310 * @ngdoc method | 318 * @ngdoc method |
311 * @name ngMock.$httpBackend#whenPOST | 319 * @name ngMock.httpBackend#whenPOST |
312 * @methodOf ngMock.$httpBackend | 320 * @methodOf ngMock.httpBackend |
313 * @description | 321 * @description |
314 * Creates a new backend definition for POST requests. For more info see `when
()`. | 322 * Creates a new backend definition for POST requests. For more info see `when
()`. |
315 * | 323 * |
316 * @param {string|RegExp} url HTTP url. | 324 * @param {string|RegExp} url HTTP url. |
317 * @param {(string|RegExp)=} data HTTP request body. | 325 * @param {(string|RegExp)=} data HTTP request body. |
318 * @param {(Object|function(Object))=} headers HTTP headers. | 326 * @param {(Object|function(Object))=} headers HTTP headers. |
319 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 327 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
320 * request is handled. | 328 * request is handled. |
321 */ | 329 */ |
322 _Chain whenPOST(url, [data, headers]) => when('POST', url, data, headers); | 330 _Chain whenPOST(url, [data, headers]) => when('POST', url, data, headers); |
323 | 331 |
324 _Chain whenPATCH(url, [data, headers]) => when('PATCH', url, data, headers); | 332 _Chain whenPATCH(url, [data, headers]) => when('PATCH', url, data, headers); |
325 | 333 |
326 /** | 334 /** |
327 * @ngdoc method | 335 * @ngdoc method |
328 * @name ngMock.$httpBackend#whenHEAD | 336 * @name ngMock.httpBackend#whenHEAD |
329 * @methodOf ngMock.$httpBackend | 337 * @methodOf ngMock.httpBackend |
330 * @description | 338 * @description |
331 * Creates a new backend definition for HEAD requests. For more info see `when
()`. | 339 * Creates a new backend definition for HEAD requests. For more info see `when
()`. |
332 * | 340 * |
333 * @param {string|RegExp} url HTTP url. | 341 * @param {string|RegExp} url HTTP url. |
334 * @param {(Object|function(Object))=} headers HTTP headers. | 342 * @param {(Object|function(Object))=} headers HTTP headers. |
335 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 343 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
336 * request is handled. | 344 * request is handled. |
337 */ | 345 */ |
338 | 346 |
339 /** | 347 /** |
340 * @ngdoc method | 348 * @ngdoc method |
341 * @name ngMock.$httpBackend#expect | 349 * @name ngMock.httpBackend#expect |
342 * @methodOf ngMock.$httpBackend | 350 * @methodOf ngMock.httpBackend |
343 * @description | 351 * @description |
344 * Creates a new request expectation. | 352 * Creates a new request expectation. |
345 * | 353 * |
346 * @param {string} method HTTP method. | 354 * @param {string} method HTTP method. |
347 * @param {string|RegExp} url HTTP url. | 355 * @param {string|RegExp} url HTTP url. |
348 * @param {(string|RegExp)=} data HTTP request body. | 356 * @param {(string|RegExp)=} data HTTP request body. |
349 * @param {(Object|function(Object))=} headers HTTP headers or function that r
eceives http header | 357 * @param {(Object|function(Object))=} headers HTTP headers or function that r
eceives http header |
350 * object and returns true if the headers match the current expectation. | 358 * object and returns true if the headers match the current expectation. |
351 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 359 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
352 * request is handled. | 360 * request is handled. |
353 * | 361 * |
354 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` | 362 * - respond – `{function([status,] data[, headers])|function(function(method
, url, data, headers)}` |
355 * – The respond method takes a set of static data to be returned or a func
tion that can return | 363 * – The respond method takes a set of static data to be returned or a func
tion that can return |
356 * an array containing response status (number), response data (string) and
response headers | 364 * an array containing response status (number), response data (string) and
response headers |
357 * (Object). | 365 * (Object). |
358 */ | 366 */ |
359 _Chain expect(method, [url, data, headers]) { | 367 _Chain expect(method, [url, data, headers]) { |
360 var expectation = new MockHttpExpectation(method, url, data, headers); | 368 var expectation = new MockHttpExpectation(method, url, data, headers); |
361 expectations.add(expectation); | 369 expectations.add(expectation); |
362 return new _Chain(respond: (status, data, headers) { | 370 return new _Chain(respond: (status, data, headers) { |
363 expectation.response = _createResponse(status, data, headers); | 371 expectation.response = _createResponse(status, data, headers); |
364 }); | 372 }); |
365 } | 373 } |
366 | 374 |
367 | 375 |
368 /** | 376 /** |
369 * @ngdoc method | 377 * @ngdoc method |
370 * @name ngMock.$httpBackend#expectGET | 378 * @name ngMock.httpBackend#expectGET |
371 * @methodOf ngMock.$httpBackend | 379 * @methodOf ngMock.httpBackend |
372 * @description | 380 * @description |
373 * Creates a new request expectation for GET requests. For more info see `expe
ct()`. | 381 * Creates a new request expectation for GET requests. For more info see `expe
ct()`. |
374 * | 382 * |
375 * @param {string|RegExp} url HTTP url. | 383 * @param {string|RegExp} url HTTP url. |
376 * @param {Object=} headers HTTP headers. | 384 * @param {Object=} headers HTTP headers. |
377 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 385 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
378 * request is handled. See #expect for more info. | 386 * request is handled. See #expect for more info. |
379 */ | 387 */ |
380 _Chain expectGET(url, [headers]) => expect('GET', url, null, headers); | 388 _Chain expectGET(url, [headers]) => expect('GET', url, null, headers); |
381 | 389 |
382 /** | 390 /** |
383 * @ngdoc method | 391 * @ngdoc method |
384 * @name ngMock.$httpBackend#expectDELETE | 392 * @name ngMock.httpBackend#expectDELETE |
385 * @methodOf ngMock.$httpBackend | 393 * @methodOf ngMock.httpBackend |
386 * @description | 394 * @description |
387 * Creates a new request expectation for DELETE requests. For more info see `e
xpect()`. | 395 * Creates a new request expectation for DELETE requests. For more info see `e
xpect()`. |
388 * | 396 * |
389 * @param {string|RegExp} url HTTP url. | 397 * @param {string|RegExp} url HTTP url. |
390 * @param {Object=} headers HTTP headers. | 398 * @param {Object=} headers HTTP headers. |
391 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 399 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
392 * request is handled. | 400 * request is handled. |
393 */ | 401 */ |
394 _Chain expectDELETE(url, [headers]) => expect('DELETE', url, null, headers); | 402 _Chain expectDELETE(url, [headers]) => expect('DELETE', url, null, headers); |
395 | 403 |
396 /** | 404 /** |
397 * @ngdoc method | 405 * @ngdoc method |
398 * @name ngMock.$httpBackend#expectJSONP | 406 * @name ngMock.httpBackend#expectJSONP |
399 * @methodOf ngMock.$httpBackend | 407 * @methodOf ngMock.httpBackend |
400 * @description | 408 * @description |
401 * Creates a new request expectation for JSONP requests. For more info see `ex
pect()`. | 409 * Creates a new request expectation for JSONP requests. For more info see `ex
pect()`. |
402 * | 410 * |
403 * @param {string|RegExp} url HTTP url. | 411 * @param {string|RegExp} url HTTP url. |
404 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 412 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
405 * request is handled. | 413 * request is handled. |
406 */ | 414 */ |
407 _Chain expectJSONP(url, [headers]) => expect('JSONP', url, null, headers); | 415 _Chain expectJSONP(url, [headers]) => expect('JSONP', url, null, headers); |
408 | 416 |
409 /** | 417 /** |
410 * @ngdoc method | 418 * @ngdoc method |
411 * @name ngMock.$httpBackend#expectPUT | 419 * @name ngMock.httpBackend#expectPUT |
412 * @methodOf ngMock.$httpBackend | 420 * @methodOf ngMock.httpBackend |
413 * @description | 421 * @description |
414 * Creates a new request expectation for PUT requests. For more info see `expe
ct()`. | 422 * Creates a new request expectation for PUT requests. For more info see `expe
ct()`. |
415 * | 423 * |
416 * @param {string|RegExp} url HTTP url. | 424 * @param {string|RegExp} url HTTP url. |
417 * @param {(string|RegExp)=} data HTTP request body. | 425 * @param {(string|RegExp)=} data HTTP request body. |
418 * @param {Object=} headers HTTP headers. | 426 * @param {Object=} headers HTTP headers. |
419 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 427 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
420 * request is handled. | 428 * request is handled. |
421 */ | 429 */ |
422 _Chain expectPUT(url, [data, headers]) => expect('PUT', url, data, headers); | 430 _Chain expectPUT(url, [data, headers]) => expect('PUT', url, data, headers); |
423 | 431 |
424 /** | 432 /** |
425 * @ngdoc method | 433 * @ngdoc method |
426 * @name ngMock.$httpBackend#expectPOST | 434 * @name ngMock.httpBackend#expectPOST |
427 * @methodOf ngMock.$httpBackend | 435 * @methodOf ngMock.httpBackend |
428 * @description | 436 * @description |
429 * Creates a new request expectation for POST requests. For more info see `exp
ect()`. | 437 * Creates a new request expectation for POST requests. For more info see `exp
ect()`. |
430 * | 438 * |
431 * @param {string|RegExp} url HTTP url. | 439 * @param {string|RegExp} url HTTP url. |
432 * @param {(string|RegExp)=} data HTTP request body. | 440 * @param {(string|RegExp)=} data HTTP request body. |
433 * @param {Object=} headers HTTP headers. | 441 * @param {Object=} headers HTTP headers. |
434 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 442 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
435 * request is handled. | 443 * request is handled. |
436 */ | 444 */ |
437 _Chain expectPOST(url, [data, headers]) => expect('POST', url, data, headers); | 445 _Chain expectPOST(url, [data, headers]) => expect('POST', url, data, headers); |
438 | 446 |
439 /** | 447 /** |
440 * @ngdoc method | 448 * @ngdoc method |
441 * @name ngMock.$httpBackend#expectPATCH | 449 * @name ngMock.httpBackend#expectPATCH |
442 * @methodOf ngMock.$httpBackend | 450 * @methodOf ngMock.httpBackend |
443 * @description | 451 * @description |
444 * Creates a new request expectation for PATCH requests. For more info see `ex
pect()`. | 452 * Creates a new request expectation for PATCH requests. For more info see `ex
pect()`. |
445 * | 453 * |
446 * @param {string|RegExp} url HTTP url. | 454 * @param {string|RegExp} url HTTP url. |
447 * @param {(string|RegExp)=} data HTTP request body. | 455 * @param {(string|RegExp)=} data HTTP request body. |
448 * @param {Object=} headers HTTP headers. | 456 * @param {Object=} headers HTTP headers. |
449 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 457 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
450 * request is handled. | 458 * request is handled. |
451 */ | 459 */ |
452 _Chain expectPATCH(url, [data, headers]) => expect('PATCH', url, data, headers
); | 460 _Chain expectPATCH(url, [data, headers]) => expect('PATCH', url, data, headers
); |
453 | 461 |
454 /** | 462 /** |
455 * @ngdoc method | 463 * @ngdoc method |
456 * @name ngMock.$httpBackend#expectHEAD | 464 * @name ngMock.httpBackend#expectHEAD |
457 * @methodOf ngMock.$httpBackend | 465 * @methodOf ngMock.httpBackend |
458 * @description | 466 * @description |
459 * Creates a new request expectation for HEAD requests. For more info see `exp
ect()`. | 467 * Creates a new request expectation for HEAD requests. For more info see `exp
ect()`. |
460 * | 468 * |
461 * @param {string|RegExp} url HTTP url. | 469 * @param {string|RegExp} url HTTP url. |
462 * @param {Object=} headers HTTP headers. | 470 * @param {Object=} headers HTTP headers. |
463 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched | 471 * @returns {requestHandler} Returns an object with `respond` method that cont
rol how a matched |
464 * request is handled. | 472 * request is handled. |
465 */ | 473 */ |
466 | 474 |
467 /** | 475 /** |
468 * @ngdoc method | 476 * @ngdoc method |
469 * @name ngMock.$httpBackend#flush | 477 * @name ngMock.httpBackend#flush |
470 * @methodOf ngMock.$httpBackend | 478 * @methodOf ngMock.httpBackend |
471 * @description | 479 * @description |
472 * Flushes all pending requests using the trained responses. | 480 * Flushes all pending requests using the trained responses. |
473 * | 481 * |
474 * @param {number=} count Number of responses to flush (in the order they arri
ved). If undefined, | 482 * @param {number=} count Number of responses to flush (in the order they arri
ved). If undefined, |
475 * all pending requests will be flushed. If there are no pending requests wh
en the flush method | 483 * all pending requests will be flushed. If there are no pending requests wh
en the flush method |
476 * is called an exception is thrown (as this typically a sign of programming
error). | 484 * is called an exception is thrown (as this typically a sign of programming
error). |
477 */ | 485 */ |
478 void flush([count]) { | 486 void flush([count]) { |
479 if (responses.isEmpty) throw ['No pending request to flush !']; | 487 if (responses.isEmpty) throw ['No pending request to flush !']; |
480 | 488 |
481 if (count != null) { | 489 if (count != null) { |
482 while (count-- > 0) { | 490 while (count-- > 0) { |
483 if (responses.isEmpty) throw ['No more pending request to flush !']; | 491 if (responses.isEmpty) throw ['No more pending request to flush !']; |
484 responses.removeAt(0)(); | 492 responses.removeAt(0)(); |
485 } | 493 } |
486 } else { | 494 } else { |
487 while (!responses.isEmpty) { | 495 while (!responses.isEmpty) { |
488 responses.removeAt(0)(); | 496 responses.removeAt(0)(); |
489 } | 497 } |
490 } | 498 } |
491 verifyNoOutstandingExpectation(); | 499 verifyNoOutstandingExpectation(); |
492 } | 500 } |
493 | 501 |
494 | 502 |
495 /** | 503 /** |
496 * @ngdoc method | 504 * @ngdoc method |
497 * @name ngMock.$httpBackend#verifyNoOutstandingExpectation | 505 * @name ngMock.httpBackend#verifyNoOutstandingExpectation |
498 * @methodOf ngMock.$httpBackend | 506 * @methodOf ngMock.httpBackend |
499 * @description | 507 * @description |
500 * Verifies that all of the requests defined via the `expect` api were made. I
f any of the | 508 * Verifies that all of the requests defined via the `expect` api were made. I
f any of the |
501 * requests were not made, verifyNoOutstandingExpectation throws an exception. | 509 * requests were not made, verifyNoOutstandingExpectation throws an exception. |
502 * | 510 * |
503 * Typically, you would call this method following each test case that asserts
requests using an | 511 * Typically, you would call this method following each test case that asserts
requests using an |
504 * "afterEach" clause. | 512 * "afterEach" clause. |
505 * | 513 * |
506 * <pre> | 514 * <pre> |
507 * afterEach($httpBackend.verifyNoOutstandingExpectation); | 515 * afterEach(httpBackend.verifyNoOutstandingExpectation); |
508 * </pre> | 516 * </pre> |
509 */ | 517 */ |
510 void verifyNoOutstandingExpectation() { | 518 void verifyNoOutstandingExpectation() { |
511 if (!expectations.isEmpty) { | 519 if (!expectations.isEmpty) { |
512 throw ['Unsatisfied requests: ${expectations.join(', ')}']; | 520 throw ['Unsatisfied requests: ${expectations.join(', ')}']; |
513 } | 521 } |
514 } | 522 } |
515 | 523 |
516 | 524 |
517 /** | 525 /** |
518 * @ngdoc method | 526 * @ngdoc method |
519 * @name ngMock.$httpBackend#verifyNoOutstandingRequest | 527 * @name ngMock.httpBackend#verifyNoOutstandingRequest |
520 * @methodOf ngMock.$httpBackend | 528 * @methodOf ngMock.httpBackend |
521 * @description | 529 * @description |
522 * Verifies that there are no outstanding requests that need to be flushed. | 530 * Verifies that there are no outstanding requests that need to be flushed. |
523 * | 531 * |
524 * Typically, you would call this method following each test case that asserts
requests using an | 532 * Typically, you would call this method following each test case that asserts
requests using an |
525 * "afterEach" clause. | 533 * "afterEach" clause. |
526 * | 534 * |
527 * <pre> | 535 * <pre> |
528 * afterEach($httpBackend.verifyNoOutstandingRequest); | 536 * afterEach(httpBackend.verifyNoOutstandingRequest); |
529 * </pre> | 537 * </pre> |
530 */ | 538 */ |
531 void verifyNoOutstandingRequest() { | 539 void verifyNoOutstandingRequest() { |
532 if (!responses.isEmpty) throw ['Unflushed requests: ${responses.length}']; | 540 if (!responses.isEmpty) throw ['Unflushed requests: ${responses.length}']; |
533 } | 541 } |
534 | 542 |
535 | 543 |
536 /** | 544 /** |
537 * @ngdoc method | 545 * @ngdoc method |
538 * @name ngMock.$httpBackend#resetExpectations | 546 * @name ngMock.httpBackend#resetExpectations |
539 * @methodOf ngMock.$httpBackend | 547 * @methodOf ngMock.httpBackend |
540 * @description | 548 * @description |
541 * Resets all request expectations, but preserves all backend definitions. Typ
ically, you would | 549 * Resets all request expectations, but preserves all backend definitions. Typ
ically, you would |
542 * call resetExpectations during a multiple-phase test when you want to reuse
the same instance of | 550 * call resetExpectations during a multiple-phase test when you want to reuse
the same instance of |
543 * $httpBackend mock. | 551 * httpBackend mock. |
544 */ | 552 */ |
545 void resetExpectations() { | 553 void resetExpectations() { |
546 expectations.length = 0; | 554 expectations.length = 0; |
547 responses.length = 0; | 555 responses.length = 0; |
548 } | 556 } |
549 } | 557 } |
550 | 558 |
551 /** | 559 /** |
552 * Mock implementation of the [HttpRequest] object returned from the HttpBackend
. | 560 * Mock implementation of the [HttpRequest] object returned from the HttpBackend
. |
553 */ | 561 */ |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 final String type = null; | 626 final String type = null; |
619 | 627 |
620 bool cancelBubble = false; | 628 bool cancelBubble = false; |
621 | 629 |
622 MockProgressEvent(MockHttpRequest this.currentTarget); | 630 MockProgressEvent(MockHttpRequest this.currentTarget); |
623 | 631 |
624 void preventDefault() {} | 632 void preventDefault() {} |
625 void stopImmediatePropagation() {} | 633 void stopImmediatePropagation() {} |
626 void stopPropagation() {} | 634 void stopPropagation() {} |
627 } | 635 } |
OLD | NEW |