Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
/*
2
* webhook-test.js: Tests for instances of the Webhook transport
3
*
4
* (C) 2011 Marak Squires
5
* MIT LICENSE
6
*
7
*/
8
9
var path = require('path'),
10
vows = require('vows'),
11
fs = require('fs'),
12
http = require('http'),
13
https = require('https'),
14
assert = require('assert'),
15
winston = require('../../lib/winston'),
16
helpers = require('../helpers');
17
18
var webhookTransport = new (winston.transports.Webhook)({
19
"host": "localhost",
20
"port": 8080,
21
"path": "/winston-test"
22
});
23
24
var httpsWebhookTransport = new (winston.transports.Webhook)({
25
"host": "localhost",
26
"port": 8081,
27
"path": "/winston-test",
28
"ssl": true
29
});
30
31
var authWebhookTransport = new (winston.transports.Webhook)({
32
"host": "localhost",
33
"port": 8080,
34
"path": "/winston-auth-test",
35
"auth": {
36
"username": "winston",
37
"password": "churchill"
38
}
39
});
40
41
var requestsAuthenticated = true;
42
43
var server = http.createServer(function (req, res) {
44
if (req.url == '/winston-auth-test') {
45
//
46
// Test if request has been correctly authenticated
47
//
48
// Strip 'Basic' from Authorization header
49
var signature = req.headers['authorization'].substr(6);
50
requestsAuthenticated = requestsAuthenticated &&
51
new Buffer(signature, 'base64').toString('utf8') == 'winston:churchill';
52
}
53
res.end();
54
});
55
56
server.listen(8080);
57
58
59
var httpsServer = https.createServer({
60
cert: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-cert.pem')),
61
key: fs.readFileSync(path.join(__dirname, '..', 'fixtures', 'keys', 'agent2-key.pem'))
62
}, function (req, res) {
63
res.end();
64
});
65
66
httpsServer.listen(8081);
67
68
vows.describe('winston/transports/webhook').addBatch({
69
"An instance of the Webhook Transport": {
70
"when passed valid options": {
71
"should have the proper methods defined": function () {
72
helpers.assertWebhook(webhookTransport);
73
},
74
"the log() method": helpers.testNpmLevels(webhookTransport, "should respond with true", function (ign, err, logged) {
75
assert.isNull(err);
76
assert.isTrue(logged);
77
})
78
}
79
},
80
"An https instance of the Webhook Transport": {
81
"when passed valid options": {
82
"should have the proper methods defined": function () {
83
helpers.assertWebhook(httpsWebhookTransport);
84
},
85
"the log() method": helpers.testNpmLevels(httpsWebhookTransport, "should respond with true", function (ign, err, logged) {
86
assert.isNull(err);
87
assert.isTrue(logged);
88
})
89
}
90
},
91
"An http Basic Auth instance of the Webhook Transport": {
92
"when passed valid options": {
93
"should have the proper methods defined": function () {
94
helpers.assertWebhook(authWebhookTransport);
95
},
96
"the log() method": helpers.testNpmLevels(authWebhookTransport, "should respond with true", function (ign, err, logged) {
97
assert.isNull(err);
98
assert.isTrue(logged);
99
})
100
}
101
}
102
}).addBatch({
103
"When the tests are over": {
104
topic: function () {
105
//
106
// Delay destruction of the server since the
107
// WebHook transport responds before the request
108
// has actually be completed.
109
//
110
setTimeout(this.callback, 1000);
111
},
112
"the server should cleanup": function () {
113
server.close();
114
},
115
"requests have been correctly authenticated": function () {
116
assert.ok(requestsAuthenticated);
117
}
118
}
119
}).addBatch({
120
// "An instance of the Webhook Transport": transport(winston.transports.Webhook, {
121
// "host": "localhost",
122
// "port": 8080,
123
// "path": "/winston-test"
124
// })
125
}).export(module);
126
127