1
1
'use strict' ;
2
2
3
+ /* global routeToRegExp: false */
4
+
3
5
/**
4
6
* @ngdoc object
5
7
* @name angular.mock
@@ -1481,8 +1483,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1481
1483
* ```
1482
1484
* – The respond method takes a set of static data to be returned or a function that can
1483
1485
* return an array containing response status (number), response data (Array|Object|string),
1484
- * response headers (Object), and the text for the status (string). The respond method returns
1485
- * the `requestHandler` object for possible overrides.
1486
+ * response headers (Object), HTTP status text (string), and XMLHttpRequest status (string:
1487
+ * `complete`, `error`, `timeout` or `abort`). The respond method returns the `requestHandler`
1488
+ * object for possible overrides.
1486
1489
*/
1487
1490
$httpBackend . when = function ( method , url , data , headers , keys ) {
1488
1491
@@ -1663,38 +1666,21 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1663
1666
* See {@link ngMock.$httpBackend#when `when`} for more info.
1664
1667
*/
1665
1668
$httpBackend . whenRoute = function ( method , url ) {
1666
- var pathObj = parseRoute ( url ) ;
1667
- return $httpBackend . when ( method , pathObj . regexp , undefined , undefined , pathObj . keys ) ;
1668
- } ;
1669
-
1670
- function parseRoute ( url ) {
1671
- var ret = {
1672
- regexp : url
1673
- } ,
1674
- keys = ret . keys = [ ] ;
1675
-
1676
- if ( ! url || ! angular . isString ( url ) ) return ret ;
1677
-
1678
- url = url
1679
- . replace ( / ( [ ( ) . ] ) / g, '\\$1' )
1680
- . replace ( / ( \/ ) ? : ( \w + ) ( [ ? * ] ) ? / g, function ( _ , slash , key , option ) {
1681
- var optional = option === '?' ? option : null ;
1682
- var star = option === '*' ? option : null ;
1683
- keys . push ( { name : key , optional : ! ! optional } ) ;
1684
- slash = slash || '' ;
1685
- return ''
1686
- + ( optional ? '' : slash )
1687
- + '(?:'
1688
- + ( optional ? slash : '' )
1689
- + ( star && '(.+?)' || '([^/]+)' )
1690
- + ( optional || '' )
1691
- + ')'
1692
- + ( optional || '' ) ;
1693
- } )
1694
- . replace ( / ( [ / $ * ] ) / g, '\\$1' ) ;
1695
-
1696
- ret . regexp = new RegExp ( '^' + url , 'i' ) ;
1697
- return ret ;
1669
+ var quacksLikeRegExp = parseRoute ( url ) ;
1670
+ return $httpBackend . when ( method , quacksLikeRegExp , undefined , undefined , quacksLikeRegExp . keys ) ;
1671
+ } ;
1672
+
1673
+ function parseRoute ( route ) {
1674
+ var parsed = routeToRegExp ( route , { caseInsensitiveMatch : true } ) ;
1675
+ return {
1676
+ test : function ( url ) {
1677
+ return parsed . regexp . test ( url . replace ( / [ ? # ] .* / , '' ) ) ;
1678
+ } ,
1679
+ exec : function ( url ) {
1680
+ return parsed . regexp . exec ( url . replace ( / [ ? # ] .* / , '' ) ) ;
1681
+ } ,
1682
+ keys : parsed . keys
1683
+ } ;
1698
1684
}
1699
1685
1700
1686
/**
@@ -1717,14 +1703,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1717
1703
* order to change how a matched request is handled.
1718
1704
*
1719
1705
* - respond –
1720
- * ```
1721
- * { function([status,] data[, headers, statusText])
1722
- * | function(function(method, url, data, headers, params)}
1723
- * ```
1706
+ * ```js
1707
+ * { function([status,] data[, headers, statusText])
1708
+ * | function(function(method, url, data, headers, params)}
1709
+ * ```
1724
1710
* – The respond method takes a set of static data to be returned or a function that can
1725
1711
* return an array containing response status (number), response data (Array|Object|string),
1726
- * response headers (Object), and the text for the status (string). The respond method returns
1727
- * the `requestHandler` object for possible overrides.
1712
+ * response headers (Object), HTTP status text (string), and XMLHttpRequest status (string:
1713
+ * `complete`, `error`, `timeout` or `abort`). The respond method returns the `requestHandler`
1714
+ * object for possible overrides.
1728
1715
*/
1729
1716
$httpBackend . expect = function ( method , url , data , headers , keys ) {
1730
1717
@@ -1876,8 +1863,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1876
1863
* See {@link ngMock.$httpBackend#expect `expect`} for more info.
1877
1864
*/
1878
1865
$httpBackend . expectRoute = function ( method , url ) {
1879
- var pathObj = parseRoute ( url ) ;
1880
- return $httpBackend . expect ( method , pathObj . regexp , undefined , undefined , pathObj . keys ) ;
1866
+ var quacksLikeRegExp = parseRoute ( url ) ;
1867
+ return $httpBackend . expect ( method , quacksLikeRegExp , undefined , undefined , quacksLikeRegExp . keys ) ;
1881
1868
} ;
1882
1869
1883
1870
0 commit comments