diff options
| -rw-r--r-- | docs/content/error/ngResource/badcfg.ngdoc | 4 | ||||
| -rw-r--r-- | src/ngResource/resource.js | 5 | ||||
| -rw-r--r-- | test/ngResource/resourceSpec.js | 49 |
3 files changed, 57 insertions, 1 deletions
diff --git a/docs/content/error/ngResource/badcfg.ngdoc b/docs/content/error/ngResource/badcfg.ngdoc new file mode 100644 index 00000000..c27dc7c0 --- /dev/null +++ b/docs/content/error/ngResource/badcfg.ngdoc @@ -0,0 +1,4 @@ +@ngdoc error +@name ngResource:badcfg +@fullName Response does not match configured parameter +@description diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 92cbcd16..003b41f1 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -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) { diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 52395297..19456f01 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -807,7 +807,6 @@ describe("resource", function() { }); }); - it('should transform request/response', function() { var Person = $resource('/Person/:id', {}, { save: { @@ -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'); + }); + + +});
\ No newline at end of file |
