spec/riak/client_spec.rb in riak-client-0.9.0.beta vs spec/riak/client_spec.rb in riak-client-0.9.0.beta2

- old
+ new

@@ -19,20 +19,34 @@ client = Riak::Client.new client.host.should == "127.0.0.1" client.port.should == 8098 end + it "should accept a protocol" do + client = Riak::Client.new :protocol => 'https' + client.protocol.should eq('https') + end + it "should accept a host" do client = Riak::Client.new :host => "riak.basho.com" client.host.should == "riak.basho.com" end it "should accept a port" do client = Riak::Client.new :port => 9000 client.port.should == 9000 end + it "should default the port to 8087 when the protocol is pbc" do + Riak::Client.new(:protocol => "pbc").port.should == 8087 + end + + it "should accept basic_auth" do + client = Riak::Client.new :basic_auth => "user:pass" + client.basic_auth.should eq("user:pass") + end + it "should accept a client ID" do client = Riak::Client.new :client_id => "AAAAAA==" client.client_id.should == "AAAAAA==" end @@ -76,10 +90,29 @@ describe "reconfiguring" do before :each do @client = Riak::Client.new end + describe "setting the protocol" do + it "should allow setting the protocol" do + @client.should respond_to(:protocol=) + @client.protocol = "https" + @client.protocol.should eq("https") + end + + it "should require a valid protocol to be set" do + lambda { @client.protocol = 'invalid-protocol' }.should( + raise_error(ArgumentError, /^'invalid-protocol' is not a valid protocol/)) + end + + it "should reset the unified backend when changing the protocol" do + old = @client.backend + @client.protocol = "pbc" + old.should_not eq(@client.backend) + end + end + describe "setting the host" do it "should allow setting the host" do @client.should respond_to(:host=) @client.host = "riak.basho.com" @client.host.should == "riak.basho.com" @@ -110,10 +143,22 @@ lambda { @client.port = valid }.should_not raise_error end end end + describe "setting http auth" do + it "should allow setting basic_auth" do + @client.should respond_to(:basic_auth=) + @client.basic_auth = "user:pass" + @client.basic_auth.should eq("user:pass") + end + + it "should require that basic auth splits into two even parts" do + lambda { @client.basic_auth ="user" }.should raise_error(ArgumentError, "basic auth must be set using 'user:pass'") + end + end + it "should allow setting the prefix" do @client.should respond_to(:prefix=) @client.prefix = "/another-prefix" @client.prefix.should == "/another-prefix" end @@ -155,15 +200,49 @@ end end it "should raise an error when the chosen backend is not valid" do Riak::Client::NetHTTPBackend.should_receive(:configured?).and_return(false) - @client = Riak::Client.new(:http_backend => :NetHTTP) + # @client = Riak::Client.new(:http_backend => :NetHTTP) lambda { @client.http }.should raise_error end end + describe "choosing a Protobuffs backend" do + before :each do + @client = Riak::Client.new(:protocol => "pbc") + end + + it "should choose the selected backend" do + @client.protobuffs_backend = :Beefcake + @client.protobuffs.should be_instance_of(Riak::Client::BeefcakeProtobuffsBackend) + end + + it "should raise an error when the chosen backend is not valid" do + Riak::Client::BeefcakeProtobuffsBackend.should_receive(:configured?).and_return(false) + lambda { @client.protobuffs }.should raise_error + end + end + + describe "choosing a unified backend" do + before :each do + @client = Riak::Client.new + end + + it "should use HTTP when the protocol is http or https" do + %w[http https].each do |p| + @client.protocol = p + @client.backend.should be_kind_of(Riak::Client::HTTPBackend) + end + end + + it "should use Protobuffs when the protocol is pbc" do + @client.protocol = "pbc" + @client.backend.should be_kind_of(Riak::Client::ProtobuffsBackend) + end + end + describe "retrieving a bucket" do before :each do @client = Riak::Client.new @backend = mock("Backend") @client.stub!(:backend).and_return(@backend) @@ -181,11 +260,10 @@ it "should fetch keys if asked" do @backend.should_receive(:list_keys) {|b| b.name.should == "foo"; ["bar"] } @client.bucket("foo", :keys => true) end - it "should memoize bucket parameters" do @bucket = mock("Bucket") Riak::Bucket.should_receive(:new).with(@client, "baz").once.and_return(@bucket) @client.bucket("baz").should == @bucket @client.bucket("baz").should == @bucket @@ -280,12 +358,140 @@ # Get escapes keys @http.should_receive(:get).with(200, "/luwak", "docs%2FA%20Big%20PDF.pdf").and_yield("foo").and_return(:headers => {"content-type" => ["text/plain"]}, :code => 200) @client.get_file("docs/A Big PDF.pdf") # Streamed get escapes keys @http.should_receive(:get).with(200, "/luwak", "docs%2FA%20Big%20PDF.pdf").and_yield("foo").and_return(:headers => {"content-type" => ["text/plain"]}, :code => 200) - @client.get_file("docs/A Big PDF.pdf"){|chunk| chunk } + @client.get_file("docs/A Big PDF.pdf"){|chunk| chunk} # Put escapes keys @http.should_receive(:put).with(204, "/luwak", "docs%2FA%20Big%20PDF.pdf", "foo", {"Content-Type" => "text/plain"}) @client.store_file("docs/A Big PDF.pdf", "text/plain", "foo") + end + end + + describe "ssl", :ssl => true do + before :each do + @client = Riak::Client.new + end + + it "should allow passing ssl options into the initializer" do + lambda { client = Riak::Client.new(:ssl => {:verify_mode => "peer"}) }.should_not raise_error + end + + it "should not have ssl options by default" do + @client.ssl_options.should be_nil + end + + it "should have a blank hash for ssl options if the protocol is set to https" do + @client.protocol = 'https' + @client.ssl_options.should be_a(Hash) + end + + # The api should have an ssl= method for setting up all of the ssl + # options. Once the ssl options have been assigned via `ssl=` they should + # be read back out using the read only `ssl_options`. This is to provide + # a seperate api for setting ssl options via client configuration and + # reading them inside of a http backend. + it "should not allow reading ssl options via ssl" do + @client.should_not respond_to(:ssl) + end + + it "should now allow writing ssl options via ssl_options=" do + @client.should_not respond_to(:ssl_options=) + end + + it "should allow setting ssl to true" do + @client.ssl = true + @client.ssl_options[:verify_mode].should eq('none') + end + + it "should allow setting ssl options as a hash" do + @client.ssl = {:verify_mode => "peer"} + @client.ssl_options[:verify_mode].should eq('peer') + end + + it "should set the protocol to https when setting ssl to true" do + @client.ssl = true + @client.protocol.should eq("https") + end + + it "should set the protocol to http when setting ssl to false" do + @client.protocol = 'https' + @client.ssl = false + @client.protocol.should eq('http') + end + + it "should should clear ssl options when setting ssl to false" do + @client.ssl = true + @client.ssl_options.should_not be_nil + @client.ssl = false + @client.ssl_options.should be_nil + end + + it "should set the protocol to https when setting ssl options" do + @client.ssl = {:verify_mode => "peer"} + @client.protocol.should eq("https") + end + + it "should allow setting the verify_mode to none" do + @client.ssl = {:verify_mode => "none"} + @client.ssl_options[:verify_mode].should eq("none") + end + + it "should allow setting the verify_mode to peer" do + @client.ssl = {:verify_mode => "peer"} + @client.ssl_options[:verify_mode].should eq("peer") + end + + it "should not allow setting the verify_mode to anything else" do + lambda {@client.ssl = {:verify_mode => :your_mom}}.should raise_error(ArgumentError) + end + + it "should default verify_mode to none" do + @client.ssl = true + @client.ssl_options[:verify_mode].should eq("none") + end + + it "should let the backend know if ssl is enabled" do + @client.should_not be_ssl_enabled + @client.ssl = true + @client.should be_ssl_enabled + end + + it "should allow setting the pem" do + @client.ssl = {:pem => 'i-am-a-pem'} + @client.ssl_options[:pem].should eq('i-am-a-pem') + end + + it "should set them pem from the contents of pem_file" do + filepath = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test.pem')) + @client.ssl = {:pem_file => filepath} + @client.ssl_options[:pem].should eq("i-am-a-pem\n") + end + + it "should allow setting the pem_password" do + @client.ssl = {:pem_password => 'pem-pass'} + @client.ssl_options[:pem_password].should eq('pem-pass') + end + + it "should allow setting the ca_file" do + @client.ssl = {:ca_file => '/path/to/ca.crt'} + @client.ssl_options[:ca_file].should eq('/path/to/ca.crt') + end + + it "should allow setting the ca_path" do + @client.ssl = {:ca_path => '/path/to/certs/'} + @client.ssl_options[:ca_path].should eq('/path/to/certs/') + end + + %w[pem ca_file ca_path].each do |option| + it "should default the verify_mode to peer when setting the #{option}" do + @client.ssl = {option.to_sym => 'test-data'} + @client.ssl_options[:verify_mode].should eq("peer") + end + + it "should allow setting the verify mode when setting the #{option}" do + @client.ssl = {option.to_sym => 'test-data', :verify_mode => "none"} + @client.ssl_options[:verify_mode].should eq("none") + end end end end