Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d6cfcac

Browse files
caiotoonIgorMinar
authored andcommitted
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
1 parent 299b220 commit d6cfcac

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

src/ngMock/angular-mocks.js

+47-23
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11781178
* Creates a new backend definition.
11791179
*
11801180
* @param {string} method HTTP method.
1181-
* @param {string|RegExp} url HTTP url.
1181+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1182+
* and returns true if the url match the current definition.
11821183
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
11831184
* data string and returns true if the data is as expected.
11841185
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
@@ -1216,7 +1217,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12161217
* @description
12171218
* Creates a new backend definition for GET requests. For more info see `when()`.
12181219
*
1219-
* @param {string|RegExp} url HTTP url.
1220+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1221+
* and returns true if the url match the current definition.
12201222
* @param {(Object|function(Object))=} headers HTTP headers.
12211223
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12221224
* request is handled.
@@ -1228,7 +1230,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12281230
* @description
12291231
* Creates a new backend definition for HEAD requests. For more info see `when()`.
12301232
*
1231-
* @param {string|RegExp} url HTTP url.
1233+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1234+
* and returns true if the url match the current definition.
12321235
* @param {(Object|function(Object))=} headers HTTP headers.
12331236
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12341237
* request is handled.
@@ -1240,7 +1243,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12401243
* @description
12411244
* Creates a new backend definition for DELETE requests. For more info see `when()`.
12421245
*
1243-
* @param {string|RegExp} url HTTP url.
1246+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1247+
* and returns true if the url match the current definition.
12441248
* @param {(Object|function(Object))=} headers HTTP headers.
12451249
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12461250
* request is handled.
@@ -1252,7 +1256,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12521256
* @description
12531257
* Creates a new backend definition for POST requests. For more info see `when()`.
12541258
*
1255-
* @param {string|RegExp} url HTTP url.
1259+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1260+
* and returns true if the url match the current definition.
12561261
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
12571262
* data string and returns true if the data is as expected.
12581263
* @param {(Object|function(Object))=} headers HTTP headers.
@@ -1266,7 +1271,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12661271
* @description
12671272
* Creates a new backend definition for PUT requests. For more info see `when()`.
12681273
*
1269-
* @param {string|RegExp} url HTTP url.
1274+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1275+
* and returns true if the url match the current definition.
12701276
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
12711277
* data string and returns true if the data is as expected.
12721278
* @param {(Object|function(Object))=} headers HTTP headers.
@@ -1280,7 +1286,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12801286
* @description
12811287
* Creates a new backend definition for JSONP requests. For more info see `when()`.
12821288
*
1283-
* @param {string|RegExp} url HTTP url.
1289+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1290+
* and returns true if the url match the current definition.
12841291
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12851292
* request is handled.
12861293
*/
@@ -1294,7 +1301,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12941301
* Creates a new request expectation.
12951302
*
12961303
* @param {string} method HTTP method.
1297-
* @param {string|RegExp} url HTTP url.
1304+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1305+
* and returns true if the url match the current definition.
12981306
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
12991307
* receives data string and returns true if the data is as expected, or Object if request body
13001308
* is in JSON format.
@@ -1326,7 +1334,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13261334
* @description
13271335
* Creates a new request expectation for GET requests. For more info see `expect()`.
13281336
*
1329-
* @param {string|RegExp} url HTTP url.
1337+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1338+
* and returns true if the url match the current definition.
13301339
* @param {Object=} headers HTTP headers.
13311340
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13321341
* request is handled. See #expect for more info.
@@ -1338,7 +1347,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13381347
* @description
13391348
* Creates a new request expectation for HEAD requests. For more info see `expect()`.
13401349
*
1341-
* @param {string|RegExp} url HTTP url.
1350+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1351+
* and returns true if the url match the current definition.
13421352
* @param {Object=} headers HTTP headers.
13431353
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13441354
* request is handled.
@@ -1350,7 +1360,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13501360
* @description
13511361
* Creates a new request expectation for DELETE requests. For more info see `expect()`.
13521362
*
1353-
* @param {string|RegExp} url HTTP url.
1363+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1364+
* and returns true if the url match the current definition.
13541365
* @param {Object=} headers HTTP headers.
13551366
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13561367
* request is handled.
@@ -1362,7 +1373,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13621373
* @description
13631374
* Creates a new request expectation for POST requests. For more info see `expect()`.
13641375
*
1365-
* @param {string|RegExp} url HTTP url.
1376+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1377+
* and returns true if the url match the current definition.
13661378
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
13671379
* receives data string and returns true if the data is as expected, or Object if request body
13681380
* is in JSON format.
@@ -1377,7 +1389,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13771389
* @description
13781390
* Creates a new request expectation for PUT requests. For more info see `expect()`.
13791391
*
1380-
* @param {string|RegExp} url HTTP url.
1392+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1393+
* and returns true if the url match the current definition.
13811394
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
13821395
* receives data string and returns true if the data is as expected, or Object if request body
13831396
* is in JSON format.
@@ -1392,7 +1405,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13921405
* @description
13931406
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
13941407
*
1395-
* @param {string|RegExp} url HTTP url.
1408+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1409+
* and returns true if the url match the current definition.
13961410
* @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
13971411
* receives data string and returns true if the data is as expected, or Object if request body
13981412
* is in JSON format.
@@ -1407,7 +1421,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
14071421
* @description
14081422
* Creates a new request expectation for JSONP requests. For more info see `expect()`.
14091423
*
1410-
* @param {string|RegExp} url HTTP url.
1424+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1425+
* and returns true if the url match the current definition.
14111426
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
14121427
* request is handled.
14131428
*/
@@ -1531,6 +1546,7 @@ function MockHttpExpectation(method, url, data, headers) {
15311546
this.matchUrl = function(u) {
15321547
if (!url) return true;
15331548
if (angular.isFunction(url.test)) return url.test(u);
1549+
if (angular.isFunction(url)) return url(u);
15341550
return url == u;
15351551
};
15361552

@@ -1808,7 +1824,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18081824
* Creates a new backend definition.
18091825
*
18101826
* @param {string} method HTTP method.
1811-
* @param {string|RegExp} url HTTP url.
1827+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1828+
* and returns true if the url match the current definition.
18121829
* @param {(string|RegExp)=} data HTTP request body.
18131830
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
18141831
* object and returns true if the headers match the current definition.
@@ -1832,7 +1849,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18321849
* @description
18331850
* Creates a new backend definition for GET requests. For more info see `when()`.
18341851
*
1835-
* @param {string|RegExp} url HTTP url.
1852+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1853+
* and returns true if the url match the current definition.
18361854
* @param {(Object|function(Object))=} headers HTTP headers.
18371855
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
18381856
* control how a matched request is handled.
@@ -1845,7 +1863,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18451863
* @description
18461864
* Creates a new backend definition for HEAD requests. For more info see `when()`.
18471865
*
1848-
* @param {string|RegExp} url HTTP url.
1866+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1867+
* and returns true if the url match the current definition.
18491868
* @param {(Object|function(Object))=} headers HTTP headers.
18501869
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
18511870
* control how a matched request is handled.
@@ -1858,7 +1877,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18581877
* @description
18591878
* Creates a new backend definition for DELETE requests. For more info see `when()`.
18601879
*
1861-
* @param {string|RegExp} url HTTP url.
1880+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1881+
* and returns true if the url match the current definition.
18621882
* @param {(Object|function(Object))=} headers HTTP headers.
18631883
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
18641884
* control how a matched request is handled.
@@ -1871,7 +1891,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18711891
* @description
18721892
* Creates a new backend definition for POST requests. For more info see `when()`.
18731893
*
1874-
* @param {string|RegExp} url HTTP url.
1894+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1895+
* and returns true if the url match the current definition.
18751896
* @param {(string|RegExp)=} data HTTP request body.
18761897
* @param {(Object|function(Object))=} headers HTTP headers.
18771898
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
@@ -1885,7 +1906,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18851906
* @description
18861907
* Creates a new backend definition for PUT requests. For more info see `when()`.
18871908
*
1888-
* @param {string|RegExp} url HTTP url.
1909+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1910+
* and returns true if the url match the current definition.
18891911
* @param {(string|RegExp)=} data HTTP request body.
18901912
* @param {(Object|function(Object))=} headers HTTP headers.
18911913
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
@@ -1899,7 +1921,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18991921
* @description
19001922
* Creates a new backend definition for PATCH requests. For more info see `when()`.
19011923
*
1902-
* @param {string|RegExp} url HTTP url.
1924+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1925+
* and returns true if the url match the current definition.
19031926
* @param {(string|RegExp)=} data HTTP request body.
19041927
* @param {(Object|function(Object))=} headers HTTP headers.
19051928
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
@@ -1913,7 +1936,8 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
19131936
* @description
19141937
* Creates a new backend definition for JSONP requests. For more info see `when()`.
19151938
*
1916-
* @param {string|RegExp} url HTTP url.
1939+
* @param {string|RegExp|function(string)} url HTTP url or function that receives the url
1940+
* and returns true if the url match the current definition.
19171941
* @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
19181942
* control how a matched request is handled.
19191943
*/

test/ngMock/angular-mocksSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,17 @@ describe('ngMock', function() {
14311431
});
14321432

14331433

1434+
it('should accept url as function', function() {
1435+
var urlValidator = function(url) {
1436+
return url !== '/not-accepted';
1437+
};
1438+
var exp = new MockHttpExpectation('POST', urlValidator);
1439+
1440+
expect(exp.match('POST', '/url')).toBe(true);
1441+
expect(exp.match('POST', '/not-accepted')).toBe(false);
1442+
});
1443+
1444+
14341445
it('should accept data as regexp', function() {
14351446
var exp = new MockHttpExpectation('POST', '/url', /\{.*?\}/);
14361447

0 commit comments

Comments
 (0)