Skip to content

Commit

Permalink
Merge pull request #339 from reidab/fix-333
Browse files Browse the repository at this point in the history
Sanitize Elasticsearch::Client config
  • Loading branch information
pyromaniac committed Feb 26, 2016
2 parents d24f86f + 06e5fad commit 3705036
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions spec/chewy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 3705036

Please sign in to comment.