a0cd080ed7f06d21f1b91da08650f8f399b7349c.svn-base
1.89 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
var server = require('./server')
, events = require('events')
, stream = require('stream')
, assert = require('assert')
, request = require('../main.js')
;
var s = server.createServer();
var expectedBody = "waited";
var remainingTests = 5;
s.listen(s.port, function () {
// Request that waits for 200ms
s.on('/timeout', function (req, resp) {
setTimeout(function(){
resp.writeHead(200, {'content-type':'text/plain'})
resp.write(expectedBody)
resp.end()
}, 200);
});
// Scenario that should timeout
var shouldTimeout = {
url: s.url + "/timeout",
timeout:100
}
request(shouldTimeout, function (err, resp, body) {
assert.equal(err.code, "ETIMEDOUT");
checkDone();
})
// Scenario that shouldn't timeout
var shouldntTimeout = {
url: s.url + "/timeout",
timeout:300
}
request(shouldntTimeout, function (err, resp, body) {
assert.equal(err, null);
assert.equal(expectedBody, body)
checkDone();
})
// Scenario with no timeout set, so shouldn't timeout
var noTimeout = {
url: s.url + "/timeout"
}
request(noTimeout, function (err, resp, body) {
assert.equal(err);
assert.equal(expectedBody, body)
checkDone();
})
// Scenario with a negative timeout value, should be treated a zero or the minimum delay
var negativeTimeout = {
url: s.url + "/timeout",
timeout:-1000
}
request(negativeTimeout, function (err, resp, body) {
assert.equal(err.code, "ETIMEDOUT");
checkDone();
})
// Scenario with a float timeout value, should be rounded by setTimeout anyway
var floatTimeout = {
url: s.url + "/timeout",
timeout: 100.76
}
request(floatTimeout, function (err, resp, body) {
assert.equal(err.code, "ETIMEDOUT");
checkDone();
})
function checkDone() {
if(--remainingTests == 0) {
s.close();
console.log("All tests passed.");
}
}
})