@@ -5,6 +5,26 @@ QUnit = require('steal-qunit');
5
5
6
6
QUnit . module ( "can-util/dom/ajax" ) ;
7
7
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
+
8
28
if ( __dirname !== '/' ) {
9
29
QUnit . asyncTest ( "basic get request" , function ( ) {
10
30
ajax ( {
@@ -35,11 +55,10 @@ if (__dirname !== '/') {
35
55
}
36
56
37
57
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 = {
40
59
CONTENT_TYPE : "Content-Type"
41
60
} ,
42
- xhrFixture = function ( ) {
61
+ restore = makeFixture ( function ( ) {
43
62
this . open = function ( type , url ) {
44
63
} ;
45
64
@@ -56,18 +75,13 @@ QUnit.asyncTest("ignores case of type parameter for a post request (#100)", func
56
75
this . responseText = JSON . stringify ( o ) ;
57
76
}
58
77
} ;
59
- } ;
78
+ } ) ;
79
+
60
80
61
- // replace with fixture
62
- if ( window . XMLHttpRequest ) {
63
- window . XMLHttpRequest = xhrFixture ;
64
- } else if ( window . ActiveXObject ) {
65
- window . ActiveXObject = xhrFixture ;
66
- }
67
81
68
82
ajax ( {
69
83
type : "post" ,
70
- url : "/foo" ,
84
+ url : "http://anotherdomain.com /foo" ,
71
85
data : {
72
86
bar : "qux"
73
87
}
@@ -77,11 +91,7 @@ QUnit.asyncTest("ignores case of type parameter for a post request (#100)", func
77
91
QUnit . notOk ( reason , "request failed with reason = " , reason ) ;
78
92
} ) . then ( function ( ) {
79
93
// restore original values
80
- if ( window . XMLHttpRequest ) {
81
- window . XMLHttpRequest = oldXhr ;
82
- } else if ( window . ActiveXObject ) {
83
- window . ActiveXObject = oldXhr ;
84
- }
94
+ restore ( ) ;
85
95
start ( ) ;
86
96
} ) ;
87
97
} ) ;
@@ -122,3 +132,42 @@ if(System.env !== 'canjs-test' && __dirname !== '/') {
122
132
promise . abort ( ) ;
123
133
} ) ;
124
134
}
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
+ } ) ;
0 commit comments