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+ } ) ;
0 commit comments