From 76f86793cde0a0e7e816a743c73f69002a53483e Mon Sep 17 00:00:00 2001 From: Karel Minarik Date: Tue, 7 Feb 2017 10:41:06 +0100 Subject: [PATCH] [CLIENT] Added default value 'application/json' for the 'Content-Type' header In future, Elasticsearch will require specifying the format of data which is being sent (eg. when indexing a document). Current versions of Elasticsearch will start to print a warning to the deprecation log. A default value 'application/json' for the 'Content-Type' header has been added to prevent the deprecation messages and to be prepared for the requirement in next major version of Elasticsearch. See: https://github.com/elastic/elasticsearch/pull/22691 Related: * c22ec891b0be0e3e8f3c10fd7844adfbd2d608ed * https://github.com/logstash-plugins/logstash-input-elasticsearch/issues/55 * https://github.com/logstash-plugins/logstash-input-elasticsearch/pull/56 * https://github.com/elastic/elasticsearch/pull/22691 Closes #400 --- .../lib/elasticsearch/transport/client.rb | 3 +++ .../elasticsearch/transport/transport/http/curb.rb | 6 +++++- elasticsearch-transport/test/unit/client_test.rb | 5 +++++ .../test/unit/transport_curb_test.rb | 6 ++++++ .../test/unit/transport_faraday_test.rb | 11 +++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/elasticsearch-transport/lib/elasticsearch/transport/client.rb b/elasticsearch-transport/lib/elasticsearch/transport/client.rb index 2aa9134cd9..9bd6859dd2 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/client.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/client.rb @@ -102,6 +102,9 @@ def initialize(arguments={}, &block) @arguments[:transport_options].update(:request => { :timeout => @arguments[:request_timeout] } ) if @arguments[:request_timeout] + @arguments[:transport_options][:headers] ||= {} + @arguments[:transport_options][:headers].update 'Content-Type' => 'application/json' unless @arguments[:transport_options][:headers].keys.any? {|k| k.to_s.downcase =~ /content\-?\_?type/} + @send_get_body_as = @arguments[:send_get_body_as] || 'GET' transport_class = @arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS diff --git a/elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb b/elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb index c46ff64697..58c3d5c598 100644 --- a/elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb +++ b/elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb @@ -43,7 +43,11 @@ def perform_request(method, path, params={}, body=nil) # def __build_connection(host, options={}, block=nil) client = ::Curl::Easy.new - client.headers = {'User-Agent' => "Curb #{Curl::CURB_VERSION}"} + + headers = options[:headers] || {} + headers.update('User-Agent' => "Curb #{Curl::CURB_VERSION}") + + client.headers = headers client.url = __full_url(host) if host[:user] diff --git a/elasticsearch-transport/test/unit/client_test.rb b/elasticsearch-transport/test/unit/client_test.rb index 1baa1a24fe..bd5f8ea95a 100644 --- a/elasticsearch-transport/test/unit/client_test.rb +++ b/elasticsearch-transport/test/unit/client_test.rb @@ -73,6 +73,11 @@ def initialize(*); end assert_equal 120, client.transport.options[:transport_options][:request][:timeout] end + should "set the 'Content-Type' header to 'application/json' by default" do + client = Elasticsearch::Transport::Client.new + assert_equal 'application/json', client.transport.options[:transport_options][:headers]['Content-Type'] + end + context "when passed hosts" do should "have localhost by default" do c = Elasticsearch::Transport::Client.new diff --git a/elasticsearch-transport/test/unit/transport_curb_test.rb b/elasticsearch-transport/test/unit/transport_curb_test.rb index 06d8c9a163..3dc3127867 100644 --- a/elasticsearch-transport/test/unit/transport_curb_test.rb +++ b/elasticsearch-transport/test/unit/transport_curb_test.rb @@ -77,6 +77,12 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' } end + should "properly pass the Content-Type header option" do + transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => { :transport_options => { :headers => { 'Content-Type' => 'foo/bar' } } } + + assert_equal "foo/bar", transport.connections.first.connection.headers["Content-Type"] + end + should "allow to set options for Curb" do transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234 } ] do |curl| curl.headers["User-Agent"] = "myapp-0.0" diff --git a/elasticsearch-transport/test/unit/transport_faraday_test.rb b/elasticsearch-transport/test/unit/transport_faraday_test.rb index 41ae3a86dd..d486d74281 100644 --- a/elasticsearch-transport/test/unit/transport_faraday_test.rb +++ b/elasticsearch-transport/test/unit/transport_faraday_test.rb @@ -42,6 +42,17 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC @transport.perform_request 'POST', '/', {}, {:foo => 'bar'} end + should "properly pass the Content-Type header option" do + transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => { :transport_options => { :headers => { 'Content-Type' => 'foo/bar' } } } + + transport.connections.first.connection.expects(:run_request).with do |method, url, body, headers| + assert_equal 'foo/bar', headers['Content-Type'] + true + end.returns(stub_everything) + + transport.perform_request 'GET', '/' + end + should "serialize the request body" do @transport.connections.first.connection.expects(:run_request).returns(stub_everything) @transport.serializer.expects(:dump)