Skip to content

Commit

Permalink
Merge pull request #103 from rsolr/only_one_wt
Browse files Browse the repository at this point in the history
Allow setting wt as a string key to params
  • Loading branch information
ndushay committed Mar 4, 2015
2 parents a4f7df4 + 23408b9 commit 122a96d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
10 changes: 8 additions & 2 deletions lib/rsolr/client.rb
Expand Up @@ -240,7 +240,7 @@ def build_request path, opts
opts[:proxy] = proxy unless proxy.nil?
opts[:method] ||= :get
raise "The :data option can only be used if :method => :post" if opts[:method] != :post and opts[:data]
opts[:params] = opts[:params].nil? ? {:wt => default_wt} : {:wt => default_wt}.merge(opts[:params])
opts[:params] = params_with_wt(opts[:params])
query = RSolr::Uri.params_to_solr(opts[:params]) unless opts[:params].empty?
opts[:query] = query
if opts[:data].is_a? Hash
Expand All @@ -252,7 +252,13 @@ def build_request path, opts
opts[:uri] = base_uri.merge(path.to_s + (query ? "?#{query}" : "")) if base_uri
opts
end


def params_with_wt(params)
return { wt: default_wt } if params.nil?
return params if params.key?(:wt) || params.key?('wt')
{ wt: default_wt }.merge(params)
end

def build_paginated_request page, per_page, path, opts
per_page = per_page.to_s.to_i
page = page.to_s.to_i-1
Expand Down
58 changes: 33 additions & 25 deletions spec/api/client_spec.rb
Expand Up @@ -258,34 +258,42 @@ def client

context "build_request" do
include ClientHelper
it 'should return a request context array' do
result = client.build_request('select',
:method => :post,
:params => {:q=>'test', :fq=>[0,1]},
:data => "data",
:headers => {}
)
[/fq=0/, /fq=1/, /q=test/, /wt=ruby/].each do |pattern|
expect(result[:query]).to match pattern
let(:data) { 'data' }
let(:params) { { q: 'test', fq: [0,1] } }
let(:options) { { method: :post, params: params, data: data, headers: {} } }
subject { client.build_request('select', options) }

context "when params are symbols" do
it 'should return a request context array' do
[/fq=0/, /fq=1/, /q=test/, /wt=ruby/].each do |pattern|
expect(subject[:query]).to match pattern
end
expect(subject[:data]).to eq("data")
expect(subject[:headers]).to eq({})
end
expect(result[:data]).to eq("data")
expect(result[:headers]).to eq({})
end

it "should set the Content-Type header to application/x-www-form-urlencoded; charset=UTF-8 if a hash is passed in to the data arg" do
result = client.build_request('select',
:method => :post,
:data => {:q=>'test', :fq=>[0,1]},
:headers => {}
)
expect(result[:query]).to eq("wt=ruby")
[/fq=0/, /fq=1/, /q=test/].each do |pattern|
expect(result[:data]).to match pattern

context "when params are strings" do
let(:params) { { 'q' => 'test', 'wt' => 'json' } }
it 'should return a request context array' do
expect(subject[:query]).to eq 'q=test&wt=json'
expect(subject[:data]).to eq("data")
expect(subject[:headers]).to eq({})
end
end

context "when a Hash is passed in as data" do
let(:data) { { q: 'test', fq: [0,1] } }
let(:options) { { method: :post, data: data, headers: {} } }

it "sets the Content-Type header to application/x-www-form-urlencoded; charset=UTF-8" do
expect(subject[:query]).to eq("wt=ruby")
[/fq=0/, /fq=1/, /q=test/].each do |pattern|
expect(subject[:data]).to match pattern
end
expect(subject[:data]).not_to match /wt=ruby/
expect(subject[:headers]).to eq({"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"})
end
expect(result[:data]).not_to match /wt=ruby/
expect(result[:headers]).to eq({"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"})
end

end

end

0 comments on commit 122a96d

Please sign in to comment.