Skip to content

Commit

Permalink
pull construction of rsolr request params into separate method of Bla…
Browse files Browse the repository at this point in the history
…cklight::Solr::Repository
  • Loading branch information
barmintor committed Jan 13, 2022
1 parent 467796e commit e911c69
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
31 changes: 19 additions & 12 deletions lib/blacklight/solr/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,7 @@ def ping
# @return [Blacklight::Solr::Response] the solr response object
def send_and_receive(path, solr_params = {})
benchmark("Solr fetch", level: :debug) do
res = if solr_params[:json].present?
connection.send_and_receive(
path,
data: { params: solr_params.to_hash.except(:json) }.merge(solr_params[:json]).to_json,
method: :post,
headers: { 'Content-Type' => 'application/json' }
)
else
key = blacklight_config.http_method == :post ? :data : :params
connection.send_and_receive(path, { key => solr_params.to_hash, method: blacklight_config.http_method })
end

res = connection.send_and_receive(path, build_solr_request(solr_params))
solr_response = blacklight_config.response_model.new(res, solr_params, document_model: blacklight_config.document_model, blacklight_config: blacklight_config)

Blacklight.logger&.debug("Solr query: #{blacklight_config.http_method} #{path} #{solr_params.to_hash.inspect}")
Expand All @@ -86,6 +75,24 @@ def send_and_receive(path, solr_params = {})
raise Blacklight::Exceptions::InvalidRequest, e.message
end

# @return [Hash]
# @!visibility private
def build_solr_request(solr_params)
if solr_params[:json].present?
{
data: { params: solr_params.to_hash.except(:json) }.merge(solr_params[:json]).to_json,
method: :post,
headers: { 'Content-Type' => 'application/json' }
}
else
key = blacklight_config.http_method == :post ? :data : :params
{
key => solr_params.to_hash,
method: blacklight_config.http_method
}
end
end

private

##
Expand Down
48 changes: 27 additions & 21 deletions spec/models/blacklight/solr/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
CatalogController.blacklight_config.deep_copy
end

let(:all_docs_query) { '' }

let :mock_response do
{ response: { docs: [document] } }
end
Expand Down Expand Up @@ -104,19 +106,29 @@
expect(response).to be_a_kind_of Blacklight::Solr::Response
expect(response.params).to be_a_kind_of ActiveSupport::HashWithIndifferentAccess
end

it "calls send_and_receive with params returned from request factory method" do
expect(blacklight_config.http_method).to eq :get
input_params = { q: all_docs_query }
expect(subject.connection).to receive(:send_and_receive) do |path, params|
expect(path).to eq 'select'
expect(params[:method]).to eq :get
expect(params[:params]).to include input_params
end.and_return('response' => { 'docs' => [] })
subject.search(input_params)
end
end

describe "#send_and_receive" do
describe "#build_solr_request" do
let(:input_params) { { q: all_docs_query } }
let(:actual_params) { subject.build_solr_request(input_params) }
describe "http_method configuration" do
describe "using default" do
it "defaults to get" do
expect(blacklight_config.http_method).to eq :get
allow(subject.connection).to receive(:send_and_receive) do |path, params|
expect(path).to eq 'select'
expect(params[:method]).to eq :get
expect(params[:params]).to include(:q)
end.and_return('response' => { 'docs' => [] })
subject.search(q: @all_docs_query)
expect(actual_params[:method]).to eq :get
expect(actual_params[:params]).to include input_params
expect(actual_params).not_to have_key :data
end
end

Expand All @@ -125,25 +137,19 @@

it "keep value set to post" do
expect(blacklight_config.http_method).to eq :post
allow(subject.connection).to receive(:send_and_receive) do |path, params|
expect(path).to eq 'select'
expect(params[:method]).to eq :post
expect(params[:data]).to include(:q)
end.and_return('response' => { 'docs' => [] })
subject.search(q: @all_docs_query)
expect(actual_params[:method]).to eq :post
expect(actual_params[:data]).to include input_params
expect(actual_params).not_to have_key :params
end
end
end

context 'with json parameters' do
let(:input_params) { { json: { query: { bool: {} } } } }
it 'sends a post request with some json' do
allow(subject.connection).to receive(:send_and_receive) do |path, params|
expect(path).to eq 'select'
expect(params[:method]).to eq :post
expect(JSON.parse(params[:data]).with_indifferent_access).to include(query: { bool: {} })
expect(params[:headers]).to include({ 'Content-Type' => 'application/json' })
end.and_return('response' => { 'docs' => [] })
subject.search(json: { query: { bool: {} } })
expect(actual_params[:method]).to eq :post
expect(JSON.parse(actual_params[:data]).with_indifferent_access).to include(query: { bool: {} })
expect(actual_params[:headers]).to include({ 'Content-Type' => 'application/json' })
end
end
end
Expand All @@ -152,7 +158,7 @@
let (:blacklight_config) { config = Blacklight::Configuration.new; config.http_method = :post; config }

it "sends a post request to solr and get a response back" do
response = subject.search(q: @all_docs_query)
response = subject.search(q: all_docs_query)
expect(response.docs.length).to be >= 1
end
end
Expand Down

0 comments on commit e911c69

Please sign in to comment.