diff --git a/lib/elasticsearch/transport/client.rb b/lib/elasticsearch/transport/client.rb index 2aa9134..9bd6859 100644 --- a/lib/elasticsearch/transport/client.rb +++ b/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/lib/elasticsearch/transport/transport/http/curb.rb b/lib/elasticsearch/transport/transport/http/curb.rb index c46ff64..58c3d5c 100644 --- a/lib/elasticsearch/transport/transport/http/curb.rb +++ b/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/test/unit/client_test.rb b/test/unit/client_test.rb index 1baa1a2..bd5f8ea 100644 --- a/test/unit/client_test.rb +++ b/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/test/unit/transport_curb_test.rb b/test/unit/transport_curb_test.rb index 06d8c9a..3dc3127 100644 --- a/test/unit/transport_curb_test.rb +++ b/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/test/unit/transport_faraday_test.rb b/test/unit/transport_faraday_test.rb index 41ae3a8..d486d74 100644 --- a/test/unit/transport_faraday_test.rb +++ b/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)