diff options
| author | Chirayu Krishnappa | 2013-11-08 20:44:32 -0800 |
|---|---|---|
| committer | Chirayu Krishnappa | 2013-11-11 16:17:34 -0800 |
| commit | 9577702e8d2519c1a60f5ac4058e63bd7b919815 (patch) | |
| tree | 69a063c50e1b575a77d86abec1587a74d07cc3fd /test/ngResource/resourceSpec.js | |
| parent | a61b65d01b468502fe53d68818949d3fcc9f20f6 (diff) | |
| download | angular.js-9577702e8d2519c1a60f5ac4058e63bd7b919815.tar.bz2 | |
fix($resource): don't use $parse for @dotted.member
params and paramDefaults support looking up the parameter value from the
data object. The syntax for that is `@nested.property.name`.
Currently, $resource uses $parse to do this. This is too liberal
(you can use values like `@a=b` or `@a | filter` and have it work -
which doesn't really make sense). It also puts up a dependency on
$parse which is has restrictions to secure expressions used in
templates. The value here, though a string, is specified in Javascript
code and shouldn't have those restrictions.
Diffstat (limited to 'test/ngResource/resourceSpec.js')
| -rw-r--r-- | test/ngResource/resourceSpec.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index d13156b3..5b75c8cf 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -31,6 +31,48 @@ describe("resource", function() { $httpBackend.verifyNoOutstandingExpectation(); }); + describe('isValidDottedPath', function() { + it('should support arbitrary dotted names', function() { + expect(isValidDottedPath('')).toBe(false); + expect(isValidDottedPath('1')).toBe(false); + expect(isValidDottedPath('1abc')).toBe(false); + expect(isValidDottedPath('.')).toBe(false); + expect(isValidDottedPath('$')).toBe(true); + expect(isValidDottedPath('a')).toBe(true); + expect(isValidDottedPath('A')).toBe(true); + expect(isValidDottedPath('a1')).toBe(true); + expect(isValidDottedPath('$a')).toBe(true); + expect(isValidDottedPath('$1')).toBe(true); + expect(isValidDottedPath('$$')).toBe(true); + expect(isValidDottedPath('$.$')).toBe(true); + expect(isValidDottedPath('.$')).toBe(false); + expect(isValidDottedPath('$.')).toBe(false); + }); + }); + + describe('lookupDottedPath', function() { + var data = {a: {b: 'foo', c: null}}; + + it('should throw for invalid path', function() { + expect(function() { + lookupDottedPath(data, '.ckck') + }).toThrowMinErr('$resource', 'badmember', + 'Dotted member path "@.ckck" is invalid.'); + }); + + it('should get dotted paths', function() { + expect(lookupDottedPath(data, 'a')).toEqual({b: 'foo', c: null}); + expect(lookupDottedPath(data, 'a.b')).toBe('foo'); + expect(lookupDottedPath(data, 'a.c')).toBeNull(); + }); + + it('should skip over null/undefined members', function() { + expect(lookupDottedPath(data, 'a.b.c')).toBe(undefined); + expect(lookupDottedPath(data, 'a.c.c')).toBe(undefined); + expect(lookupDottedPath(data, 'a.b.c.d')).toBe(undefined); + expect(lookupDottedPath(data, 'NOT_EXIST')).toBe(undefined); + }); + }); it('should not include a request body when calling $delete', function() { $httpBackend.expect('DELETE', '/fooresource', null).respond({}); @@ -189,6 +231,19 @@ describe("resource", function() { }); + it('should support @_property lookups with underscores', function() { + $httpBackend.expect('GET', '/Order/123').respond({_id: {_key:'123'}, count: 0}); + var LineItem = $resource('/Order/:_id', {_id: '@_id._key'}); + var item = LineItem.get({_id: 123}); + $httpBackend.flush(); + expect(item).toEqualData({_id: {_key: '123'}, count: 0}); + $httpBackend.expect('POST', '/Order/123').respond({_id: {_key:'123'}, count: 1}); + item.$save(); + $httpBackend.flush(); + expect(item).toEqualData({_id: {_key: '123'}, count: 1}); + }); + + it('should not pass default params between actions', function() { var R = $resource('/Path', {}, {get: {method: 'GET', params: {objId: '1'}}, perform: {method: 'GET'}}); |
