aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarl Seamon2013-12-06 14:35:34 -0500
committerBrian Ford2013-12-11 15:18:45 -0800
commitf69dc16241c8b631123ad0b09674f0a5e0ff32fe (patch)
tree7c3845c1de4da3daa05977ecab657b0bc0c9835e
parentf1a8d419d529ced136441d0eae5f4dd474a8c8ff (diff)
downloadangular.js-f69dc16241c8b631123ad0b09674f0a5e0ff32fe.tar.bz2
fix(angular-mocks): use copy of mock data in $httpBackend
Copy mock data returned from the mock $httpBackend. This prevents modifications to the response from affecting future responses. Previously, this misbehavior was being mitigated by the deep copy in $resource, but that no longer exists.
-rw-r--r--src/ngMock/angular-mocks.js5
-rw-r--r--test/ngMock/angular-mocksSpec.js30
2 files changed, 31 insertions, 4 deletions
diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js
index 24b8d84b..d11ef0c1 100644
--- a/src/ngMock/angular-mocks.js
+++ b/src/ngMock/angular-mocks.js
@@ -1087,7 +1087,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
var definitions = [],
expectations = [],
responses = [],
- responsesPush = angular.bind(responses, responses.push);
+ responsesPush = angular.bind(responses, responses.push),
+ copy = angular.copy;
function createResponse(status, data, headers) {
if (angular.isFunction(status)) return status;
@@ -1119,7 +1120,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
function handleResponse() {
var response = wrapped.response(method, url, data, headers);
xhr.$$respHeaders = response[2];
- callback(response[0], response[1], xhr.getAllResponseHeaders());
+ callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders());
}
function handleTimeout() {
diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js
index f96112fe..ef7e88fa 100644
--- a/test/ngMock/angular-mocksSpec.js
+++ b/test/ngMock/angular-mocksSpec.js
@@ -766,7 +766,7 @@ describe('ngMock', function() {
describe('object literal format', function() {
var mock = { log: 'module' };
-
+
beforeEach(function() {
module({
'service': mock,
@@ -782,7 +782,7 @@ describe('ngMock', function() {
expect(service).toEqual(mock);
});
});
-
+
it('should support multiple key value pairs', function() {
inject(function(service, other) {
expect(other.some).toEqual('replacement');
@@ -891,6 +891,32 @@ describe('ngMock', function() {
});
+ it('should respond with a copy of the mock data', function() {
+ var mockObject = {a: 'b'};
+
+ hb.when('GET', '/url1').respond(200, mockObject, {});
+
+ callback.andCallFake(function(status, response) {
+ expect(status).toBe(200);
+ expect(response).toEqual({a: 'b'});
+ expect(response).not.toBe(mockObject);
+ response.a = 'c';
+ });
+
+ hb('GET', '/url1', null, callback);
+ hb.flush();
+ expect(callback).toHaveBeenCalledOnce();
+
+ // Fire it again and verify that the returned mock data has not been
+ // modified.
+ callback.reset();
+ hb('GET', '/url1', null, callback);
+ hb.flush();
+ expect(callback).toHaveBeenCalledOnce();
+ expect(mockObject).toEqual({a: 'b'});
+ });
+
+
it('should throw error when unexpected request', function() {
hb.when('GET', '/url1').respond(200, 'content');
expect(function() {