diff options
| author | Caio Cunha | 2013-10-22 09:41:51 -0400 | 
|---|---|---|
| committer | Igor Minar | 2014-03-18 15:19:26 -0700 | 
| commit | d6cfcacee101f2738e0a224a3377232ff85f78a4 (patch) | |
| tree | 4f3237302917e7e12fab43980f528c5007413234 | |
| parent | 299b220f5e05e1d4e26bfd58d0b2fd7329ca76b1 (diff) | |
| download | angular.js-d6cfcacee101f2738e0a224a3377232ff85f78a4.tar.bz2 | |
feat(ngMock.$httpBackend): added support for function as URL matcher
It's now possible to pass a function to match the URL in $httpBackend mocked
expectations. This gives a more sophisticate control over the URL matching
without requiring complex RegExp mantainance or the workaround of creating
an object with a `test` function in order to mimic RegExp interface.
This approach was suggested in [this
thread](https://groups.google.com/d/msg/angular/3QsCUEvvxlM/Q4C4ZIqNIuEJ)
Closes #4580
| -rw-r--r-- | src/ngMock/angular-mocks.js | 70 | ||||
| -rw-r--r-- | test/ngMock/angular-mocksSpec.js | 11 | 
2 files changed, 58 insertions, 23 deletions
| diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 67decace..bb2ac754 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1178,7 +1178,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * Creates a new backend definition.     *     * @param {string} method HTTP method. -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives     *   data string and returns true if the data is as expected.     * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header @@ -1216,7 +1217,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for GET requests. For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(Object|function(Object))=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     * request is handled. @@ -1228,7 +1230,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for HEAD requests. For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(Object|function(Object))=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     * request is handled. @@ -1240,7 +1243,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for DELETE requests. For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(Object|function(Object))=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     * request is handled. @@ -1252,7 +1256,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for POST requests. For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives     *   data string and returns true if the data is as expected.     * @param {(Object|function(Object))=} headers HTTP headers. @@ -1266,7 +1271,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for PUT requests.  For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives     *   data string and returns true if the data is as expected.     * @param {(Object|function(Object))=} headers HTTP headers. @@ -1280,7 +1286,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new backend definition for JSONP requests. For more info see `when()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     * request is handled.     */ @@ -1294,7 +1301,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * Creates a new request expectation.     *     * @param {string} method HTTP method. -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that     *  receives data string and returns true if the data is as expected, or Object if request body     *  is in JSON format. @@ -1326,7 +1334,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for GET requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {Object=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     * request is handled. See #expect for more info. @@ -1338,7 +1347,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for HEAD requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {Object=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     *   request is handled. @@ -1350,7 +1360,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for DELETE requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {Object=} headers HTTP headers.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     *   request is handled. @@ -1362,7 +1373,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for POST requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that     *  receives data string and returns true if the data is as expected, or Object if request body     *  is in JSON format. @@ -1377,7 +1389,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for PUT requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that     *  receives data string and returns true if the data is as expected, or Object if request body     *  is in JSON format. @@ -1392,7 +1405,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for PATCH requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that     *  receives data string and returns true if the data is as expected, or Object if request body     *  is in JSON format. @@ -1407,7 +1421,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {     * @description     * Creates a new request expectation for JSONP requests. For more info see `expect()`.     * -   * @param {string|RegExp} url HTTP url. +   * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  +   *   and returns true if the url match the current definition.     * @returns {requestHandler} Returns an object with `respond` method that control how a matched     *   request is handled.     */ @@ -1531,6 +1546,7 @@ function MockHttpExpectation(method, url, data, headers) {    this.matchUrl = function(u) {      if (!url) return true;      if (angular.isFunction(url.test)) return url.test(u); +    if (angular.isFunction(url)) return url(u);      return url == u;    }; @@ -1808,7 +1824,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * Creates a new backend definition.   *   * @param {string} method HTTP method. - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(string|RegExp)=} data HTTP request body.   * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header   *   object and returns true if the headers match the current definition. @@ -1832,7 +1849,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for GET requests. For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that   *   control how a matched request is handled. @@ -1845,7 +1863,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for HEAD requests. For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that   *   control how a matched request is handled. @@ -1858,7 +1877,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for DELETE requests. For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that   *   control how a matched request is handled. @@ -1871,7 +1891,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for POST requests. For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(string|RegExp)=} data HTTP request body.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that @@ -1885,7 +1906,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for PUT requests.  For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(string|RegExp)=} data HTTP request body.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that @@ -1899,7 +1921,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for PATCH requests.  For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @param {(string|RegExp)=} data HTTP request body.   * @param {(Object|function(Object))=} headers HTTP headers.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that @@ -1913,7 +1936,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {   * @description   * Creates a new backend definition for JSONP requests. For more info see `when()`.   * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url  + *   and returns true if the url match the current definition.   * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that   *   control how a matched request is handled.   */ diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index c2b6108d..b3608013 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1431,6 +1431,17 @@ describe('ngMock', function() {        }); +      it('should accept url as function', function() { +        var urlValidator = function(url) { +          return url !== '/not-accepted'; +        }; +        var exp = new MockHttpExpectation('POST', urlValidator); + +        expect(exp.match('POST', '/url')).toBe(true); +        expect(exp.match('POST', '/not-accepted')).toBe(false); +      }); + +        it('should accept data as regexp', function() {          var exp = new MockHttpExpectation('POST', '/url', /\{.*?\}/); | 
