spec/api/client_spec.rb in rsolr-1.0.0.beta3 vs spec/api/client_spec.rb in rsolr-1.0.0.beta4

- old
+ new

@@ -2,49 +2,46 @@ describe "RSolr::Client" do module ClientHelper def client @client ||= ( - connection = RSolr::Http.new :url => "http://localhost:9999/solr" - RSolr::Client.new connection + connection = RSolr::Connection.new + RSolr::Client.new connection, :url => "http://localhost:9999/solr" ) end end context "initialize" do it "should accept whatevs and set it as the @connection" do RSolr::Client.new(:whatevs).connection.should == :whatevs end end - context "send_request" do + context "send_and_receive" do include ClientHelper it "should forward these method calls the #connection object" do [:get, :post, :head].each do |meth| - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). and_return({:status => 200, :body => "{}", :headers => {}}) - client.send_request '', :method => meth, :params => {}, :data => nil, :headers => {} + client.send_and_receive '', :method => meth, :params => {}, :data => nil, :headers => {} end end end context "post" do include ClientHelper it "should pass the expected params to the connection's #post method" do - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/plain"}, :method=>:post, :data=>"the data"} + client, hash_including({:path => "update", :headers=>{"Content-Type"=>"text/plain"}, :method=>:post, :data=>"the data"}) ). and_return( - :params=>{:wt=>:ruby}, - :query=>"wt=ruby", - :path => "update", - :data=>"the data", - :method=>:post, - :headers=>{"Content-Type"=>"text/plain"} + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/plain"} ) - client.post "update", :data => "the data", :headers => {"Content-Type" => "text/plain"} + client.post "update", :data => "the data", :method=>:post, :headers => {"Content-Type" => "text/plain"} end end context "xml" do include ClientHelper @@ -54,21 +51,18 @@ end context "add" do include ClientHelper it "should send xml to the connection's #post method" do - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<xml/>"} + client, hash_including({:path => "update", :headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<xml/>"}) ). and_return( - :path => "update", - :data => "<xml/>", - :headers => {"Content-Type"=>"text/xml"}, - :method => :post, - :query => "wt=ruby", - :params => {:wt=>:ruby} + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} ) # the :xml attr is lazy loaded... so load it up first client.xml client.xml.should_receive(:add). with({:id=>1}, {:commitWith=>10}). @@ -78,81 +72,116 @@ end context "update" do include ClientHelper it "should send data to the connection's #post method" do - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<optimize/>"} + client, hash_including({:path => "update", :headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<optimize/>"}) ). and_return( - :path => "update", - :data => "<optimize/>", - :headers => {"Content-Type"=>"text/xml"}, - :method => :post, - :query => "wt=ruby", - :params => {:wt=>:ruby} + :body => "", + :status => 200, + :headers => {"Content-Type"=>"text/xml"} ) client.update(:data => "<optimize/>") 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 - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>"} + client, hash_including({:path => "update", :headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>"}) ). and_return( - :path => "update", - :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>", - :headers => {"Content-Type"=>"text/xml"}, - :method => :post, - :query => "wt=ruby", - :params => {:wt=>:ruby} + :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 - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id></delete>"} + client, hash_including({:path => "update", :headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id></delete>"}) ). and_return( - :path => "update", - :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id></delete>", - :headers => {"Content-Type"=>"text/xml"}, - :method => :post, - :query => "wt=ruby", - :params => {:wt=>:ruby} + :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 - client.connection.should_receive(:send_request). + client.connection.should_receive(:execute). with( - "update", {:headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query fq=\"category:&quot;trash&quot;\"/></delete>"} + client, hash_including({: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( - :path => "update", - :data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query fq=\"category:&quot;trash&quot;\"/></delete>", - :headers => {"Content-Type"=>"text/xml"}, - :method => :post, - :query => "wt=ruby", - :params => {:wt=>:ruby} + :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 => {}}) + result.should be_a(String) + result.should == 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 => {}}) + result.should be_a(Hash) + result.should == {:time=>"NOW"} + end + + it "ought raise a RSolr::Error::InvalidRubyResponse when the ruby is indeed frugged" do + lambda { + client.adapt_response({:params=>{:wt => :ruby}}, {:status => 200, :body => "<woops/>", :headers => {}}) + }.should raise_error RSolr::Error::InvalidRubyResponse + end + + end + + 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| + result[:query].should match pattern + end + result[:data].should == "data" + result[:headers].should == {} + end + + it "should set the Content-Type header to application/x-www-form-urlencoded 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 => {} + result[:query].should == "wt=ruby" + [/fq=0/, /fq=1/, /q=test/].each do |pattern| + result[:data].should match pattern + end + result[:data].should_not match /wt=ruby/ + result[:headers].should == {"Content-Type" => "application/x-www-form-urlencoded"} + end + end end \ No newline at end of file