Skip to content

Commit 70ef0d2

Browse files
committed
Solved code style issues
1 parent d98b81d commit 70ef0d2

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

lib/logstash/inputs/elasticsearch.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,14 @@ def setup_basic_auth(user, password)
565565
def setup_api_key(api_key)
566566
return {} unless (api_key&.value)
567567

568-
token = is_base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
568+
token = base64?(api_key.value) ? api_key.value : Base64.strict_encode64(api_key.value)
569569
{ 'Authorization' => "ApiKey #{token}" }
570570
end
571571

572-
def is_base64?(string)
573-
begin
574-
string == Base64.strict_encode64(Base64.strict_decode64(string))
575-
rescue ArgumentError
576-
false
577-
end
572+
def base64?(string)
573+
string == Base64.strict_encode64(Base64.strict_decode64(string))
574+
rescue ArgumentError
575+
false
578576
end
579577

580578
def prepare_user_agent

spec/inputs/elasticsearch_spec.rb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -823,26 +823,28 @@ def synchronize_method!(object, method_name)
823823
end
824824

825825
context "with ssl" do
826-
let(:config) { super().merge("ssl_enabled" => true) }
827-
encoded_api_key = Base64.strict_encode64('foo:bar')
826+
let(:api_key_value) { nil }
827+
let(:config) { super().merge("ssl_enabled" => true, 'api_key' => LogStash::Util::Password.new(api_key_value)) }
828+
let(:encoded_api_key) { Base64.strict_encode64('foo:bar') }
828829

829-
scenarios = {
830-
'with non-encoded api-key' => LogStash::Util::Password.new('foo:bar'),
831-
'with encoded api-key' => LogStash::Util::Password.new(encoded_api_key)
832-
}
830+
shared_examples "a plugin that sets the ApiKey authorization header" do
831+
it "correctly sets the Authorization header" do
832+
plugin.register
833+
client = plugin.send(:client)
834+
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
833835

834-
scenarios.each do |description, api_key_value|
835-
context description do
836-
let(:config) { super().merge('api_key' => api_key_value) }
836+
expect(auth_header).to eql("ApiKey #{encoded_api_key}")
837+
end
838+
end
837839

838-
it "should set authorization" do
839-
plugin.register
840-
client = plugin.send(:client)
841-
auth_header = extract_transport(client).options[:transport_options][:headers]['Authorization']
840+
context "with a non-encoded API key" do
841+
let(:api_key_value) { "foo:bar" }
842+
it_behaves_like "a plugin that sets the ApiKey authorization header"
843+
end
842844

843-
expect(auth_header).to eql "ApiKey #{encoded_api_key}"
844-
end
845-
end
845+
context "with an encoded API key" do
846+
let(:api_key_value) { encoded_api_key }
847+
it_behaves_like "a plugin that sets the ApiKey authorization header"
846848
end
847849

848850
context 'user also set' do

0 commit comments

Comments
 (0)