forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxhr-tests.ts
More file actions
365 lines (317 loc) · 11.6 KB
/
xhr-tests.ts
File metadata and controls
365 lines (317 loc) · 11.6 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* tslint:disable:no-unused-variable */
import TKUnit = require("./TKUnit");
import types = require("utils/types");
export var test_XMLHttpRequest_isDefined = function () {
TKUnit.assert(types.isDefined(global["XMLHttpRequest"]), "XMLHttpRequest should be defined!");
};
var xhr = new XMLHttpRequest();
export var test_XMLHttpRequest_open_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.open), "XMLHttpRequest.open should be defined!");
};
export var test_XMLHttpRequest_send_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.send), "XMLHttpRequest.send should be defined!");
};
export var test_XMLHttpRequest_setRequestHeader_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.setRequestHeader), "XMLHttpRequest.setRequestHeader should be defined!");
};
export var test_XMLHttpRequest_getAllResponseHeaders_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.getAllResponseHeaders), "XMLHttpRequest.getAllResponseHeaders should be defined!");
};
export var test_XMLHttpRequest_getResponseHeader_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.getResponseHeader), "XMLHttpRequest.getResponseHeader should be defined!");
};
export var test_XMLHttpRequest_overrideMimeType_isDefined = function () {
TKUnit.assert(types.isFunction(xhr.overrideMimeType), "XMLHttpRequest.overrideMimeType should be defined!");
};
export var test_XMLHttpRequest_readyState_isDefined = function () {
TKUnit.assert(types.isNumber(xhr.readyState), "XMLHttpRequest.readyState should be defined!");
};
export var test_XMLHttpRequest_responseText_isDefined = function () {
TKUnit.assert(types.isString(xhr.responseText), "XMLHttpRequest.responseText should be defined!");
};
export var test_XMLHttpRequest_responseType_isDefined = function () {
TKUnit.assert(types.isString(xhr.responseType), "XMLHttpRequest.responseType should be defined!");
};
export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
var count = 0;
// <snippet module="xhr" title="xhr">
// ### Check readyState
// ``` JavaScript
let xhr = new XMLHttpRequest();
TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!");
xhr.onreadystatechange = function () {
// var state = xhr.readyState;
// <hide>
try {
if (count === 0) {
TKUnit.assert(xhr.readyState === 1, "xhr.readyState should be OPEN!");
} else if (count === 1) {
TKUnit.assert(xhr.readyState === 2, "xhr.readyState should be HEADERS_RECEIVED!");
} else if (count === 2) {
TKUnit.assert(xhr.readyState === 3, "xhr.readyState should be LOADING!");
} else if (count === 3) {
TKUnit.assert(xhr.readyState === 4, "xhr.readyState should be DONE!");
}
count++;
done(null);
}
catch (err) {
done(err);
}
// </hide>
};
xhr.open("GET", "https://httpbin.org/get");
xhr.send();
// ```
// </snippet>
};
export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) {
// <snippet module="xhr" title="xhr">
// ### Send/receive headers
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://httpbin.org/get");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState > 1) {
// var contentTypeHeader = xhr.getResponseHeader("Content-Type");
// <hide>
try {
TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!");
done(null);
}
catch (err) {
done(err);
}
// </hide>
}
};
xhr.send();
// ```
// </snippet>
};
export var test_XMLHttpRequest_setResponseTypeShouldNotThrow = function (done) {
try {
var xhr = new XMLHttpRequest();
(<any>xhr)._setResponseType();
done(null);
}
catch (err) {
done(err);
}
};
export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) {
// <snippet module="xhr" title="xhr">
// ### Send/receive JSON
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState > 3) {
var result = JSON.parse(xhr.responseText);
// var valueOne = result["json"]["MyVariableOne"];
// <hide>
try {
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
TKUnit.assert(xhr.response.json.MyVariableOne === "ValueOne" && xhr.response.json.MyVariableTwo === "ValueTwo", "Response content not parsed properly!");
done(null);
}
catch (err) {
done(err);
}
// </hide>
}
};
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
// ```
// </snippet>
};
export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) {
// <snippet module="xhr" title="xhr">
// ### Send/receive FormData
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState > 3) {
var result = JSON.parse(xhr.responseText);
// var valueOne = result["form"]["MyVariableOne"];
// <hide>
try {
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText);
done(null);
}
catch (err) {
done(err);
}
// </hide>
}
};
var data = new FormData();
data.append("MyVariableOne", "ValueOne");
data.append("MyVariableTwo", "ValueTwo");
xhr.send(data);
// ```
// </snippet>
};
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
var flag = false;
// <snippet module="xhr" title="xhr">
// ### Abort request
// ``` JavaScript
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/json");
// <hide>
xhr.onreadystatechange = function () {
flag = true;
};
// </hide>
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
xhr.abort();
// ```
// </snippet>
TKUnit.assert(flag === false, "Content not sent/received properly!");
done(null);
};
export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (done) {
xhr = new XMLHttpRequest();
xhr.open("POST", "https://httpbin.org/post");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState > 3) {
var result = JSON.parse(xhr.responseText);
try {
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
done(null);
}
catch (err) {
done(err);
}
}
};
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
xhr.abort();
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
};
export function test_ignore_zero_length_request_body() {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://httpbin.org/get");
xhr.send('');
}
export function test_raises_onload_Event(done) {
let xhr = new XMLHttpRequest();
xhr.onload = () => {
done(null);
}
xhr.open("GET", "https://httpbin.org/get");
xhr.send();
}
export function test_xhr_events() {
let xhr = <any>new XMLHttpRequest();
let loadCallbackFired = false, loadEventFired = false;
xhr.onload = () => loadCallbackFired = true;
let badEvent = () => { throw new Error("Shouldn't call me") }
xhr.addEventListener('load', () => loadEventFired = true);
xhr.addEventListener('load', badEvent);
xhr.removeEventListener('load', badEvent);
xhr._errorFlag = false;
xhr._setReadyState(xhr.DONE);
TKUnit.assertTrue(loadCallbackFired);
TKUnit.assertTrue(loadEventFired);
let errorCallbackData = null, errorEventData = null;
xhr.onerror = (e) => errorCallbackData = e;
xhr.addEventListener('error', (e) => errorEventData = e);
xhr.addEventListener('error', badEvent);
xhr.removeEventListener('error', badEvent);
xhr._errorFlag = true;
xhr._setReadyState(xhr.DONE, 'error data');
TKUnit.assertEqual(errorCallbackData, 'error data');
TKUnit.assertEqual(errorEventData, 'error data');
}
export function test_xhr_responseType_text() {
const xhr = <any>new XMLHttpRequest();
const response = {
statusCode: 200,
content: {
toString: function(){ return this.raw },
raw: 'response body'
},
headers: {
"Content-Type": "text/plain"
}
}
xhr._loadResponse(response);
TKUnit.assertEqual(xhr.responseType, "text");
TKUnit.assertEqual(xhr.response, 'response body');
}
export function test_xhr_responseType_switched_to_JSON_if_header_present() {
const xhr = <any>new XMLHttpRequest();
const response = {
statusCode: 200,
content: {
toString: function(){ return this.raw },
raw: '{"data": 42}'
},
headers: {
"Content-Type": "application/json"
}
}
xhr._loadResponse(response);
TKUnit.assertEqual(xhr.responseType, "json");
TKUnit.assertEqual(xhr.response.data, 42);
}
export function test_sets_status_and_statusText(done) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState > 3) {
try {
TKUnit.assertEqual(xhr.status, 200);
TKUnit.assertEqual(xhr.statusText, 'OK');
done(null);
}
catch (err) {
done(err);
}
}
};
xhr.open("GET", "https://httpbin.org/get");
xhr.send();
}
export function test_raises_onerror_Event(done) {
let xhr = new XMLHttpRequest();
xhr.onerror = () => {
done(null);
}
xhr.open("GET", "https://no-such-domain-httpbin.org");
xhr.send();
}
export function test_responseType(done) {
let xhr = new XMLHttpRequest();
xhr.responseType = "";
xhr.responseType = "text";
xhr.responseType = "json";
TKUnit.assertThrows(
() => xhr.responseType = "arraybuffer",
"Didn't raise on unsupported type.",
"Response type of 'arraybuffer' not supported."
);
done(null);
}
export function test_getResponseHeader() {
const xhr = <any>new XMLHttpRequest();
const response = {
statusCode: 200,
content: {
toString: function() { return this.raw },
raw: '{"data": 42}'
},
headers: {
"content-type": "application/json"
}
}
xhr._loadResponse(response);
TKUnit.assertEqual(xhr.getResponseHeader("Content-Type"), "application/json");
};