elastic-transport 8.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.github/check_license_headers.rb +33 -0
  3. data/.github/license-header.txt +16 -0
  4. data/.github/workflows/license.yml +13 -0
  5. data/.github/workflows/tests.yml +45 -0
  6. data/.gitignore +19 -0
  7. data/CHANGELOG.md +224 -0
  8. data/Gemfile +38 -0
  9. data/LICENSE +202 -0
  10. data/README.md +552 -0
  11. data/Rakefile +87 -0
  12. data/elastic-transport.gemspec +74 -0
  13. data/lib/elastic/transport/client.rb +276 -0
  14. data/lib/elastic/transport/meta_header.rb +135 -0
  15. data/lib/elastic/transport/redacted.rb +73 -0
  16. data/lib/elastic/transport/transport/base.rb +450 -0
  17. data/lib/elastic/transport/transport/connections/collection.rb +126 -0
  18. data/lib/elastic/transport/transport/connections/connection.rb +160 -0
  19. data/lib/elastic/transport/transport/connections/selector.rb +91 -0
  20. data/lib/elastic/transport/transport/errors.rb +91 -0
  21. data/lib/elastic/transport/transport/http/curb.rb +120 -0
  22. data/lib/elastic/transport/transport/http/faraday.rb +95 -0
  23. data/lib/elastic/transport/transport/http/manticore.rb +179 -0
  24. data/lib/elastic/transport/transport/loggable.rb +83 -0
  25. data/lib/elastic/transport/transport/response.rb +36 -0
  26. data/lib/elastic/transport/transport/serializer/multi_json.rb +52 -0
  27. data/lib/elastic/transport/transport/sniffer.rb +101 -0
  28. data/lib/elastic/transport/version.rb +22 -0
  29. data/lib/elastic/transport.rb +37 -0
  30. data/lib/elastic-transport.rb +18 -0
  31. data/spec/elasticsearch/connections/collection_spec.rb +266 -0
  32. data/spec/elasticsearch/connections/selector_spec.rb +166 -0
  33. data/spec/elasticsearch/transport/base_spec.rb +264 -0
  34. data/spec/elasticsearch/transport/client_spec.rb +1651 -0
  35. data/spec/elasticsearch/transport/meta_header_spec.rb +274 -0
  36. data/spec/elasticsearch/transport/sniffer_spec.rb +275 -0
  37. data/spec/spec_helper.rb +90 -0
  38. data/test/integration/transport_test.rb +98 -0
  39. data/test/profile/client_benchmark_test.rb +132 -0
  40. data/test/test_helper.rb +83 -0
  41. data/test/unit/connection_test.rb +135 -0
  42. data/test/unit/response_test.rb +30 -0
  43. data/test/unit/serializer_test.rb +33 -0
  44. data/test/unit/transport_base_test.rb +664 -0
  45. data/test/unit/transport_curb_test.rb +135 -0
  46. data/test/unit/transport_faraday_test.rb +228 -0
  47. data/test/unit/transport_manticore_test.rb +251 -0
  48. metadata +412 -0
@@ -0,0 +1,135 @@
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
+ if JRUBY
21
+ puts "'#{File.basename(__FILE__)}' not supported on JRuby #{RUBY_VERSION}"
22
+ else
23
+ require 'elastic/transport/transport/http/curb'
24
+ require 'curb'
25
+
26
+ class Elastic::Transport::Transport::HTTP::FaradayTest < Minitest::Test
27
+ include Elastic::Transport::Transport::HTTP
28
+
29
+ context "Curb transport" do
30
+ setup do
31
+ @transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234 } ]
32
+ end
33
+
34
+ should "implement host_unreachable_exceptions" do
35
+ assert_instance_of Array, @transport.host_unreachable_exceptions
36
+ end
37
+
38
+ should "implement __build_connections" do
39
+ assert_equal 1, @transport.hosts.size
40
+ assert_equal 1, @transport.connections.size
41
+
42
+ assert_instance_of ::Curl::Easy, @transport.connections.first.connection
43
+ assert_equal 'http://foobar:1234', @transport.connections.first.connection.url
44
+ end
45
+
46
+ should "perform the request" do
47
+ @transport.connections.first.connection.expects(:http).returns(stub_everything)
48
+ @transport.perform_request 'GET', '/'
49
+ end
50
+
51
+ should "set body for GET request" do
52
+ @transport.connections.first.connection.expects(:put_data=).with('{"foo":"bar"}')
53
+ @transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
54
+ @transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
55
+ end
56
+
57
+ should "perform request with headers" do
58
+ @transport.connections.first.connection.expects(:put_data=).with('{"foo":"bar"}')
59
+ @transport.connections.first.connection.expects(:http).with(:POST).returns(stub_everything)
60
+ @transport.connections.first.connection.headers.expects(:merge!).with("Content-Type" => "application/x-ndjson")
61
+
62
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}, {"Content-Type" => "application/x-ndjson"}
63
+ end
64
+
65
+ should "set body for PUT request" do
66
+ @transport.connections.first.connection.expects(:put_data=)
67
+ @transport.connections.first.connection.expects(:http).with(:PUT).returns(stub_everything)
68
+ @transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
69
+ end
70
+
71
+ should "serialize the request body" do
72
+ @transport.connections.first.connection.expects(:http).with(:POST).returns(stub_everything)
73
+ @transport.serializer.expects(:dump)
74
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}
75
+ end
76
+
77
+ should "not serialize a String request body" do
78
+ @transport.connections.first.connection.expects(:http).with(:POST).returns(stub_everything)
79
+ @transport.serializer.expects(:dump).never
80
+ @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
81
+ end
82
+
83
+ should "set application/json response header" do
84
+ @transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
85
+ @transport.connections.first.connection.expects(:body_str).returns('{"foo":"bar"}')
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
+ response = @transport.perform_request 'GET', '/'
89
+
90
+ assert_equal 'application/json', response.headers['content-type']
91
+ end
92
+
93
+ should "handle HTTP methods" do
94
+ @transport.connections.first.connection.expects(:http).with(:HEAD).returns(stub_everything)
95
+ @transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
96
+ @transport.connections.first.connection.expects(:http).with(:PUT).returns(stub_everything)
97
+ @transport.connections.first.connection.expects(:http).with(:POST).returns(stub_everything)
98
+ @transport.connections.first.connection.expects(:http).with(:DELETE).returns(stub_everything)
99
+
100
+ %w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
101
+
102
+ assert_raise(ArgumentError) { @transport.perform_request 'FOOBAR', '/' }
103
+ end
104
+
105
+ should "properly pass the Content-Type header option" do
106
+ transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => { :transport_options => { :headers => { 'Content-Type' => 'foo/bar' } } }
107
+
108
+ assert_equal "foo/bar", transport.connections.first.connection.headers["Content-Type"]
109
+ end
110
+
111
+ should "allow to set options for Curb" do
112
+ transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234 } ] do |curl|
113
+ curl.headers["User-Agent"] = "myapp-0.0"
114
+ end
115
+
116
+ assert_equal "myapp-0.0", transport.connections.first.connection.headers["User-Agent"]
117
+ end
118
+
119
+ should "set the credentials if passed" do
120
+ transport = Curb.new :hosts => [ { :host => 'foobar', :port => 1234, :user => 'foo', :password => 'bar' } ]
121
+ assert_equal 'foo', transport.connections.first.connection.username
122
+ assert_equal 'bar', transport.connections.first.connection.password
123
+ end
124
+
125
+ should "use global http configuration" do
126
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
127
+ :options => { :http => { :scheme => 'https', :user => 'U', :password => 'P' } }
128
+
129
+ assert_equal 'https://U:P@foobar:1234/', transport.connections.first.full_url('')
130
+ end
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,228 @@
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 Elastic::Transport::Transport::HTTP::FaradayTest < Minitest::Test
21
+ include Elastic::Transport::Transport::HTTP
22
+
23
+ context "Faraday transport" do
24
+ setup do
25
+ @transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ]
26
+ end
27
+
28
+ should "implement host_unreachable_exceptions" do
29
+ assert_instance_of Array, @transport.host_unreachable_exceptions
30
+ end
31
+
32
+ should "implement __build_connections" do
33
+ assert_equal 1, @transport.hosts.size
34
+ assert_equal 1, @transport.connections.size
35
+
36
+ assert_instance_of ::Faraday::Connection, @transport.connections.first.connection
37
+ assert_equal 'http://foobar:1234/', @transport.connections.first.connection.url_prefix.to_s
38
+ end
39
+
40
+ should "perform the request" do
41
+ @transport.connections.first.connection.expects(:run_request).returns(stub_everything)
42
+ @transport.perform_request 'GET', '/'
43
+ end
44
+
45
+ should "return a Response" do
46
+ @transport.connections.first.connection.expects(:run_request).returns(stub_everything)
47
+ response = @transport.perform_request 'GET', '/'
48
+ assert_instance_of Elastic::Transport::Transport::Response, response
49
+ end
50
+
51
+ should "properly prepare the request" do
52
+ @transport.connections.first.connection.expects(:run_request).with do |method, url, body, headers|
53
+ assert_equal :post, method
54
+ assert_equal '{"foo":"bar"}', body
55
+ assert_nil headers['Accept']
56
+ true
57
+ end.returns(stub_everything)
58
+
59
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}
60
+ end
61
+
62
+ should "properly prepare the request with custom headers" do
63
+ @transport.connections.first.connection.expects(:run_request).with do |method, url, body, headers|
64
+ assert_equal :post, method
65
+ assert_equal '{"foo":"bar"}', body
66
+ assert_nil headers['Accept']
67
+ assert_equal "application/x-ndjson", headers['Content-Type']
68
+ true
69
+ end.returns(stub_everything)
70
+
71
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}, {"Content-Type" => "application/x-ndjson"}
72
+ end
73
+
74
+ should "properly pass the Content-Type header option" do
75
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ], :options => { :transport_options => { :headers => { 'Content-Type' => 'foo/bar' } } }
76
+
77
+ transport.connections.first.connection.expects(:run_request).with do |method, url, body, headers|
78
+ assert_equal 'foo/bar', headers['Content-Type']
79
+ true
80
+ end.returns(stub_everything)
81
+
82
+ transport.perform_request 'GET', '/'
83
+ end
84
+
85
+ should "serialize the request body" do
86
+ @transport.connections.first.connection.expects(:run_request).returns(stub_everything)
87
+ @transport.serializer.expects(:dump)
88
+ @transport.perform_request 'POST', '/', {}, {:foo => 'bar'}
89
+ end
90
+
91
+ should "not serialize a String request body" do
92
+ @transport.connections.first.connection.expects(:run_request).returns(stub_everything)
93
+ @transport.serializer.expects(:dump).never
94
+ @transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
95
+ end
96
+
97
+ should "pass the selector_class options to collection" do
98
+ @transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
99
+ :options => { :selector_class => Elastic::Transport::Transport::Connections::Selector::Random }
100
+ assert_instance_of Elastic::Transport::Transport::Connections::Selector::Random,
101
+ @transport.connections.selector
102
+ end
103
+
104
+ should "pass the selector option to collection" do
105
+ @transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
106
+ :options => { :selector => Elastic::Transport::Transport::Connections::Selector::Random.new }
107
+ assert_instance_of Elastic::Transport::Transport::Connections::Selector::Random,
108
+ @transport.connections.selector
109
+ end
110
+
111
+ should "pass a configuration block to the Faraday constructor" do
112
+ config_block = lambda do |f|
113
+ f.response :logger
114
+ f.path_prefix = '/moo'
115
+ end
116
+
117
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ], &config_block
118
+
119
+ handlers = transport.connections.first.connection.builder.handlers
120
+
121
+ assert_equal 1, handlers.size
122
+ assert handlers.include?(::Faraday::Response::Logger), "#{handlers.inspect} does not include <::Faraday::Adapter::Logger>"
123
+
124
+ assert_equal '/moo', transport.connections.first.connection.path_prefix
125
+ assert_equal 'http://foobar:1234/moo', transport.connections.first.connection.url_prefix.to_s
126
+ end
127
+
128
+ should "pass transport_options to the Faraday constructor" do
129
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
130
+ :options => { :transport_options => {
131
+ :request => { :open_timeout => 1 },
132
+ :headers => { :foo_bar => 'bar' },
133
+ :ssl => { :verify => false }
134
+ }
135
+ }
136
+
137
+ assert_equal 1, transport.connections.first.connection.options.open_timeout
138
+ assert_equal 'bar', transport.connections.first.connection.headers['Foo-Bar']
139
+ assert_equal false, transport.connections.first.connection.ssl.verify?
140
+ end
141
+
142
+ should "merge in parameters defined in the Faraday connection parameters" do
143
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
144
+ :options => { :transport_options => {
145
+ :params => { :format => 'yaml' }
146
+ }
147
+ }
148
+ # transport.logger = Logger.new(STDERR)
149
+
150
+ transport.connections.first.connection.expects(:run_request).
151
+ with do |method, url, params, body|
152
+ assert_match(/\?format=yaml/, url)
153
+ true
154
+ end.
155
+ returns(stub_everything)
156
+
157
+ transport.perform_request 'GET', ''
158
+ end
159
+
160
+ should "not overwrite request parameters with the Faraday connection parameters" do
161
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
162
+ :options => { :transport_options => {
163
+ :params => { :format => 'yaml' }
164
+ }
165
+ }
166
+ # transport.logger = Logger.new(STDERR)
167
+
168
+ transport.connections.first.connection.expects(:run_request).
169
+ with do |method, url, params, body|
170
+ assert_match(/\?format=json/, url)
171
+ true
172
+ end.
173
+ returns(stub_everything)
174
+
175
+ transport.perform_request 'GET', '', { :format => 'json' }
176
+ end
177
+
178
+ should "set the credentials if passed" do
179
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234, :user => 'foo', :password => 'bar' } ]
180
+ assert_equal 'Basic Zm9vOmJhcg==', transport.connections.first.connection.headers['Authorization']
181
+ end
182
+
183
+ should "set the credentials if they exist in options" do
184
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
185
+ :options => { :user => 'foo', :password => 'bar' }
186
+ assert_equal 'Basic Zm9vOmJhcg==', transport.connections.first.connection.headers['Authorization']
187
+ end
188
+
189
+ should "override options credentials if passed explicitly" do
190
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234, :user => 'foo', :password => 'bar' },
191
+ { :host => 'foobar2', :port => 1234 } ],
192
+ :options => { :user => 'foo2', :password => 'bar2' }
193
+ assert_equal 'Basic Zm9vOmJhcg==', transport.connections.first.connection.headers['Authorization']
194
+ assert_equal 'Basic Zm9vMjpiYXIy', transport.connections[1].connection.headers['Authorization']
195
+ end
196
+
197
+ should "set connection scheme to https if passed" do
198
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234, :scheme => 'https' } ]
199
+
200
+ assert_instance_of ::Faraday::Connection, transport.connections.first.connection
201
+ assert_equal 'https://foobar:1234/', transport.connections.first.connection.url_prefix.to_s
202
+ end
203
+
204
+ should "set connection scheme to https if it exist in options" do
205
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234} ],
206
+ :options => { :scheme => 'https' }
207
+
208
+ assert_instance_of ::Faraday::Connection, transport.connections.first.connection
209
+ assert_equal 'https://foobar:1234/', transport.connections.first.connection.url_prefix.to_s
210
+ end
211
+
212
+ should "override options scheme if passed explicitly" do
213
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234, :scheme => 'http'} ],
214
+ :options => { :scheme => 'https' }
215
+
216
+ assert_instance_of ::Faraday::Connection, transport.connections.first.connection
217
+ assert_equal 'http://foobar:1234/', transport.connections.first.connection.url_prefix.to_s
218
+ end
219
+
220
+ should "use global http configuration" do
221
+ transport = Faraday.new :hosts => [ { :host => 'foobar', :port => 1234 } ],
222
+ :options => { :http => { :scheme => 'https', :user => 'U', :password => 'P' } }
223
+
224
+ assert_equal 'https://U:P@foobar:1234/', transport.connections.first.full_url('')
225
+ end
226
+ end
227
+
228
+ end
@@ -0,0 +1,251 @@
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
+ if JRUBY
21
+ require 'manticore'
22
+ require 'elastic/transport/transport/http/manticore'
23
+
24
+ module Elastic
25
+ module Transport
26
+ module Transport
27
+ module HTTP
28
+ class ManticoreTest < Minitest::Test
29
+ include Elastic::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
243
+ end
244
+ end
245
+ end
246
+ end
247
+ end
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})"
251
+ end