elasticsearch-transport 7.13.3 → 7.17.11

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +9 -9
  3. data/Gemfile-faraday1.gemfile +47 -0
  4. data/README.md +3 -8
  5. data/Rakefile +47 -10
  6. data/elasticsearch-transport.gemspec +17 -18
  7. data/lib/elasticsearch/transport/client.rb +34 -3
  8. data/lib/elasticsearch/transport/transport/base.rb +41 -22
  9. data/lib/elasticsearch/transport/transport/connections/connection.rb +2 -1
  10. data/lib/elasticsearch/transport/transport/errors.rb +1 -0
  11. data/lib/elasticsearch/transport/transport/http/curb.rb +44 -32
  12. data/lib/elasticsearch/transport/transport/http/faraday.rb +5 -3
  13. data/lib/elasticsearch/transport/transport/http/manticore.rb +41 -29
  14. data/lib/elasticsearch/transport/transport/response.rb +1 -1
  15. data/lib/elasticsearch/transport/version.rb +1 -1
  16. data/lib/elasticsearch/transport.rb +19 -30
  17. data/spec/elasticsearch/transport/base_spec.rb +50 -9
  18. data/spec/elasticsearch/transport/client_spec.rb +138 -64
  19. data/spec/elasticsearch/transport/http/curb_spec.rb +126 -0
  20. data/spec/elasticsearch/transport/http/faraday_spec.rb +141 -0
  21. data/spec/elasticsearch/transport/http/manticore_spec.rb +161 -0
  22. data/spec/elasticsearch/transport/meta_header_spec.rb +66 -30
  23. data/spec/spec_helper.rb +13 -5
  24. data/test/integration/jruby_test.rb +43 -0
  25. data/test/integration/transport_test.rb +89 -52
  26. data/test/test_helper.rb +10 -22
  27. data/test/unit/adapters_test.rb +88 -0
  28. data/test/unit/response_test.rb +1 -1
  29. data/test/unit/transport_base_test.rb +16 -7
  30. data/test/unit/transport_curb_test.rb +0 -1
  31. data/test/unit/transport_manticore_test.rb +242 -155
  32. metadata +68 -79
@@ -17,7 +17,7 @@
17
17
 
18
18
  require 'test_helper'
19
19
 
20
- class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::IntegrationTestCase
20
+ class Elasticsearch::Transport::Transport::ClientIntegrationTest < Elasticsearch::Test::IntegrationTestCase
21
21
  startup do
22
22
  Elasticsearch::Extensions::Test::Cluster.start(number_of_nodes: 2) if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?(number_of_nodes: 2)
23
23
  end
@@ -29,79 +29,116 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
29
29
  context "Transport" do
30
30
  setup do
31
31
  @host, @port = ELASTICSEARCH_HOSTS.first.split(':')
32
- begin; Object.send(:remove_const, :Patron); rescue NameError; end
32
+ @hosts = { hosts: [ { host: @host, port: @port } ] }
33
33
  end
34
34
 
35
- should "allow to customize the Faraday adapter to Typhoeus" do
36
- require 'typhoeus'
37
- require 'typhoeus/adapters/faraday'
35
+ should "use the default Faraday adapter" do
36
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
37
+ f.response :logger
38
+ end
39
+
40
+ client = Elasticsearch::Transport::Client.new transport: transport
41
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttp)
42
+ client.perform_request 'GET', ''
43
+ end
44
+
45
+ unless jruby?
46
+ should "allow to customize the Faraday adapter to Typhoeus" do
47
+ if is_faraday_v2?
48
+ require 'faraday/typhoeus'
49
+ else
50
+ require 'typhoeus'
51
+ end
38
52
 
39
- transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
40
- :hosts => [ { host: @host, port: @port } ] do |f|
53
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
41
54
  f.response :logger
42
55
  f.adapter :typhoeus
43
56
  end
44
57
 
45
- client = Elasticsearch::Transport::Client.new transport: transport
46
- client.perform_request 'GET', ''
47
- end unless jruby?
58
+ client = Elasticsearch::Transport::Client.new transport: transport
59
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Typhoeus)
60
+ client.perform_request 'GET', ''
61
+ end
48
62
 
49
- should "allow to customize the Faraday adapter to NetHttpPersistent" do
50
- require 'net/http/persistent'
63
+ should "use the Curb client" do
64
+ require 'curb'
65
+ require 'elasticsearch/transport/transport/http/curb'
66
+ transport = Elasticsearch::Transport::Transport::HTTP::Curb.new(@hosts) do |curl|
67
+ curl.verbose = true
68
+ end
51
69
 
52
- transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
53
- :hosts => [ { host: @host, port: @port } ] do |f|
54
- f.response :logger
55
- f.adapter :net_http_persistent
70
+ client = Elasticsearch::Transport::Client.new transport: transport
71
+ assert_equal(client.transport.class, Elasticsearch::Transport::Transport::HTTP::Curb)
72
+ client.perform_request 'GET', ''
56
73
  end
57
74
 
58
- client = Elasticsearch::Transport::Client.new transport: transport
59
- client.perform_request 'GET', ''
60
- end
75
+ should "deserialize JSON responses in the Curb client" do
76
+ require 'curb'
77
+ require 'elasticsearch/transport/transport/http/curb'
78
+ transport = Elasticsearch::Transport::Transport::HTTP::Curb.new(@hosts) do |curl|
79
+ curl.verbose = true
80
+ end
61
81
 
62
- should "allow to define connection parameters and pass them" do
63
- transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
64
- :hosts => [ { host: @host, port: @port } ],
65
- :options => { :transport_options => {
66
- :params => { :format => 'yaml' }
67
- }
68
- }
82
+ client = Elasticsearch::Transport::Client.new transport: transport
83
+ response = client.perform_request 'GET', ''
69
84
 
70
- client = Elasticsearch::Transport::Client.new transport: transport
71
- response = client.perform_request 'GET', ''
85
+ assert_respond_to(response.body, :to_hash)
86
+ assert_not_nil response.body['name']
87
+ assert_equal 'application/json', response.headers['content-type']
88
+ end
72
89
 
73
- assert response.body.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
74
- end
90
+ should 'allow to customize the Faraday adapter to Patron' do
91
+ if is_faraday_v2?
92
+ require 'faraday/patron'
93
+ else
94
+ require 'patron'
95
+ end
96
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
97
+ f.response :logger
98
+ f.adapter :patron
99
+ end
75
100
 
76
- should "use the Curb client" do
77
- require 'curb'
78
- require 'elasticsearch/transport/transport/http/curb'
101
+ client = Elasticsearch::Transport::Client.new(transport: transport)
102
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Patron)
103
+ client.perform_request 'GET', ''
104
+ end
79
105
 
80
- transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
81
- :hosts => [ { host: @host, port: @port } ] do |curl|
82
- curl.verbose = true
106
+ should "allow to customize the Faraday adapter to NetHttpPersistent" do
107
+ require 'faraday/net_http_persistent'
108
+
109
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
110
+ f.response :logger
111
+ f.adapter :net_http_persistent
83
112
  end
84
113
 
85
- client = Elasticsearch::Transport::Client.new transport: transport
86
- client.perform_request 'GET', ''
87
- end unless JRUBY
114
+ client = Elasticsearch::Transport::Client.new transport: transport
115
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttpPersistent)
116
+ client.perform_request 'GET', ''
117
+ end
88
118
 
89
- should "deserialize JSON responses in the Curb client" do
90
- require 'curb'
91
- require 'elasticsearch/transport/transport/http/curb'
119
+ should 'allow to customize the Faraday adapter to HTTPClient' do
120
+ require 'faraday/httpclient'
92
121
 
93
- transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
94
- :hosts => [ { host: @host, port: @port } ] do |curl|
95
- curl.verbose = true
122
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(@hosts) do |f|
123
+ f.response :logger
124
+ f.adapter :httpclient
96
125
  end
97
126
 
98
- client = Elasticsearch::Transport::Client.new transport: transport
99
- response = client.perform_request 'GET', ''
127
+ client = Elasticsearch::Transport::Client.new(transport: transport)
128
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::HTTPClient)
129
+ client.perform_request 'GET', ''
130
+ end
100
131
 
101
- assert_respond_to(response.body, :to_hash)
102
- assert_not_nil response.body['name']
103
- assert_equal 'application/json', response.headers['content-type']
104
- end unless JRUBY
105
- end
132
+ should "allow to define connection parameters and pass them" do
133
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new(
134
+ hosts: [ { host: @host, port: @port } ],
135
+ options: { transport_options: { params: { :format => 'yaml' } } }
136
+ )
137
+ client = Elasticsearch::Transport::Client.new transport: transport
138
+ response = client.perform_request 'GET', ''
106
139
 
140
+ assert response.body.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
141
+ end
142
+ end
143
+ end
107
144
  end
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'
@@ -111,6 +97,10 @@ module Minitest
111
97
  end
112
98
  end
113
99
 
100
+ def is_faraday_v2?
101
+ Gem::Version.new(Faraday::VERSION) >= Gem::Version.new(2)
102
+ end
103
+
114
104
  Minitest::Reporters.use! FixedMinitestSpecReporter.new
115
105
 
116
106
  module Elasticsearch
@@ -119,8 +109,7 @@ module Elasticsearch
119
109
  extend Elasticsearch::Extensions::Test::StartupShutdown
120
110
 
121
111
  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'
112
+ end
124
113
  end
125
114
 
126
115
  module Test
@@ -129,7 +118,6 @@ module Elasticsearch
129
118
  extend Elasticsearch::Extensions::Test::Profiling
130
119
 
131
120
  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
121
+ end unless JRUBY
134
122
  end
135
123
  end
@@ -0,0 +1,88 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require 'test_helper'
19
+
20
+ class Elasticsearch::Transport::Transport::ClientAdaptersUnitTest < Minitest::Test
21
+ context 'Adapters' do
22
+ setup do
23
+ begin
24
+ Object.send(:remove_const, :Patron)
25
+ rescue NameError
26
+ end
27
+ end
28
+
29
+ should 'use the default Faraday adapter' do
30
+ fork do
31
+ client = Elasticsearch::Transport::Client.new
32
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttp)
33
+ end
34
+ end
35
+
36
+ should 'use Patron Faraday adapter' do
37
+ fork do
38
+ if is_faraday_v2?
39
+ require 'faraday/patron'
40
+ else
41
+ require 'patron'
42
+ end
43
+
44
+ client = Elasticsearch::Transport::Client.new
45
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Patron)
46
+ end
47
+ end
48
+
49
+ should 'use Typhoeus Faraday adapter' do
50
+ fork do
51
+ if is_faraday_v2?
52
+ require 'faraday/typhoeus'
53
+ else
54
+ require 'typhoeus'
55
+ end
56
+
57
+ client = Elasticsearch::Transport::Client.new
58
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::Typhoeus)
59
+ end
60
+ end
61
+
62
+ should 'use NetHttpPersistent Faraday adapter' do
63
+ fork do
64
+ if is_faraday_v2?
65
+ require 'faraday/net_http_persistent'
66
+ else
67
+ require 'net/http/persistent'
68
+ end
69
+
70
+ client = Elasticsearch::Transport::Client.new
71
+ assert_equal(client.transport.connections.first.connection.adapter, Faraday::Adapter::NetHttpPersistent)
72
+ end
73
+ end
74
+
75
+ should 'use HTTPClient Faraday adapter' do
76
+ fork do
77
+ if is_faraday_v2?
78
+ require 'faraday/httpclient'
79
+ else
80
+ require 'httpclient'
81
+ end
82
+
83
+ client = Elasticsearch::Transport::Client.new
84
+ assert_equal(Faraday::Adapter::HTTPClient, client.transport.connections.first.connection.adapter)
85
+ end
86
+ end
87
+ end unless jruby?
88
+ 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']