diff options
| author | Igor Minar | 2011-06-29 00:25:13 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-06-30 00:34:50 -0700 |
| commit | c5f3a413bc00acf9ac1046fb15b454096a8890c6 (patch) | |
| tree | 9000f1f5f377d5f022aa8e338dfe810dc913d282 /test | |
| parent | d3fb5b411e979d0a4815c663c3489652fc5350f9 (diff) | |
| download | angular.js-c5f3a413bc00acf9ac1046fb15b454096a8890c6.tar.bz2 | |
feat:$xhr: provide access to $xhr header defaults
$xhr header defaults are now exposed as $xhr.defaults.headers.common and
$xhr.default.headers.<httpmethod>. This allows applications to configure
their defaults as needed.
This commit doesn't allow headers to be set per request, only per
application. Per request change would require api change, which I tried
to avoid *for now*.
Diffstat (limited to 'test')
| -rw-r--r-- | test/BrowserSpecs.js | 59 | ||||
| -rw-r--r-- | test/service/xhrSpec.js | 125 |
2 files changed, 144 insertions, 40 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js index 08756904..cb59137e 100644 --- a/test/BrowserSpecs.js +++ b/test/BrowserSpecs.js @@ -100,31 +100,6 @@ describe('browser', function(){ }); }); - it('should set headers for all requests', function(){ - var code, response, headers = {}; - browser.xhr('GET', 'URL', 'POST', function(c,r){ - code = c; - response = r; - }, {'X-header': 'value'}); - - expect(xhr.method).toEqual('GET'); - expect(xhr.url).toEqual('URL'); - expect(xhr.post).toEqual('POST'); - expect(xhr.headers).toEqual({ - "Accept": "application/json, text/plain, */*", - "X-Requested-With": "XMLHttpRequest", - "X-header":"value" - }); - - xhr.status = 202; - xhr.responseText = 'RESPONSE'; - xhr.readyState = 4; - xhr.onreadystatechange(); - - expect(code).toEqual(202); - expect(response).toEqual('RESPONSE'); - }); - it('should normalize IE\'s 1223 status code into 204', function() { var callback = jasmine.createSpy('XHR'); @@ -138,24 +113,28 @@ describe('browser', function(){ expect(callback.argsForCall[0][0]).toEqual(204); }); - it('should not set Content-type header for GET requests', function() { - browser.xhr('GET', 'URL', 'POST-DATA', function(c, r) {}); - - expect(xhr.headers['Content-Type']).not.toBeDefined(); - }); - - it('should set Content-type header for POST requests', function() { - browser.xhr('POST', 'URL', 'POST-DATA', function(c, r) {}); + it('should set only the requested headers', function() { + var code, response, headers = {}; + browser.xhr('POST', 'URL', null, function(c,r){ + code = c; + response = r; + }, {'X-header1': 'value1', 'X-header2': 'value2'}); - expect(xhr.headers['Content-Type']).toBeDefined(); - expect(xhr.headers['Content-Type']).toEqual('application/x-www-form-urlencoded'); - }); + expect(xhr.method).toEqual('POST'); + expect(xhr.url).toEqual('URL'); + expect(xhr.post).toEqual(''); + expect(xhr.headers).toEqual({ + "X-header1":"value1", + "X-header2":"value2" + }); - it('should set default headers for custom methods', function() { - browser.xhr('CUSTOM', 'URL', 'POST-DATA', function(c, r) {}); + xhr.status = 202; + xhr.responseText = 'RESPONSE'; + xhr.readyState = 4; + xhr.onreadystatechange(); - expect(xhr.headers['Accept']).toEqual('application/json, text/plain, */*'); - expect(xhr.headers['X-Requested-With']).toEqual('XMLHttpRequest'); + expect(code).toEqual(202); + expect(response).toEqual('RESPONSE'); }); }); diff --git a/test/service/xhrSpec.js b/test/service/xhrSpec.js index ebcd90d4..1f31bb6f 100644 --- a/test/service/xhrSpec.js +++ b/test/service/xhrSpec.js @@ -102,6 +102,131 @@ describe('$xhr', function() { expect(response).toEqual([1, 'abc', {foo:'bar'}]); }); + + describe('http headers', function() { + + describe('default headers', function() { + + it('should set default headers for GET request', function(){ + var callback = jasmine.createSpy('callback'); + + $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest'}). + respond(234, 'OK'); + + $xhr('GET', 'URL', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + + + it('should set default headers for POST request', function(){ + var callback = jasmine.createSpy('callback'); + + $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded'}). + respond(200, 'OK'); + + $xhr('POST', 'URL', 'xx', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + + + it('should set default headers for custom HTTP method', function(){ + var callback = jasmine.createSpy('callback'); + + $browserXhr.expect('FOO', 'URL', '', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest'}). + respond(200, 'OK'); + + $xhr('FOO', 'URL', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + + + describe('custom headers', function() { + + it('should allow appending a new header to the common defaults', function() { + var callback = jasmine.createSpy('callback'); + + $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest', + 'Custom-Header': 'value'}). + respond(200, 'OK'); + + $xhr.defaults.headers.common['Custom-Header'] = 'value'; + $xhr('GET', 'URL', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + callback.reset(); + + $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded', + 'Custom-Header': 'value'}). + respond(200, 'OK'); + + $xhr('POST', 'URL', 'xx', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + + + it('should allow appending a new header to a method specific defaults', function() { + var callback = jasmine.createSpy('callback'); + + $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/json'}). + respond(200, 'OK'); + + $xhr.defaults.headers.get['Content-Type'] = 'application/json'; + $xhr('GET', 'URL', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + callback.reset(); + + $browserXhr.expectPOST('URL', 'x', {'Accept': 'application/json, text/plain, */*', + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded'}). + respond(200, 'OK'); + + $xhr('POST', 'URL', 'x', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + + + it('should support overwriting and deleting default headers', function() { + var callback = jasmine.createSpy('callback'); + + $browserXhr.expectGET('URL', '', {'Accept': 'application/json, text/plain, */*'}). + respond(200, 'OK'); + + //delete a default header + delete $xhr.defaults.headers.common['X-Requested-With']; + $xhr('GET', 'URL', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + callback.reset(); + + $browserXhr.expectPOST('URL', 'xx', {'Accept': 'application/json, text/plain, */*', + 'Content-Type': 'application/json'}). + respond(200, 'OK'); + + //overwrite a default header + $xhr.defaults.headers.post['Content-Type'] = 'application/json'; + $xhr('POST', 'URL', 'xx', callback); + $browserXhr.flush(); + expect(callback).toHaveBeenCalled(); + }); + }); + }); + }); + describe('xsrf', function(){ it('should copy the XSRF cookie into a XSRF Header', function(){ var code, response; |
