~cordova-ubuntu/cordova-cli/trunk

« back to all changes in this revision

Viewing changes to node_modules/cordova/_vendor/request/2.22.0/tests/test-timeout.js

  • Committer: Robert Bruce Park
  • Date: 2014-02-26 21:27:56 UTC
  • mfrom: (44.1.5 3.4-release)
  • Revision ID: robert.park@canonical.com-20140226212756-6jmoiqugw0f1ebxb
Update to 3.4.0 stable release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var server = require('./server')
 
2
  , events = require('events')
 
3
  , stream = require('stream')
 
4
  , assert = require('assert')
 
5
  , request = require('../index')
 
6
  ;
 
7
 
 
8
var s = server.createServer();
 
9
var expectedBody = "waited";
 
10
var remainingTests = 5;
 
11
 
 
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)
 
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