test/plugin/test_out_http.rb in fluent-plugin-out-http-0.1.0 vs test/plugin/test_out_http.rb in fluent-plugin-out-http-0.1.1

- old
+ new

@@ -1,6 +1,8 @@ -require 'json' +# -*- coding: utf-8 -*- +require 'uri' +require 'yajl' require 'fluent/test/http_output_test' require 'fluent/plugin/out_http' TEST_LISTEN_PORT = 5126 @@ -11,10 +13,11 @@ def setup Fluent::Test.setup @posts = [] @puts = [] @prohibited = 0 + @requests = 0 @auth = false @dummy_server_thread = Thread.new do srv = if ENV['VERBOSE'] WEBrick::HTTPServer.new({:BindAddress => '127.0.0.1', :Port => TEST_LISTEN_PORT}) else @@ -22,10 +25,11 @@ WEBrick::HTTPServer.new({:BindAddress => '127.0.0.1', :Port => TEST_LISTEN_PORT, :Logger => logger, :AccessLog => []}) end begin allowed_methods = %w(POST PUT) srv.mount_proc('/api/') { |req,res| + @requests += 1 unless allowed_methods.include? req.request_method res.status = 405 res.body = 'request method mismatch' next end @@ -39,11 +43,11 @@ # ok, authorization not required end record = {:auth => nil} if req.content_type == 'application/json' - record[:json] = JSON.parse(req.body) + record[:json] = Yajl.load(req.body) else record[:form] = Hash[*(req.body.split('&').map{|kv|kv.split('=')}.flatten)] end instance_variable_get("@#{req.request_method.downcase}s").push(record) @@ -141,10 +145,14 @@ CONFIG_PUT = %[ endpoint_url http://127.0.0.1:#{TEST_LISTEN_PORT}/api/ http_method put ] + CONFIG_HTTP_ERROR = %[ + endpoint_url https://127.0.0.1:#{TEST_LISTEN_PORT + 1}/api/ + ] + RATE_LIMIT_MSEC = 1200 CONFIG_RATE_LIMIT = %[ endpoint_url http://127.0.0.1:#{TEST_LISTEN_PORT}/api/ rate_limit_msec #{RATE_LIMIT_MSEC} @@ -164,20 +172,21 @@ assert_equal :json, d.instance.serializer end def test_emit_form d = create_driver - d.emit({ 'field1' => 50, 'field2' => 20, 'field3' => 10, 'otherfield' => 1 }) + d.emit({ 'field1' => 50, 'field2' => 20, 'field3' => 10, 'otherfield' => 1, 'binary' => "\xe3\x81\x82".force_encoding("ascii-8bit") }) d.run assert_equal 1, @posts.size record = @posts[0] assert_equal '50', record[:form]['field1'] assert_equal '20', record[:form]['field2'] assert_equal '10', record[:form]['field3'] assert_equal '1', record[:form]['otherfield'] + assert_equal URI.escape("あ"), record[:form]['binary'] assert_nil record[:auth] d.emit({ 'field1' => 50, 'field2' => 20, 'field3' => 10, 'otherfield' => 1 }) d.run @@ -202,21 +211,33 @@ assert_equal 0, @posts.size assert_equal 2, @puts.size end def test_emit_json + binary_string = "\xe3\x81\x82".force_encoding("ascii-8bit") d = create_driver CONFIG_JSON - d.emit({ 'field1' => 50, 'field2' => 20, 'field3' => 10, 'otherfield' => 1 }) + d.emit({ 'field1' => 50, 'field2' => 20, 'field3' => 10, 'otherfield' => 1, 'binary' => binary_string }) d.run assert_equal 1, @posts.size record = @posts[0] assert_equal 50, record[:json]['field1'] assert_equal 20, record[:json]['field2'] assert_equal 10, record[:json]['field3'] assert_equal 1, record[:json]['otherfield'] + assert_equal binary_string, record[:json]['binary'] assert_nil record[:auth] + end + + def test_http_error + d = create_driver CONFIG_HTTP_ERROR + d.emit({ 'field1' => 50 }) + d.run + # drive asserts the next output chain is called; + # so no exception means our plugin handled the error + + assert_equal 0, @requests end def test_rate_limiting d = create_driver CONFIG_RATE_LIMIT record = { :k => 1 }