diff options
| -rw-r--r-- | src/Browser.js | 15 | ||||
| -rw-r--r-- | test/BrowserSpecs.js | 25 |
2 files changed, 31 insertions, 9 deletions
diff --git a/src/Browser.js b/src/Browser.js index 55439762..b10c43cf 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -7,10 +7,14 @@ var XHR = window.XMLHttpRequest || function () { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {} throw new Error("This browser does not support XMLHttpRequest."); }; + +// default xhr headers var XHR_HEADERS = { - "Content-Type": "application/x-www-form-urlencoded", - "Accept": "application/json, text/plain, */*", - "X-Requested-With": "XMLHttpRequest" + DEFAULT: { + "Accept": "application/json, text/plain, */*", + "X-Requested-With": "XMLHttpRequest" + }, + POST: {'Content-Type': 'application/x-www-form-urlencoded'} }; /** @@ -103,8 +107,9 @@ function Browser(window, document, body, XHR, $log) { } else { var xhr = new XHR(); xhr.open(method, url, true); - forEach(extend(XHR_HEADERS, headers || {}), function(value, key){ - if (value) xhr.setRequestHeader(key, value); + forEach(extend({}, XHR_HEADERS.DEFAULT, XHR_HEADERS[uppercase(method)] || {}, headers || {}), + function(value, key) { + if (value) xhr.setRequestHeader(key, value); }); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js index 3b5a9ba0..5a18e1b9 100644 --- a/test/BrowserSpecs.js +++ b/test/BrowserSpecs.js @@ -101,16 +101,15 @@ describe('browser', function(){ it('should set headers for all requests', function(){ var code, response, headers = {}; - browser.xhr('METHOD', 'URL', 'POST', function(c,r){ + browser.xhr('GET', 'URL', 'POST', function(c,r){ code = c; response = r; }, {'X-header': 'value'}); - expect(xhr.method).toEqual('METHOD'); + expect(xhr.method).toEqual('GET'); expect(xhr.url).toEqual('URL'); expect(xhr.post).toEqual('POST'); expect(xhr.headers).toEqual({ - "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json, text/plain, */*", "X-Requested-With": "XMLHttpRequest", "X-header":"value" @@ -124,9 +123,27 @@ describe('browser', function(){ expect(code).toEqual(202); expect(response).toEqual('RESPONSE'); }); + + 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) {}); + + expect(xhr.headers['Content-Type']).toBeDefined(); + expect(xhr.headers['Content-Type']).toEqual('application/x-www-form-urlencoded'); + }); + + it('should set default headers for custom methods', function() { + browser.xhr('CUSTOM', 'URL', 'POST-DATA', function(c, r) {}); + expect(xhr.headers['Accept']).toEqual('application/json, text/plain, */*'); + expect(xhr.headers['X-Requested-With']).toEqual('XMLHttpRequest'); + }); + }); describe('defer', function() { it('should execute fn asynchroniously via setTimeout', function() { |
