Skip to content

Commit 21a4671

Browse files
committed
[GEM] Updates specs
1 parent f9bafc4 commit 21a4671

File tree

7 files changed

+106
-43
lines changed

7 files changed

+106
-43
lines changed

elasticsearch-transport/spec/elasticsearch/transport/base_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
it 'does not include the password in the logged string' do
3232
expect(logger).not_to receive(:error).with(/secret_password/)
3333
expect {
34-
client.cluster.stats
34+
client.perform_request('GET', '/_cluster/stats')
3535
}.to raise_exception(Faraday::ConnectionFailed)
3636
end
3737

3838
it 'replaces the password with the string \'REDACTED\'' do
3939
expect(logger).to receive(:error).with(/REDACTED/)
4040
expect {
41-
client.cluster.stats
41+
client.perform_request('GET', '/_cluster/stats')
4242
}.to raise_exception(Faraday::ConnectionFailed)
4343
end
4444
end
@@ -93,7 +93,7 @@
9393
end
9494

9595
it 'raises an exception' do
96-
expect { client.info }.to raise_exception(Faraday::ConnectionFailed)
96+
expect { client.perform_request('GET', '/info') }.to raise_exception(Faraday::ConnectionFailed)
9797
end
9898
end
9999

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

-37
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
end
2525
end
2626

27-
it 'is aliased as Elasticsearch::Client' do
28-
expect(Elasticsearch::Client.new).to be_a(described_class)
29-
end
30-
3127
it 'has a default transport' do
3228
expect(client.transport).to be_a(Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS)
3329
end
@@ -1221,39 +1217,6 @@
12211217
end
12221218
end
12231219
end
1224-
1225-
context 'when a header is set on an endpoint request' do
1226-
let(:client) { described_class.new(host: hosts) }
1227-
let(:headers) { { 'user-agent' => 'my ruby app' } }
1228-
1229-
it 'performs the request with the header' do
1230-
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
1231-
expect { client.search(headers: headers) }.not_to raise_error
1232-
expect(client).to have_received(:perform_request)
1233-
.with('GET', '_search', {}, nil, headers)
1234-
end
1235-
end
1236-
1237-
context 'when a header is set on an endpoint request and on initialization' do
1238-
let!(:client) do
1239-
described_class.new(
1240-
host: hosts,
1241-
transport_options: { headers: instance_headers }
1242-
)
1243-
end
1244-
let(:instance_headers) { { set_in_instantiation: 'header value' } }
1245-
let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}}
1246-
1247-
it 'performs the request with the header' do
1248-
expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers)
1249-
1250-
expect_any_instance_of(Faraday::Connection)
1251-
.to receive(:run_request)
1252-
.with(:get, "http://#{hosts[0]}/_search", nil, expected_headers) { OpenStruct.new(body: '')}
1253-
1254-
client.search(headers: param_headers)
1255-
end
1256-
end
12571220
end
12581221

12591222
context 'when the client connects to Elasticsearch' do

elasticsearch-transport/spec/elasticsearch/transport/meta_header_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class MyTransport
242242
include Elasticsearch::Transport::Transport::Base
243243
def initialize(args); end
244244
end
245-
let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
246-
let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
245+
let(:client) { Elasticsearch::Transport::Client.new(transport_class: MyTransport) }
246+
let(:subject) { client.instance_variable_get('@arguments')[:transport_options][:headers] }
247247
let(:meta_header) do
248248
if jruby?
249249
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"

elasticsearch/spec/spec_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
def meta_version
2626
client.send(:client_meta_version, Elasticsearch::VERSION)
2727
end
28+
29+
def jruby?
30+
defined?(JRUBY_VERSION)
31+
end

elasticsearch/spec/unit/api_key_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
context 'Metaheader' do
7676
let(:adapter_code) { "nh=#{defined?(Net::HTTP::VERSION) ? Net::HTTP::VERSION : Net::HTTP::HTTPVersion}" }
7777
let(:meta_header) do
78-
if defined?(JRUBY)
78+
if jruby?
7979
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
8080
else
8181
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},fd=#{Faraday::VERSION},#{adapter_code}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
require 'spec_helper'
18+
19+
describe Elasticsearch::Client do
20+
context 'when using custom transport implementation' do
21+
class MyTransport
22+
include Elasticsearch::Transport::Transport::Base
23+
def initialize(args); end
24+
end
25+
let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
26+
let(:arguments) { client.instance_variable_get('@transport').instance_variable_get('@arguments') }
27+
let(:subject) do
28+
arguments[:transport_options][:headers]
29+
end
30+
31+
let(:meta_header) do
32+
if jruby?
33+
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"
34+
else
35+
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}"
36+
end
37+
end
38+
39+
it 'doesnae set any info about the implementation in the metaheader' do
40+
expect(subject).to include('x-elastic-client-meta' => meta_header)
41+
end
42+
end
43+
end
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
require 'spec_helper'
18+
require 'ostruct'
19+
20+
describe Elasticsearch::Client do
21+
context 'when a header is set on an endpoint request' do
22+
let(:client) { described_class.new }
23+
let(:headers) { { 'user-agent' => 'my ruby app' } }
24+
25+
it 'performs the request with the header' do
26+
allow(client).to receive(:perform_request) { OpenStruct.new(body: '') }
27+
expect { client.search(headers: headers) }.not_to raise_error
28+
expect(client).to have_received(:perform_request)
29+
.with('GET', '_search', {}, nil, headers)
30+
end
31+
end
32+
33+
context 'when a header is set on an endpoint request and on initialization' do
34+
let!(:client) do
35+
described_class.new(
36+
host: 'http://localhost:9200',
37+
transport_options: { headers: instance_headers }
38+
)
39+
end
40+
let(:instance_headers) { { set_in_instantiation: 'header value' } }
41+
let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}}
42+
43+
it 'performs the request with the header' do
44+
expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers)
45+
46+
expect_any_instance_of(Faraday::Connection)
47+
.to receive(:run_request)
48+
.with(:get, "http://localhost:9200/_search", nil, expected_headers) { OpenStruct.new(body: '')}
49+
50+
client.search(headers: param_headers)
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)