11'use strict' ;
22
3+ /* global routeToRegExp: false */
4+
35/**
46 * @ngdoc object
57 * @name angular.mock
@@ -1282,7 +1284,7 @@ angular.mock.dump = function(object) {
12821284 * ## Matching route requests
12831285 *
12841286 * For extra convenience, `whenRoute` and `expectRoute` shortcuts are available. These methods offer colon
1285- * delimited matching of the url path, ignoring the query string. This allows declarations
1287+ * delimited matching of the url path, ignoring the query string and trailing slashes . This allows declarations
12861288 * similar to how application routes are configured with `$routeProvider`. Because these methods convert
12871289 * the definition url to regex, declaration order is important. Combined with query parameter parsing,
12881290 * the following is possible:
@@ -1481,8 +1483,9 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14811483 * ```
14821484 * – The respond method takes a set of static data to be returned or a function that can
14831485 * 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.
14861489 */
14871490 $httpBackend . when = function ( method , url , data , headers , keys ) {
14881491
@@ -1663,40 +1666,10 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
16631666 * See {@link ngMock.$httpBackend#when `when`} for more info.
16641667 */
16651668 $httpBackend . whenRoute = function ( method , url ) {
1666- var pathObj = parseRoute ( url ) ;
1669+ var pathObj = routeToRegExp ( url , { caseInsensitiveMatch : true , ignoreTrailingSlashes : true } ) ;
16671670 return $httpBackend . when ( method , pathObj . regexp , undefined , undefined , pathObj . keys ) ;
16681671 } ;
16691672
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 ;
1698- }
1699-
17001673 /**
17011674 * @ngdoc method
17021675 * @name $httpBackend#expect
@@ -1717,14 +1690,15 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
17171690 * order to change how a matched request is handled.
17181691 *
17191692 * - respond –
1720- * ```
1721- * { function([status,] data[, headers, statusText])
1722- * | function(function(method, url, data, headers, params)}
1723- * ```
1693+ * ```js
1694+ * { function([status,] data[, headers, statusText])
1695+ * | function(function(method, url, data, headers, params)}
1696+ * ```
17241697 * – The respond method takes a set of static data to be returned or a function that can
17251698 * 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.
1699+ * response headers (Object), HTTP status text (string), and XMLHttpRequest status (string:
1700+ * `complete`, `error`, `timeout` or `abort`). The respond method returns the `requestHandler`
1701+ * object for possible overrides.
17281702 */
17291703 $httpBackend . expect = function ( method , url , data , headers , keys ) {
17301704
@@ -1876,7 +1850,7 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
18761850 * See {@link ngMock.$httpBackend#expect `expect`} for more info.
18771851 */
18781852 $httpBackend . expectRoute = function ( method , url ) {
1879- var pathObj = parseRoute ( url ) ;
1853+ var pathObj = routeToRegExp ( url , { caseInsensitiveMatch : true , ignoreTrailingSlashes : true } ) ;
18801854 return $httpBackend . expect ( method , pathObj . regexp , undefined , undefined , pathObj . keys ) ;
18811855 } ;
18821856
0 commit comments