Skip to content

Commit

Permalink
Merge pull request #2640 from achinchen/modify-response-http-code
Browse files Browse the repository at this point in the history
Semantic Status Code - modify on_request response from 200 to 204 in plugin/in_http
  • Loading branch information
repeatedly authored Oct 9, 2019
2 parents a14af30 + ee2ae7b commit 02c1e2b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class HttpInput < Input
config_param :cors_allow_origins, :array, default: nil
desc 'Respond with empty gif image of 1x1 pixel.'
config_param :respond_with_empty_img, :bool, default: false
desc 'Respond status code with 204.'
config_param :use_204_response, :bool, default: false

config_section :parse do
config_set_default :@type, 'in_http'
Expand Down Expand Up @@ -152,7 +154,11 @@ def on_request(path_info, params)
if @respond_with_empty_img
return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE]
else
return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
if @use_204_response
return ["204 No Content", {}]
else
return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
end
end
end

Expand Down Expand Up @@ -219,7 +225,11 @@ def on_request(path_info, params)
if @respond_with_empty_img
return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE]
else
return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
if @use_204_response
return ["204 No Content", {}]
else
return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
end
end
end

Expand Down
60 changes: 58 additions & 2 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def teardown
body_size_limit 10m
keepalive_timeout 5
respond_with_empty_img true
use_204_response false
]

def create_driver(conf=CONFIG)
Expand Down Expand Up @@ -549,8 +550,8 @@ def test_with_csv
assert_equal_event_time time, d.events[1][1]
end

def test_resonse_with_empty_img
d = create_driver(CONFIG + "respond_with_empty_img true")
def test_response_with_empty_img
d = create_driver(CONFIG)
assert_equal true, d.instance.respond_with_empty_img

time = event_time("2011-01-02 13:14:15 UTC")
Expand All @@ -577,6 +578,61 @@ def test_resonse_with_empty_img
assert_equal_event_time time, d.events[1][1]
end

def test_response_without_empty_img
d = create_driver(CONFIG + "respond_with_empty_img false")
assert_equal false, d.instance.respond_with_empty_img

time = event_time("2011-01-02 13:14:15 UTC")
time_i = time.to_i
events = [
["tag1", time, {"a"=>1}],
["tag2", time, {"a"=>2}],
]
res_codes = []
res_bodies = []

d.run do
events.each do |tag, _t, record|
res = post("/#{tag}", {"json"=>record.to_json, "time"=>time_i.to_s})
res_codes << res.code
end
end
assert_equal ["200", "200"], res_codes
assert_equal [], res_bodies
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_response_use_204_response
d = create_driver(CONFIG + %[
respond_with_empty_img false
use_204_response true
])
assert_equal true, d.instance.use_204_response

time = event_time("2011-01-02 13:14:15 UTC")
time_i = time.to_i
events = [
["tag1", time, {"a"=>1}],
["tag2", time, {"a"=>2}],
]
res_codes = []
res_bodies = []

d.run do
events.each do |tag, _t, record|
res = post("/#{tag}", {"json"=>record.to_json, "time"=>time_i.to_s})
res_codes << res.code
end
end
assert_equal ["204", "204"], res_codes
assert_equal [], res_bodies
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_allowed
d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]")
assert_equal ["http://foo.com"], d.instance.cors_allow_origins
Expand Down

0 comments on commit 02c1e2b

Please sign in to comment.