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

Commit 7193eae

Browse files
committed
feat(ngMocks): add $httpBackend.whenOverride method
This PR adds a new method to $httpBackend that allows to override any existing definition, by adding the new definition at the beginning of the definition list, not at the end of it, like $httpBackend.when does. Closes #11637
1 parent 05fdf91 commit 7193eae

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

src/ngMock/angular-mocks.js

+66-17
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,30 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14501450
throw error;
14511451
}
14521452

1453+
function when(method, url, data, headers, keys) {
1454+
var definition = new MockHttpExpectation(method, url, data, headers, keys),
1455+
chain = {
1456+
respond: function(status, data, headers, statusText) {
1457+
definition.passThrough = undefined;
1458+
definition.response = createResponse(status, data, headers, statusText);
1459+
return chain;
1460+
}
1461+
};
1462+
1463+
if ($browser) {
1464+
chain.passThrough = function() {
1465+
definition.response = undefined;
1466+
definition.passThrough = true;
1467+
return chain;
1468+
};
1469+
}
1470+
1471+
return {
1472+
definition: definition,
1473+
chain: chain
1474+
};
1475+
}
1476+
14531477
/**
14541478
* @ngdoc method
14551479
* @name $httpBackend#when
@@ -1482,25 +1506,48 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
14821506

14831507
assertArgDefined(arguments, 1, 'url');
14841508

1485-
var definition = new MockHttpExpectation(method, url, data, headers, keys),
1486-
chain = {
1487-
respond: function(status, data, headers, statusText) {
1488-
definition.passThrough = undefined;
1489-
definition.response = createResponse(status, data, headers, statusText);
1490-
return chain;
1491-
}
1492-
};
1509+
var mock = when(method, url, data, headers, keys);
14931510

1494-
if ($browser) {
1495-
chain.passThrough = function() {
1496-
definition.response = undefined;
1497-
definition.passThrough = true;
1498-
return chain;
1499-
};
1500-
}
1511+
definitions.push(mock.definition);
1512+
return mock.chain;
1513+
};
15011514

1502-
definitions.push(definition);
1503-
return chain;
1515+
/**
1516+
* @ngdoc method
1517+
* @name $httpBackend#whenOverride
1518+
* @description
1519+
* Creates a new backend definition that will override any already existing definitions with the same parameters
1520+
*
1521+
* @param {string} method HTTP method.
1522+
* @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
1523+
* and returns true if the url matches the current definition.
1524+
* @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
1525+
* data string and returns true if the data is as expected.
1526+
* @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
1527+
* object and returns true if the headers match the current definition.
1528+
* @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
1529+
* @returns {requestHandler} Returns an object with `respond` method that controls how a matched
1530+
* request is handled. You can save this object for later use and invoke `respond` again in
1531+
* order to change how a matched request is handled.
1532+
*
1533+
* - respond –
1534+
* ```js
1535+
* {function([status,] data[, headers, statusText])
1536+
* | function(function(method, url, data, headers, params)}
1537+
* ```
1538+
* – The respond method takes a set of static data to be returned or a function that can
1539+
* return an array containing response status (number), response data (Array|Object|string),
1540+
* response headers (Object), and the text for the status (string). The respond method returns
1541+
* the `requestHandler` object for possible overrides.
1542+
*/
1543+
$httpBackend.whenOverride = function(method, url, data, headers, keys) {
1544+
1545+
assertArgDefined(arguments, 1, 'url');
1546+
1547+
var mock = when(method, url, data, headers, keys);
1548+
1549+
definitions.unshift(mock.definition);
1550+
return mock.chain;
15041551
};
15051552

15061553
/**
@@ -1597,6 +1644,8 @@ function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
15971644
*/
15981645
createShortMethods('when');
15991646

1647+
createShortMethods('whenOverride');
1648+
16001649
/**
16011650
* @ngdoc method
16021651
* @name $httpBackend#whenRoute

test/ngMock/angular-mocksSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,16 @@ describe('ngMock', function() {
10841084
});
10851085

10861086

1087+
it('should provide "whenOverride" methods for each HTTP verb', function() {
1088+
expect(typeof hb.whenOverrideGET).toBe('function');
1089+
expect(typeof hb.whenOverridePOST).toBe('function');
1090+
expect(typeof hb.whenOverridePUT).toBe('function');
1091+
expect(typeof hb.whenOverridePATCH).toBe('function');
1092+
expect(typeof hb.whenOverrideDELETE).toBe('function');
1093+
expect(typeof hb.whenOverrideHEAD).toBe('function');
1094+
});
1095+
1096+
10871097
it('should provide "route" shortcuts for expect and when', function() {
10881098
expect(typeof hb.whenRoute).toBe('function');
10891099
expect(typeof hb.expectRoute).toBe('function');
@@ -1106,6 +1116,22 @@ describe('ngMock', function() {
11061116
});
11071117

11081118

1119+
it('should respond with last matched definition', function() {
1120+
hb.when('GET', '/url1').respond(200, 'content', {});
1121+
hb.whenOverride('GET', '/url1').respond(201, 'another', {});
1122+
1123+
callback.and.callFake(function(status, response) {
1124+
expect(status).toBe(201);
1125+
expect(response).toBe('another');
1126+
});
1127+
1128+
hb('GET', '/url1', null, callback);
1129+
expect(callback).not.toHaveBeenCalled();
1130+
hb.flush();
1131+
expect(callback).toHaveBeenCalledOnce();
1132+
});
1133+
1134+
11091135
it('should respond with a copy of the mock data', function() {
11101136
var mockObject = {a: 'b'};
11111137

0 commit comments

Comments
 (0)