From 8aa7eadc64ff7c8523ca6cd87083a8f445660273 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Mon, 10 Sep 2012 22:53:16 +0200 Subject: [PATCH] Add Connection unit test. --- Rakefile | 3 +- lib/neography/connection.rb | 105 ++++++++++++++++----------- spec/unit/connection_spec.rb | 137 +++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 45 deletions(-) create mode 100644 spec/unit/connection_spec.rb diff --git a/Rakefile b/Rakefile index 7aaf336..4f05a31 100644 --- a/Rakefile +++ b/Rakefile @@ -6,10 +6,9 @@ require 'neography/tasks' RSpec::Core::RakeTask.new(:spec) do |t| t.rspec_opts = "--color" - t.pattern = "spec/integration/*_spec.rb" + t.pattern = "spec/**/*_spec.rb" end desc "Run Tests" task :default => :spec - diff --git a/lib/neography/connection.rb b/lib/neography/connection.rb index bdadc3b..e69d117 100644 --- a/lib/neography/connection.rb +++ b/lib/neography/connection.rb @@ -10,58 +10,43 @@ class Connection :authentication, :username, :password, :parser - def initialize(options=ENV['NEO4J_URL'] || {}) - init = { - :protocol => Neography::Config.protocol, - :server => Neography::Config.server, - :port => Neography::Config.port, - :directory => Neography::Config.directory, - :cypher_path => Neography::Config.cypher_path, - :gremlin_path => Neography::Config.gremlin_path, - :log_file => Neography::Config.log_file, - :log_enabled => Neography::Config.log_enabled, - :max_threads => Neography::Config.max_threads, - :authentication => Neography::Config.authentication, - :username => Neography::Config.username, - :password => Neography::Config.password, - :parser => Neography::Config.parser - } + def initialize(options = ENV['NEO4J_URL'] || {}) + options = parse_string_options(options) unless options.is_a? Hash + config = initial_configuration + config.merge!(options) + + @protocol = config[:protocol] + @server = config[:server] + @port = config[:port] + @directory = config[:directory] + @cypher_path = config[:cypher_path] + @gremlin_path = config[:gremlin_path] + @log_file = config[:log_file] + @log_enabled = config[:log_enabled] + @max_threads = config[:max_threads] + @parser = config[:parser] + @user_agent = { "User-Agent" => USER_AGENT } - unless options.respond_to?(:each_pair) - url = URI.parse(options) - options = { - :protocol => url.scheme + "://", - :server => url.host, - :port => url.port, - :directory => url.path, - :username => url.user, - :password => url.password + @authentication = {} + + unless config[:authentication].empty? + @authentication = { + "#{config[:authentication]}_auth".to_sym => { + :username => config[:username], + :password => config[:password] + } } - options[:authentication] = 'basic' unless url.user.nil? end - init.merge!(options) - - @protocol = init[:protocol] - @server = init[:server] - @port = init[:port] - @directory = init[:directory] - @cypher_path = init[:cypher_path] - @gremlin_path = init[:gremlin_path] - @log_file = init[:log_file] - @log_enabled = init[:log_enabled] - @logger = Logger.new(@log_file) if @log_enabled - @max_threads = init[:max_threads] - @authentication = {} - @authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty? - @parser = init[:parser] - @user_agent = {"User-Agent" => USER_AGENT} + if @log_enabled + @logger = Logger.new(@log_file) + end end def configure(protocol, server, port, directory) @protocol = protocol @server = server - @port = port + @port = port @directory = directory end @@ -91,6 +76,8 @@ def delete(path, options={}) evaluate_response(HTTParty.delete(configuration + path, merge_options(options))) end + private + def evaluate_response(response) code = response.code body = response.body @@ -116,5 +103,37 @@ def evaluate_response(response) end end + def initial_configuration + { + :protocol => Neography::Config.protocol, + :server => Neography::Config.server, + :port => Neography::Config.port, + :directory => Neography::Config.directory, + :cypher_path => Neography::Config.cypher_path, + :gremlin_path => Neography::Config.gremlin_path, + :log_file => Neography::Config.log_file, + :log_enabled => Neography::Config.log_enabled, + :max_threads => Neography::Config.max_threads, + :authentication => Neography::Config.authentication, + :username => Neography::Config.username, + :password => Neography::Config.password, + :parser => Neography::Config.parser + } + end + + def parse_string_options(options) + url = URI.parse(options) + options = { + :protocol => url.scheme + "://", + :server => url.host, + :port => url.port, + :directory => url.path, + :username => url.user, + :password => url.password + } + options[:authentication] = 'basic' unless url.user.nil? + options + end + end end diff --git a/spec/unit/connection_spec.rb b/spec/unit/connection_spec.rb new file mode 100644 index 0000000..068de86 --- /dev/null +++ b/spec/unit/connection_spec.rb @@ -0,0 +1,137 @@ +require 'spec_helper' + +module Neography + describe Connection do + + subject(:connection) + + context "defaults" do + + it "intializes with defaults" do + connection.configuration.should == "http://localhost:7474/db/data" + end + + end + + context "custom options" do + + subject(:connection) { Connection.new(options) } + + context "hash options" do + let(:options) do + { + :protocol => "https://", + :server => "foobar", + :port => 4242, + :directory => "/dir", + :cypher_path => "/cyph", + :gremlin_path => "/grem", + :log_file => "neo.log", + :log_enabled => false, + :max_threads => 10, + :parser => Foo, + :authentication => "foo", + :username => "bar", + :password => "baz" + } + end + + it "accepts all options in a hash" do + connection.configuration.should == "https://foobar:4242/dir/db/data" + + connection.protocol.should == "https://" + connection.server.should == "foobar" + connection.port.should == 4242 + connection.directory.should == "/dir" + connection.cypher_path.should == "/cyph" + connection.gremlin_path.should == "/grem" + connection.log_file.should == "neo.log" + connection.log_enabled.should == false + connection.max_threads.should == 10 + connection.parser.should == Foo + + connection.authentication.should == { + :foo_auth => { + :username => "bar", + :password => "baz" + } + } + end + end + + context "string option" do + let(:options) { "https://user:pass@somehost:8585/path" } + + it "accepts a string as configuration" do + connection.configuration.should == "https://somehost:8585/path/db/data" + connection.authentication.should == { + :basic_auth => { + :username => "user", + :password => "pass" + } + } + end + end + + end + + context "requests" do + + it "does a GET request" do + HTTParty.should_receive(:get).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object } + connection.get("/foo/bar") + end + + it "does a POST request" do + HTTParty.should_receive(:post).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object } + connection.post("/foo/bar") + end + + it "does a PUT request" do + HTTParty.should_receive(:put).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object } + connection.put("/foo/bar") + end + + it "does a DELETE request" do + HTTParty.should_receive(:delete).with("http://localhost:7474/db/data/foo/bar", { :parser => MultiJsonParser }) { stub.as_null_object } + connection.delete("/foo/bar") + end + + context "authentication" do + subject(:connection) do + Connection.new({ + :authentication => "basic", + :username => "foo", + :password => "bar" + }) + end + + it "does requests with authentication" do + HTTParty.should_receive(:get).with( + "http://localhost:7474/db/data/foo/bar", + { :parser => MultiJsonParser, + :basic_auth => { + :username => "foo", + :password => "bar" + } + }) { stub.as_null_object } + + connection.get("/foo/bar") + end + end + + it "adds the User-Agent to the headers" do + HTTParty.should_receive(:get).with( + "http://localhost:7474/db/data/foo/bar", + { :parser => MultiJsonParser, + :headers => { "User-Agent" => "Neography/#{Neography::VERSION}" } + }) { stub.as_null_object } + + connection.get("/foo/bar", :headers => {}) + end + + end + end +end + +class Foo; end