Skip to content

Commit

Permalink
[CLIENT] Added default value 'application/json' for the 'Content-Type…
Browse files Browse the repository at this point in the history
…' 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: elastic/elasticsearch#22691

Related:

* c22ec89
* logstash-plugins/logstash-input-elasticsearch#55
* logstash-plugins/logstash-input-elasticsearch#56
* elastic/elasticsearch#22691

Closes #400
  • Loading branch information
karmi committed Feb 7, 2017
1 parent c13cf5f commit 76f8679
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions elasticsearch-transport/lib/elasticsearch/transport/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions elasticsearch-transport/test/unit/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions elasticsearch-transport/test/unit/transport_curb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions elasticsearch-transport/test/unit/transport_faraday_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 76f8679

Please sign in to comment.