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

Commit 08daa77

Browse files
kenspiritpetebacondarwin
authored andcommitted
feat(ngMock/$httpBackend): support a matching function for data param
Add support for passing function as validating data: - To avoid hacking test method of RegExp - Optionally overwrite `toString` method of fn to show validation tips - change docs: param description for `when`, `whenPost`, `whenPut`, `expect`, `expectPost`, `expectPut`, `expectPATCH` Closes: #2981
1 parent ac5105b commit 08daa77

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/ngMock/angular-mocks.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11231123
*
11241124
* @param {string} method HTTP method.
11251125
* @param {string|RegExp} url HTTP url.
1126-
* @param {(string|RegExp)=} data HTTP request body.
1126+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1127+
* data string and returns true if the data is as expected.
11271128
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
11281129
* object and returns true if the headers match the current definition.
11291130
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
@@ -1199,7 +1200,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11991200
* Creates a new backend definition for POST requests. For more info see `when()`.
12001201
*
12011202
* @param {string|RegExp} url HTTP url.
1202-
* @param {(string|RegExp)=} data HTTP request body.
1203+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1204+
* data string and returns true if the data is as expected.
12031205
* @param {(Object|function(Object))=} headers HTTP headers.
12041206
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12051207
* request is handled.
@@ -1213,7 +1215,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12131215
* Creates a new backend definition for PUT requests. For more info see `when()`.
12141216
*
12151217
* @param {string|RegExp} url HTTP url.
1216-
* @param {(string|RegExp)=} data HTTP request body.
1218+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1219+
* data string and returns true if the data is as expected.
12171220
* @param {(Object|function(Object))=} headers HTTP headers.
12181221
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
12191222
* request is handled.
@@ -1242,7 +1245,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
12421245
*
12431246
* @param {string} method HTTP method.
12441247
* @param {string|RegExp} url HTTP url.
1245-
* @param {(string|RegExp)=} data HTTP request body.
1248+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1249+
* data string and returns true if the data is as expected.
12461250
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
12471251
* object and returns true if the headers match the current expectation.
12481252
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
@@ -1311,7 +1315,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13111315
* Creates a new request expectation for POST requests. For more info see `expect()`.
13121316
*
13131317
* @param {string|RegExp} url HTTP url.
1314-
* @param {(string|RegExp)=} data HTTP request body.
1318+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1319+
* data string and returns true if the data is as expected.
13151320
* @param {Object=} headers HTTP headers.
13161321
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13171322
* request is handled.
@@ -1325,7 +1330,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13251330
* Creates a new request expectation for PUT requests. For more info see `expect()`.
13261331
*
13271332
* @param {string|RegExp} url HTTP url.
1328-
* @param {(string|RegExp)=} data HTTP request body.
1333+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1334+
* data string and returns true if the data is as expected.
13291335
* @param {Object=} headers HTTP headers.
13301336
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13311337
* request is handled.
@@ -1339,7 +1345,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13391345
* Creates a new request expectation for PATCH requests. For more info see `expect()`.
13401346
*
13411347
* @param {string|RegExp} url HTTP url.
1342-
* @param {(string|RegExp)=} data HTTP request body.
1348+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1349+
* data string and returns true if the data is as expected.
13431350
* @param {Object=} headers HTTP headers.
13441351
* @returns {requestHandler} Returns an object with `respond` method that control how a matched
13451352
* request is handled.
@@ -1492,6 +1499,7 @@ function MockHttpExpectation(method, url, data, headers) {
14921499
this.matchData = function(d) {
14931500
if (angular.isUndefined(data)) return true;
14941501
if (data && angular.isFunction(data.test)) return data.test(d);
1502+
if (data && angular.isFunction(data)) return data(d);
14951503
if (data && !angular.isString(data)) return angular.toJson(data) == d;
14961504
return data == d;
14971505
};

test/ngMock/angular-mocksSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,19 @@ describe('ngMock', function() {
10371037
});
10381038

10391039

1040+
it('should accept data as function', function() {
1041+
var dataValidator = function(data) {
1042+
var json = angular.fromJson(data);
1043+
return !!json.id && json.status === 'N';
1044+
};
1045+
var exp = new MockHttpExpectation('POST', '/url', dataValidator);
1046+
1047+
expect(exp.matchData({})).toBe(false);
1048+
expect(exp.match('POST', '/url', '{"id": "xxx", "status": "N"}')).toBe(true);
1049+
expect(exp.match('POST', '/url', {"id": "xxx", "status": "N"})).toBe(true);
1050+
});
1051+
1052+
10401053
it('should ignore data only if undefined (not null or false)', function() {
10411054
var exp = new MockHttpExpectation('POST', '/url', null);
10421055
expect(exp.matchData(null)).toBe(true);

0 commit comments

Comments
 (0)