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

fix(resource): check whether response matches action.isArray #3400

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/content/error/ngResource/badcfg.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ngdoc error
@name ngResource:badcfg
@fullName Response does not match configured parameter
@description
4 changes: 2 additions & 2 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ function qFactory(nextTick, exceptionHandler) {
try {
result.resolve((callback || defaultCallback)(value));
} catch(e) {
exceptionHandler(e);
result.reject(e);
exceptionHandler(e);
}
};

var wrappedErrback = function(reason) {
try {
result.resolve((errback || defaultErrback)(reason));
} catch(e) {
exceptionHandler(e);
result.reject(e);
exceptionHandler(e);
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,11 @@ angular.module('ngResource', ['ng']).
promise = value.$promise;

if (data) {
if ( angular.isArray(data) != !!action.isArray ) {
throw ngResourceMinErr('badcfg', 'Error in resource configuration. Expected response' +
' to contain an {0} but got an {1}',
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
}
if (action.isArray) {
value.length = 0;
forEach(data, function(item) {
Expand Down
51 changes: 50 additions & 1 deletion test/ng/qSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ describe('q', function() {
mockNextTick.queue.push(task);
},
queue: [],
logExceptions: true,
flush: function() {
if (!mockNextTick.queue.length) throw new Error('Nothing to be flushed!');
while (mockNextTick.queue.length) {
Expand All @@ -169,7 +170,9 @@ describe('q', function() {
try {
task();
} catch(e) {
dump('exception in mockNextTick:', e, e.name, e.message, task);
if ( mockNextTick.logExceptions ) {
dump('exception in mockNextTick:', e, e.name, e.message, task);
}
}
});
}
Expand Down Expand Up @@ -1393,4 +1396,50 @@ describe('q', function() {
});
});
});


describe('when exceptionHandler rethrows exceptions, ', function() {
var originalLogExceptions, deferred, errorSpy, exceptionExceptionSpy;

beforeEach(function() {
// Turn off exception logging for these particular tests
originalLogExceptions = mockNextTick.logExceptions;
mockNextTick.logExceptions = false;

// Set up spies
exceptionExceptionSpy = jasmine.createSpy('rethrowExceptionHandler')
.andCallFake(function rethrowExceptionHandler(e) {
throw e;
});
errorSpy = jasmine.createSpy('errorSpy');


q = qFactory(mockNextTick.nextTick, exceptionExceptionSpy);
deferred = q.defer();
});


afterEach(function() {
// Restore the original exception logging mode
mockNextTick.logExceptions = originalLogExceptions;
});


it('should still reject the promise, when exception is thrown in success handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(function() { throw 'reject'; }).then(null, errorSpy);
deferred.resolve('resolve');
mockNextTick.flush();
expect(exceptionExceptionSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});


it('should still reject the promise, when exception is thrown in success handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(null, function() { throw 'reject again'; }).then(null, errorSpy);
deferred.reject('reject');
mockNextTick.flush();
expect(exceptionExceptionSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});
});
});
49 changes: 48 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,6 @@ describe("resource", function() {
});
});


it('should transform request/response', function() {
var Person = $resource('/Person/:id', {}, {
save: {
Expand Down Expand Up @@ -1034,3 +1033,51 @@ describe("resource", function() {
});
});
});

describe('resource', function() {
var $httpBackend, $resource;

beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));

beforeEach(module('ngResource'));

beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$resource = $injector.get('$resource');
}));


it('should fail if action expects an object but response is an array', function() {
var successSpy = jasmine.createSpy('successSpy');
var failureSpy = jasmine.createSpy('failureSpy');

$httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'});

$resource('/Customer/123').query()
.$promise.then(successSpy, function(e) { failureSpy(e.message); });
$httpBackend.flush();

expect(successSpy).not.toHaveBeenCalled();
expect(failureSpy).toHaveBeenCalledWith(
'[ngResource:badcfg] Error in resource configuration. Expected response to contain an array but got an object');
});

it('should fail if action expects an array but response is an object', function() {
var successSpy = jasmine.createSpy('successSpy');
var failureSpy = jasmine.createSpy('failureSpy');

$httpBackend.expect('GET', '/Customer/123').respond([1,2,3]);

$resource('/Customer/123').get()
.$promise.then(successSpy, function(e) { failureSpy(e.message); });
$httpBackend.flush();

expect(successSpy).not.toHaveBeenCalled();
expect(failureSpy).toHaveBeenCalledWith(
'[ngResource:badcfg] Error in resource configuration. Expected response to contain an object but got an array');
});


});