diff --git a/lib/fluent/plugin/out_uri_decode.rb b/lib/fluent/plugin/out_uri_decode.rb index 32af405..8636548 100644 --- a/lib/fluent/plugin/out_uri_decode.rb +++ b/lib/fluent/plugin/out_uri_decode.rb @@ -1,16 +1,16 @@ -class Fluent::URIDecoder < Fluent::Output +require 'fluent/plugin/output' +require 'uri' + +class Fluent::Plugin::URIDecoderOutput < Fluent::Plugin::Output Fluent::Plugin.register_output('uri_decode', self) - config_param :tag, :string, :default => nil - config_param :remove_prefix, :string, :default => nil - config_param :add_prefix, :string, :default => nil - config_param :key_name, :string, :default => nil - config_param :key_names, :string, :default => '' + helpers :event_emitter - def initialize - super - require 'uri' - end + config_param :tag, :string, default: nil + config_param :remove_prefix, :string, default: nil + config_param :add_prefix, :string, default: nil + config_param :key_name, :string, default: nil + config_param :key_names, :array, default: [] def configure(conf) super @@ -28,6 +28,8 @@ def configure(conf) if @add_prefix @added_prefix_string = @add_prefix + '.' end + @key_names << @key_name if @key_name + @key_names.uniq! end def tag_mangle(tag) @@ -50,21 +52,15 @@ def tag_mangle(tag) end end - def emit(tag, es, chain) + def process(tag, es) tag = tag_mangle(tag) - @_key_names ||= @key_names.split(/,\s*/) - @_key_names << @key_name if @key_name - @_key_names.uniq! - es.each do |time, record| - @_key_names.each do |key_name| + @key_names.each do |key_name| record[key_name] = URI.decode_www_form_component(record[key_name] || '') end router.emit(tag, time, record) end - - chain.next end end diff --git a/test/plugin/test_out_uri_decoder.rb b/test/plugin/test_out_uri_decoder.rb index efe917b..c29223e 100644 --- a/test/plugin/test_out_uri_decoder.rb +++ b/test/plugin/test_out_uri_decoder.rb @@ -1,7 +1,7 @@ require 'test_helper' require 'fluent/plugin/out_uri_decode' -class Fluent::URIDecorderTest < Test::Unit::TestCase +class Fluent::URIDecoderOutputTest < Test::Unit::TestCase CONFIG0 = %[ type uri_decode key_name encoded @@ -26,8 +26,8 @@ def setup Fluent::Test.setup end - def create_driver(conf=CONFIG0, tag='test') - Fluent::Test::OutputTestDriver.new(Fluent::URIDecoder, tag).configure(conf) + def create_driver(conf=CONFIG0) + Fluent::Test::Driver::Output.new(Fluent::Plugin::URIDecoderOutput).configure(conf) end def test_configure @@ -40,7 +40,7 @@ def test_configure assert_equal 'decoded', d.instance.add_prefix d = create_driver(CONFIG2) - assert_equal 'encoded, another_encoded', d.instance.key_names + assert_equal ['encoded', 'another_encoded'], d.instance.key_names end def test_tag_mangle @@ -55,65 +55,65 @@ def test_tag_mangle def test_emit_with_tag_specification d = create_driver(CONFIG0) - time = Time.now.to_i - d.run do - d.emit({'encoded' => '%23hash', 'value' => 1}, time) + time = event_time + d.run(default_tag: 'test') do + d.feed(time, {'encoded' => '%23hash', 'value' => 1}) end - emits = d.emits - assert_equal 1, emits.size - assert_equal 'decoded', emits[0][0] - assert_equal time, emits[0][1] - assert_equal '#hash', emits[0][2]['encoded'] + events = d.events + assert_equal 1, events.size + assert_equal 'decoded', events[0][0] + assert_equal time, events[0][1] + assert_equal '#hash', events[0][2]['encoded'] end def test_emit_with_prefix_specification - d = create_driver(CONFIG1, 'encoded.message') - time = Time.now.to_i - d.run do - d.emit({'encoded' => '%23hash', 'value' => 1}, time) + d = create_driver(CONFIG1) + time = event_time + d.run(default_tag: 'encoded.message') do + d.feed(time, {'encoded' => '%23hash', 'value' => 1}) end - emits = d.emits - assert_equal 1, emits.size - assert_equal 'decoded.message', emits[0][0] - assert_equal time, emits[0][1] - assert_equal '#hash', emits[0][2]['encoded'] + events = d.events + assert_equal 1, events.size + assert_equal 'decoded.message', events[0][0] + assert_equal time, events[0][1] + assert_equal '#hash', events[0][2]['encoded'] end def test_emit_with_multi_key_names - d = create_driver(CONFIG2, 'encoded.message') - time = Time.now.to_i - d.run do - d.emit({'encoded' => '%23hash', 'another_encoded' => '%23another_hash'}, time) + d = create_driver(CONFIG2) + time = event_time + d.run(default_tag: 'encoded.message') do + d.feed(time, {'encoded' => '%23hash', 'another_encoded' => '%23another_hash'}) end - emits = d.emits - assert_equal 1, emits.size - assert_equal 'decoded.message', emits[0][0] - assert_equal time, emits[0][1] - assert_equal '#hash', emits[0][2]['encoded'] - assert_equal '#another_hash', emits[0][2]['another_encoded'] + events = d.events + assert_equal 1, events.size + assert_equal 'decoded.message', events[0][0] + assert_equal time, events[0][1] + assert_equal '#hash', events[0][2]['encoded'] + assert_equal '#another_hash', events[0][2]['another_encoded'] end def test_multiple_emit - d = create_driver(CONFIG2, 'encoded.message') - time = Time.now.to_i - d.run do - d.emit({'encoded' => '%23hash', 'another_encoded' => '%23another_hash'}, time) + d = create_driver(CONFIG2) + time = event_time + d.run(default_tag: 'encoded.message') do + d.feed(time, {'encoded' => '%23hash', 'another_encoded' => '%23another_hash'}) end - emits = d.emits - emits << d.emits.flatten - assert_equal 2, emits.size - assert_equal 'decoded.message', emits[0][0] - assert_equal time, emits[0][1] - assert_equal '#hash', emits[0][2]['encoded'] - assert_equal '#another_hash', emits[0][2]['another_encoded'] - - assert_equal 'decoded.message', emits[1][0] - assert_equal time, emits[1][1] - assert_equal '#hash', emits[1][2]['encoded'] - assert_equal '#another_hash', emits[1][2]['another_encoded'] + events = d.events + events << d.events.flatten + assert_equal 2, events.size + assert_equal 'decoded.message', events[0][0] + assert_equal time, events[0][1] + assert_equal '#hash', events[0][2]['encoded'] + assert_equal '#another_hash', events[0][2]['another_encoded'] + + assert_equal 'decoded.message', events[1][0] + assert_equal time, events[1][1] + assert_equal '#hash', events[1][2]['encoded'] + assert_equal '#another_hash', events[1][2]['another_encoded'] end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 246f822..9654f43 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,21 +1,8 @@ require 'bundler' - -begin - Bundler.setup(:default, :development) -rescue Bundler::BundlerError => e - $stderr.puts e.message - $stderr.puts "Run `bundle install` to install missing gems" - exit e.status_code -end - +require 'bundler/setup' require 'fluent/test' +require 'fluent/test/helpers' +require 'fluent/test/driver/output' +require 'test-unit' -unless ENV.has_key?('VERBOSE') - nulllogger = Object.new - nulllogger.instance_eval {|obj| - def method_missing(method, *args) - # pass - end - } - $log = nulllogger -end +Test::Unit::TestCase.include(Fluent::Test::Helpers)