diff --git a/lib/fake_web.rb b/lib/fake_web.rb index ea17978..72c80ac 100644 --- a/lib/fake_web.rb +++ b/lib/fake_web.rb @@ -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 # :any 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 \ No newline at end of file diff --git a/test/test_fake_web.rb b/test/test_fake_web.rb index 43879c3..9ad2531 100644 --- a/test/test_fake_web.rb +++ b/test/test_fake_web.rb @@ -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