From 06e5fadd136ad918af721f3ad5b13e57d95cd793 Mon Sep 17 00:00:00 2001 From: Reid Beels Date: Fri, 26 Feb 2016 12:08:03 +0100 Subject: [PATCH] Sanitize Elasticsearch::Client config This avoids passing invalid config values to Elasticsearch::Client.new by stripping `prefix` and `transport_options[proc]` from the config hash. Fixes #333 --- lib/chewy.rb | 6 ++++-- spec/chewy_spec.rb | 14 +++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/chewy.rb b/lib/chewy.rb index 2c06a379a..d7d006e77 100644 --- a/lib/chewy.rb +++ b/lib/chewy.rb @@ -123,8 +123,10 @@ def create_type index, target, options = {}, &block # def client Thread.current[:chewy_client] ||= begin - block = configuration[:transport_options].try { |c| c[:proc] } - ::Elasticsearch::Client.new(configuration, &block) + client_configuration = configuration.deep_dup + client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client + block = client_configuration[:transport_options].try(:delete, :proc) + ::Elasticsearch::Client.new(client_configuration, &block) end end diff --git a/spec/chewy_spec.rb b/spec/chewy_spec.rb index ba360d540..4c46c3b43 100644 --- a/spec/chewy_spec.rb +++ b/spec/chewy_spec.rb @@ -113,13 +113,21 @@ describe '.client' do let!(:initial_client) { Thread.current[:chewy_client] } - let(:block) { proc { } } + let(:faraday_block) { proc { } } let(:mock_client) { double(:client) } + let(:expected_client_config) { { transport_options: {} } } before do Thread.current[:chewy_client] = nil - allow(Chewy).to receive_messages(configuration: { transport_options: { proc: block } }) - allow(::Elasticsearch::Client).to receive(:new).with(Chewy.configuration, &block).and_return(mock_client) + allow(Chewy).to receive_messages(configuration: { transport_options: { proc: faraday_block } }) + + allow(::Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*args, &passed_block| + # RSpec's `with(…, &block)` was used previously, but doesn't actually do + # any verification of the passed block (even of its presence). + expect(passed_block.source_location).to eq(faraday_block.source_location) + + mock_client + end end its(:client) { is_expected.to eq(mock_client) }