Skip to content

Commit 7d695cd

Browse files
committed
add tests
1 parent d846db2 commit 7d695cd

File tree

6 files changed

+135
-2
lines changed

6 files changed

+135
-2
lines changed

.npmignore

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

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
"homepage": "https://github.com/claygregory/node-cloudfront-log-parser",
88
"license": "MIT",
99
"main": "index.js",
10-
"dependencies": {
10+
"scripts": {
11+
"test": "./node_modules/.bin/mocha"
1112
},
13+
"dependencies": {},
1214
"devDependencies": {
13-
"eslint": "^3.13.1"
15+
"eslint": "^3.13.1",
16+
"mocha": "^3.2.0"
1417
}
1518
}

test/fixtures/rtmp.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Version: 1.0
2+
#Fields: date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid
3+
2010-03-12 23:51:20 SEA4 192.0.2.147 connect 2014 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=204 LNX%2010,0,32,18 - - - -
4+
2010-03-12 23:51:21 SEA4 192.0.2.222 play 3914 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=2014 LNX%2010,0,32,18 myvideo p=2&q=44 flv 1
5+
2010-03-12 23:53:44 SEA4 192.0.2.4 stop 323914 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=2014 LNX%2010,0,32,18 dir/other/myvideo p=2&q=44 flv 1
6+
2010-03-12 23:53:44 SEA4 192.0.2.103 play 8783724 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=2014 LNX%2010,0,32,18 dir/favs/myothervideo p=42&q=147 mp4 2
7+
2010-03-12 23:56:21 SEA4 192.0.2.199 stop 429822014 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=2014 LNX%2010,0,32,18 dir/favs/myothervideo p=42&q=147 mp4 2
8+
2010-03-12 23:59:44 SEA4 192.0.2.14 disconnect 429824092 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=2014 LNX%2010,0,32,18 - - - -

test/fixtures/web.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Version: 1.0
2+
#Fields: date time x-edge-location sc-bytes c-ip cs-method cs(Host) cs-uri-stem sc-status cs(Referer) cs(User-Agent) cs-uri-query cs(Cookie) x-edge-result-type x-edge-request-id x-host-header cs-protocol cs-bytes time-taken x-forwarded-for ssl-protocol ssl-cipher x-edge-response-result-type cs-protocol-version
3+
2014-05-23 01:13:11 FRA2 182 192.0.2.10 GET d111111abcdef8.cloudfront.net /view/my/file.html 200 www.displaymyfiles.com Mozilla/5.0%2520(iPhone;%2520CPU%2520iPhone%2520OS%252010_2_1%2520like%2520Mac%2520OS%2520X)%2520AppleWebKit/602.4.6%2520(KHTML,%2520like%2520Gecko)%2520Version/10.0%2520Mobile/14D27%2520Safari/602.1 - zip=98101 RefreshHit MRVMF7KydIvxMWfJIglgwHQwZsbG2IhRJ07sn9AkKUFSHS9EXAMPLE== d111111abcdef8.cloudfront.net http - 0.001 - - - RefreshHit HTTP/1.1
4+
2014-05-23 01:13:12 LAX1 2390282 192.0.2.202 GET d111111abcdef8.cloudfront.net /soundtrack/happy.mp3 304 www.unknownsingers.com Mozilla/5.0%2520(iPhone;%2520CPU%2520iPhone%2520OS%252010_2_1%2520like%2520Mac%2520OS%2520X)%2520AppleWebKit/602.4.6%2520(KHTML,%2520like%2520Gecko)%2520Version/10.0%2520Mobile/14D27%2520Safari/602.1 a=b&c=d zip=50158 Hit xGN7KWpVEmB9Dp7ctcVFQC4E-nrcOcEKS3QyAez--06dV7TEXAMPLE== d111111abcdef8.cloudfront.net http - 0.002 - - - Hit HTTP/1.1

test/parse.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const CloudFrontParser = require('../');
8+
9+
/* eslint-env node, mocha */
10+
11+
const web_example = fs.readFileSync(path.join(__dirname, './fixtures/web.txt'), 'utf-8');
12+
const rtmp_example = fs.readFileSync(path.join(__dirname, './fixtures/rtmp.txt'), 'utf-8');
13+
14+
describe('parse', function () {
15+
16+
it('should parse web distribution v1.0 logs without error', function () {
17+
CloudFrontParser.parse(web_example, { format: 'web' });
18+
});
19+
20+
it('should parse RTMP distribution v1.0 logs without error', function () {
21+
CloudFrontParser.parse(rtmp_example, { format: 'rtmp' });
22+
});
23+
24+
it('should create a single object out of each line of web log, ignoring comments', function () {
25+
const result = CloudFrontParser.parse(web_example, { format: 'web' });
26+
assert.equal(2, result.length);
27+
});
28+
29+
it('should create a single object out of each line of rtmp log, ignoring comments', function () {
30+
const result = CloudFrontParser.parse(rtmp_example, { format: 'rtmp' });
31+
assert.equal(6, result.length);
32+
});
33+
34+
it('should default to web if format unspecified', function () {
35+
const result = CloudFrontParser.parse(web_example);
36+
37+
assert.equal(2, result.length);
38+
assert.equal(24, Object.keys(result[0]).length);
39+
});
40+
41+
it('should error on unrecognized format option', function () {
42+
assert.throws(CloudFrontParser.parse.bind(web_example, { format: 'not-valid' }), Error, 'Format not recognized: not-valid');
43+
});
44+
45+
it('should map each web log field into correct result field', function () {
46+
const result = CloudFrontParser.parse(web_example, { format: 'web' });
47+
48+
assert.equal('2014-05-23', result[0]['date']);
49+
assert.equal('FRA2', result[0]['x-edge-location']);
50+
assert.equal('/view/my/file.html', result[0]['cs-uri-stem']);
51+
assert.equal('RefreshHit', result[0]['x-edge-response-result-type']);
52+
assert.equal('LAX1', result[1]['x-edge-location']);
53+
assert.equal('/soundtrack/happy.mp3', result[1]['cs-uri-stem']);
54+
});
55+
56+
it('should map each rtmp log field into correct result field', function () {
57+
const result = CloudFrontParser.parse(rtmp_example, { format: 'rtmp' });
58+
59+
assert.equal('2010-03-12', result[0]['date']);
60+
assert.equal('SEA4', result[0]['x-edge-location']);
61+
assert.equal('myvideo', result[1]['x-sname']);
62+
assert.equal('flv', result[1]['x-file-ext']);
63+
assert.equal('2', result[4]['x-sid']);
64+
assert.equal('disconnect', result[5]['x-event']);
65+
});
66+
67+
it('should correctly decode percent-encoded fields', function () {
68+
const result = CloudFrontParser.parse(web_example, { format: 'web' });
69+
assert.equal('Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1', result[0]['cs-user-agent']);
70+
});
71+
72+
it('should handle Buffers as well as strings', function () {
73+
const result = CloudFrontParser.parse(Buffer.from(web_example), { format: 'web' });
74+
assert.equal(2, result.length);
75+
});
76+
77+
it('should call the callback function when specified', function (done) {
78+
CloudFrontParser.parse(Buffer.from(web_example), { format: 'web' }, function (err, result) {
79+
assert(result);
80+
done();
81+
});
82+
});
83+
});

test/stream_transform.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const CloudFrontParser = require('../');
8+
9+
/* eslint-env node, mocha */
10+
11+
describe('stream_transform', function () {
12+
13+
it('should emit each line of log on read', function (done) {
14+
15+
let i = 0;
16+
const parser = new CloudFrontParser({ format: 'web' });
17+
parser.on('readable', function (){
18+
let access;
19+
while ((access = parser.read()) !== null) {
20+
assert(access);
21+
i++;
22+
}
23+
});
24+
25+
parser.on('end', function (){
26+
assert(2, i);
27+
done();
28+
});
29+
30+
fs.createReadStream(path.join(__dirname, './fixtures/web.txt'))
31+
.pipe(parser);
32+
});
33+
34+
});

0 commit comments

Comments
 (0)