From 4ec1d8ee86e3138fb91543ca0dca28463895c090 Mon Sep 17 00:00:00 2001 From: Karl Seamon Date: Fri, 29 Jul 2011 18:32:30 -0400 Subject: feat($xhr,$resource): expose response headers in callbacks all $xhr*, $resource and related mocks now have access to headers from their callbacks --- test/service/xhrSpec.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'test/service/xhrSpec.js') diff --git a/test/service/xhrSpec.js b/test/service/xhrSpec.js index 9f496535..12ee614d 100644 --- a/test/service/xhrSpec.js +++ b/test/service/xhrSpec.js @@ -18,15 +18,16 @@ describe('$xhr', function() { }); - function callback(code, response) { - log = log + '{code=' + code + '; response=' + toJson(response) + '}'; + function callback(code, response, responseHeader) { + log = log + '{code=' + code + '; response=' + toJson(response) + '; responseHeaders=' + + toJson(responseHeader()) + '}'; } it('should forward the request to $browser and decode JSON', function(){ - $browserXhr.expectGET('/reqGET').respond('first'); - $browserXhr.expectGET('/reqGETjson').respond('["second"]'); - $browserXhr.expectPOST('/reqPOST', {post:'data'}).respond('third'); + $browserXhr.expectGET('/reqGET').respond('first', {h: 'first'}); + $browserXhr.expectGET('/reqGETjson').respond('["second"]', {h: 'second'}); + $browserXhr.expectPOST('/reqPOST', {post:'data'}).respond('third', {h: 'third'}); $xhr('GET', '/reqGET', null, callback); $xhr('GET', '/reqGETjson', null, callback); @@ -35,23 +36,23 @@ describe('$xhr', function() { $browserXhr.flush(); expect(log).toEqual( - '{code=200; response="third"}' + - '{code=200; response=["second"]}' + - '{code=200; response="first"}'); + '{code=200; response="third"; responseHeaders={"h":"third"}}' + + '{code=200; response=["second"]; responseHeaders={"h":"second"}}' + + '{code=200; response="first"; responseHeaders={"h":"first"}}'); }); it('should allow all 2xx requests', function(){ - $browserXhr.expectGET('/req1').respond(200, '1'); + $browserXhr.expectGET('/req1').respond(200, '1', {h: '1'}); $xhr('GET', '/req1', null, callback); $browserXhr.flush(); - $browserXhr.expectGET('/req2').respond(299, '2'); + $browserXhr.expectGET('/req2').respond(299, '2', {h: '2'}); $xhr('GET', '/req2', null, callback); $browserXhr.flush(); expect(log).toEqual( - '{code=200; response="1"}' + - '{code=299; response="2"}'); + '{code=200; response="1"; responseHeaders={"h":"1"}}' + + '{code=299; response="2"; responseHeaders={"h":"2"}}'); }); @@ -120,18 +121,24 @@ describe('$xhr', function() { var errorSpy = jasmine.createSpy('error'), successSpy = jasmine.createSpy('success'); - $browserXhr.expectGET('/url').respond(500, 'error'); + $browserXhr.expectGET('/url').respond(500, 'error', {foo: 'bar'}); $xhr('GET', '/url', null, successSpy, errorSpy); $browserXhr.flush(); - expect(errorSpy).toHaveBeenCalledWith(500, 'error'); + expect(errorSpy.mostRecentCall.args[0]).toEqual(500); + expect(errorSpy.mostRecentCall.args[1]).toEqual('error'); + expect(errorSpy.mostRecentCall.args[2]('foo')).toEqual('bar'); + expect(errorSpy.mostRecentCall.args[2]()).toEqual({foo: 'bar'}); expect(successSpy).not.toHaveBeenCalled(); errorSpy.reset(); $xhr('GET', '/url', successSpy, errorSpy); $browserXhr.flush(); - expect(errorSpy).toHaveBeenCalledWith(500, 'error'); + expect(errorSpy.mostRecentCall.args[0]).toEqual(500); + expect(errorSpy.mostRecentCall.args[1]).toEqual('error'); + expect(errorSpy.mostRecentCall.args[2]('foo')).toEqual('bar'); + expect(errorSpy.mostRecentCall.args[2]()).toEqual({foo: 'bar'}); expect(successSpy).not.toHaveBeenCalled(); }); -- cgit v1.2.3