From 60f1f099fc7e5197808cd6acb7407cdc40f50a3f Mon Sep 17 00:00:00 2001 From: zeflasher Date: Fri, 22 Feb 2013 15:49:26 +1300 Subject: feat($resource): ability to override url in resource actions Resources now can defined per action url override. The url is treated as a template rather than a literal string, so fancy interpolations are possible. See attached tests for example usage. --- test/ngResource/resourceSpec.js | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'test') diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index e5366f4f..11124739 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -703,4 +703,66 @@ describe("resource", function() { $httpBackend.flush(); expect(person.id).toEqual(456); }); + + + describe('action-level url override', function() { + + it('should support overriding url template with static url', function() { + $httpBackend.expect('GET', '/override-url?type=Customer&typeId=123').respond({id: 'abc'}); + var TypeItem = $resource('/:type/:typeId', {type: 'Order'}, { + get: { + method: 'GET', + params: {type: 'Customer'}, + url: '/override-url' + } + }); + var item = TypeItem.get({typeId: 123}); + $httpBackend.flush(); + expect(item).toEqualData({id: 'abc'}); + }); + + + it('should support overriding url template with a new template ending in param', function() { + // url parameter in action, parameter ending the string + $httpBackend.expect('GET', '/Customer/123').respond({id: 'abc'}); + var TypeItem = $resource('/foo/:type', {type: 'Order'}, { + get: { + method: 'GET', + params: {type: 'Customer'}, + url: '/:type/:typeId' + } + }); + var item = TypeItem.get({typeId: 123}); + $httpBackend.flush(); + expect(item).toEqualData({id: 'abc'}); + + // url parameter in action, parameter not ending the string + $httpBackend.expect('GET', '/Customer/123/pay').respond({id: 'abc'}); + var TypeItem = $resource('/foo/:type', {type: 'Order'}, { + get: { + method: 'GET', + params: {type: 'Customer'}, + url: '/:type/:typeId/pay' + } + }); + var item = TypeItem.get({typeId: 123}); + $httpBackend.flush(); + expect(item).toEqualData({id: 'abc'}); + }); + + + it('should support overriding url template with a new template ending in string', function() { + $httpBackend.expect('GET', '/Customer/123/pay').respond({id: 'abc'}); + var TypeItem = $resource('/foo/:type', {type: 'Order'}, { + get: { + method: 'GET', + params: {type: 'Customer'}, + url: '/:type/:typeId/pay' + } + }); + var item = TypeItem.get({typeId: 123}); + $httpBackend.flush(); + expect(item).toEqualData({id: 'abc'}); + }); + }); }); -- cgit v1.2.3