test/plugin/test_in_http.rb in fluentd-1.2.2 vs test/plugin/test_in_http.rb in fluentd-1.2.3

- old
+ new

@@ -160,10 +160,33 @@ assert_instance_of Fluent::EventTime, d.events[1][1] assert_equal_event_time time, d.events[0][1] assert_equal_event_time time, d.events[1][1] end + def test_multi_json_with_nonexistent_time_key + d = create_driver(CONFIG + %[ + <parse> + time_key missing + </parse> + ]) + time = event_time("2011-01-02 13:14:15 UTC") + time_i = time.to_i + time_f = time.to_f + + records = [{"a" => 1, 'time' => time_i},{"a" => 2, 'time' => time_f}] + tag = "tag1" + res_codes = [] + d.run(expect_records: 2, timeout: 5) do + res = post("/#{tag}", {"json" => records.to_json}) + res_codes << res.code + end + assert_equal ["200"], res_codes + assert_equal 2, d.events.size + assert_not_equal time_i, d.events[0][1].sec # current time is used because "missing" field doesn't exist + assert_not_equal time_i, d.events[1][1].sec + end + def test_json_with_add_remote_addr d = create_driver(CONFIG + "add_remote_addr true") time = event_time("2011-01-02 13:14:15 UTC") time_i = time.to_i @@ -345,10 +368,42 @@ assert include_http_header?(d.events[0][2]) assert include_http_header?(d.events[1][2]) end + def test_multi_json_with_custom_parser + d = create_driver(CONFIG + %[ + <parse> + @type json + keep_time_key true + time_key foo + time_format %iso8601 + </parse> + ]) + + time = event_time("2011-01-02 13:14:15 UTC") + time_s = Time.at(time).iso8601 + + records = [{"foo"=>time_s,"bar"=>"test1"},{"foo"=>time_s,"bar"=>"test2"}] + tag = "tag1" + res_codes = [] + + d.run(expect_records: 2, timeout: 5) do + res = post("/#{tag}", records.to_json, {"Content-Type"=>"application/octet-stream"}) + res_codes << res.code + end + assert_equal ["200"], res_codes + + assert_equal "tag1", d.events[0][0] + assert_equal_event_time time, d.events[0][1] + assert_equal d.events[0][2], records[0] + + assert_equal "tag1", d.events[1][0] + assert_equal_event_time time, d.events[1][1] + assert_equal d.events[1][2], records[1] + end + def test_application_json d = create_driver time = event_time("2011-01-02 13:14:15 UTC") time_i = time.to_i events = [ @@ -547,10 +602,58 @@ assert_equal events, d.events assert_equal_event_time time, d.events[0][1] assert_equal_event_time time, d.events[1][1] end + def test_content_encoding_gzip + d = create_driver + + time = event_time("2011-01-02 13:14:15 UTC") + events = [ + ["tag1", time, {"a"=>1}], + ["tag2", time, {"a"=>2}], + ] + res_codes = [] + res_headers = [] + + d.run do + events.each do |tag, time, record| + header = {'Content-Type'=>'application/json', 'Content-Encoding'=>'gzip'} + res = post("/#{tag}?time=#{time}", compress_gzip(record.to_json), header) + res_codes << res.code + end + end + assert_equal ["200", "200"], res_codes + assert_equal events, d.events + assert_equal_event_time time, d.events[0][1] + assert_equal_event_time time, d.events[1][1] + end + + def test_content_encoding_deflate + d = create_driver + + time = event_time("2011-01-02 13:14:15 UTC") + events = [ + ["tag1", time, {"a"=>1}], + ["tag2", time, {"a"=>2}], + ] + res_codes = [] + res_headers = [] + + d.run do + events.each do |tag, time, record| + header = {'Content-Type'=>'application/msgpack', 'Content-Encoding'=>'deflate'} + res = post("/#{tag}?time=#{time}", Zlib.deflate(record.to_msgpack), header) + res_codes << res.code + end + end + assert_equal ["200", "200"], res_codes + assert_equal events, d.events + assert_equal_event_time time, d.events[0][1] + assert_equal_event_time time, d.events[1][1] + end + def test_cors_disallowed d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]") assert_equal ["http://foo.com"], d.instance.cors_allow_origins time = event_time("2011-01-02 13:14:15 UTC") @@ -635,9 +738,16 @@ header['Content-Type'] = 'application/x-www-form-urlencoded' end req.set_form_data(params) end http.request(req) + end + + def compress_gzip(data) + io = StringIO.new + io.binmode + Zlib::GzipWriter.wrap(io) { |gz| gz.write data } + return io.string end def include_http_header?(record) record.keys.find { |header| header.start_with?('HTTP_') } end