Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9f56af9

Browse files
Vojta JinaIgorMinar
Vojta Jina
authored andcommitted
XHR should add Content-type header only for POST
Sending Content-type header causes JSTD (Jetty) proxy to change GET methods into POST.
1 parent c5f0342 commit 9f56af9

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/Browser.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ var XHR = window.XMLHttpRequest || function () {
77
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
88
throw new Error("This browser does not support XMLHttpRequest.");
99
};
10+
11+
// default xhr headers
1012
var XHR_HEADERS = {
11-
"Content-Type": "application/x-www-form-urlencoded",
12-
"Accept": "application/json, text/plain, */*",
13-
"X-Requested-With": "XMLHttpRequest"
13+
DEFAULT: {
14+
"Accept": "application/json, text/plain, */*",
15+
"X-Requested-With": "XMLHttpRequest"
16+
},
17+
POST: {'Content-Type': 'application/x-www-form-urlencoded'}
1418
};
1519

1620
/**
@@ -103,8 +107,9 @@ function Browser(window, document, body, XHR, $log) {
103107
} else {
104108
var xhr = new XHR();
105109
xhr.open(method, url, true);
106-
forEach(extend(XHR_HEADERS, headers || {}), function(value, key){
107-
if (value) xhr.setRequestHeader(key, value);
110+
forEach(extend({}, XHR_HEADERS.DEFAULT, XHR_HEADERS[uppercase(method)] || {}, headers || {}),
111+
function(value, key) {
112+
if (value) xhr.setRequestHeader(key, value);
108113
});
109114
xhr.onreadystatechange = function() {
110115
if (xhr.readyState == 4) {

test/BrowserSpecs.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,15 @@ describe('browser', function(){
101101

102102
it('should set headers for all requests', function(){
103103
var code, response, headers = {};
104-
browser.xhr('METHOD', 'URL', 'POST', function(c,r){
104+
browser.xhr('GET', 'URL', 'POST', function(c,r){
105105
code = c;
106106
response = r;
107107
}, {'X-header': 'value'});
108108

109-
expect(xhr.method).toEqual('METHOD');
109+
expect(xhr.method).toEqual('GET');
110110
expect(xhr.url).toEqual('URL');
111111
expect(xhr.post).toEqual('POST');
112112
expect(xhr.headers).toEqual({
113-
"Content-Type": "application/x-www-form-urlencoded",
114113
"Accept": "application/json, text/plain, */*",
115114
"X-Requested-With": "XMLHttpRequest",
116115
"X-header":"value"
@@ -124,9 +123,27 @@ describe('browser', function(){
124123
expect(code).toEqual(202);
125124
expect(response).toEqual('RESPONSE');
126125
});
126+
127+
it('should not set Content-type header for GET requests', function() {
128+
browser.xhr('GET', 'URL', 'POST-DATA', function(c, r) {});
127129

128-
});
130+
expect(xhr.headers['Content-Type']).not.toBeDefined();
131+
});
132+
133+
it('should set Content-type header for POST requests', function() {
134+
browser.xhr('POST', 'URL', 'POST-DATA', function(c, r) {});
135+
136+
expect(xhr.headers['Content-Type']).toBeDefined();
137+
expect(xhr.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
138+
});
139+
140+
it('should set default headers for custom methods', function() {
141+
browser.xhr('CUSTOM', 'URL', 'POST-DATA', function(c, r) {});
129142

143+
expect(xhr.headers['Accept']).toEqual('application/json, text/plain, */*');
144+
expect(xhr.headers['X-Requested-With']).toEqual('XMLHttpRequest');
145+
});
146+
});
130147

131148
describe('defer', function() {
132149
it('should execute fn asynchroniously via setTimeout', function() {

0 commit comments

Comments
 (0)