elasticsearch-transport 5.0.5 → 6.8.3

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 (42) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +5 -0
  3. data/README.md +88 -34
  4. data/Rakefile +14 -17
  5. data/elasticsearch-transport.gemspec +45 -63
  6. data/lib/elasticsearch/transport/client.rb +207 -67
  7. data/lib/elasticsearch/transport/redacted.rb +79 -0
  8. data/lib/elasticsearch/transport/transport/base.rb +28 -14
  9. data/lib/elasticsearch/transport/transport/connections/collection.rb +4 -0
  10. data/lib/elasticsearch/transport/transport/connections/connection.rb +5 -1
  11. data/lib/elasticsearch/transport/transport/connections/selector.rb +4 -0
  12. data/lib/elasticsearch/transport/transport/errors.rb +4 -0
  13. data/lib/elasticsearch/transport/transport/http/curb.rb +7 -2
  14. data/lib/elasticsearch/transport/transport/http/faraday.rb +11 -8
  15. data/lib/elasticsearch/transport/transport/http/manticore.rb +6 -1
  16. data/lib/elasticsearch/transport/transport/response.rb +4 -0
  17. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +4 -0
  18. data/lib/elasticsearch/transport/transport/sniffer.rb +31 -3
  19. data/lib/elasticsearch/transport/version.rb +5 -1
  20. data/lib/elasticsearch/transport.rb +5 -0
  21. data/lib/elasticsearch-transport.rb +4 -0
  22. data/spec/elasticsearch/transport/base_spec.rb +260 -0
  23. data/spec/elasticsearch/transport/client_spec.rb +1063 -0
  24. data/spec/elasticsearch/transport/meta_header_spec.rb +214 -0
  25. data/spec/elasticsearch/transport/sniffer_spec.rb +269 -0
  26. data/spec/spec_helper.rb +72 -0
  27. data/test/integration/transport_test.rb +9 -5
  28. data/test/profile/client_benchmark_test.rb +23 -25
  29. data/test/test_helper.rb +10 -0
  30. data/test/unit/connection_collection_test.rb +4 -0
  31. data/test/unit/connection_selector_test.rb +4 -0
  32. data/test/unit/connection_test.rb +4 -0
  33. data/test/unit/response_test.rb +5 -1
  34. data/test/unit/serializer_test.rb +4 -0
  35. data/test/unit/transport_base_test.rb +21 -1
  36. data/test/unit/transport_curb_test.rb +12 -0
  37. data/test/unit/transport_faraday_test.rb +16 -0
  38. data/test/unit/transport_manticore_test.rb +11 -0
  39. metadata +90 -76
  40. data/test/integration/client_test.rb +0 -237
  41. data/test/unit/client_test.rb +0 -366
  42. data/test/unit/sniffer_test.rb +0 -179
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::ProfilingTest
@@ -9,15 +13,15 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
9
13
  setup do
10
14
  @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
11
15
  client = Elasticsearch::Client.new host: "localhost:#{@port}", adapter: ::Faraday.default_adapter
12
- client.perform_request 'DELETE', '/ruby_test_benchmark/' rescue nil
13
- client.perform_request 'POST', '/ruby_test_benchmark/', {index: {number_of_shards: 1, number_of_replicas: 0}}
14
- 100.times do client.perform_request 'POST', '/ruby_test_benchmark_search/test/', {}, {foo: 'bar'}; end
15
- client.perform_request 'POST', '/ruby_test_benchmark_search/_refresh'
16
+ client.perform_request 'DELETE', 'ruby_test_benchmark' rescue nil
17
+ client.perform_request 'PUT', 'ruby_test_benchmark', {}, {settings: {index: {number_of_shards: 1, number_of_replicas: 0}}}
18
+ 100.times do client.perform_request 'POST', 'ruby_test_benchmark_search/test', {}, {foo: 'bar'}; end
19
+ client.perform_request 'POST', 'ruby_test_benchmark_search/_refresh'
16
20
  end
17
21
  teardown do
18
22
  client = Elasticsearch::Client.new host: "localhost:#{@port}"
19
- client.perform_request 'DELETE', '/ruby_test_benchmark/'
20
- client.perform_request 'DELETE', '/ruby_test_benchmark_search/'
23
+ client.perform_request 'DELETE', 'ruby_test_benchmark' rescue nil
24
+ client.perform_request 'DELETE', 'ruby_test_benchmark_search' rescue nil
21
25
  end
22
26
 
23
27
  context "with a single-node cluster and the default adapter" do
@@ -30,11 +34,11 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
30
34
  end
31
35
 
32
36
  measure "index a document" do
33
- @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
37
+ @client.perform_request 'POST', 'ruby_test_benchmark/test', {}, {foo: 'bar'}
34
38
  end
35
39
 
36
40
  measure "search" do
37
- @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
41
+ @client.perform_request 'GET', 'ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
38
42
  end
39
43
  end
40
44
 
@@ -48,11 +52,11 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
48
52
  end
49
53
 
50
54
  measure "index a document"do
51
- @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
55
+ @client.perform_request 'POST', 'ruby_test_benchmark/test', {}, {foo: 'bar'}
52
56
  end
53
57
 
54
58
  measure "search" do
55
- @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
59
+ @client.perform_request 'GET', 'ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
56
60
  end
57
61
  end
58
62
 
@@ -69,25 +73,19 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
69
73
  end
70
74
 
71
75
  measure "index a document" do
72
- @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
76
+ @client.perform_request 'POST', 'ruby_test_benchmark/test', {}, {foo: 'bar'}
73
77
  end
74
78
 
75
79
  measure "search" do
76
- @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
80
+ @client.perform_request 'GET', 'ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
77
81
  end
78
82
  end
79
83
 
80
84
  context "with a single-node cluster and the Typhoeus client" do
81
- require 'typhoeus'
82
- require 'typhoeus/adapters/faraday'
83
-
84
85
  setup do
85
- transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
86
- :hosts => [ { :host => 'localhost', :port => @port } ] do |f|
87
- f.adapter :typhoeus
88
- end
89
-
90
- @client = Elasticsearch::Client.new transport: transport
86
+ require 'typhoeus'
87
+ require 'typhoeus/adapters/faraday'
88
+ @client = Elasticsearch::Client.new host: "localhost:#{@port}", adapter: :typhoeus
91
89
  end
92
90
 
93
91
  measure "get the cluster info", count: 1_000 do
@@ -95,11 +93,11 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
95
93
  end
96
94
 
97
95
  measure "index a document" do
98
- @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
96
+ @client.perform_request 'POST', 'ruby_test_benchmark/test', {}, {foo: 'bar'}
99
97
  end
100
98
 
101
99
  measure "search" do
102
- @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
100
+ @client.perform_request 'GET', 'ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
103
101
  end
104
102
  end
105
103
 
@@ -114,11 +112,11 @@ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::Profi
114
112
  end
115
113
 
116
114
  measure "index a document" do
117
- @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
115
+ @client.perform_request 'POST', 'ruby_test_benchmark/test', {}, {foo: 'bar'}
118
116
  end
119
117
 
120
118
  measure "search" do
121
- @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
119
+ @client.perform_request 'GET', 'ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
122
120
  end
123
121
  end
124
122
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,16 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
6
  JRUBY = defined?(JRUBY_VERSION)
3
7
 
8
+ ELASTICSEARCH_HOSTS = if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
9
+ hosts.split(',').map do |host|
10
+ /(http\:\/\/)?(\S+)/.match(host)[2]
11
+ end
12
+ end.freeze
13
+
4
14
  if RUBY_1_8 and not ENV['BUNDLE_GEMFILE']
5
15
  require 'rubygems'
6
16
  gem 'test-unit'
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::Connections::CollectionTest < Test::Unit::TestCase
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::Connections::SelectorTest < Test::Unit::TestCase
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::Connections::ConnectionTest < Test::Unit::TestCase
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::ResponseTest < Test::Unit::TestCase
@@ -12,4 +16,4 @@ class Elasticsearch::Transport::Transport::ResponseTest < Test::Unit::TestCase
12
16
  end unless RUBY_1_8
13
17
 
14
18
  end
15
- end
19
+ end
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::SerializerTest < Test::Unit::TestCase
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::BaseTest < Test::Unit::TestCase
@@ -353,7 +357,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Test::Unit::TestCase
353
357
  never
354
358
 
355
359
  assert_raise Elasticsearch::Transport::Transport::Errors::BadRequest do
356
- @transport.perform_request('GET', '/', &@block)
360
+ @transport.perform_request('GET', '/', {}, nil, &@block)
357
361
  end
358
362
  end
359
363
 
@@ -555,6 +559,22 @@ class Elasticsearch::Transport::Transport::BaseTest < Test::Unit::TestCase
555
559
  assert_equal 1, @transport.connections.size
556
560
  assert_equal 1, @transport.connections.all.size
557
561
  end
562
+
563
+ should "not duplicate connections" do
564
+ @transport.__rebuild_connections :hosts => [ { :host => 'node1', :port => 1 },
565
+ { :host => 'node2', :port => 2 } ],
566
+ :options => { :http => {} }
567
+ assert_equal 2, @transport.connections.size
568
+
569
+ @transport.connections[0].dead!
570
+
571
+ @transport.__rebuild_connections :hosts => [ { :host => 'node1', :port => 1 },
572
+ { :host => 'node2', :port => 2 } ],
573
+ :options => { :http => {} }
574
+
575
+ assert_equal 2, @transport.connections.all.size
576
+ assert_equal 1, @transport.connections.size
577
+ end
558
578
  end
559
579
 
560
580
  context "rebuilding connections" do
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  if JRUBY
@@ -37,6 +41,14 @@ else
37
41
  @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
38
42
  end
39
43
 
44
+ should "perform request with headers" do
45
+ @transport.connections.first.connection.expects(:put_data=).with('{"foo":"bar"}')
46
+ @transport.connections.first.connection.expects(:http).with(:POST).returns(stub_everything)
47
+ @transport.connections.first.connection.expects(:headers=).with({"Content-Type" => "application/x-ndjson"})
48
+
49
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}, {"Content-Type" => "application/x-ndjson"}
50
+ end
51
+
40
52
  should "set body for PUT request" do
41
53
  @transport.connections.first.connection.expects(:put_data=)
42
54
  @transport.connections.first.connection.expects(:http).with(:PUT).returns(stub_everything)
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestCase
@@ -42,6 +46,18 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC
42
46
  @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}
43
47
  end
44
48
 
49
+ should "properly prepare the request with custom headers" do
50
+ @transport.connections.first.connection.expects(:run_request).with do |method, url, body, headers|
51
+ assert_equal :post, method
52
+ assert_equal '{"foo":"bar"}', body
53
+ assert_nil headers['Accept']
54
+ assert_equal "application/x-ndjson", headers['Content-Type']
55
+ true
56
+ end.returns(stub_everything)
57
+
58
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}, {"Content-Type" => "application/x-ndjson"}
59
+ end
60
+
45
61
  should "properly pass the Content-Type header option" do
46
62
  transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => { :transport_options => { :headers => { 'Content-Type' => 'foo/bar' } } }
47
63
 
@@ -1,3 +1,7 @@
1
+ # Licensed to Elasticsearch B.V under one or more agreements.
2
+ # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
+ # See the LICENSE file in the project root for more information
4
+
1
5
  require 'test_helper'
2
6
 
3
7
  unless JRUBY
@@ -55,6 +59,13 @@ else
55
59
  @transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
56
60
  end
57
61
 
62
+ should "set custom headers for PUT request" do
63
+ @transport.connections.first.connection.expects(:put).
64
+ with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}', :headers => {"Content-Type" => "application/x-ndjson"}})
65
+ .returns(stub_everything)
66
+ @transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', {"Content-Type" => "application/x-ndjson"}
67
+ end
68
+
58
69
  should "not serialize a String request body" do
59
70
  @transport.connections.first.connection.expects(:post).
60
71
  with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'}).returns(stub_everything)