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

Commit

Permalink
fix(angular-mocks): use copy of mock data in $httpBackend
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kseamon authored and btford committed Dec 11, 2013
1 parent f1a8d41 commit f69dc16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
30 changes: 28 additions & 2 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ describe('ngMock', function() {

describe('object literal format', function() {
var mock = { log: 'module' };

beforeEach(function() {
module({
'service': mock,
Expand All @@ -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');
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit f69dc16

Please sign in to comment.