Skip to content

Commit 1594ab1

Browse files
committed
fixes #172
1 parent 686a95c commit 1594ab1

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

ajax-test.js

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ QUnit = require('steal-qunit');
55

66
QUnit.module("can-util/dom/ajax");
77

8+
var makeFixture = function(XHR){
9+
10+
var oldXhr = window.XMLHttpRequest || window.ActiveXObject;
11+
if (window.XMLHttpRequest) {
12+
window.XMLHttpRequest = XHR;
13+
} else if (window.ActiveXObject) {
14+
window.ActiveXObject = XHR;
15+
}
16+
17+
return function restoreXHR(){
18+
if (window.XMLHttpRequest) {
19+
window.XMLHttpRequest = oldXhr;
20+
} else if (window.ActiveXObject) {
21+
window.ActiveXObject = oldXhr;
22+
}
23+
};
24+
};
25+
26+
27+
828
if (__dirname !== '/') {
929
QUnit.asyncTest("basic get request", function () {
1030
ajax({
@@ -35,11 +55,10 @@ if (__dirname !== '/') {
3555
}
3656

3757
QUnit.asyncTest("ignores case of type parameter for a post request (#100)", function () {
38-
var oldXhr = window.XMLHttpRequest || window.ActiveXObject,
39-
requestHeaders = {
58+
var requestHeaders = {
4059
CONTENT_TYPE: "Content-Type"
4160
},
42-
xhrFixture = function () {
61+
restore = makeFixture(function () {
4362
this.open = function (type, url) {
4463
};
4564

@@ -56,18 +75,13 @@ QUnit.asyncTest("ignores case of type parameter for a post request (#100)", func
5675
this.responseText = JSON.stringify(o);
5776
}
5877
};
59-
};
78+
});
79+
6080

61-
// replace with fixture
62-
if (window.XMLHttpRequest) {
63-
window.XMLHttpRequest = xhrFixture;
64-
} else if (window.ActiveXObject) {
65-
window.ActiveXObject = xhrFixture;
66-
}
6781

6882
ajax({
6983
type: "post",
70-
url: "/foo",
84+
url: "http://anotherdomain.com/foo",
7185
data: {
7286
bar: "qux"
7387
}
@@ -77,11 +91,7 @@ QUnit.asyncTest("ignores case of type parameter for a post request (#100)", func
7791
QUnit.notOk(reason, "request failed with reason = ", reason);
7892
}).then(function () {
7993
// restore original values
80-
if (window.XMLHttpRequest) {
81-
window.XMLHttpRequest = oldXhr;
82-
} else if (window.ActiveXObject) {
83-
window.ActiveXObject = oldXhr;
84-
}
94+
restore();
8595
start();
8696
});
8797
});
@@ -122,3 +132,42 @@ if(System.env !== 'canjs-test' && __dirname !== '/') {
122132
promise.abort();
123133
});
124134
}
135+
136+
137+
QUnit.asyncTest("crossDomain is true for relative requests", function(){
138+
var headers = {},
139+
restore = makeFixture(function () {
140+
this.open = function (type, url) {
141+
};
142+
143+
this.send = function () {
144+
this.readyState = 4;
145+
this.status = 200;
146+
this.responseText = JSON.stringify({great: "success"});
147+
this.onreadystatechange();
148+
};
149+
150+
this.setRequestHeader = function (header, value) {
151+
headers[header] = value;
152+
};
153+
});
154+
155+
ajax({
156+
type: "post",
157+
url: "/foo",
158+
data: {
159+
bar: "qux"
160+
},
161+
dataType: "json"
162+
}).then(function (value) {
163+
QUnit.deepEqual(headers, {
164+
"Content-Type": "application/json",
165+
"X-Requested-With": "XMLHttpRequest"});
166+
}, function (reason) {
167+
QUnit.notOk(reason, "request failed with reason = ", reason);
168+
}).then(function () {
169+
// restore original values
170+
restore();
171+
start();
172+
});
173+
});

ajax.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ module.exports = namespace.ajax = function (o) {
104104
if(o.crossDomain == null){
105105
try {
106106
requestUrl = parseURI(o.url);
107-
o.crossDomain = originUrl.protocol + "//" + originUrl.host !== requestUrl.protocol + "//" + requestUrl.host;
107+
o.crossDomain = !!((requestUrl.protocol && requestUrl.protocol !== originUrl.protocol) ||
108+
(requestUrl.host && requestUrl.host !== originUrl.host));
109+
108110
} catch (e){
109111
o.crossDomain = true;
110112
}

0 commit comments

Comments
 (0)