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

Commit 173db93

Browse files
committed
fixup! feat($resource): add support for request and requestError interceptors
1 parent 2131f06 commit 173db93

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

src/ngResource/resource.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ function shallowClearAndCopy(src, dst) {
187187
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
188188
* - **`interceptor`** - `{Object=}` - The interceptor object has four optional methods -
189189
* `request`, `requestError`, `response`, and `responseError`. See
190-
* {@link ng.$http $http interceptors} for details. In addition, the
191-
* resource instance or array object is accessible by the `resource` property of the
190+
* {@link ng.$http $http interceptors} for details. Note that `request`/`requestError`
191+
* interceptors are applied before calling `$http`, thus before any global `$http` interceptors.
192+
* The resource instance or array object is accessible by the `resource` property of the
192193
* `http response` object passed to response interceptors.
193194
* Keep in mind that the associated promise will be resolved with the value returned by the
194195
* response interceptor, if one is specified. The default response interceptor returns

test/ngResource/resourceSpec.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,9 @@ describe('basic usage', function() {
11891189
resource.$promise.then(successSpy, failureSpy);
11901190

11911191
$httpBackend.flush();
1192-
expect(successSpy).toHaveBeenCalledOnce();
1192+
expect(successSpy).toHaveBeenCalledOnceWith(jasmine.arrayContaining([
1193+
jasmine.objectContaining({id: 1})
1194+
]));
11931195
expect(failureSpy).not.toHaveBeenCalled();
11941196
});
11951197

@@ -1215,8 +1217,7 @@ describe('basic usage', function() {
12151217
// Ensure the resource promise was rejected
12161218
expect(resource.$resolved).toBeTruthy();
12171219
expect(successSpy).not.toHaveBeenCalled();
1218-
expect(failureSpy).toHaveBeenCalledOnce();
1219-
expect(failureSpy).toHaveBeenCalledWith(rejectReason);
1220+
expect(failureSpy).toHaveBeenCalledOnceWith(rejectReason);
12201221

12211222
// Ensure that no requests were made.
12221223
$httpBackend.verifyNoOutstandingRequest();
@@ -1243,44 +1244,41 @@ describe('basic usage', function() {
12431244
resource.$promise.then(successSpy, failureSpy);
12441245
$rootScope.$digest();
12451246

1246-
expect(callback).toHaveBeenCalledOnce();
1247-
expect(callback).toHaveBeenCalledWith(rejectReason);
1247+
expect(callback).toHaveBeenCalledOnceWith(rejectReason);
12481248
expect(successSpy).not.toHaveBeenCalled();
1249-
expect(failureSpy).toHaveBeenCalledOnce();
1250-
expect(failureSpy).toHaveBeenCalledWith(rejectReason);
1249+
expect(failureSpy).toHaveBeenCalledOnceWith(rejectReason);
12511250

12521251
// Ensure that no requests were made.
12531252
$httpBackend.verifyNoOutstandingRequest();
12541253
});
12551254

12561255
it('should abort the operation if a requestErrorInterceptor rejects the operation', function() {
1257-
var CreditCard = $resource('/CreditCard', {}, {
1258-
query: {
1259-
method: 'get',
1260-
isArray: true,
1261-
interceptor: {
1262-
request: function() {
1263-
return $q.reject(rejectReason);
1264-
},
1265-
requestError: function(rejection) {
1266-
return $q.reject(rejection);
1267-
}
1256+
var CreditCard = $resource('/CreditCard', {}, {
1257+
query: {
1258+
method: 'get',
1259+
isArray: true,
1260+
interceptor: {
1261+
request: function() {
1262+
return $q.reject(rejectReason);
1263+
},
1264+
requestError: function(rejection) {
1265+
return $q.reject(rejection);
12681266
}
12691267
}
1270-
});
1268+
}
1269+
});
12711270

1272-
var resource = CreditCard.query();
1273-
resource.$promise.then(successSpy, failureSpy);
1274-
$rootScope.$apply();
1271+
var resource = CreditCard.query();
1272+
resource.$promise.then(successSpy, failureSpy);
1273+
$rootScope.$apply();
12751274

1276-
expect(resource.$resolved).toBeTruthy();
1277-
expect(successSpy).not.toHaveBeenCalled();
1278-
expect(failureSpy).toHaveBeenCalledOnce();
1279-
expect(failureSpy).toHaveBeenCalledWith(rejectReason);
1275+
expect(resource.$resolved).toBeTruthy();
1276+
expect(successSpy).not.toHaveBeenCalled();
1277+
expect(failureSpy).toHaveBeenCalledOnceWith(rejectReason);
12801278

1281-
// Ensure that no requests were made.
1282-
$httpBackend.verifyNoOutstandingRequest();
1283-
});
1279+
// Ensure that no requests were made.
1280+
$httpBackend.verifyNoOutstandingRequest();
1281+
});
12841282

12851283
it('should continue the operation if a requestErrorInterceptor rescues it', function() {
12861284
var CreditCard = $resource('/CreditCard', {}, {
@@ -1305,8 +1303,11 @@ describe('basic usage', function() {
13051303
$httpBackend.flush();
13061304

13071305
expect(resource.$resolved).toBeTruthy();
1308-
expect(successSpy).toHaveBeenCalledOnce();
1306+
expect(successSpy).toHaveBeenCalledOnceWith(jasmine.arrayContaining([
1307+
jasmine.objectContaining({id: 1})
1308+
]));
13091309
expect(failureSpy).not.toHaveBeenCalled();
1310+
13101311
$httpBackend.verifyNoOutstandingRequest();
13111312
});
13121313
});
@@ -2038,7 +2039,7 @@ describe('handling rejections', function() {
20382039
failureSpy = jasmine.createSpy('failureSpy');
20392040
callback = jasmine.createSpy();
20402041
}));
2041-
2042+
20422043
it('should call requestErrorInterceptor if requestInterceptor throws an error', function() {
20432044
var CreditCard = $resource('/CreditCard', {}, {
20442045
query: {

0 commit comments

Comments
 (0)