require 'helper' require 'fluent/test' require 'fluent/parser' module ParserTest include Fluent def str2time(str_time, format = nil) if format Time.strptime(str_time, format).to_i else Time.parse(str_time).to_i end end class TimeParserTest < ::Test::Unit::TestCase include ParserTest def test_call_with_parse parser = TextParser::TimeParser.new(nil) time = str2time('2013-09-18 12:00:00 +0900') assert_equal(time, parser.parse('2013-09-18 12:00:00 +0900')) end def test_call_with_strptime parser = TextParser::TimeParser.new('%d/%b/%Y:%H:%M:%S %z') time = str2time('28/Feb/2013:12:00:00 +0900', '%d/%b/%Y:%H:%M:%S %z') assert_equal(time, parser.parse('28/Feb/2013:12:00:00 +0900')) end def test_call_with_invalid_argument parser = TextParser::TimeParser.new(nil) [[], {}, nil, true, 10000].each { |v| assert_raise ArgumentError do parser.parse(v) end } end end class RegexpParserTest < ::Test::Unit::TestCase include ParserTest def internal_test_case(parser) text = '192.168.0.1 - - [28/Feb/2013:12:00:00 +0900] [14/Feb/2013:12:00:00 +0900] "true /,/user HTTP/1.1" 200 777' [parser.call(text), parser.call(text) { |time, record| return time, record}].each { |time, record| assert_equal(str2time('28/Feb/2013:12:00:00 +0900', '%d/%b/%Y:%H:%M:%S %z'), time) assert_equal({ 'user' => '-', 'flag' => true, 'code' => 200.0, 'size' => 777, 'date' => str2time('14/Feb/2013:12:00:00 +0900', '%d/%b/%Y:%H:%M:%S %z'), 'host' => '192.168.0.1', 'path' => ['/', '/user'] }, record) } end def test_call_with_typed # Use Regexp.new instead of // literal to avoid different parser behaviour in 1.9 and 2.0 internal_test_case(TextParser::RegexpParser.new(Regexp.new(%q!^(?[^ ]*) [^ ]* (?[^ ]*) \[(?