Skip to content

Commit 56b1cbb

Browse files
committed
[CLIENT] Handle headers when using JRuby and Manticore
1 parent 6b0742a commit 56b1cbb

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/http/manticore.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def perform_request(method, path, params={}, body=nil, headers=nil)
111111
def __build_connections
112112
@request_options = {}
113113
apply_headers(@request_options, options[:transport_options])
114-
apply_headers(@request_options, options[:headers])
114+
apply_headers(@request_options, options)
115115

116116
Connections::Collection.new \
117117
:connections => hosts.map { |host|
@@ -155,17 +155,10 @@ def host_unreachable_exceptions
155155
private
156156

157157
def apply_headers(request_options, options)
158-
headers = (options[:headers] || {}).inject({}) do |h, (k, v)|
159-
if k.to_s.downcase =~ /content\-?\_?type/
160-
h[CONTENT_TYPE_STR] = v
161-
else
162-
h[k] = v
163-
end
164-
h
165-
end
166-
headers[CONTENT_TYPE_STR] = 'application/json' unless headers[CONTENT_TYPE_STR]
167-
headers[USER_AGENT_STR] = user_agent_header(request_options) unless headers[USER_AGENT_STR]
168-
request_options.merge!(headers)
158+
headers = (options && options[:headers]) || {}
159+
headers[CONTENT_TYPE_STR] = find_key_value(headers, CONTENT_TYPE_REGEX) || DEFAULT_CONTENT_TYPE
160+
headers[USER_AGENT_STR] = find_key_value(headers, USER_AGENT_REGEX) || user_agent_header
161+
request_options.merge!(headers: headers)
169162
end
170163

171164
def user_agent_header
@@ -174,7 +167,7 @@ def user_agent_header
174167
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
175168
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
176169
end
177-
meta << "Manticore #{Manticore::Manticore}"
170+
meta << "Manticore #{::Manticore::VERSION}"
178171
"elasticsearch-ruby/#{VERSION} (#{meta.join('; ')})"
179172
end
180173
end

elasticsearch-transport/test/unit/transport_manticore_test.rb

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,42 @@ class Elasticsearch::Transport::Transport::HTTP::ManticoreTest < Minitest::Test
5656

5757
should "set body for GET request" do
5858
@transport.connections.first.connection.expects(:get).
59-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'}).returns(stub_everything)
59+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
60+
:headers => {"Content-Type" => "application/json",
61+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
6062
@transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
6163
end
6264

6365
should "set body for PUT request" do
6466
@transport.connections.first.connection.expects(:put).
65-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'}).returns(stub_everything)
67+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
68+
:headers => {"Content-Type" => "application/json",
69+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
6670
@transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
6771
end
6872

6973
should "serialize the request body" do
7074
@transport.connections.first.connection.expects(:post).
71-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'}).returns(stub_everything)
75+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
76+
:headers => {"Content-Type" => "application/json",
77+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
7278
@transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
7379
end
7480

7581
should "set custom headers for PUT request" do
7682
@transport.connections.first.connection.expects(:put).
77-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}', :headers => {"Content-Type" => "application/x-ndjson"}})
83+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
84+
:headers => {"Content-Type" => "application/json",
85+
"User-Agent" => @transport.send(:user_agent_header)}})
7886
.returns(stub_everything)
7987
@transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', {"Content-Type" => "application/x-ndjson"}
8088
end
8189

8290
should "not serialize a String request body" do
8391
@transport.connections.first.connection.expects(:post).
84-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'}).returns(stub_everything)
92+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
93+
:headers => {"Content-Type" => "application/json",
94+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
8595
@transport.serializer.expects(:dump).never
8696
@transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
8797
end
@@ -93,7 +103,8 @@ class Elasticsearch::Transport::Transport::HTTP::ManticoreTest < Minitest::Test
93103

94104
transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
95105

96-
transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"content-type" => "application/json"}, :code => 200 )
106+
transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"Content-Type" => "application/x-ndjson",
107+
"User-Agent" => @transport.send(:user_agent_header)}, :code => 200 )
97108

98109
response = transport.perform_request 'GET', '/', {}
99110
assert_equal response.status, 200
@@ -113,11 +124,16 @@ class Elasticsearch::Transport::Transport::HTTP::ManticoreTest < Minitest::Test
113124
end
114125

115126
should "handle HTTP methods" do
116-
@transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', {}).returns(stub_everything)
117-
@transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080//', {}).returns(stub_everything)
118-
@transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080//', {}).returns(stub_everything)
119-
@transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080//', {}).returns(stub_everything)
120-
@transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080//', {}).returns(stub_everything)
127+
@transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
128+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
129+
@transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
130+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
131+
@transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
132+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
133+
@transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
134+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
135+
@transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
136+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
121137

122138
%w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
123139

0 commit comments

Comments
 (0)