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

Fix confusing behaviour of ngResource callbacks #6865

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,18 +551,36 @@ angular.module('ngResource', ['ng']).
}, function(response) {
value.$resolved = true;

(error||noop)(response);

return $q.reject(response);
});

promise = promise.then(
function(response) {
var value = responseInterceptor(response);
(success||noop)(value, response.headers);

$q.when(value).then(function(value) {
(success||noop)(value, response.headers);
}, function(response) {
(error||noop)(response);
});

return value;
},
responseErrorInterceptor);
function(response) {
if (responseErrorInterceptor) {
response = responseErrorInterceptor(response);
} else {
response = $q.reject(response);
}

$q.when(response).then(function(value) {
(success||noop)(value, response.headers);
}, function(response) {
(error||noop)(response);
});

return response;
});

if (!isInstanceCall) {
// we are creating instance / collection
Expand Down
106 changes: 106 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,112 @@ describe("resource", function() {
expect(response.status).toBe(404);
expect(response.config).toBeDefined();
});

describe('when the response interceptor returns a promise', function() {
var makeCreditCard, $q;

beforeEach(inject(function(_$q_) {
makeCreditCard = function(interceptor) {
return $resource('/CreditCard', {}, {
query: {
method: 'get',
isArray: true,
interceptor: interceptor
}
});
};

$q = _$q_;

$httpBackend.expect('GET', '/CreditCard').respond([{id: 1}]);
}));

it('should call the success callback with the resolution value of the promise', function() {
CreditCard = makeCreditCard({
response: function(response) {
return $q.when(response.data);
}
});

CreditCard.query(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();

var response = callback.mostRecentCall.args[0];
expect(response.then).not.toBeDefined();
expect(response[0].id).toEqual(1);
});

it('should call the error callback with the rejection value of the promise', function() {
CreditCard = makeCreditCard({
response: function(response) {
return $q.reject(response);
}
});

CreditCard.query(function() {}, callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();

var response = callback.mostRecentCall.args[0];
expect(response.then).not.toBeDefined();
expect(response.status).toEqual(200);
expect(response.config).toBeDefined();
});
});

describe('when the responseError interceptor returns a promise', function() {
var makeCreditCard, $q;

beforeEach(inject(function(_$q_) {
makeCreditCard = function(interceptor) {
return $resource('/CreditCard', {}, {
query: {
method: 'get',
isArray: true,
interceptor: interceptor
}
});
};

$q = _$q_;

$httpBackend.expect('GET', '/CreditCard').respond(404);
}));

it('should call the success callback with the resolution value of the promise', function() {
CreditCard = makeCreditCard({
responseError: function(response) {
return $q.when({id: 1});
}
});

CreditCard.query(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();

var response = callback.mostRecentCall.args[0];
expect(response.then).not.toBeDefined();
expect(response.id).toEqual(1);
});

it('should call the error callback with the rejection value of the promise', function() {
CreditCard = makeCreditCard({
responseError: function(response) {
return $q.reject(response);
}
});

CreditCard.query(function() {}, callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledOnce();

var response = callback.mostRecentCall.args[0];
expect(response.then).not.toBeDefined();
expect(response.status).toEqual(404);
expect(response.config).toBeDefined();
});
});
});


Expand Down