Skip to content

Commit

Permalink
Cleanup arbitrary arguments handling in FakeWeb main methods.
Browse files Browse the repository at this point in the history
Single argument doesn't throw an ArgumentError anymore
  • Loading branch information
mislav committed Jan 9, 2009
1 parent 6b0bdf9 commit 708aefe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
41 changes: 20 additions & 21 deletions lib/fake_web.rb
Expand Up @@ -106,14 +106,7 @@ class NetConnectNotAllowedError < StandardError; end;
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
#
def self.register_uri(*args)
method = :any
case args.length
when 3 then method, uri, options = *args
when 2 then uri, options = *args
else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri, options)")
end

Registry.instance.register_uri(method, uri, options)
Registry.instance.register_uri(*extract_arguments(args))
end

# call-seq:
Expand All @@ -122,13 +115,7 @@ def self.register_uri(*args)
#
# Returns the faked Net::HTTPResponse object associated with +uri+.
def self.response_for(*args, &block) #:nodoc: :yields: response
method = :any
case args.length
when 2 then method, uri = *args
when 1 then uri = *args
else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)")
end

method, uri = extract_arguments(args)
Registry.instance.response_for(method, uri, &block)
end

Expand All @@ -140,14 +127,26 @@ def self.response_for(*args, &block) #:nodoc: :yields: response
# specify +method+ to limit the search to a certain HTTP method (or use
# <tt>:any</tt> to explicitly check against any method).
def self.registered_uri?(*args)
method = :any
method, uri = extract_arguments(args)
Registry.instance.registered_uri?(method, uri)
end

private

def self.extract_arguments(args)
args.push({}) unless args.last.is_a?(Hash) or args.last.is_a?(Array)

case args.length
when 2 then method, uri = *args
when 1 then uri = *args
else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)")
when 2
uri, options = *args
method = :any
when 3
method, uri, options = *args
else
raise ArgumentError, "wrong number of arguments (expected the URI at least)"
end

Registry.instance.registered_uri?(method, uri)
[method, uri, options]
end

end
9 changes: 6 additions & 3 deletions test/test_fake_web.rb
Expand Up @@ -11,11 +11,14 @@ def test_register_uri
FakeWeb.register_uri('http://mock/test_example.txt', :string => "example")
assert FakeWeb.registered_uri?('http://mock/test_example.txt')
end

def test_register_uri_with_wrong_number_of_arguments
assert_raises ArgumentError do
def test_register_uri_with_a_single_argument
assert_nothing_raised do
FakeWeb.register_uri("http://example.com")
end
end

def test_register_uri_with_wrong_number_of_arguments
assert_raises ArgumentError do
FakeWeb.register_uri(:get, "http://example.com", "/example", :string => "example")
end
Expand Down

0 comments on commit 708aefe

Please sign in to comment.