Skip to content

Commit 2cb45d7

Browse files
authored
http_server: add an accessor to read request header (#4903)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: I'm creating a plugin using http_server, but I need to change the processing depending on the received request header. So, I want an accessor to read request header in `Fluent::PluginHelper::HttpServer::Request` **Docs Changes**: TODO: https://docs.fluentd.org/plugin-helper-overview/api-plugin-helper-http_server#request **Release Note**: * `http_server` helper: add `header` method for `Request`. --------- Signed-off-by: Shizuo Fujita <[email protected]>
1 parent 83c19d1 commit 2cb45d7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/fluent/plugin_helper/http_server/request.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515
#
1616

17+
require 'cgi'
1718
require 'async/http/protocol'
1819
require 'fluent/plugin_helper/http_server/methods'
1920

@@ -29,6 +30,10 @@ def initialize(request)
2930
@path, @query_string = path.split('?', 2)
3031
end
3132

33+
def headers
34+
@request.headers
35+
end
36+
3237
def query
3338
@query_string && CGI.parse(@query_string)
3439
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require_relative '../../helper'
2+
require 'flexmock/test_unit'
3+
require 'fluent/plugin_helper/http_server/request'
4+
5+
class HttpHelperRequestTest < Test::Unit::TestCase
6+
def test_request
7+
headers = Protocol::HTTP::Headers.new({ 'Content-Type' => 'text/html', 'Content-Encoding' => 'gzip' })
8+
req = flexmock('request', path: '/path?foo=42', headers: headers)
9+
10+
request = Fluent::PluginHelper::HttpServer::Request.new(req)
11+
12+
assert_equal('/path', request.path)
13+
assert_equal('foo=42', request.query_string)
14+
assert_equal({'foo'=>['42']}, request.query)
15+
assert_equal('text/html', request.headers['content-type'])
16+
assert_equal(['gzip'], request.headers['content-encoding'])
17+
end
18+
end

0 commit comments

Comments
 (0)