-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathtest-timeout.js
More file actions
47 lines (39 loc) · 1.25 KB
/
test-timeout.js
File metadata and controls
47 lines (39 loc) · 1.25 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
var sys = require("util")
, assert = require("assert")
, http = require('http')
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest;
// Test server
http.createServer(function (req, res) {
setTimeout(() => {
res.end();
this.close();
}, 3000);
}).listen(8000);
var xhr = new XMLHttpRequest();
// Testing simple timeout case
xhr.open("GET", "http://localhost:8000/nowhere");
xhr.timeout = 1000;
xhr.ontimeout = function () {
console.log('Request timed out!');
}
xhr.send(null);
// Testing when timeout is set on a sync request
// Should throw an Error
xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:8000/nowhere", false);
try {
xhr.timeout = 1000;
} catch (e) {
console.log(e.message);
assert.equal(e.message, 'InvalidAccessError: synchronous XMLHttpRequests do not support timeout and responseType.');
}
// Should also throw an Error if the timeout is set
// before xhr.open() on a sync request
xhr = new XMLHttpRequest();
xhr.timeout = 1000;
try {
xhr.open("GET", "http://localhost:8000/nowhere", false);
} catch (e) {
console.log(e.message);
assert.equal(e.message, 'InvalidAccessError: synchronous XMLHttpRequests do not support timeout and responseType.');
}