1
var server = require('./server')
2
, events = require('events')
3
, stream = require('stream')
4
, assert = require('assert')
5
, request = require('../index')
8
var s = server.createServer();
9
var expectedBody = "waited";
10
var remainingTests = 5;
12
s.listen(s.port, function () {
13
// Request that waits for 200ms
14
s.on('/timeout', function (req, resp) {
15
setTimeout(function(){
16
resp.writeHead(200, {'content-type':'text/plain'})
17
resp.write(expectedBody)
22
// Scenario that should timeout
24
url: s.url + "/timeout",
29
request(shouldTimeout, function (err, resp, body) {
30
assert.equal(err.code, "ETIMEDOUT");
35
// Scenario that shouldn't timeout
36
var shouldntTimeout = {
37
url: s.url + "/timeout",
41
request(shouldntTimeout, function (err, resp, body) {
42
assert.equal(err, null);
43
assert.equal(expectedBody, body)
47
// Scenario with no timeout set, so shouldn't timeout
49
url: s.url + "/timeout"
52
request(noTimeout, function (err, resp, body) {
54
assert.equal(expectedBody, body)
58
// Scenario with a negative timeout value, should be treated a zero or the minimum delay
59
var negativeTimeout = {
60
url: s.url + "/timeout",
64
request(negativeTimeout, function (err, resp, body) {
65
assert.equal(err.code, "ETIMEDOUT");
69
// Scenario with a float timeout value, should be rounded by setTimeout anyway
71
url: s.url + "/timeout",
75
request(floatTimeout, function (err, resp, body) {
76
assert.equal(err.code, "ETIMEDOUT");
80
function checkDone() {
81
if(--remainingTests == 0) {
83
console.log("All tests passed.");