spec/api/client_spec.rb in rsolr-1.1.1.pre1 vs spec/api/client_spec.rb in rsolr-1.1.1.pre2

- old
+ new

@@ -1,39 +1,61 @@ require 'spec_helper' describe "RSolr::Client" do - + module ClientHelper def client @client ||= ( connection = RSolr::Connection.new RSolr::Client.new connection, :url => "http://localhost:9999/solr", :read_timeout => 42, :open_timeout=>43 ) end + + def client_with_proxy + @client_with_proxy ||= ( + connection = RSolr::Connection.new + RSolr::Client.new connection, :url => "http://localhost:9999/solr", :proxy => 'http://localhost:8080', :read_timeout => 42, :open_timeout=>43 + ) + end end - + context "initialize" do it "should accept whatevs and set it as the @connection" do expect(RSolr::Client.new(:whatevs).connection).to eq(:whatevs) end - + it "should use :update_path from options" do client = RSolr::Client.new(:whatevs, { update_path: 'update_test' }) expect(client.update_path).to eql('update_test') end - + it "should use 'update' for update_path by default" do client = RSolr::Client.new(:whatevs) expect(client.update_path).to eql('update') end + + it "should use :proxy from options" do + client = RSolr::Client.new(:whatevs, { proxy: 'http://my.proxy/' }) + expect(client.proxy.to_s).to eql('http://my.proxy/') + end + + it "should use 'nil' for proxy by default" do + client = RSolr::Client.new(:whatevs) + expect(client.proxy).to be_nil + end + + it "should use 'false' for proxy if passed 'false'" do + client = RSolr::Client.new(:whatevs, { proxy: false }) + expect(client.proxy).to eq(false) + end end - + context "send_and_receive" do include ClientHelper it "should forward these method calls the #connection object" do [:get, :post, :head].each do |meth| expect(client.connection).to receive(:execute). - and_return({:status => 200, :body => "{}", :headers => {}}) + and_return({:status => 200, :body => "{}", :headers => {}}) client.send_and_receive '', :method => meth, :params => {}, :data => nil, :headers => {} end end it "should be timeout aware" do @@ -85,21 +107,21 @@ and_return( :body => "", :status => 200, :headers => {"Content-Type"=>"text/plain"} ) - client.post "update", request_opts + client.post "update", request_opts end end - + context "xml" do include ClientHelper it "should return an instance of RSolr::Xml::Generator" do expect(client.xml).to be_a RSolr::Xml::Generator end end - + context "add" do include ClientHelper it "should send xml to the connection's #post method" do expect(client.connection).to receive(:execute). with( @@ -107,23 +129,23 @@ :path => "update", :headers => {"Content-Type"=>"text/xml"}, :method => :post, :data => "<xml/>" }) - ). - and_return( - :body => "", - :status => 200, - :headers => {"Content-Type"=>"text/xml"} - ) + ). + and_return( + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} + ) expect(client.xml).to receive(:add). with({:id=>1}, {:commitWith=>10}). - and_return("<xml/>") + and_return("<xml/>") client.add({:id=>1}, :add_attributes => {:commitWith=>10}) end end - + context "update" do include ClientHelper it "should send data to the connection's #post method" do expect(client.connection).to receive(:execute). with( @@ -131,32 +153,32 @@ :path => "update", :headers => {"Content-Type"=>"text/xml"}, :method => :post, :data => "<optimize/>" }) - ). - and_return( - :body => "", - :status => 200, - :headers => {"Content-Type"=>"text/xml"} - ) + ). + and_return( + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} + ) client.update(:data => "<optimize/>") end it "should use #update_path" do expect(client).to receive(:post).with('update_test', any_args) expect(client).to receive(:update_path).and_return('update_test') client.update({}) end - + it "should use path from opts" do expect(client).to receive(:post).with('update_opts', any_args) allow(client).to receive(:update_path).and_return('update_test') client.update({path: 'update_opts'}) end end - + context "post based helper methods:" do include ClientHelper [:commit, :optimize, :rollback].each do |meth| it "should send a #{meth} message to the connection's #post method" do expect(client.connection).to receive(:execute). @@ -165,21 +187,21 @@ :path => "update", :headers => {"Content-Type"=>"text/xml"}, :method => :post, :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>" }) - ). - and_return( - :body => "", - :status => 200, - :headers => {"Content-Type"=>"text/xml"} - ) + ). + and_return( + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} + ) client.send meth end end end - + context "delete_by_id" do include ClientHelper it "should send data to the connection's #post method" do expect(client.connection).to receive(:execute). with( @@ -187,20 +209,20 @@ :path => "update", :headers => {"Content-Type"=>"text/xml"}, :method => :post, :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id></delete>" }) - ). - and_return( - :body => "", - :status => 200, - :headers => {"Content-Type"=>"text/xml"} - ) + ). + and_return( + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} + ) client.delete_by_id 1 end end - + context "delete_by_query" do include ClientHelper it "should send data to the connection's #post method" do expect(client.connection).to receive(:execute). with( @@ -208,34 +230,34 @@ :path => "update", :headers => {"Content-Type"=>"text/xml"}, :method => :post, :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query fq=\"category:&quot;trash&quot;\"/></delete>" }) - ). - and_return( - :body => "", - :status => 200, - :headers => {"Content-Type"=>"text/xml"} - ) + ). + and_return( + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} + ) client.delete_by_query :fq => "category:\"trash\"" end end - + context "adapt_response" do include ClientHelper it 'should not try to evaluate ruby when the :qt is not :ruby' do body = '{"time"=>"NOW"}' result = client.adapt_response({:params=>{}}, {:status => 200, :body => body, :headers => {}}) expect(result).to eq(body) end - + it 'should evaluate ruby responses when the :wt is :ruby' do body = '{"time"=>"NOW"}' result = client.adapt_response({:params=>{:wt=>:ruby}}, {:status => 200, :body => body, :headers => {}}) expect(result).to eq({"time"=>"NOW"}) end - + it 'should evaluate json responses when the :wt is :json' do body = '{"time": "NOW"}' result = client.adapt_response({:params=>{:wt=>:json}}, {:status => 200, :body => body, :headers => {}}) if defined? JSON expect(result).to eq({"time"=>"NOW"}) @@ -253,13 +275,13 @@ it "ought raise a RSolr::Error::InvalidRubyResponse when the ruby is indeed frugged, or even fruggified" do expect { client.adapt_response({:params=>{:wt => :ruby}}, {:status => 200, :body => "<woops/>", :headers => {}}) }.to raise_error RSolr::Error::InvalidRubyResponse end - + end - + context "indifferent access" do include ClientHelper it "should raise a RuntimeError if the #with_indifferent_access extension isn't loaded" do hide_const("HashWithIndifferentAccess") body = "{'foo'=>'bar'}" @@ -320,7 +342,16 @@ 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 end + + it "should properly handle proxy configuration" do + result = client_with_proxy.build_request('select', + :method => :post, + :data => {:q=>'test', :fq=>[0,1]}, + :headers => {} + ) + result[:uri].to_s.should match /^http:\/\/localhost:9999\/solr\// + end end end