@@ -97,6 +97,76 @@ describe('basic usage', function() {
9797 $httpBackend . flush ( ) ;
9898 } ) ;
9999
100+ it ( 'should include a request body when calling custom method with hasBody is true' , function ( ) {
101+ var instant = { name : 'info.txt' } ;
102+ var condition = { at : '2038-01-19 03:14:08' } ;
103+
104+ $httpBackend . expect ( 'CREATE' , '/fooresource' , instant ) . respond ( { fid : 42 } ) ;
105+ $httpBackend . expect ( 'DELETE' , '/fooresource' , condition ) . respond ( { } ) ;
106+
107+ var r = $resource ( '/fooresource' , { } , {
108+ create : { method : 'CREATE' , hasBody : true } ,
109+ delete : { method : 'DELETE' , hasBody : true }
110+ } ) ;
111+
112+ var creationResponse = r . create ( instant ) ;
113+ var deleteResponse = r . delete ( condition ) ;
114+
115+ $httpBackend . flush ( ) ;
116+
117+ expect ( creationResponse . fid ) . toBe ( 42 ) ;
118+ expect ( deleteResponse . $resolved ) . toBe ( true ) ;
119+ } ) ;
120+
121+ it ( 'should not include a request body if hasBody is false on POST, PUT and PATCH' , function ( ) {
122+ function verifyRequest ( method , url , data ) {
123+ expect ( data ) . toBeUndefined ( ) ;
124+ return [ 200 , { id : 42 } ] ;
125+ }
126+
127+ $httpBackend . expect ( 'POST' , '/foo' ) . respond ( verifyRequest ) ;
128+ $httpBackend . expect ( 'PUT' , '/foo' ) . respond ( verifyRequest ) ;
129+ $httpBackend . expect ( 'PATCH' , '/foo' ) . respond ( verifyRequest ) ;
130+
131+ var R = $resource ( '/foo' , { } , {
132+ post : { method : 'POST' , hasBody : false } ,
133+ put : { method : 'PUT' , hasBody : false } ,
134+ patch : { method : 'PATCH' , hasBody : false }
135+ } ) ;
136+
137+ var postResponse = R . post ( ) ;
138+ var putResponse = R . put ( ) ;
139+ var patchResponse = R . patch ( ) ;
140+
141+ $httpBackend . flush ( ) ;
142+
143+ expect ( postResponse . id ) . toBe ( 42 ) ;
144+ expect ( putResponse . id ) . toBe ( 42 ) ;
145+ expect ( patchResponse . id ) . toBe ( 42 ) ;
146+ } ) ;
147+
148+ it ( 'should expect a body if hasBody is true' , function ( ) {
149+ var username = 'yathos' ;
150+ var loginRequest = { name : username , password : 'Smile' } ;
151+ var user = { id : 1 , name : username } ;
152+
153+ $httpBackend . expect ( 'LOGIN' , '/user/me' , loginRequest ) . respond ( user ) ;
154+
155+ $httpBackend . expect ( 'LOGOUT' , '/user/me' , null ) . respond ( null ) ;
156+
157+ var UserService = $resource ( '/user/me' , { } , {
158+ login : { method : 'LOGIN' , hasBody : true } ,
159+ logout : { method : 'LOGOUT' , hasBody : false }
160+ } ) ;
161+
162+ var loginResponse = UserService . login ( loginRequest ) ;
163+ var logoutResponse = UserService . logout ( ) ;
164+
165+ $httpBackend . flush ( ) ;
166+
167+ expect ( loginResponse . id ) . toBe ( user . id ) ;
168+ expect ( logoutResponse . $resolved ) . toBe ( true ) ;
169+ } ) ;
100170
101171 it ( 'should build resource' , function ( ) {
102172 expect ( typeof CreditCard ) . toBe ( 'function' ) ;
0 commit comments