feat($xhr): add custom error callback to $xhr, $xhr.cache, $xhr.bulk, $resource

Closes #408
This commit is contained in:
Karl Seamon
2011-07-22 15:56:45 -04:00
committed by Igor Minar
parent f39420e7d7
commit b5594a773a
10 changed files with 291 additions and 124 deletions

View File

@@ -1,12 +1,12 @@
'use strict';
describe("resource", function() {
var xhr, resource, CreditCard, callback;
var xhr, resource, CreditCard, callback, $xhrErr;
beforeEach(function(){
var browser = new MockBrowser();
xhr = browser.xhr;
resource = new ResourceFactory(xhr);
beforeEach(function() {
var scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')});
xhr = scope.$service('$browser').xhr;
resource = new ResourceFactory(scope.$service('$xhr'));
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
charge:{
method:'POST',
@@ -242,19 +242,25 @@ describe("resource", function() {
dealoc(scope);
});
describe('failure mode', function(){
it('should report error when non 200', function(){
xhr.expectGET('/CreditCard/123').respond(500, "Server Error");
var cc = CreditCard.get({id:123});
try {
xhr.flush();
fail('expected exception, non thrown');
} catch (e) {
expect(e.status).toEqual(500);
expect(e.response).toEqual('Server Error');
expect(e.message).toEqual('500: Server Error');
}
describe('failure mode', function() {
var ERROR_CODE = 500,
ERROR_RESPONSE = 'Server Error';
beforeEach(function() {
xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);
});
it('should report error when non 2xx if error callback is not provided', function() {
CreditCard.get({id:123});
xhr.flush();
expect($xhrErr).toHaveBeenCalled();
});
it('should call the error callback if provided on non 2xx response', function() {
CreditCard.get({id:123}, noop, callback);
xhr.flush();
expect(callback).toHaveBeenCalledWith(500, ERROR_RESPONSE);
expect($xhrErr).not.toHaveBeenCalled();
});
});
});

View File

@@ -4,13 +4,11 @@ describe('$xhr.bulk', function() {
var scope, $browser, $browserXhr, $log, $xhrBulk, $xhrError, log;
beforeEach(function(){
scope = angular.scope({}, angular.service, {
'$xhr.error': $xhrError = jasmine.createSpy('$xhr.error'),
'$log': $log = {}
});
scope = angular.scope({}, null, {'$xhr.error': $xhrError = jasmine.createSpy('$xhr.error')});
$browser = scope.$service('$browser');
$browserXhr = $browser.xhr;
$xhrBulk = scope.$service('$xhr.bulk');
$log = scope.$service('$log');
log = '';
});
@@ -60,12 +58,29 @@ describe('$xhr.bulk', function() {
$browserXhr.flush();
expect($xhrError).toHaveBeenCalled();
var cb = $xhrError.mostRecentCall.args[0].callback;
var cb = $xhrError.mostRecentCall.args[0].success;
expect(typeof cb).toEqual($function);
expect($xhrError).toHaveBeenCalledWith(
{url:'/req1', method:'GET', data:null, callback:cb},
{status:404, response:'NotFound'});
{url: '/req1', method: 'GET', data: null, success: cb},
{status: 404, response: 'NotFound'});
expect(log).toEqual('"second";DONE');
});
it('should handle non 200 status code by calling error callback if provided', function() {
var callback = jasmine.createSpy('error');
$xhrBulk.urls['/'] = {match: /.*/};
$xhrBulk('GET', '/req1', null, noop, callback);
$browserXhr.expectPOST('/', {
requests:[{method: 'GET', url: '/req1', data: null}]
}).respond([{status: 404, response: 'NotFound'}]);
$xhrBulk.flush();
$browserXhr.flush();
expect($xhrError).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledWith(404, 'NotFound');
});
});

View File

@@ -1,10 +1,10 @@
'use strict';
describe('$xhr.cache', function() {
var scope, $browser, $browserXhr, cache, log;
var scope, $browser, $browserXhr, $xhrErr, cache, log;
beforeEach(function(){
scope = angular.scope();
beforeEach(function() {
scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')});
$browser = scope.$service('$browser');
$browserXhr = $browser.xhr;
cache = scope.$service('$xhr.cache');
@@ -143,4 +143,36 @@ describe('$xhr.cache', function() {
$browser.defer.flush();
expect(evalSpy).toHaveBeenCalled();
});
it('should call the error callback on error if provided', function() {
var errorSpy = jasmine.createSpy('error'),
successSpy = jasmine.createSpy('success');
$browserXhr.expectGET('/url').respond(500, 'error');
cache('GET', '/url', null, successSpy, errorSpy, false, true);
$browserXhr.flush();
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
expect(successSpy).not.toHaveBeenCalled();
errorSpy.reset();
cache('GET', '/url', successSpy, errorSpy, false, true);
$browserXhr.flush();
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
expect(successSpy).not.toHaveBeenCalled();
});
it('should call the $xhr.error on error if error callback not provided', function() {
var errorSpy = jasmine.createSpy('error'),
successSpy = jasmine.createSpy('success');
$browserXhr.expectGET('/url').respond(500, 'error');
cache('GET', '/url', null, successSpy, false, true);
$browserXhr.flush();
expect(successSpy).not.toHaveBeenCalled();
expect($xhrErr).toHaveBeenCalledWith(
{method: 'GET', url: '/url', data: null, success: successSpy},
{status: 500, body: 'error'});
});
});

View File

@@ -29,10 +29,10 @@ describe('$xhr.error', function() {
$browserXhr.expectPOST('/req', 'MyData').respond(500, 'MyError');
$xhr('POST', '/req', 'MyData', callback);
$browserXhr.flush();
var cb = $xhrError.mostRecentCall.args[0].callback;
var cb = $xhrError.mostRecentCall.args[0].success;
expect(typeof cb).toEqual($function);
expect($xhrError).toHaveBeenCalledWith(
{url:'/req', method:'POST', data:'MyData', callback:cb},
{status:500, body:'MyError'});
{url: '/req', method: 'POST', data: 'MyData', success: cb},
{status: 500, body: 'MyError'});
});
});

View File

@@ -1,12 +1,11 @@
'use strict';
describe('$xhr', function() {
var scope, $browser, $browserXhr, $log, $xhr, log;
var scope, $browser, $browserXhr, $log, $xhr, $xhrErr, log;
beforeEach(function(){
scope = angular.scope({}, angular.service, { '$log': $log = {
error: dump
} });
var scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('xhr.error')});
$log = scope.$service('$log');
$browser = scope.$service('$browser');
$browserXhr = $browser.xhr;
$xhr = scope.$service('$xhr');
@@ -57,12 +56,11 @@ describe('$xhr', function() {
it('should handle exceptions in callback', function(){
$log.error = jasmine.createSpy('$log.error');
$browserXhr.expectGET('/reqGET').respond('first');
$xhr('GET', '/reqGET', null, function(){ throw "MyException"; });
$browserXhr.flush();
expect($log.error).toHaveBeenCalledWith("MyException");
expect($log.error.logs.shift()).toContain('MyException');
});
@@ -104,6 +102,38 @@ describe('$xhr', function() {
expect(response).toEqual([1, 'abc', {foo:'bar'}]);
});
it('should call $xhr.error on error if no error callback provided', function() {
var successSpy = jasmine.createSpy('success');
$browserXhr.expectGET('/url').respond(500, 'error');
$xhr('GET', '/url', null, successSpy);
$browserXhr.flush();
expect(successSpy).not.toHaveBeenCalled();
expect($xhrErr).toHaveBeenCalledWith(
{method: 'GET', url: '/url', data: null, success: successSpy},
{status: 500, body: 'error'}
);
});
it('should call the error callback on error if provided', function() {
var errorSpy = jasmine.createSpy('error'),
successSpy = jasmine.createSpy('success');
$browserXhr.expectGET('/url').respond(500, 'error');
$xhr('GET', '/url', null, successSpy, errorSpy);
$browserXhr.flush();
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
expect(successSpy).not.toHaveBeenCalled();
errorSpy.reset();
$xhr('GET', '/url', successSpy, errorSpy);
$browserXhr.flush();
expect(errorSpy).toHaveBeenCalledWith(500, 'error');
expect(successSpy).not.toHaveBeenCalled();
});
describe('http headers', function() {