Skip to content

Commit fc0de2b

Browse files
Merge pull request #11 from HatsuneMiku3939/feature/request-method
add support of request method
2 parents 5595b4f + 94e6d99 commit fc0de2b

File tree

5 files changed

+124
-5
lines changed

5 files changed

+124
-5
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ $ bundle
3434

3535
## Example
3636

37+
You can found more examples in `test/plugin/test_in_http_pull.rb`
38+
3739
### Monitoring http status code only
3840
```
3941
<source>
@@ -133,7 +135,17 @@ for more detail.
133135

134136
### status_only (bool) (optional, default: false)
135137

136-
If status_only is true, body is not parsed.
138+
If `status_only` is true, body is not parsed.
139+
140+
### http_method (enum) (optional, default: :get)
141+
142+
The http request method for each requests. Avaliable options are listed below.
143+
144+
* `get`
145+
* `post`
146+
* `delete`
147+
148+
If `status_only` is true, `http_method` was override to `head`
137149

138150
### timeout (time) (optional, default: 10s)
139151

@@ -151,6 +163,14 @@ The user for basic auth
151163

152164
The password for basic auth
153165

166+
### response_header (section) (optional, default: nil)
167+
168+
The name of response header for capture.
169+
170+
### request_header (section) (optional, default: nil)
171+
172+
The name, value pair of custom reuqest header.
173+
154174
## In case of remote error
155175

156176
### Can receive response from remote

fluent-plugin-http-pull.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
33

44
Gem::Specification.new do |spec|
55
spec.name = "fluent-plugin-http-pull"
6-
spec.version = "0.5.0"
6+
spec.version = "0.6.0"
77
spec.authors = ["filepang"]
88
spec.email = ["[email protected]"]
99

lib/fluent/plugin/in_http_pull.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Fluent
2020
module Plugin
2121
class HttpPullInput < Fluent::Plugin::Input
2222
Fluent::Plugin.register_input("http_pull", self)
23-
2423
helpers :timer, :parser, :compat_parameters
2524

2625
def initialize
@@ -29,19 +28,28 @@ def initialize
2928

3029
desc 'The tag of the event.'
3130
config_param :tag, :string
31+
3232
desc 'The url of monitoring target'
3333
config_param :url, :string
34+
3435
desc 'The interval time between periodic request'
3536
config_param :interval, :time
37+
3638
desc 'status_only'
3739
config_param :status_only, :bool, default: false
38-
desc 'The timeout stime of each request'
40+
41+
desc 'The http method for each request'
42+
config_param :http_method, :enum, list: [:get, :post, :delete], default: :get
43+
44+
desc 'The timeout second of each request'
3945
config_param :timeout, :time, default: 10
46+
4047
desc 'The HTTP proxy URL to use for each requests'
4148
config_param :proxy, :string, default: nil
4249

4350
desc 'user of basic auth'
4451
config_param :user, :string, default: nil
52+
4553
desc 'password of basic auth'
4654
config_param :password, :string, default: nil
4755

@@ -69,6 +77,8 @@ def configure(conf)
6977

7078
[header.to_sym, value]
7179
end.to_h
80+
81+
@http_method = :head if @status_only
7282
end
7383

7484
def start
@@ -81,7 +91,7 @@ def on_timer
8191
record = { "url" => @url }
8292

8393
begin
84-
request_options = { method: :get, url: @url, timeout: @timeout }
94+
request_options = { method: @http_method, url: @url, timeout: @timeout }
8595

8696
request_options[:proxy] = @proxy if @proxy
8797
request_options[:user] = @user if @user

test/helper/stub_server.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
require 'webrick'
22

3+
class DeleteService < WEBrick::HTTPServlet::AbstractServlet
4+
def service(req, res)
5+
if req.request_method != "DELETE"
6+
res.status = 405
7+
else
8+
res.status = 200
9+
res['Content-Type'] = 'application/json'
10+
res.body = '{ "status": "OK" }'
11+
end
12+
end
13+
end
14+
315
class StubServer
416
def initialize
517
create_server
@@ -12,6 +24,9 @@ def initialize
1224
@server.mount_proc '/redirect', &method(:redirect)
1325
@server.mount_proc '/protected', &method(:protected)
1426
@server.mount_proc '/custom_header', &method(:custom_header)
27+
28+
@server.mount_proc '/method_post', &method(:method_post)
29+
@server.mount '/method_delete', DeleteService
1530
end
1631

1732
def start
@@ -90,4 +105,15 @@ def custom_header(req, res)
90105
res['Content-Type'] = 'application/json'
91106
res.body = '{ "status": "OK" }'
92107
end
108+
109+
def method_post(req, res)
110+
if req.request_method != "POST"
111+
res.status = 405
112+
else
113+
res.status = 200
114+
res['Content-Type'] = 'application/json'
115+
res.body = '{ "status": "OK" }'
116+
end
117+
end
93118
end
119+

test/plugin/test_in_http_pull.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class HttpPullInputTest < Test::Unit::TestCase
5858

5959
assert_equal(nil, d.instance.proxy)
6060
end
61+
62+
test 'http_method' do
63+
d = create_driver TEST_DEFAULT_VALUE_CONFIG
64+
assert_equal("test", d.instance.tag)
65+
66+
assert_equal(:get, d.instance.http_method)
67+
end
6168
end
6269

6370
sub_test_case "success case" do
@@ -556,6 +563,62 @@ class HttpPullInputTest < Test::Unit::TestCase
556563
end
557564
end
558565

566+
sub_test_case "request method" do
567+
TEST_INTERVAL_3_POST = %[
568+
tag test
569+
url http://127.0.0.1:3939/method_post
570+
571+
interval 3s
572+
format json
573+
http_method post
574+
]
575+
576+
TEST_INTERVAL_3_DELETE = %[
577+
tag test
578+
url http://127.0.0.1:3939/method_delete
579+
580+
interval 3s
581+
format json
582+
http_method delete
583+
]
584+
585+
test 'interval 3 with :post' do
586+
d = create_driver TEST_INTERVAL_3_POST
587+
assert_equal("test", d.instance.tag)
588+
assert_equal(3, d.instance.interval)
589+
590+
d.run(timeout: 8) do
591+
sleep 7
592+
end
593+
assert_equal(2, d.events.size)
594+
595+
d.events.each do |tag, time, record|
596+
assert_equal("test", tag)
597+
598+
assert_equal({"url"=>"http://127.0.0.1:3939/method_post","status"=>200, "message"=>{"status"=>"OK"}}, record)
599+
assert(time.is_a?(Fluent::EventTime))
600+
end
601+
end
602+
603+
test 'interval 3 with :delete' do
604+
d = create_driver TEST_INTERVAL_3_DELETE
605+
assert_equal("test", d.instance.tag)
606+
assert_equal(3, d.instance.interval)
607+
608+
d.run(timeout: 8) do
609+
sleep 7
610+
end
611+
assert_equal(2, d.events.size)
612+
613+
d.events.each do |tag, time, record|
614+
assert_equal("test", tag)
615+
616+
assert_equal({"url"=>"http://127.0.0.1:3939/method_delete","status"=>200, "message"=>{"status"=>"OK"}}, record)
617+
assert(time.is_a?(Fluent::EventTime))
618+
end
619+
end
620+
end
621+
559622
private
560623

561624
def create_driver(conf)

0 commit comments

Comments
 (0)