diff options
| author | Misko Hevery | 2010-05-19 11:51:17 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-05-19 11:51:17 -0700 |
| commit | 0f73084e9d21cea99f0535e6ca30a1341b7047dc (patch) | |
| tree | e4586731808a708ec0a8ce137c30e99e3cb7201b /test | |
| parent | 1bdcf72e456c74256b14f98b26e969b9de637614 (diff) | |
| download | angular.js-0f73084e9d21cea99f0535e6ca30a1341b7047dc.tar.bz2 | |
added error handler to xhr requests
Diffstat (limited to 'test')
| -rw-r--r-- | test/ResourceSpec.js | 15 | ||||
| -rw-r--r-- | test/angular-mocks.js | 10 | ||||
| -rw-r--r-- | test/servicesSpec.js | 43 |
3 files changed, 63 insertions, 5 deletions
diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index d2d52d47..2f285bcf 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -138,4 +138,19 @@ describe("resource", function() { expect(person.name).toEqual('misko'); }); + 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'); + } + }); + }); + }); diff --git a/test/angular-mocks.js b/test/angular-mocks.js index 6ae91596..c5784ac9 100644 --- a/test/angular-mocks.js +++ b/test/angular-mocks.js @@ -43,7 +43,7 @@ function MockBrowser() { throw "Unexepected request for method '" + method + "' and url '" + url + "'."; } requests.push(function(){ - callback(200, response); + callback(response.code, response.response); }); }; self.xhr.expectations = expectations; @@ -53,8 +53,12 @@ function MockBrowser() { if (data && angular.isString(data)) url += "|" + data; var expect = expectations[method] || (expectations[method] = {}); return { - respond: function(response) { - expect[url] = response; + respond: function(code, response) { + if (!isNumber(code)) { + response = code; + code = 200; + } + expect[url] = {code:code, response:response}; } }; }; 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(){ |
