forked from senchalabs/connect
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtimeout.js
88 lines (77 loc) · 2.12 KB
/
timeout.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
var connect = require('../');
var app = connect()
.use(connect.timeout(300))
.use(function(req, res){
res.end('Hello');
});
describe('connect.timeout()', function(){
describe('when below the timeout', function(){
it('should do nothing', function(done){
app.request()
.get('/')
.expect('Hello', done);
})
})
describe('when above the timeout', function(){
describe('with no response made', function(){
it('should respond with 408 Request timeout', function(done){
var app = connect()
.use(connect.timeout(300))
.use(function(req, res){
setTimeout(function(){
res.end('Hello');
}, 400);
});
app.request()
.get('/')
.expect(503, done);
})
it('should pass the error to next()', function(done){
var app = connect()
.use(connect.timeout(300))
.use(function(req, res){
setTimeout(function(){
res.end('Hello');
}, 400);
})
.use(function(err, req, res, next){
res.statusCode = err.status;
res.end('timeout of ' + err.timeout + 'ms exceeded');
});
app.request()
.get('/')
.expect('timeout of 300ms exceeded', done);
})
})
describe('with a partial response', function(){
it('should do nothing', function(done){
var app = connect()
.use(connect.timeout(300))
.use(function(req, res){
res.write('Hello');
setTimeout(function(){
res.end(' World');
}, 400);
});
app.request()
.get('/')
.expect('Hello World', done);
})
})
})
describe('req.clearTimeout()', function(){
it('should revert this behavior', function(done){
var app = connect()
.use(connect.timeout(300))
.use(function(req, res){
req.clearTimeout();
setTimeout(function(){
res.end('Hello');
}, 400);
});
app.request()
.get('/')
.expect('Hello', done);
})
})
})