test/plugin/test_in_http.rb in fluentd-0.12.23 vs test/plugin/test_in_http.rb in fluentd-0.12.24
- old
+ new
@@ -1,7 +1,8 @@
require_relative '../helper'
require 'fluent/test'
+require 'fluent/plugin/in_http'
require 'net/http'
class HttpInputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
@@ -265,9 +266,82 @@
assert_equal "200", res.code
# Ruby returns ASCII-8 encoded string for GIF.
assert_equal Fluent::HttpInput::EMPTY_GIF_IMAGE, res.body.force_encoding("UTF-8")
}
end
+ 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
+
+ test_in_http_cros_allowed = nil
+ acao = nil
+
+ begin
+ d.run do
+ Net::HTTP.start("127.0.0.1", PORT) do |http|
+ req = Net::HTTP::Post.new("/foo/bar", {"Origin" => "http://foo.com"})
+ res = http.request(req)
+
+ acao = res["Access-Control-Allow-Origin"]
+ end
+ end
+ test_in_http_cros_allowed = true
+ rescue
+ test_in_http_cros_allowed = false
+ end
+
+ assert_equal true, test_in_http_cros_allowed
+ assert_equal "http://foo.com", acao
+ 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
+
+ test_in_http_cros_disallowed = nil
+ response_code = nil
+
+ begin
+ d.run do
+ Net::HTTP.start("127.0.0.1", PORT) do |http|
+ req = Net::HTTP::Post.new("/foo/bar", {"Origin" => "http://bar.com"})
+ res = http.request(req)
+
+ response_code = res.code
+ end
+ end
+ test_in_http_cros_disallowed = true
+ rescue
+ test_in_http_cros_disallowed = false
+ end
+
+ assert_equal true, test_in_http_cros_disallowed
+ assert_equal "403", response_code
+ end
+
+ $test_in_http_connection_object_ids = []
+ $test_in_http_content_types = []
+ $test_in_http_content_types_flag = false
+ module ContentTypeHook
+ def on_headers_complete(headers)
+ super
+ if $test_in_http_content_types_flag
+ $test_in_http_content_types << self.content_type
+ end
+ end
+
+ def on_message_begin
+ super
+ if $test_in_http_content_types_flag
+ $test_in_http_connection_object_ids << @io_handler.object_id
+ end
+ end
+ end
+
+ class Fluent::HttpInput::Handler
+ prepend ContentTypeHook
end
def test_if_content_type_is_initialized_properly
# This test is to check if Fluent::HttpInput::Handler's @content_type is initialized properly.
# Especially when in Keep-Alive and the second request has no 'Content-Type'.