/
deventForwarderWriter.js
68 lines (46 loc) · 1.48 KB
/
deventForwarderWriter.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
68
var http = require('http');
var lilbro = require('../lib/lilbro');
var fixtures = require('./data/fixtures');
exports.event = function(test) {
var options = clone(fixtures.defaults);
options.writer = 'devent-forwarder';
options['devent-host'] = 'localhost';
options['devent-port'] = 11991;
lilbro.initialize(options);
var req = { url: fixtures.encodedRequestURL, method: 'GET' };
var res = {
write: function() {},
end: function() {},
writeHead: function() {}
};
var devent_listener = function(req, res) {
var chunks = [];
var body_len = 0;
req.on('data', function(chunk) {
body_len += chunk.length;
chunks.push(chunk);
});
req.on('end', function() {
var body = new Buffer(body_len);
var cur_len = 0;
for (var i = 0; i < chunks.length; i++) {
chunks[i].copy(body, cur_len);
cur_len += chunks[i].length;
}
// test content type, method
test.equal(req.method, 'POST', 'devent forwarder is a POST');
test.equal(req.headers['content-type'], 'application/json', 'devent forwarder gets application/json content type');
var data = decodeURIComponent(body.toString());
test.deepEqual( JSON.parse(data), JSON.parse(fixtures.jsonData), 'event data makes it to devent-forwarder' );
test.done();
server.close();
res.end();
});
};
var server = http.createServer(devent_listener);
server.listen(options['devent-port']);
lilbro.listener(req, res);
};
var clone = function(object) {
return JSON.parse(JSON.stringify(object));
}