Skip to content

Commit 9cebb98

Browse files
committed
Improving test coverage for handlers.
1 parent 353e2ee commit 9cebb98

File tree

8 files changed

+317
-1
lines changed

8 files changed

+317
-1
lines changed

handlers/decodeBase64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
exports.process = function(config) {
22
console.log('decodeBase64');
33
config.data = new Buffer(config.data, 'base64');
4-
return config;
4+
return Promise.resolve(config);
55
};

test/assets/cloudwatch.data.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"logEvents": [
3+
{"id":"1234567890","timestamp":1466263061409,"message":"START RequestId: abcdef-1234-5678-9012-abcdef Version: $LATEST\n"},
4+
{"id":"1234567891","timestamp":1466263061419,"message":"2016-06-18T15:17:41.419Z\tabcdef-1234-5678-9012-abcdef\tExample log message 1\n"},
5+
{"id":"1234567892","timestamp":1466263061429,"message":"2016-06-18T15:17:41.429Z\tabcdef-1234-5678-9012-abcdef\tExample log message 2\nLine 2\n"},
6+
{"id":"1234567893","timestamp":1466263061439,"message":"END RequestId: abcdef-1234-5678-9012-abcdef\n"},
7+
{"id":"1234567894","timestamp":1466263061449,"message":"REPORT RequestId: abcdef-1234-5678-9012-abcdef\tDuration: 432.10 ms\tBilled Duration: 500 ms \tMemory Size: 256 MB\tMax Memory Used: 123 MB\n"}
8+
]
9+
}

test/assets/cloudwatch.format.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"id":"1234567890",
4+
"timestamp":1466263061409,
5+
"message":"START RequestId: abcdef-1234-5678-9012-abcdef Version: $LATEST\n",
6+
"logType": "start",
7+
"requestId": "abcdef-1234-5678-9012-abcdef",
8+
"lambdaVersion": "$LATEST",
9+
"date": "2016-06-18T15:17:41.409Z"
10+
},
11+
{
12+
"id":"1234567891",
13+
"timestamp":1466263061419,
14+
"date": "2016-06-18T15:17:41.419Z",
15+
"requestId": "abcdef-1234-5678-9012-abcdef",
16+
"logType": "message",
17+
"message":"Example log message 1\n"
18+
},
19+
{
20+
"id":"1234567892",
21+
"timestamp":1466263061429,
22+
"date": "2016-06-18T15:17:41.429Z",
23+
"requestId": "abcdef-1234-5678-9012-abcdef",
24+
"logType": "message",
25+
"message":"Example log message 2\nLine 2\n"
26+
},
27+
{
28+
"id":"1234567893",
29+
"timestamp":1466263061439,
30+
"date": "2016-06-18T15:17:41.439Z",
31+
"requestId": "abcdef-1234-5678-9012-abcdef",
32+
"logType": "end",
33+
"message":"END RequestId: abcdef-1234-5678-9012-abcdef\n"
34+
},
35+
{
36+
"id":"1234567894",
37+
"timestamp":1466263061449,
38+
"date": "2016-06-18T15:17:41.449Z",
39+
"requestId": "abcdef-1234-5678-9012-abcdef",
40+
"logType": "report",
41+
"duration": "432.10",
42+
"durationBilled": "500",
43+
"memConfigured": "256",
44+
"memUsed": "123",
45+
"message":"REPORT RequestId: abcdef-1234-5678-9012-abcdef\tDuration: 432.10 ms\tBilled Duration: 500 ms \tMemory Size: 256 MB\tMax Memory Used: 123 MB\n"
46+
}
47+
]

test/assets/log.string.base64.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ZGF0ZT0iMjAxNi0wMy0yM1QxMjozMzowMFoiIGxldmVsPSI0MCIgdGl0bGU9InVzZXIgYXV0aGVudGljYXRpb24iCmRhdGVfdGltZT0iMjAxNi0wMy0yM1QxMjozMzowMFoiIGFwcGxldmVsPSI0MCIgdGl0bGU9InVzZXIgYXV0aGVudGljYXRpb24iCg==

test/decodeBase64.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* global before, describe, it */
2+
3+
var assert = require("assert");
4+
var fs = require("fs");
5+
6+
var handler = require('../handlers/decodeBase64');
7+
8+
describe('handler/decodeBase64.js', function() {
9+
describe('#process()', function() {
10+
var dataBase64;
11+
var dataPlain;
12+
13+
before(function() {
14+
dataBase64 = fs.readFileSync("test/assets/log.string.base64.txt")
15+
.toString();
16+
dataPlain = fs.readFileSync("test/assets/log.string.txt").toString();
17+
});
18+
19+
it('should decode Base64 data',
20+
function(done) {
21+
handler.process({data: dataBase64, setting: true})
22+
.then(function(result) {
23+
assert.ok(result.hasOwnProperty('setting'),
24+
'process returns config object');
25+
assert.strictEqual(result.data.toString(), dataPlain,
26+
'Base64 data decoded successfully');
27+
done();
28+
});
29+
});
30+
});
31+
});

test/formatCloudwatchLogs.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* global before, describe, it */
2+
3+
var assert = require("assert");
4+
var fs = require("fs");
5+
6+
var handler = require("../handlers/formatCloudwatchLogs");
7+
8+
describe('handler/formatCloudwatchLogs.js', function() {
9+
describe('#process()', function() {
10+
var dataSource;
11+
var dataJson;
12+
13+
before(function() {
14+
dataSource = JSON.parse(fs.readFileSync(
15+
"test/assets/cloudwatch.data.json"));
16+
dataJson = JSON.parse(fs.readFileSync(
17+
"test/assets/cloudwatch.format.json"));
18+
});
19+
20+
it('should format parsed CloudWatch Logs data',
21+
function(done) {
22+
var config = {
23+
data: dataSource,
24+
dateField: 'date',
25+
setting: true
26+
};
27+
handler.process(config)
28+
.then(function(result) {
29+
assert.ok(result.hasOwnProperty('setting'),
30+
'process returns config object');
31+
assert.deepStrictEqual(result.data, dataJson,
32+
'CloudWatch Logs data formatted successfully');
33+
done();
34+
});
35+
});
36+
37+
it('should fail if malformed Cloudtrail data is provided',
38+
function(done) {
39+
var config = {
40+
data: dataJson,
41+
dateField: 'date',
42+
setting: true
43+
};
44+
handler.process(config)
45+
.catch(function(err) {
46+
assert.ok(err, 'error is thrown');
47+
done();
48+
});
49+
});
50+
});
51+
});

test/shipHttp.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* global beforeEach, describe, it */
2+
3+
var assert = require("assert");
4+
5+
var handler;
6+
7+
describe('handler/shipHttp.js', function() {
8+
describe('#process()', function() {
9+
beforeEach(function() {
10+
// Clear the module cache to reset overridden functions.
11+
delete require.cache[require.resolve('../handlers/shipHttp')];
12+
handler = require('../handlers/shipHttp');
13+
});
14+
15+
it('should ship data using the http module',
16+
function(done) {
17+
var http = require('http');
18+
var httpReqSent = false;
19+
var httpReqEnded = false;
20+
http.request = function(options, _callback) {
21+
var callback = _callback;
22+
assert.strictEqual(options.method, 'POST', 'HTTP method provided');
23+
assert.strictEqual(options.protocol, 'http:',
24+
'HTTP protocol provided');
25+
assert.strictEqual(options.hostname, 'mock', 'HTTP host provided');
26+
assert.strictEqual(options.pathname, '/logs', 'HTTP path provided');
27+
assert.strictEqual(options.query, 'app=abc123',
28+
'HTTP query provided');
29+
return {
30+
end: function() {
31+
httpReqEnded = true;
32+
callback({statusCode: 201});
33+
},
34+
on: function(event, _callback) {
35+
return;
36+
},
37+
write: function(data) {
38+
assert.strictEqual(data, 'test data 123456',
39+
'data written in request');
40+
httpReqSent = true;
41+
}
42+
};
43+
};
44+
var config = {
45+
http: {
46+
url: 'http://mock/logs?app=abc123'
47+
},
48+
data: 'test data 123456',
49+
test: 'test'
50+
};
51+
handler.process(config)
52+
.then(function(result) {
53+
assert.ok(httpReqSent, 'HTTP request sent');
54+
assert.ok(httpReqEnded, 'HTTP request closed');
55+
assert.strictEqual(result.test, 'test', 'config data returned');
56+
done();
57+
});
58+
});
59+
60+
it('should fail if the HTTP request throws an error',
61+
function(done) {
62+
var http = require('http');
63+
http.request = function(options, _callback) {
64+
return {
65+
end: function() {
66+
},
67+
on: function(event, callback) {
68+
callback(new Error('test error'));
69+
},
70+
write: function(data) {
71+
}
72+
};
73+
};
74+
var config = {
75+
http: {
76+
url: 'http://mock/logs?app=abc123'
77+
},
78+
data: 'test data 123456',
79+
test: 'test'
80+
};
81+
handler.process(config)
82+
.catch(function(err) {
83+
assert.ok(err, 'error is thrown');
84+
done();
85+
});
86+
});
87+
});
88+
});

test/shipTcp.spec.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* global beforeEach, describe, it */
2+
3+
var assert = require("assert");
4+
5+
var handler;
6+
7+
describe('handler/shipTcp.js', function() {
8+
describe('#process()', function() {
9+
beforeEach(function() {
10+
// Clear the module cache to reset overridden functions.
11+
delete require.cache[require.resolve('../handlers/shipTcp')];
12+
handler = require('../handlers/shipTcp');
13+
});
14+
15+
it('should ship data using the net module',
16+
function(done) {
17+
var net = require('net');
18+
var tcpReqSent = false;
19+
var tcpReqEnded = false;
20+
net.connect = function(port, host, _callback) {
21+
var closeCallback;
22+
assert.strictEqual(port, '7654', 'TCP port provided');
23+
assert.strictEqual(host, 'mock', 'TCP host provided');
24+
process.nextTick(_callback);
25+
return {
26+
end: function() {
27+
tcpReqEnded = true;
28+
closeCallback();
29+
},
30+
on: function(event, _callback) {
31+
closeCallback = _callback;
32+
},
33+
write: function(data, _callback) {
34+
assert.strictEqual(data, 'test data 123456',
35+
'data written in request');
36+
tcpReqSent = true;
37+
_callback();
38+
}
39+
};
40+
};
41+
var config = {
42+
tcp: {
43+
host: 'mock',
44+
port: '7654'
45+
},
46+
data: 'test data 123456',
47+
test: 'test'
48+
};
49+
handler.process(config)
50+
.then(function(result) {
51+
assert.ok(tcpReqSent, 'TCP request sent');
52+
assert.ok(tcpReqEnded, 'TCP request closed');
53+
assert.strictEqual(result.test, 'test', 'config data returned');
54+
done();
55+
});
56+
});
57+
58+
it('should fail if the TCP request throws an error',
59+
function(done) {
60+
var net = require('net');
61+
net.connect = function(port, host, _callback) {
62+
process.nextTick(_callback);
63+
return {
64+
end: function() {
65+
},
66+
on: function(event, _callback) {
67+
assert.strictEqual(event, 'close', 'TCP close event handled');
68+
_callback();
69+
},
70+
write: function(data, _callback) {
71+
}
72+
};
73+
};
74+
var config = {
75+
tcp: {
76+
host: 'mock',
77+
port: '7654'
78+
},
79+
data: 'test data 123456',
80+
test: 'test'
81+
};
82+
handler.process(config)
83+
.catch(function(err) {
84+
assert.ok(err, 'error is thrown');
85+
done();
86+
});
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)