Skip to content

Commit

Permalink
Merge pull request #62 from rdvdijk/master
Browse files Browse the repository at this point in the history
Cleaned up Connection and added unit tests.
  • Loading branch information
maxdemarzi committed Sep 10, 2012
2 parents b80126e + e12e795 commit c80d560
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


105 changes: 62 additions & 43 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
137 changes: 137 additions & 0 deletions spec/unit/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c80d560

Please sign in to comment.