From 0f73084e9d21cea99f0535e6ca30a1341b7047dc Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 19 May 2010 11:51:17 -0700 Subject: added error handler to xhr requests --- test/servicesSpec.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'test/servicesSpec.js') diff --git a/test/servicesSpec.js b/test/servicesSpec.js index 17f71bdc..4e144dd1 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -1,8 +1,11 @@ describe("service", function(){ - var scope; + var scope, xhrErrorHandler; beforeEach(function(){ - scope = createScope(null, angularService, {}); + xhrErrorHandler = jasmine.createSpy('$xhr.error'); + scope = createScope(null, angularService, { + '$xhr.error': xhrErrorHandler + }); }); afterEach(function(){ @@ -194,6 +197,17 @@ describe("service", function(){ expect(log).toEqual('"third";["second"];"first";'); }); + it('should handle non 200 status codes by forwarding to error handler', function(){ + xhr.expectPOST('/req', 'MyData').respond(500, 'MyError'); + scope.$xhr('POST', '/req', 'MyData', callback); + xhr.flush(); + var cb = xhrErrorHandler.mostRecentCall.args[0].callback; + expect(typeof cb).toEqual('function'); + expect(xhrErrorHandler).wasCalledWith( + {url:'/req', method:'POST', data:'MyData', callback:cb}, + {status:500, body:'MyError'}); + }); + describe('bulk', function(){ it('should collect requests', function(){ scope.$xhr.bulk.urls["/"] = {match:/.*/}; @@ -211,6 +225,31 @@ describe("service", function(){ xhr.flush(); expect(log).toEqual('"first";"second";DONE'); }); + + it('should handle non 200 status code by forwarding to error handler', function(){ + scope.$xhr.bulk.urls['/'] = {match:/.*/}; + scope.$xhr.bulk('GET', '/req1', null, callback); + scope.$xhr.bulk('POST', '/req2', {post:'data'}, callback); + + xhr.expectPOST('/', { + requests:[{method:'GET', url:'/req1', data: null}, + {method:'POST', url:'/req2', data:{post:'data'} }] + }).respond([ + {status:404, response:'NotFound'}, + {status:200, response:'second'} + ]); + scope.$xhr.bulk.flush(function(){ log += 'DONE';}); + xhr.flush(); + + expect(xhrErrorHandler).wasCalled(); + var cb = xhrErrorHandler.mostRecentCall.args[0].callback; + expect(typeof cb).toEqual('function'); + expect(xhrErrorHandler).wasCalledWith( + {url:'/req1', method:'GET', data:null, callback:cb}, + {status:404, body:'NotFound'}); + + expect(log).toEqual('"second";DONE'); + }); }); describe('cache', function(){ -- cgit v1.2.3