elasticsearch-transport 7.9.0 → 7.17.10
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.
- checksums.yaml +4 -4
- data/Gemfile +10 -10
- data/Gemfile-faraday1.gemfile +47 -0
- data/README.md +23 -17
- data/Rakefile +47 -10
- data/elasticsearch-transport.gemspec +16 -18
- data/lib/elasticsearch/transport/client.rb +133 -56
- data/lib/elasticsearch/transport/meta_header.rb +135 -0
- data/lib/elasticsearch/transport/transport/base.rb +41 -22
- data/lib/elasticsearch/transport/transport/connections/collection.rb +2 -5
- data/lib/elasticsearch/transport/transport/connections/connection.rb +6 -4
- data/lib/elasticsearch/transport/transport/errors.rb +1 -0
- data/lib/elasticsearch/transport/transport/http/curb.rb +44 -32
- data/lib/elasticsearch/transport/transport/http/faraday.rb +15 -5
- data/lib/elasticsearch/transport/transport/http/manticore.rb +41 -29
- data/lib/elasticsearch/transport/transport/response.rb +1 -1
- data/lib/elasticsearch/transport/version.rb +1 -1
- data/lib/elasticsearch/transport.rb +19 -30
- data/spec/elasticsearch/connections/collection_spec.rb +12 -0
- data/spec/elasticsearch/transport/base_spec.rb +90 -33
- data/spec/elasticsearch/transport/client_spec.rb +569 -164
- data/spec/elasticsearch/transport/http/curb_spec.rb +126 -0
- data/spec/elasticsearch/transport/http/faraday_spec.rb +141 -0
- data/spec/elasticsearch/transport/http/manticore_spec.rb +161 -0
- data/spec/elasticsearch/transport/meta_header_spec.rb +301 -0
- data/spec/spec_helper.rb +13 -5
- data/test/integration/jruby_test.rb +43 -0
- data/test/integration/transport_test.rb +93 -43
- data/test/test_helper.rb +10 -22
- data/test/unit/adapters_test.rb +88 -0
- data/test/unit/connection_test.rb +7 -2
- data/test/unit/response_test.rb +1 -1
- data/test/unit/transport_base_test.rb +17 -8
- data/test/unit/transport_curb_test.rb +0 -1
- data/test/unit/transport_faraday_test.rb +2 -2
- data/test/unit/transport_manticore_test.rb +242 -155
- metadata +62 -84
|
@@ -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
|
|
@@ -57,10 +57,15 @@ class Elasticsearch::Transport::Transport::Connections::ConnectionTest < Minites
|
|
|
57
57
|
assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('_search', {:foo => 'bar'})
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
should "return right full url with path when path starts with /" do
|
|
61
|
+
c = Connection.new :host => { :protocol => 'http', :host => 'localhost', :port => '9200', :path => '/foo' }
|
|
62
|
+
assert_equal 'http://localhost:9200/foo/_search?foo=bar', c.full_url('/_search', {:foo => 'bar'})
|
|
63
|
+
end
|
|
64
|
+
|
|
60
65
|
should "have a string representation" do
|
|
61
66
|
c = Connection.new :host => 'x'
|
|
62
|
-
assert_match
|
|
63
|
-
assert_match
|
|
67
|
+
assert_match(/host: x/, c.to_s)
|
|
68
|
+
assert_match(/alive/, c.to_s)
|
|
64
69
|
end
|
|
65
70
|
|
|
66
71
|
should "not be dead by default" do
|
data/test/unit/response_test.rb
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
403
|
+
end
|
|
395
404
|
|
|
396
405
|
context "logging" do
|
|
397
406
|
setup do
|
|
@@ -429,7 +438,7 @@ class Elasticsearch::Transport::Transport::BaseTest < Minitest::Test
|
|
|
429
438
|
@transport.stubs(:get_connection).returns(fake_connection)
|
|
430
439
|
|
|
431
440
|
@transport.logger.expects(:info).with do |message|
|
|
432
|
-
assert_match
|
|
441
|
+
assert_match(/http:\/\/user:\*{1,15}@localhost\:9200/, message)
|
|
433
442
|
true
|
|
434
443
|
end
|
|
435
444
|
|
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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']
|
|
@@ -149,7 +149,7 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Minitest::Test
|
|
|
149
149
|
|
|
150
150
|
transport.connections.first.connection.expects(:run_request).
|
|
151
151
|
with do |method, url, params, body|
|
|
152
|
-
assert_match
|
|
152
|
+
assert_match(/\?format=yaml/, url)
|
|
153
153
|
true
|
|
154
154
|
end.
|
|
155
155
|
returns(stub_everything)
|
|
@@ -167,7 +167,7 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Minitest::Test
|
|
|
167
167
|
|
|
168
168
|
transport.connections.first.connection.expects(:run_request).
|
|
169
169
|
with do |method, url, params, body|
|
|
170
|
-
assert_match
|
|
170
|
+
assert_match(/\?format=json/, url)
|
|
171
171
|
true
|
|
172
172
|
end.
|
|
173
173
|
returns(stub_everything)
|
|
@@ -17,166 +17,253 @@
|
|
|
17
17
|
|
|
18
18
|
require 'test_helper'
|
|
19
19
|
|
|
20
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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/x-ndjson',
|
|
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
|
+
assert_equal(transport.options[:ssl][:truststore_password], 'test')
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
should 'pass :transport_options to Manticore::Client' do
|
|
214
|
+
options = {
|
|
215
|
+
transport_options: { potatoes: 1 }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
::Manticore::Client.expects(:new).with(potatoes: 1, ssl: {})
|
|
219
|
+
transport = Manticore.new(hosts: [{ host: 'foobar', port: 1234 }], options: options)
|
|
220
|
+
assert_equal(transport.options[:transport_options][:potatoes], 1)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
context 'custom headers' do
|
|
224
|
+
should 'allow authorization headers' do
|
|
225
|
+
transport_options = { headers: { 'Authorization' => 'Basic token' } }
|
|
226
|
+
transport = Manticore.new(
|
|
227
|
+
hosts: [{ host: 'foobar', port: 1234 }],
|
|
228
|
+
transport_options: transport_options
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
assert_equal(
|
|
232
|
+
transport.instance_variable_get(:@request_options)[:headers]['Authorization'],
|
|
233
|
+
'Basic token'
|
|
234
|
+
)
|
|
235
|
+
transport.connections.first.connection.expects(:get).with do |_host, options|
|
|
236
|
+
assert_equal('Basic token', options[:headers]['Authorization'])
|
|
237
|
+
true
|
|
238
|
+
end.returns(stub_everything)
|
|
239
|
+
transport.perform_request('GET', '/', {})
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
should 'allow user agent headers' do
|
|
243
|
+
transport_options = { headers: { 'User-Agent' => 'Custom UA' } }
|
|
244
|
+
transport = Manticore.new(
|
|
245
|
+
hosts: [{ host: 'localhost' }],
|
|
246
|
+
transport_options: transport_options
|
|
247
|
+
)
|
|
248
|
+
transport.connections.first.connection.expects(:get).with do |_host, options|
|
|
249
|
+
assert_equal('Custom UA', options[:headers]['User-Agent'])
|
|
250
|
+
true
|
|
251
|
+
end.returns(stub_everything)
|
|
252
|
+
transport.perform_request('GET', '/', {})
|
|
253
|
+
|
|
254
|
+
assert_equal(
|
|
255
|
+
transport.instance_variable_get('@request_options')[:headers]['User-Agent'],
|
|
256
|
+
'Custom UA'
|
|
257
|
+
)
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
151
261
|
end
|
|
152
|
-
|
|
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
|
|
262
|
+
end
|
|
177
263
|
end
|
|
178
264
|
end
|
|
179
|
-
|
|
180
265
|
end
|
|
181
|
-
|
|
266
|
+
else
|
|
267
|
+
version = "#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'Ruby'} #{RUBY_VERSION}"
|
|
268
|
+
puts "SKIP: '#{File.basename(__FILE__)}' only supported on JRuby (you're running #{version})"
|
|
182
269
|
end
|