Permalink
Browse files

Cleanup arbitrary arguments handling in FakeWeb main methods.

Single argument doesn't throw an ArgumentError anymore
  • Loading branch information...
1 parent 6b0bdf9 commit 708aefee4ed34f91262211c4e110882b7b8e7a7d @mislav committed Jan 9, 2009
Showing with 26 additions and 24 deletions.
  1. +20 −21 lib/fake_web.rb
  2. +6 −3 test/test_fake_web.rb
View
@@ -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:
@@ -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
@@ -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
View
@@ -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

0 comments on commit 708aefe

Please sign in to comment.