elasticsearch-transport 6.1.0 → 6.2.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.
@@ -1,373 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
4
-
5
- class DummyTransport
6
- def initialize(*); end
7
- end
8
-
9
- context "Client" do
10
- setup do
11
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.stubs(:__build_connections)
12
- @client = Elasticsearch::Transport::Client.new
13
- end
14
-
15
- should "be aliased as Elasticsearch::Client" do
16
- assert_nothing_raised do
17
- assert_instance_of(Elasticsearch::Transport::Client, Elasticsearch::Client.new)
18
- end
19
- end
20
-
21
- should "have default transport" do
22
- assert_instance_of Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS, @client.transport
23
- end
24
-
25
- should "instantiate custom transport class" do
26
- client = Elasticsearch::Transport::Client.new :transport_class => DummyTransport
27
- assert_instance_of DummyTransport, client.transport
28
- end
29
-
30
- should "take custom transport instance" do
31
- client = Elasticsearch::Transport::Client.new :transport => DummyTransport.new
32
- assert_instance_of DummyTransport, client.transport
33
- end
34
-
35
- should "delegate performing requests to transport" do
36
- assert_respond_to @client, :perform_request
37
- @client.transport.expects(:perform_request)
38
- @client.perform_request 'GET', '/'
39
- end
40
-
41
- should "send GET request as POST with the send_get_body_as option" do
42
- transport = DummyTransport.new
43
- client = Elasticsearch::Transport::Client.new :transport => transport, :send_get_body_as => 'POST'
44
- transport.expects(:perform_request).with 'POST', '/', {}, '{"foo":"bar"}', nil
45
- client.perform_request 'GET', '/', {}, '{"foo":"bar"}'
46
- end
47
-
48
- should "call perform_request with custom headers" do
49
- transport = DummyTransport.new
50
- client = Elasticsearch::Transport::Client.new :transport => transport, :send_get_body_as => 'POST'
51
- transport.expects(:perform_request).with 'POST', '/', {}, '{"foo":"bar"}', '{"Content-Type":"application/x-ndjson"}'
52
- client.perform_request 'POST', '/', {}, '{"foo":"bar"}', '{"Content-Type":"application/x-ndjson"}'
53
- end
54
-
55
- should "have default logger for transport" do
56
- client = Elasticsearch::Transport::Client.new :log => true
57
- assert_respond_to client.transport.logger, :info
58
- end
59
-
60
- should "have default tracer for transport" do
61
- client = Elasticsearch::Transport::Client.new :trace => true
62
- assert_respond_to client.transport.tracer, :info
63
- end
64
-
65
- should "initialize the default transport class" do
66
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
67
- unstub(:__build_connections)
68
-
69
- client = Elasticsearch::Client.new
70
- assert_match /Faraday/, client.transport.connections.first.connection.headers['User-Agent']
71
- end
72
-
73
- should "pass options to the transport" do
74
- client = Elasticsearch::Transport::Client.new :transport_options => { :foo => 'bar' }
75
- assert_equal 'bar', client.transport.options[:transport_options][:foo]
76
- end
77
-
78
- should "merge request_timeout to the transport options" do
79
- client = Elasticsearch::Transport::Client.new :request_timeout => 120
80
- assert_equal 120, client.transport.options[:transport_options][:request][:timeout]
81
- end
82
-
83
- should "set the 'Content-Type' header to 'application/json' by default" do
84
- client = Elasticsearch::Transport::Client.new
85
- assert_equal 'application/json', client.transport.options[:transport_options][:headers]['Content-Type']
86
- end
87
-
88
- context "when passed hosts" do
89
- should "have localhost by default" do
90
- c = Elasticsearch::Transport::Client.new
91
- assert_equal 'localhost', c.transport.hosts.first[:host]
92
- end
93
-
94
- should "take :hosts, :host, :url or :urls" do
95
- c1 = Elasticsearch::Transport::Client.new :hosts => ['foobar']
96
- c2 = Elasticsearch::Transport::Client.new :host => 'foobar'
97
- c3 = Elasticsearch::Transport::Client.new :url => 'foobar'
98
- c4 = Elasticsearch::Transport::Client.new :urls => 'foo,bar'
99
-
100
- assert_equal 'foobar', c1.transport.hosts[0][:host]
101
- assert_equal 'foobar', c2.transport.hosts[0][:host]
102
- assert_equal 'foobar', c3.transport.hosts[0][:host]
103
- assert_equal 'foo', c4.transport.hosts[0][:host]
104
- assert_equal 'bar', c4.transport.hosts[1][:host]
105
- end
106
-
107
- end
108
-
109
- context "when the URL is set in the environment variable" do
110
- setup { ENV['ELASTICSEARCH_URL'] = 'foobar' }
111
- teardown { ENV.delete('ELASTICSEARCH_URL') }
112
-
113
- should "use a single host" do
114
- c = Elasticsearch::Transport::Client.new
115
-
116
- assert_equal 1, c.transport.hosts.size
117
- assert_equal 'foobar', c.transport.hosts.first[:host]
118
- end
119
-
120
- should "use multiple hosts" do
121
- ENV['ELASTICSEARCH_URL'] = 'foo, bar'
122
- c = Elasticsearch::Transport::Client.new
123
-
124
- assert_equal 2, c.transport.hosts.size
125
- assert_equal 'foo', c.transport.hosts[0][:host]
126
- assert_equal 'bar', c.transport.hosts[1][:host]
127
- end
128
- end
129
-
130
- context "extracting hosts" do
131
- should "extract from string" do
132
- hosts = @client.__extract_hosts 'myhost'
133
-
134
- assert_equal 'myhost', hosts[0][:host]
135
- assert_nil hosts[0][:port]
136
- end
137
-
138
- should "extract from hash" do
139
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
140
- assert_equal 'myhost', hosts[0][:host]
141
- assert_equal 'https', hosts[0][:scheme]
142
- assert_nil hosts[0][:port]
143
- end
144
-
145
- should "extract from hash with a port passed as a string" do
146
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
147
- assert_equal 443, hosts[0][:port]
148
- end
149
-
150
- should "extract from hash with a port passed as an integer" do
151
- hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
152
- assert_equal 443, hosts[0][:port]
153
- end
154
-
155
- should "extract from Hashie::Mash" do
156
- hosts = @client.__extract_hosts( Hashie::Mash.new(:host => 'myhost', :scheme => 'https') )
157
- assert_equal 'myhost', hosts[0][:host]
158
- assert_equal 'https', hosts[0][:scheme]
159
- end
160
-
161
- should "extract from array" do
162
- hosts = @client.__extract_hosts ['myhost']
163
-
164
- assert_equal 'myhost', hosts[0][:host]
165
- end
166
-
167
- should "extract from array with multiple hosts" do
168
- hosts = @client.__extract_hosts ['host1', 'host2']
169
-
170
- assert_equal 'host1', hosts[0][:host]
171
- assert_equal 'host2', hosts[1][:host]
172
- end
173
-
174
- should "extract from array with ports" do
175
- hosts = @client.__extract_hosts ['host1:1000', 'host2:2000']
176
-
177
- assert_equal 2, hosts.size
178
-
179
- assert_equal 'host1', hosts[0][:host]
180
- assert_equal 1000, hosts[0][:port]
181
-
182
- assert_equal 'host2', hosts[1][:host]
183
- assert_equal 2000, hosts[1][:port]
184
- end
185
-
186
- should "extract path" do
187
- hosts = @client.__extract_hosts 'https://myhost:8080/api'
188
-
189
- assert_equal '/api', hosts[0][:path]
190
- end
191
-
192
- should "extract scheme (protocol)" do
193
- hosts = @client.__extract_hosts 'https://myhost:8080'
194
-
195
- assert_equal 'https', hosts[0][:scheme]
196
- assert_equal 'myhost', hosts[0][:host]
197
- assert_equal 8080, hosts[0][:port]
198
- end
199
-
200
- should "extract credentials" do
201
- hosts = @client.__extract_hosts 'http://USERNAME:PASSWORD@myhost:8080'
202
-
203
- assert_equal 'http', hosts[0][:scheme]
204
- assert_equal 'USERNAME', hosts[0][:user]
205
- assert_equal 'PASSWORD', hosts[0][:password]
206
- assert_equal 'myhost', hosts[0][:host]
207
- assert_equal 8080, hosts[0][:port]
208
- end
209
-
210
- should "pass hashes over" do
211
- hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]
212
-
213
- assert_equal 'myhost', hosts[0][:host]
214
- assert_equal 1000, hosts[0][:port]
215
- assert_equal 'bar', hosts[0][:foo]
216
- end
217
-
218
- should "use URL instance" do
219
- require 'uri'
220
- hosts = @client.__extract_hosts URI.parse('https://USERNAME:PASSWORD@myhost:4430')
221
-
222
- assert_equal 'https', hosts[0][:scheme]
223
- assert_equal 'USERNAME', hosts[0][:user]
224
- assert_equal 'PASSWORD', hosts[0][:password]
225
- assert_equal 'myhost', hosts[0][:host]
226
- assert_equal 4430, hosts[0][:port]
227
- end
228
-
229
- should "split comma-separated URLs" do
230
- hosts = @client.__extract_hosts 'foo, bar'
231
-
232
- assert_equal 2, hosts.size
233
-
234
- assert_equal 'foo', hosts[0][:host]
235
- assert_equal 'bar', hosts[1][:host]
236
- end
237
-
238
- should "remove trailing slash from URL path" do
239
- hosts = @client.__extract_hosts 'http://myhost/'
240
- assert_equal '', hosts[0][:path]
241
-
242
- hosts = @client.__extract_hosts 'http://myhost/foo/bar/'
243
- assert_equal '/foo/bar', hosts[0][:path]
244
- end
245
-
246
- should "raise error for incompatible argument" do
247
- assert_raise ArgumentError do
248
- @client.__extract_hosts 123
249
- end
250
- end
251
-
252
- should "randomize hosts" do
253
- hosts = [ {:host => 'host1'}, {:host => 'host2'}, {:host => 'host3'}, {:host => 'host4'}, {:host => 'host5'}]
254
-
255
- Array.any_instance.expects(:shuffle!).twice
256
-
257
- @client.__extract_hosts(hosts, :randomize_hosts => true)
258
- assert_same_elements hosts, @client.__extract_hosts(hosts, :randomize_hosts => true)
259
- end
260
- end
261
-
262
- context "detecting adapter for Faraday" do
263
- setup do
264
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
265
- begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
266
- begin; Object.send(:remove_const, :Patron); rescue NameError; end
267
- end
268
-
269
- should "use the default adapter" do
270
- c = Elasticsearch::Transport::Client.new
271
- handlers = c.transport.connections.all.first.connection.builder.handlers
272
-
273
- assert_includes handlers, Faraday::Adapter::NetHttp
274
- end
275
-
276
- should "use the adapter from arguments" do
277
- c = Elasticsearch::Transport::Client.new :adapter => :typhoeus
278
- handlers = c.transport.connections.all.first.connection.builder.handlers
279
-
280
- assert_includes handlers, Faraday::Adapter::Typhoeus
281
- end
282
-
283
- should "detect the adapter" do
284
- require 'patron'; load 'patron.rb'
285
-
286
- c = Elasticsearch::Transport::Client.new
287
- handlers = c.transport.connections.all.first.connection.builder.handlers
288
-
289
- assert_includes handlers, Faraday::Adapter::Patron
290
- end unless JRUBY
291
- end
292
-
293
- context "configuring Faraday" do
294
- setup do
295
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
296
- begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
297
- end
298
-
299
- should "apply faraday adapter" do
300
- c = Elasticsearch::Transport::Client.new do |faraday|
301
- faraday.adapter :typhoeus
302
- end
303
- handlers = c.transport.connections.all.first.connection.builder.handlers
304
-
305
- assert_includes handlers, Faraday::Adapter::Typhoeus
306
- end
307
-
308
- should "apply faraday response logger" do
309
- c = Elasticsearch::Transport::Client.new do |faraday|
310
- faraday.response :logger
311
- end
312
- handlers = c.transport.connections.all.first.connection.builder.handlers
313
-
314
- assert_includes handlers, Faraday::Response::Logger
315
- end
316
- end
317
-
318
- context "when passed options" do
319
- setup do
320
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
321
- end
322
-
323
- should "configure the HTTP scheme" do
324
- c = Elasticsearch::Transport::Client.new \
325
- :hosts => ['node1', 'node2'],
326
- :port => 1234, :scheme => 'https', :user => 'USERNAME', :password => 'PASSWORD'
327
-
328
- assert_equal 'https://USERNAME:PASSWORD@node1:1234/', c.transport.connections[0].full_url('')
329
- assert_equal 'https://USERNAME:PASSWORD@node2:1234/', c.transport.connections[1].full_url('')
330
- end
331
-
332
- should "keep the credentials after reloading" do
333
- Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
334
- stubs(:sniffer).
335
- returns( mock(:hosts => [ {:host => 'foobar', :port => 4567, :id => 'foobar4567'} ]) )
336
-
337
- c = Elasticsearch::Transport::Client.new \
338
- :url => 'http://foo:1234',
339
- :user => 'USERNAME', :password => 'PASSWORD'
340
-
341
- assert_equal 'http://USERNAME:PASSWORD@foo:1234/', c.transport.connections.first.full_url('')
342
-
343
- c.transport.reload_connections!
344
-
345
- assert_equal 'http://USERNAME:PASSWORD@foobar:4567/', c.transport.connections.first.full_url('')
346
- end
347
-
348
- should "transfer selected host parts into the 'http' options" do
349
- c = Elasticsearch::Transport::Client.new \
350
- :host => { :scheme => 'https', :port => '8080', :host => 'node1', :user => 'U', :password => 'P' }
351
-
352
- assert_equal 'https://U:P@node1:8080/', c.transport.connections.first.full_url('')
353
-
354
- assert_equal 'https', c.transport.options[:http][:scheme]
355
- assert_equal 8080, c.transport.options[:http][:port]
356
- assert_equal 'U', c.transport.options[:http][:user]
357
- assert_equal 'P', c.transport.options[:http][:password]
358
- end
359
-
360
- should "transfer selected host parts from URL into the 'http' options" do
361
- c = Elasticsearch::Transport::Client.new :url => 'https://U:P@node1:8080'
362
-
363
- assert_equal 'https://U:P@node1:8080/', c.transport.connections.first.full_url('')
364
-
365
- assert_equal 'https', c.transport.options[:http][:scheme]
366
- assert_equal 8080, c.transport.options[:http][:port]
367
- assert_equal 'U', c.transport.options[:http][:user]
368
- assert_equal 'P', c.transport.options[:http][:password]
369
- end
370
- end
371
-
372
- end
373
- end