-
Notifications
You must be signed in to change notification settings - Fork 972
/
ajax-test.js
226 lines (197 loc) · 7.35 KB
/
ajax-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
define([
'test/underscore',
'utils/ajax'
], function (_, utils) {
/* jshint qunit: true */
QUnit.module('utils.ajax');
var test = QUnit.test.bind(QUnit);
function validXHR(xhr) {
if ('XDomainRequest' in window && xhr instanceof window.XDomainRequest) {
return true;
}
return xhr instanceof window.XMLHttpRequest;
}
test('legacy params', function (assert) {
var done = assert.async();
var uri = require.toUrl('./data/playlist.xml');
var xhr = utils.ajax(uri,
function success(xhrResult) {
assert.strictEqual(xhrResult, xhr,
'success callback expects the result be the xhr instance');
assert.ok(xhrResult.responseText,
'success callback expects the result to have responseText');
assert.ok(xhrResult.responseXML,
'success callback expects the result to have responseXML for XML content');
assert.equal(xhrResult.status, 200,
'success callback expects that the result status was 200');
done();
},
function error(message, requestUrl, xhrResult) {
assert.ok(false, message, requestUrl, xhrResult);
done();
},
true
);
assert.ok(validXHR(xhr),
'utils.ajax returns an XMLHttpRequest instance');
});
test('responseType "text"', function (assert) {
var done = assert.async();
var uri = require.toUrl('./data/playlist.json');
var xhr = utils.ajax({
url: uri,
oncomplete: function(xhrResult) {
assert.ok(xhrResult.responseText,
'success callback expects the the result to have responseText');
done();
},
onerror: function(message, requestUrl, xhrResult) {
assert.ok(false, message, requestUrl, xhrResult);
done();
},
responseType: 'text'
});
assert.ok(validXHR(xhr),
'utils.ajax returns an XMLHttpRequest instance');
});
test('responseType "json"', function (assert) {
var done = assert.async();
var uri = require.toUrl('./data/playlist.json');
var xhr = utils.ajax({
url: uri,
oncomplete: function(xhrResult) {
assert.ok(_.isArray(xhrResult.response) && xhrResult.response[0].file,
'xhr.response is parsed JSON');
done();
},
onerror: function(message, requestUrl, xhrResult) {
assert.ok(false, message, requestUrl, xhrResult);
done();
},
responseType: 'json'
});
assert.ok(validXHR(xhr),
'utils.ajax returns an XMLHttpRequest instance');
});
test('timeout', function (assert) {
var done = assert.async();
var nonce = Math.random().toFixed(20).substr(2);
utils.ajax({
url: 'http://playertest.longtailvideo.com/vast/preroll.xml?n=' + nonce,
oncomplete: function(xhrResult) {
assert.notOk(xhrResult.responseText, 'What?');
assert.ok(false, 'expected request to timeout immediately');
done();
},
onerror: function(message) {
assert.equal(message, 'Timeout',
'"Timeout" error message');
done();
},
timeout: 0.0001,
responseType: 'text'
});
});
test('withCredentials', function (assert) {
var done = assert.async();
utils.ajax({
url: require.toUrl('./data/playlist.json'),
oncomplete: function(xhrResult) {
if ('withCredentials' in xhrResult) {
assert.ok(xhrResult.withCredentials, 'xhr result has withCredentials set to true');
} else {
assert.ok(true, 'withCredentials is not available in this browser');
}
done();
},
onerror: function() {
assert.ok(false, 'request failed withCredentials');
done();
},
withCredentials: true,
responseType: 'text'
});
});
test('withCredentials crossdomain', function (assert) {
var done = assert.async();
utils.ajax({
url: '//playertest.longtailvideo.com/vast/preroll.xml',
oncomplete: function(xhrResult) {
if (xhrResult.withCredentials === false) {
assert.ok(true,
'a second crossdomain requests without credentials is made');
} else {
assert.ok(true,
'the first crossdomain request with credentials succeeded');
}
assert.ok(xhrResult.responseXML.firstChild,
'xml was returned');
done();
},
onerror: function() {
assert.ok(false, 'crossdomain request failed withCredentials and retryWithoutCredentials');
done();
},
withCredentials: true,
retryWithoutCredentials: true,
requireValidXML: true
});
});
test('error "Error loading file" (bad request)', function (assert) {
var done = assert.async();
utils.ajax({
onerror: function() {
assert.ok(true, 'missing url param results in "Error loading file" error');
done();
}
});
});
test('error "Invalid XML"', function (assert) {
var done = assert.async();
utils.ajax({
url: require.toUrl('./data/invalid.xml'),
oncomplete: function(xhrResult) {
assert.notOk(xhrResult.responseXML, 'What?');
assert.ok(false, 'expected error callback with invalid "Invalid XML"');
done();
},
onerror: function(message) {
assert.equal(message, 'Invalid XML',
'"Invalid XML" error message');
done();
},
requireValidXML: true
});
});
test('error "Invalid JSON"', function (assert) {
var done = assert.async();
utils.ajax({
url: require.toUrl('./data/invalid.xml'),
oncomplete: function() {
assert.ok(false, 'expected error callback with invalid "Invalid JSON"');
done();
},
onerror: function(message) {
assert.equal(message, 'Invalid JSON',
'"Invalid JSON" error message');
done();
},
responseType: 'json'
});
});
test('error "File not found" (404)', function (assert) {
var done = assert.async();
utils.ajax({
url: 'foobar',
oncomplete: function() {
assert.ok(false, 'expected error callback with invalid "File not found"');
done();
},
onerror: function(message) {
assert.equal(message, 'File not found',
'"File not found" error message');
done();
}
});
});
});