source: Dev/trunk/src/node_modules/request/tests/test-timeout.js @ 484

Last change on this file since 484 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 1.9 KB
Line 
1var server = require('./server')
2  , events = require('events')
3  , stream = require('stream')
4  , assert = require('assert')
5  , request = require('../index')
6  ;
7
8var s = server.createServer();
9var expectedBody = "waited";
10var remainingTests = 5;
11
12s.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)
18      resp.end()
19    }, 200);
20  });
21
22  // Scenario that should timeout
23  var shouldTimeout = {
24    url: s.url + "/timeout",
25    timeout:100
26  }
27
28
29  request(shouldTimeout, function (err, resp, body) {
30    assert.equal(err.code, "ETIMEDOUT");
31    checkDone();
32  })
33
34
35  // Scenario that shouldn't timeout
36  var shouldntTimeout = {
37    url: s.url + "/timeout",
38    timeout:300
39  }
40
41  request(shouldntTimeout, function (err, resp, body) {
42    assert.equal(err, null);
43    assert.equal(expectedBody, body)
44    checkDone();
45  })
46
47  // Scenario with no timeout set, so shouldn't timeout
48  var noTimeout = {
49    url: s.url + "/timeout"
50  }
51
52  request(noTimeout, function (err, resp, body) {
53    assert.equal(err);
54    assert.equal(expectedBody, body)
55    checkDone();
56  })
57
58  // Scenario with a negative timeout value, should be treated a zero or the minimum delay
59  var negativeTimeout = {
60    url: s.url + "/timeout",
61    timeout:-1000
62  }
63
64  request(negativeTimeout, function (err, resp, body) {
65    assert.equal(err.code, "ETIMEDOUT");
66    checkDone();
67  })
68
69  // Scenario with a float timeout value, should be rounded by setTimeout anyway
70  var floatTimeout = {
71    url: s.url + "/timeout",
72    timeout: 100.76
73  }
74
75  request(floatTimeout, function (err, resp, body) {
76    assert.equal(err.code, "ETIMEDOUT");
77    checkDone();
78  })
79
80  function checkDone() {
81    if(--remainingTests == 0) {
82      s.close();
83      console.log("All tests passed.");
84    }
85  }
86})
87
Note: See TracBrowser for help on using the repository browser.