Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic Status Code - modify on_request response from 200 to 204 in plugin/in_http #2640

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def close
super
end

def on_request(path_info, params)
def on_request(path_info, params, use_204_response=false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do users set use_204_response?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I think that is my misunderstanding.
I fixed it and add config_param in in_http.

This is my first time to PR with Ruby.
let me know if anything not suitable.
Thanks for review.

begin
path = path_info[1..-1] # remove /
tag = path.split('/').join('.')
Expand All @@ -152,7 +152,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 +223,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
30 changes: 28 additions & 2 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,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 +577,32 @@ 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_cors_allowed
d = create_driver(CONFIG + "cors_allow_origins [\"http://foo.com\"]")
assert_equal ["http://foo.com"], d.instance.cors_allow_origins
Expand Down