elasticsearch-transport 7.13.3 → 7.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_helper.rb CHANGED
@@ -20,30 +20,17 @@ ELASTICSEARCH_HOSTS = if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOS
20
20
  hosts.split(',').map do |host|
21
21
  /(http\:\/\/)?(\S+)/.match(host)[2]
22
22
  end
23
+ else
24
+ ['localhost:9200']
23
25
  end.freeze
24
26
 
25
27
  TEST_HOST, TEST_PORT = ELASTICSEARCH_HOSTS.first.split(':') if ELASTICSEARCH_HOSTS
26
28
 
27
- RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
28
29
  JRUBY = defined?(JRUBY_VERSION)
29
30
 
30
- if RUBY_1_8 and not ENV['BUNDLE_GEMFILE']
31
- require 'rubygems'
32
- gem 'test-unit'
33
- end
34
-
35
- require 'rubygems' if RUBY_1_8
36
-
37
- if ENV['COVERAGE'] && ENV['CI'].nil? && !RUBY_1_8
31
+ if ENV['COVERAGE']
38
32
  require 'simplecov'
39
- SimpleCov.start { add_filter "/test|test_/" }
40
- end
41
-
42
- if ENV['CI'] && !RUBY_1_8
43
- require 'simplecov'
44
- require 'simplecov-rcov'
45
- SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
46
- SimpleCov.start { add_filter "/test|test_" }
33
+ SimpleCov.start { add_filter %r{^/test/} }
47
34
  end
48
35
 
49
36
  # Register `at_exit` handler for integration tests shutdown.
@@ -52,7 +39,6 @@ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
52
39
  at_exit { Elasticsearch::Test::IntegrationTestCase.__run_at_exit_hooks }
53
40
  end
54
41
 
55
- require 'test/unit' if RUBY_1_8
56
42
  require 'minitest/autorun'
57
43
  require 'minitest/reporters'
58
44
  require 'shoulda/context'
@@ -119,8 +105,7 @@ module Elasticsearch
119
105
  extend Elasticsearch::Extensions::Test::StartupShutdown
120
106
 
121
107
  shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? && Elasticsearch::Extensions::Test::Cluster.running? }
122
- context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
123
- end if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
108
+ end
124
109
  end
125
110
 
126
111
  module Test
@@ -129,7 +114,6 @@ module Elasticsearch
129
114
  extend Elasticsearch::Extensions::Test::Profiling
130
115
 
131
116
  shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? && Elasticsearch::Extensions::Test::Cluster.running? }
132
- context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
133
- end unless RUBY_1_8 || JRUBY
117
+ end unless JRUBY
134
118
  end
135
119
  end
@@ -26,7 +26,7 @@ class Elasticsearch::Transport::Transport::ResponseTest < Minitest::Test
26
26
 
27
27
  response = Elasticsearch::Transport::Transport::Response.new 200, body
28
28
  assert_equal 'UTF-8', response.body.encoding.name
29
- end unless RUBY_1_8
29
+ end
30
30
 
31
31
  end
32
32
  end
@@ -263,6 +263,15 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
263
263
  end
264
264
  end
265
265
 
266
+ should 'raise TooManyRequestsError on 429' do
267
+ @transport.expects(:get_connection).returns(stub_everything :failures => 1)
268
+ assert_raise Elasticsearch::Transport::Transport::Errors::TooManyRequests do
269
+ @transport.perform_request 'GET', '/' do
270
+ Elasticsearch::Transport::Transport::Response.new 429, 'ERROR'
271
+ end
272
+ end
273
+ end
274
+
266
275
  should "not raise an error when the :ignore argument has been passed" do
267
276
  @transport.stubs(:get_connection).returns(stub_everything :failures => 1)
268
277
 
@@ -310,7 +319,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
310
319
  @transport.perform_request('GET', '/', &@block)
311
320
  assert_equal 2, @transport.counter
312
321
  end
313
- end unless RUBY_1_8
322
+ end
314
323
 
315
324
  context "performing a request with retry on connection failures" do
316
325
  setup do
@@ -344,7 +353,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
344
353
  @transport.perform_request('GET', '/', &@block)
345
354
  end
346
355
  end
347
- end unless RUBY_1_8
356
+ end
348
357
 
349
358
  context "performing a request with retry on status" do
350
359
  setup do
@@ -391,7 +400,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
391
400
  @transport.perform_request('GET', '/', &@block)
392
401
  end
393
402
  end
394
- end unless RUBY_1_8
403
+ end
395
404
 
396
405
  context "logging" do
397
406
  setup do
@@ -447,7 +456,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
447
456
  assert_raise Elasticsearch::Transport::Transport::Errors::InternalServerError do
448
457
  @transport.perform_request('POST', '_search', &@block)
449
458
  end
450
- end unless RUBY_1_8
459
+ end
451
460
 
452
461
  should "not log a failed Elasticsearch request as fatal" do
453
462
  @block = Proc.new { |c, u| puts "ERROR" }
@@ -458,7 +467,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
458
467
 
459
468
  # No `BadRequest` error
460
469
  @transport.perform_request('POST', '_search', :ignore => 500, &@block)
461
- end unless RUBY_1_8
470
+ end
462
471
 
463
472
  should "log and re-raise a Ruby exception" do
464
473
  @block = Proc.new { |c, u| puts "ERROR" }
@@ -468,7 +477,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
468
477
  @transport.logger.expects(:fatal)
469
478
 
470
479
  assert_raise(Exception) { @transport.perform_request('POST', '_search', &@block) }
471
- end unless RUBY_1_8
480
+ end
472
481
  end
473
482
 
474
483
  context "tracing" do
@@ -522,7 +531,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
522
531
  assert_raise Elasticsearch::Transport::Transport::Errors::InternalServerError do
523
532
  @transport.perform_request('POST', '_search', &@block)
524
533
  end
525
- end unless RUBY_1_8
534
+ end
526
535
 
527
536
  end
528
537
 
@@ -84,7 +84,6 @@ else
84
84
  @transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
85
85
  @transport.connections.first.connection.expects(:body_str).returns('{"foo":"bar"}')
86
86
  @transport.connections.first.connection.expects(:header_str).returns('HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nContent-Length: 311\r\n\r\n')
87
-
88
87
  response = @transport.perform_request 'GET', '/'
89
88
 
90
89
  assert_equal 'application/json', response.headers['content-type']
@@ -17,166 +17,235 @@
17
17
 
18
18
  require 'test_helper'
19
19
 
20
- unless JRUBY
21
- version = ( defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'Ruby' ) + ' ' + RUBY_VERSION
22
- puts "SKIP: '#{File.basename(__FILE__)}' only supported on JRuby (you're running #{version})"
23
- else
20
+ if JRUBY
24
21
  require 'elasticsearch/transport/transport/http/manticore'
25
22
  require 'manticore'
26
23
 
27
- class Elasticsearch::Transport::Transport::HTTP::ManticoreTest < Minitest::Test
28
- include Elasticsearch::Transport::Transport::HTTP
29
-
30
- context "Manticore transport" do
31
- setup do
32
- @transport = Manticore.new :hosts => [ { :host => '127.0.0.1', :port => 8080 } ]
33
- end
34
-
35
- should "implement host_unreachable_exceptions" do
36
- assert_instance_of Array, @transport.host_unreachable_exceptions
37
- end
38
-
39
- should "implement __build_connections" do
40
- assert_equal 1, @transport.hosts.size
41
- assert_equal 1, @transport.connections.size
42
-
43
- assert_instance_of ::Manticore::Client, @transport.connections.first.connection
44
- end
45
-
46
- should "not close connections in __close_connections" do
47
- assert_equal 1, @transport.connections.size
48
- @transport.__close_connections
49
- assert_equal 1, @transport.connections.size
50
- end
51
-
52
- should "perform the request" do
53
- @transport.connections.first.connection.expects(:get).returns(stub_everything)
54
- @transport.perform_request 'GET', '/'
55
- end
56
-
57
- should "set body for GET request" do
58
- @transport.connections.first.connection.expects(:get).
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)
62
- @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
63
- end
64
-
65
- should "set body for PUT request" do
66
- @transport.connections.first.connection.expects(:put).
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)
70
- @transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
71
- end
72
-
73
- should "serialize the request body" do
74
- @transport.connections.first.connection.expects(:post).
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)
78
- @transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
79
- end
80
-
81
- should "set custom headers for PUT request" do
82
- @transport.connections.first.connection.expects(:put).
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)}})
86
- .returns(stub_everything)
87
- @transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', {"Content-Type" => "application/x-ndjson"}
88
- end
89
-
90
- should "not serialize a String request body" do
91
- @transport.connections.first.connection.expects(:post).
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)
95
- @transport.serializer.expects(:dump).never
96
- @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
97
- end
98
-
99
- should "set application/json header" do
100
- options = {
101
- :headers => { "content-type" => "application/json"}
102
- }
103
-
104
- transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
105
-
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 )
108
-
109
- response = transport.perform_request 'GET', '/', {}
110
- assert_equal response.status, 200
111
- end
112
-
113
- should "set headers from 'transport_options'" do
114
- options = {
115
- :transport_options => {
116
- :headers => { "Content-Type" => "foo/bar"}
117
- }
118
- }
119
-
120
- transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
121
-
122
- assert_equal('foo/bar', transport.connections.first.connection.instance_variable_get(:@options)[:headers]['Content-Type'])
123
- # TODO: Needs to check @request_options
124
- end
125
-
126
- should "handle HTTP methods" do
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)
137
-
138
- %w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
139
-
140
- assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' }
141
- end
142
-
143
- should "allow to set options for Manticore" do
144
- options = { :headers => {"User-Agent" => "myapp-0.0" }}
145
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
146
- transport.connections.first.connection
147
- .expects(:get)
148
- .with do |host, _options|
149
- assert_equal 'myapp-0.0', _options[:headers]['User-Agent']
150
- true
24
+ module Elasticsearch
25
+ module Transport
26
+ module Transport
27
+ module HTTP
28
+ class ManticoreTest < Minitest::Test
29
+ include Elasticsearch::Transport::Transport::HTTP
30
+
31
+ def common_headers
32
+ {
33
+ 'Content-Type' => 'application/json',
34
+ 'User-Agent' => @transport.send(:user_agent_header)
35
+ }
36
+ end
37
+
38
+ context 'Manticore transport' do
39
+ setup do
40
+ @transport = Manticore.new(hosts: [{ host: '127.0.0.1', port: 8080 }])
41
+ end
42
+
43
+ should 'implement host_unreachable_exceptions' do
44
+ assert_instance_of Array, @transport.host_unreachable_exceptions
45
+ end
46
+
47
+ should 'implement __build_connections' do
48
+ assert_equal 1, @transport.hosts.size
49
+ assert_equal 1, @transport.connections.size
50
+
51
+ assert_instance_of(::Manticore::Client, @transport.connections.first.connection)
52
+ end
53
+
54
+ should 'not close connections in __close_connections' do
55
+ assert_equal 1, @transport.connections.size
56
+ @transport.__close_connections
57
+ assert_equal 1, @transport.connections.size
58
+ end
59
+
60
+ should 'perform the request' do
61
+ @transport.connections.first.connection.expects(:get).returns(stub_everything)
62
+ response = @transport.perform_request('GET', '/')
63
+ end
64
+
65
+ should 'set body for GET request' do
66
+ @transport.connections.first.connection.expects(:get)
67
+ .with(
68
+ 'http://127.0.0.1:8080/',
69
+ {
70
+ body: '{"foo":"bar"}',
71
+ headers: common_headers
72
+ }
73
+ ).returns(stub_everything)
74
+ @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
75
+ end
76
+
77
+ should 'set body for PUT request' do
78
+ @transport.connections.first.connection.expects(:put)
79
+ .with(
80
+ 'http://127.0.0.1:8080/',
81
+ {
82
+ body: '{"foo":"bar"}',
83
+ headers: {
84
+ 'Content-Type' => 'application/json',
85
+ 'User-Agent' => @transport.send(:user_agent_header)
86
+ }
87
+ }
88
+ ).returns(stub_everything)
89
+ @transport.perform_request 'PUT', '/', {}, { foo: 'bar' }
90
+ end
91
+
92
+ should 'serialize the request body' do
93
+ @transport.connections.first.connection.expects(:post)
94
+ .with(
95
+ 'http://127.0.0.1:8080/',
96
+ {
97
+ body: '{"foo":"bar"}',
98
+ headers: {
99
+ 'Content-Type' => 'application/json',
100
+ 'User-Agent' => @transport.send(:user_agent_header)
101
+ }
102
+ }
103
+ ).returns(stub_everything)
104
+ @transport.perform_request 'POST', '/', {}, { 'foo' => 'bar' }
105
+ end
106
+
107
+ should 'set custom headers for PUT request' do
108
+ @transport.connections.first.connection.expects(:put)
109
+ .with(
110
+ 'http://127.0.0.1:8080/',
111
+ {
112
+ body: '{"foo":"bar"}',
113
+ headers: {
114
+ 'Content-Type' => 'application/json',
115
+ 'User-Agent' => @transport.send(:user_agent_header)
116
+ }
117
+ }
118
+ ).returns(stub_everything)
119
+ @transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', { 'Content-Type' => 'application/x-ndjson' }
120
+ end
121
+
122
+ should 'not serialize a String request body' do
123
+ @transport.connections.first.connection.expects(:post)
124
+ .with(
125
+ 'http://127.0.0.1:8080/',
126
+ {
127
+ body: '{"foo":"bar"}',
128
+ headers: {
129
+ 'Content-Type' => 'application/json',
130
+ 'User-Agent' => @transport.send(:user_agent_header)
131
+ }
132
+ }
133
+ ).returns(stub_everything)
134
+ @transport.serializer.expects(:dump).never
135
+ @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
136
+ end
137
+
138
+ should 'set application/json header' do
139
+ options = {
140
+ headers: { 'content-type' => 'application/json' }
141
+ }
142
+
143
+ transport = Manticore.new(hosts: [{ host: 'localhost', port: 8080 }], options: options)
144
+ transport.connections.first.connection.stub(
145
+ 'http://localhost:8080/',
146
+ body: '""',
147
+ headers: {
148
+ 'Content-Type' => 'application/x-ndjson',
149
+ 'User-Agent' => @transport.send(:user_agent_header)
150
+ },
151
+ code: 200
152
+ )
153
+ response = transport.perform_request('GET', '/', {})
154
+ assert_equal response.status, 200
155
+ end
156
+
157
+ should "set headers from 'transport_options'" do
158
+ options = {
159
+ transport_options: {
160
+ headers: { 'Content-Type' => 'foo/bar' }
161
+ }
162
+ }
163
+
164
+ transport = Manticore.new(hosts: [{ host: 'localhost', port: 8080 }], options: options)
165
+
166
+ assert_equal(
167
+ 'foo/bar',
168
+ transport.connections.first.connection.instance_variable_get(:@options)[:headers]['Content-Type']
169
+ )
170
+ # TODO: Needs to check @request_options
171
+ end
172
+
173
+ should 'handle HTTP methods' do
174
+ @transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
175
+ @transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
176
+ @transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
177
+ @transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
178
+ @transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080/', { headers: common_headers }).returns(stub_everything)
179
+
180
+ %w[HEAD GET PUT POST DELETE].each { |method| @transport.perform_request method, '/' }
181
+
182
+ assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' }
183
+ end
184
+
185
+ should 'allow to set options for Manticore' do
186
+ options = { headers: { 'User-Agent' => 'myapp-0.0' } }
187
+ transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
188
+ transport.connections.first.connection
189
+ .expects(:get)
190
+ .with do |_host, _options|
191
+ assert_equal 'myapp-0.0', _options[:headers]['User-Agent']
192
+ true
193
+ end
194
+ .returns(stub_everything)
195
+
196
+ transport.perform_request 'GET', '/', {}
197
+ end
198
+
199
+ should 'allow to set ssl options for Manticore' do
200
+ options = {
201
+ ssl: {
202
+ truststore: 'test.jks',
203
+ truststore_password: 'test',
204
+ verify: false
205
+ }
206
+ }
207
+
208
+ ::Manticore::Client.expects(:new).with(options)
209
+ transport = Manticore.new hosts: [{ host: 'foobar', port: 1234 }], options: options
210
+ end
211
+
212
+ should 'allow custom headers' do
213
+ transport_options = { headers: { 'Authorization' => 'Basic token' } }
214
+ transport = Manticore.new(
215
+ hosts: [{ host: 'foobar', port: 1234 }],
216
+ transport_options: transport_options
217
+ )
218
+
219
+ assert_equal(
220
+ transport.instance_variable_get(:@request_options)[:headers]['Authorization'],
221
+ 'Basic token'
222
+ )
223
+ transport.connections.first.connection
224
+ .expects(:get)
225
+ .with do |_host, _options|
226
+ assert_equal('Basic token', _options[:headers]['Authorization'])
227
+ true
228
+ end
229
+ .returns(stub_everything)
230
+
231
+ transport.perform_request('GET', '/', {})
232
+ end
233
+
234
+ should 'pass :transport_options to Manticore::Client' do
235
+ options = {
236
+ transport_options: { potatoes: 1 }
237
+ }
238
+
239
+ ::Manticore::Client.expects(:new).with(potatoes: 1, ssl: {})
240
+ transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
241
+ end
242
+ end
151
243
  end
152
- .returns(stub_everything)
153
-
154
- transport.perform_request 'GET', '/', {}
155
- end
156
-
157
- should "allow to set ssl options for Manticore" do
158
- options = {
159
- :ssl => {
160
- :truststore => "test.jks",
161
- :truststore_password => "test",
162
- :verify => false
163
- }
164
- }
165
-
166
- ::Manticore::Client.expects(:new).with(options)
167
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
168
- end
169
-
170
- should "pass :transport_options to Manticore::Client" do
171
- options = {
172
- :transport_options => { :potatoes => 1 }
173
- }
174
-
175
- ::Manticore::Client.expects(:new).with(:potatoes => 1, :ssl => {})
176
- transport = Manticore.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => options
244
+ end
177
245
  end
178
246
  end
179
-
180
247
  end
181
-
248
+ else
249
+ version = "#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'Ruby'} #{RUBY_VERSION}"
250
+ puts "SKIP: '#{File.basename(__FILE__)}' only supported on JRuby (you're running #{version})"
182
251
  end