@@ -1450,6 +1450,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1450
1450
throw error ;
1451
1451
}
1452
1452
1453
+ function when ( method , url , data , headers , keys ) {
1454
+ var definition = new MockHttpExpectation ( method , url , data , headers , keys ) ,
1455
+ chain = {
1456
+ respond : function ( status , data , headers , statusText ) {
1457
+ definition . passThrough = undefined ;
1458
+ definition . response = createResponse ( status , data , headers , statusText ) ;
1459
+ return chain ;
1460
+ }
1461
+ } ;
1462
+
1463
+ if ( $browser ) {
1464
+ chain . passThrough = function ( ) {
1465
+ definition . response = undefined ;
1466
+ definition . passThrough = true ;
1467
+ return chain ;
1468
+ } ;
1469
+ }
1470
+
1471
+ return {
1472
+ definition : definition ,
1473
+ chain : chain
1474
+ } ;
1475
+ }
1476
+
1453
1477
/**
1454
1478
* @ngdoc method
1455
1479
* @name $httpBackend#when
@@ -1482,25 +1506,48 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1482
1506
1483
1507
assertArgDefined ( arguments , 1 , 'url' ) ;
1484
1508
1485
- var definition = new MockHttpExpectation ( method , url , data , headers , keys ) ,
1486
- chain = {
1487
- respond : function ( status , data , headers , statusText ) {
1488
- definition . passThrough = undefined ;
1489
- definition . response = createResponse ( status , data , headers , statusText ) ;
1490
- return chain ;
1491
- }
1492
- } ;
1509
+ var mock = when ( method , url , data , headers , keys ) ;
1493
1510
1494
- if ( $browser ) {
1495
- chain . passThrough = function ( ) {
1496
- definition . response = undefined ;
1497
- definition . passThrough = true ;
1498
- return chain ;
1499
- } ;
1500
- }
1511
+ definitions . push ( mock . definition ) ;
1512
+ return mock . chain ;
1513
+ } ;
1501
1514
1502
- definitions . push ( definition ) ;
1503
- return chain ;
1515
+ /**
1516
+ * @ngdoc method
1517
+ * @name $httpBackend#whenOverride
1518
+ * @description
1519
+ * Creates a new backend definition that will override any already existing definitions with the same parameters
1520
+ *
1521
+ * @param {string } method HTTP method.
1522
+ * @param {string|RegExp|function(string)= } url HTTP url or function that receives a url
1523
+ * and returns true if the url matches the current definition.
1524
+ * @param {(string|RegExp|function(string))= } data HTTP request body or function that receives
1525
+ * data string and returns true if the data is as expected.
1526
+ * @param {(Object|function(Object))= } headers HTTP headers or function that receives http header
1527
+ * object and returns true if the headers match the current definition.
1528
+ * @param {(Array)= } keys Array of keys to assign to regex matches in request url described above.
1529
+ * @returns {requestHandler } Returns an object with `respond` method that controls how a matched
1530
+ * request is handled. You can save this object for later use and invoke `respond` again in
1531
+ * order to change how a matched request is handled.
1532
+ *
1533
+ * - respond –
1534
+ * ```js
1535
+ * {function([status,] data[, headers, statusText])
1536
+ * | function(function(method, url, data, headers, params)}
1537
+ * ```
1538
+ * – The respond method takes a set of static data to be returned or a function that can
1539
+ * return an array containing response status (number), response data (Array|Object|string),
1540
+ * response headers (Object), and the text for the status (string). The respond method returns
1541
+ * the `requestHandler` object for possible overrides.
1542
+ */
1543
+ $httpBackend . whenOverride = function ( method , url , data , headers , keys ) {
1544
+
1545
+ assertArgDefined ( arguments , 1 , 'url' ) ;
1546
+
1547
+ var mock = when ( method , url , data , headers , keys ) ;
1548
+
1549
+ definitions . unshift ( mock . definition ) ;
1550
+ return mock . chain ;
1504
1551
} ;
1505
1552
1506
1553
/**
@@ -1597,6 +1644,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1597
1644
*/
1598
1645
createShortMethods ( 'when' ) ;
1599
1646
1647
+ createShortMethods ( 'whenOverride' ) ;
1648
+
1600
1649
/**
1601
1650
* @ngdoc method
1602
1651
* @name $httpBackend#whenRoute
0 commit comments