diff options
| author | Karl Seamon | 2011-07-29 18:32:30 -0400 |
|---|---|---|
| committer | Igor Minar | 2011-08-19 01:20:45 -0700 |
| commit | 4ec1d8ee86e3138fb91543ca0dca28463895c090 (patch) | |
| tree | 9f7fff13f177317ff15f8804789a44576a151908 /test | |
| parent | c37bfde9eb31556ee1eb146795b0c1f1504a4a26 (diff) | |
| download | angular.js-4ec1d8ee86e3138fb91543ca0dca28463895c090.tar.bz2 | |
feat($xhr,$resource): expose response headers in callbacks
all $xhr*, $resource and related mocks now have access to headers from
their callbacks
Diffstat (limited to 'test')
| -rw-r--r-- | test/BrowserSpecs.js | 42 | ||||
| -rw-r--r-- | test/ResourceSpec.js | 42 | ||||
| -rw-r--r-- | test/service/xhr.bulkSpec.js | 7 | ||||
| -rw-r--r-- | test/service/xhr.cacheSpec.js | 35 | ||||
| -rw-r--r-- | test/service/xhrSpec.js | 37 |
5 files changed, 124 insertions, 39 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js index 92e4e501..93882c1a 100644 --- a/test/BrowserSpecs.js +++ b/test/BrowserSpecs.js @@ -49,6 +49,13 @@ describe('browser', function(){ this.send = function(post){ xhr.post = post; }; + this.getResponseHeader = function(header) { + return header; + }; + this.getAllResponseHeaders = function() { + return 'Content-Type: application/json\n\rContent-Encoding: gzip\n\rContent-Type: text/json'; + } + }; logs = {log:[], warn:[], info:[], error:[]}; @@ -198,6 +205,41 @@ describe('browser', function(){ expect(code).toEqual(202); expect(response).toEqual('RESPONSE'); }); + + describe('response headers', function() { + it('should return a single response header', function() { + var headerA; + + browser.xhr('GET', 'URL', null, function(code, resp, headers) { + headerA = headers('A-Header'); + }); + + xhr.status = 200; + xhr.responseText = 'RESPONSE'; + xhr.readyState = 4; + xhr.onreadystatechange(); + + expect(headerA).toEqual('a-header'); + }); + + it('should return an object containing all response headers', function() { + var allHeaders; + + browser.xhr('GET', 'URL', null, function(code, resp, headers) { + allHeaders = headers(); + }); + + xhr.status = 200; + xhr.responseText = 'RESPONSE'; + xhr.readyState = 4; + xhr.onreadystatechange(); + + expect(allHeaders).toEqual({ + 'content-type': 'application/json, text/json', + 'content-encoding': 'gzip' + }); + }); + }); }); describe('defer', function() { diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index 7bba9357..a143fc0e 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -83,7 +83,8 @@ describe("resource", function() { expect(callback).not.toHaveBeenCalled(); xhr.flush(); nakedExpect(cc).toEqual({id:123, name:'misko'}); - expect(callback).toHaveBeenCalledWith(cc); + expect(callback.mostRecentCall.args[0]).toEqual(cc); + expect(callback.mostRecentCall.args[1]()).toEqual({}); }); it("should read resource", function(){ @@ -94,7 +95,8 @@ describe("resource", function() { expect(callback).not.toHaveBeenCalled(); xhr.flush(); nakedExpect(cc).toEqual({id:123, number:'9876'}); - expect(callback).toHaveBeenCalledWith(cc); + expect(callback.mostRecentCall.args[0]).toEqual(cc); + expect(callback.mostRecentCall.args[1]()).toEqual({}); }); it("should read partial resource", function(){ @@ -108,7 +110,8 @@ describe("resource", function() { expect(cc.number).not.toBeDefined(); cc.$get(callback); xhr.flush(); - expect(callback).toHaveBeenCalledWith(cc); + expect(callback.mostRecentCall.args[0]).toEqual(cc); + expect(callback.mostRecentCall.args[1]()).toEqual({}); expect(cc.number).toEqual('9876'); }); @@ -129,7 +132,8 @@ describe("resource", function() { expect(callback).not.toHaveBeenCalled(); xhr.flush(); nakedExpect(ccs).toEqual([{id:1}, {id:2}]); - expect(callback).toHaveBeenCalledWith(ccs); + expect(callback.mostRecentCall.args[0]).toEqual(ccs); + expect(callback.mostRecentCall.args[1]()).toEqual({}); }); it("should have all arguments optional", function(){ @@ -147,14 +151,16 @@ describe("resource", function() { CreditCard.remove({id:123}, callback); expect(callback).not.toHaveBeenCalled(); xhr.flush(); - nakedExpect(callback.mostRecentCall.args).toEqual([{}]); + nakedExpect(callback.mostRecentCall.args[0]).toEqual({}); + nakedExpect(callback.mostRecentCall.args[1]()).toEqual({}); callback.reset(); xhr.expectDELETE("/CreditCard/333").respond(204, null); CreditCard.remove({id:333}, callback); expect(callback).not.toHaveBeenCalled(); xhr.flush(); - nakedExpect(callback.mostRecentCall.args).toEqual([{}]); + nakedExpect(callback.mostRecentCall.args[0]).toEqual({}); + nakedExpect(callback.mostRecentCall.args[1]()).toEqual({}); }); it('should post charge verb', function(){ @@ -171,7 +177,7 @@ describe("resource", function() { }); it('should create on save', function(){ - xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id:123}); + xhr.expectPOST('/CreditCard', {name:'misko'}).respond({id: 123}, {foo: 'bar'}); var cc = new CreditCard(); expect(cc.$get).toBeDefined(); expect(cc.$query).toBeDefined(); @@ -183,7 +189,9 @@ describe("resource", function() { nakedExpect(cc).toEqual({name:'misko'}); xhr.flush(); nakedExpect(cc).toEqual({id:123}); - expect(callback).toHaveBeenCalledWith(cc); + expect(callback.mostRecentCall.args[0]).toEqual(cc); + expect(callback.mostRecentCall.args[1]('foo')).toEqual('bar'); + expect(callback.mostRecentCall.args[1]()).toEqual({foo: 'bar'}); }); it('should not mutate the resource object if response contains no body', function(){ @@ -245,10 +253,15 @@ describe("resource", function() { describe('failure mode', function() { var ERROR_CODE = 500, ERROR_RESPONSE = 'Server Error', - errorCB; + errorCB, + headersFn; beforeEach(function() { - errorCB = jasmine.createSpy(); + errorCB = jasmine.createSpy().andCallFake(function(code, response, headers) { + headersFn = headers; + }); + + xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE, {foo: 'bar'}); }); it('should report error when non 2xx if error callback is not provided', function() { @@ -262,16 +275,19 @@ describe("resource", function() { xhr.expectGET('/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE); CreditCard.get({id:123}, callback, errorCB); xhr.flush(); - expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE); + expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE, headersFn); expect(callback).not.toHaveBeenCalled(); expect($xhrErr).not.toHaveBeenCalled(); }); it('should call the error callback if provided on non 2xx response', function() { - xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE); + xhr.expectGET('/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE, {foo: 'bar'}); CreditCard.get(callback, errorCB); xhr.flush(); - expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE); + expect(errorCB).toHaveBeenCalledWith(500, ERROR_RESPONSE, headersFn); + expect(headersFn('foo')).toBe('bar'); + expect(headersFn()).toEqual({'foo': 'bar'}); + expect(callback).not.toHaveBeenCalled(); expect($xhrErr).not.toHaveBeenCalled(); }); diff --git a/test/service/xhr.bulkSpec.js b/test/service/xhr.bulkSpec.js index adcb61fa..c278fa50 100644 --- a/test/service/xhr.bulkSpec.js +++ b/test/service/xhr.bulkSpec.js @@ -18,8 +18,9 @@ describe('$xhr.bulk', function() { }); - function callback(code, response) { + function callback(code, response, responseHeaders) { expect(code).toEqual(200); + expect(responseHeaders()).toEqual({}); log = log + toJson(response) + ';'; } @@ -81,6 +82,8 @@ describe('$xhr.bulk', function() { $browserXhr.flush(); expect($xhrError).not.toHaveBeenCalled(); - expect(callback).toHaveBeenCalledWith(404, 'NotFound'); + expect(callback.mostRecentCall.args[0]).toEqual(404); + expect(callback.mostRecentCall.args[1]).toEqual('NotFound'); + expect(callback.mostRecentCall.args[2]()).toEqual({}); }); }); diff --git a/test/service/xhr.cacheSpec.js b/test/service/xhr.cacheSpec.js index f4654cd4..cd42153f 100644 --- a/test/service/xhr.cacheSpec.js +++ b/test/service/xhr.cacheSpec.js @@ -1,7 +1,7 @@ 'use strict'; describe('$xhr.cache', function() { - var scope, $browser, $browserXhr, $xhrErr, cache, log; + var scope, $browser, $browserXhr, $xhrErr, cache, log, rHeaders; beforeEach(function() { scope = angular.scope({}, null, {'$xhr.error': $xhrErr = jasmine.createSpy('$xhr.error')}); @@ -9,6 +9,7 @@ describe('$xhr.cache', function() { $browserXhr = $browser.xhr; cache = scope.$service('$xhr.cache'); log = ''; + rHeaders = {}; }); @@ -17,18 +18,21 @@ describe('$xhr.cache', function() { }); - function callback(code, response) { + function callback(code, response, responseHeaders) { expect(code).toEqual(200); + expect(responseHeaders()).toEqual(rHeaders); log = log + toJson(response) + ';'; } it('should cache requests', function(){ - $browserXhr.expectGET('/url').respond('first'); + rHeaders = {foo: 'bar'}; + + $browserXhr.expectGET('/url').respond('first', rHeaders); cache('GET', '/url', null, callback); $browserXhr.flush(); - $browserXhr.expectGET('/url').respond('ERROR'); + $browserXhr.expectGET('/url').respond('ERROR', rHeaders); cache('GET', '/url', null, callback); $browser.defer.flush(); expect(log).toEqual('"first";"first";'); @@ -40,11 +44,13 @@ describe('$xhr.cache', function() { it('should first return cache request, then return server request', function(){ - $browserXhr.expectGET('/url').respond('first'); + rHeaders = {foo: 'bar'}; + + $browserXhr.expectGET('/url').respond('first', rHeaders); cache('GET', '/url', null, callback, true); $browserXhr.flush(); - $browserXhr.expectGET('/url').respond('ERROR'); + $browserXhr.expectGET('/url').respond('ERROR', rHeaders); cache('GET', '/url', null, callback, true); $browser.defer.flush(); expect(log).toEqual('"first";"first";'); @@ -55,7 +61,14 @@ describe('$xhr.cache', function() { it('should serve requests from cache', function(){ - cache.data.url = {value:'123'}; + rHeaders = {foo: 'bar'}; + + cache.data.url = { + value: '123', + headers: function() { + return rHeaders; + } + }; cache('GET', 'url', null, callback); $browser.defer.flush(); expect(log).toEqual('"123";'); @@ -152,13 +165,17 @@ describe('$xhr.cache', function() { cache('GET', '/url', null, successSpy, errorSpy, false, true); $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]()).toEqual({}); expect(successSpy).not.toHaveBeenCalled(); errorSpy.reset(); cache('GET', '/url', successSpy, errorSpy, false, true); $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]()).toEqual({}); expect(successSpy).not.toHaveBeenCalled(); }); 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(); }); |
