@@ -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,80 @@ 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 is put at the front of the queue for matching.
1520
+ * This means that a matching `whenOverride` definition will always respond even if
1521
+ * there is a more specific `when` definition.
1522
+ *
1523
+ * #### Expected usage:
1524
+ * ```js
1525
+ * // Without "override":
1526
+ * $httpBackend.whenPOST('/foo').respond(1);
1527
+ * $httpBackend.whenPOST('/foo', {id: 1}).respond(2);
1528
+ * $http.post('/foo'); // --> 1
1529
+ * $http.post('/foo', {id: 1}); // --> 1
1530
+ *
1531
+ * // With "overrride":
1532
+ * $httpBackend.whenPOST('/foo').respond(1);
1533
+ * $httpBackend.whenOverridePOST('/foo', {id: 1}).respond(2);
1534
+ * $http.post('/foo'); // --> 1
1535
+ * $http.post('/foo', {id: 1}); // --> 2
1536
+ * ```
1537
+ *
1538
+ * #### Potentially confusing usage:
1539
+ * ```js
1540
+ * // Without "override":
1541
+ * $httpBackend.whenPOST('/foo', {id: 1}).respond(1);
1542
+ * $httpBackend.whenPOST('/foo').respond(2);
1543
+ * $http.post('/foo'); // --> 2
1544
+ * $http.post('/foo', {id: 1}); // --> 1
1545
+ *
1546
+ * // With "overrride":
1547
+ * $httpBackend.whenPOST('/foo', {id: 1}).respond(1);
1548
+ * $httpBackend.whenOverridePOST('/foo').respond(2);
1549
+ * $http.post('/foo'); // --> 2
1550
+ * $http.post('/foo', {id: 1}); // --> 2
1551
+ * ```
1552
+ *
1553
+ * @param {string } method HTTP method.
1554
+ * @param {string|RegExp|function(string)= } url HTTP url or function that receives a url
1555
+ * and returns true if the url matches the current definition.
1556
+ * @param {(string|RegExp|function(string))= } data HTTP request body or function that receives
1557
+ * data string and returns true if the data is as expected.
1558
+ * @param {(Object|function(Object))= } headers HTTP headers or function that receives http header
1559
+ * object and returns true if the headers match the current definition.
1560
+ * @param {(Array)= } keys Array of keys to assign to regex matches in request url described above.
1561
+ * @returns {requestHandler } Returns an object with `respond` method that controls how a matched
1562
+ * request is handled. You can save this object for later use and invoke `respond` again in
1563
+ * order to change how a matched request is handled.
1564
+ *
1565
+ * - respond –
1566
+ * ```js
1567
+ * {function([status,] data[, headers, statusText])
1568
+ * | function(function(method, url, data, headers, params)}
1569
+ * ```
1570
+ * – The respond method takes a set of static data to be returned or a function that can
1571
+ * return an array containing response status (number), response data (Array|Object|string),
1572
+ * response headers (Object), and the text for the status (string). The respond method returns
1573
+ * the `requestHandler` object for possible overrides.
1574
+ */
1575
+ $httpBackend . whenOverride = function ( method , url , data , headers , keys ) {
1576
+
1577
+ assertArgDefined ( arguments , 1 , 'url' ) ;
1578
+
1579
+ var mock = when ( method , url , data , headers , keys ) ;
1580
+
1581
+ definitions . unshift ( mock . definition ) ;
1582
+ return mock . chain ;
1504
1583
} ;
1505
1584
1506
1585
/**
@@ -1597,6 +1676,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1597
1676
*/
1598
1677
createShortMethods ( 'when' ) ;
1599
1678
1679
+ createShortMethods ( 'whenOverride' ) ;
1680
+
1600
1681
/**
1601
1682
* @ngdoc method
1602
1683
* @name $httpBackend#whenRoute
0 commit comments