webhook-test.js
3.52 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* webhook-test.js: Tests for instances of the Webhook transport
*
* (C) 2011 Marak Squires
* MIT LICENSE
*
*/
var path = require('path'),
vows = require('vows'),
fs = require('fs'),
http = require('http'),
https = require('https'),
assert = require('assert'),
winston = require('../../lib/winston'),
helpers = require('../helpers');
var webhookTransport = new (winston.transports.Webhook)({
"host": "localhost",
"port": 8080,
"path": "/winston-test"
});
var httpsWebhookTransport = new (winston.transports.Webhook)({
"host": "localhost",
"port": 8081,
"path": "/winston-test",
"ssl": true
});
var authWebhookTransport = new (winston.transports.Webhook)({
"host": "localhost",
"port": 8080,
"path": "/winston-auth-test",
"auth": {
"username": "winston",
"password": "churchill"
}
});
var requestsAuthenticated = true;
var server = http.createServer(function (req, res) {
if (req.url == '/winston-auth-test') {
//
// Test if request has been correctly authenticated
//
// Strip 'Basic' from Authorization header
var signature = req.headers['authorization'].substr(6);
requestsAuthenticated = requestsAuthenticated &&
new Buffer(signature, 'base64').toString('utf8') == 'winston:churchill';
}
res.end();
});
server.listen(8080);
var httpsServer = https.createServer({
cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-cert.pem')),
key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-key.pem'))
}, function (req, res) {
res.end();
});
httpsServer.listen(8081);
vows.describe('winston/transports/webhook').addBatch({
"An instance of the Webhook Transport": {
"when passed valid options": {
"should have the proper methods defined": function () {
helpers.assertWebhook(webhookTransport);
},
"the log() method": helpers.testNpmLevels(webhookTransport, "should respond with true", function (ign, err, logged) {
assert.isNull(err);
assert.isTrue(logged);
})
}
},
"An https instance of the Webhook Transport": {
"when passed valid options": {
"should have the proper methods defined": function () {
helpers.assertWebhook(httpsWebhookTransport);
},
"the log() method": helpers.testNpmLevels(httpsWebhookTransport, "should respond with true", function (ign, err, logged) {
assert.isNull(err);
assert.isTrue(logged);
})
}
},
"An http Basic Auth instance of the Webhook Transport": {
"when passed valid options": {
"should have the proper methods defined": function () {
helpers.assertWebhook(authWebhookTransport);
},
"the log() method": helpers.testNpmLevels(authWebhookTransport, "should respond with true", function (ign, err, logged) {
assert.isNull(err);
assert.isTrue(logged);
})
}
}
}).addBatch({
"When the tests are over": {
topic: function () {
//
// Delay destruction of the server since the
// WebHook transport responds before the request
// has actually be completed.
//
setTimeout(this.callback, 1000);
},
"the server should cleanup": function () {
server.close();
},
"requests have been correctly authenticated": function () {
assert.ok(requestsAuthenticated);
}
}
}).addBatch({
// "An instance of the Webhook Transport": transport(winston.transports.Webhook, {
// "host": "localhost",
// "port": 8080,
// "path": "/winston-test"
// })
}).export(module);