forked from request/request
/
test-node-debug.js
67 lines (56 loc) · 1.9 KB
/
test-node-debug.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
var assert = require('assert')
, request = require('../index')
, http = require('http')
;
var s = http.createServer(function(req, res) {
res.statusCode = 200
res.end('')
}).listen(6767, function () {
// a simple request should not fail with debugging enabled
request.debug = true
var stderr = []
, stderrLen = 0
process.stderr.write = (function(write) {
return function(string, encoding, fd) {
stderr.push(string)
}
})(process.stderr.write)
request('http://localhost:6767', function (err, resp, body) {
assert.ifError(err, 'the request did not fail')
assert.ok(resp, 'the request did not fail')
assert.ok(stderr.length, 'stderr has some messages')
;[
/^REQUEST { uri: /,
/^REQUEST make request http:\/\/localhost:6767\/\n$/,
/^REQUEST onResponse /,
/^REQUEST finish init /,
/^REQUEST response end /,
/^REQUEST end event /,
/^REQUEST emitting complete /
].forEach(function(t) {
var found = false
stderr.forEach(function(msg) {
if (t.test(msg)) found = true
})
assert.ok(found, 'a log message matches ' + t)
})
stderrLen = stderr.length
// there should be no further lookups on process.env
process.env.NODE_DEBUG = ''
stderr = []
request('http://localhost:6767', function(err, resp, body) {
assert.ifError(err, 'the request did not fail')
assert.ok(resp, 'the request did not fail')
assert.equal(stderr.length, stderrLen, 'env.NODE_DEBUG is not retested')
// it should be possible to disable debugging at runtime
request.debug = false
stderr = []
request('http://localhost:6767', function(err, resp, body) {
assert.ifError(err, 'the request did not fail')
assert.ok(resp, 'the request did not fail')
assert.equal(stderr.length, 0, 'debugging can be disabled')
s.close(); // clean up
})
})
})
})