-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added timeout option to abort the request before the response starts …
…responding
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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; | ||
|
||
// 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.ok(err == "ETIMEDOUT"); | ||
checkDone(); | ||
}) | ||
|
||
|
||
// Scenario that shouldn't timeout | ||
var shouldntTimeout = { | ||
url: s.url + "/timeout", | ||
timeout:300 | ||
} | ||
|
||
request(shouldntTimeout, function (err, resp, body) { | ||
assert.ok(!err); | ||
assert.ok(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.ok(!err); | ||
assert.ok(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.ok(err == "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.ok(err == "ETIMEDOUT"); | ||
checkDone(); | ||
}) | ||
|
||
function checkDone() { | ||
if(--remainingTests == 0) { | ||
s.close(); | ||
console.log("All tests passed."); | ||
} | ||
} | ||
|