Skip to content

Commit

Permalink
Deprecate the implicit :any HTTP method
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisk committed Jun 1, 2009
1 parent b038c71 commit 0142d47
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 131 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
@@ -1,3 +1,10 @@
fakeweb (development)

* DEPRECATION: Calling FakeWeb.register_uri and FakeWeb.registered_uri? without
an HTTP method as the first argument is now deprecated. To match against any
HTTP method (the pre-1.2.0 behavior), use :any [Chris Kampmeier]


fakeweb (1.2.3)

* fix the #http_version of :file and :string responses, which was returning the
Expand Down
7 changes: 3 additions & 4 deletions README.rdoc
Expand Up @@ -70,8 +70,7 @@ If you use the <tt>:any</tt> symbol, the URI you specify will be completely
stubbed out (regardless of the HTTP method of the request). This can be useful
for RPC-like services, where the HTTP method isn't significant. (Older
versions of FakeWeb always behaved like this, and didn't accept the first
+method+ argument above; this syntax is still supported, for
backwards-compatibility, but it will probably be deprecated at some point.)
+method+ argument above; this syntax is now deprecated.)

=== Rotating responses

Expand All @@ -96,8 +95,8 @@ option for that response.)
You can stub requests that use basic authentication with +userinfo+ strings in
the URIs:

FakeWeb.register_uri("http://example.com/secret", :string => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri("http://user:pass@example.com/secret", :string => "Authorized")
FakeWeb.register_uri(:get, "http://example.com/secret", :string => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri(:get, "http://user:pass@example.com/secret", :string => "Authorized")

Net::HTTP.start("example.com") do |http|
req = Net::HTTP::Get.new("/secret")
Expand Down
80 changes: 45 additions & 35 deletions lib/fake_web.rb
Expand Up @@ -47,14 +47,13 @@ class NetConnectNotAllowedError < StandardError; end;

# call-seq:
# FakeWeb.register_uri(method, uri, options)
# FakeWeb.register_uri(uri, options)
#
# Register requests using the HTTP method specified by the symbol +method+ for
# +uri+ to be handled according to +options+. If no +method+ is specified, or
# you explicitly specify <tt>:any</tt>, the response will be reigstered for
# any request for +uri+. +uri+ can be a +String+ or a +URI+ object. +options+
# must be either a +Hash+ or an +Array+ of +Hashes+ (see below) that must
# contain any one of the following keys:
# Register requests using the HTTP method specified by the symbol +method+
# for +uri+ to be handled according to +options+. If you specify the method
# <tt>:any</tt>, the response will be reigstered for any request for +uri+.
# +uri+ can be a +String+ or a +URI+ object. +options+ must be either a
# +Hash+ or an +Array+ of +Hashes+ (see below) that must contain any one of
# the following keys:
#
# <tt>:string</tt>::
# Takes a +String+ argument that is returned as the body of the response.
Expand Down Expand Up @@ -85,9 +84,9 @@ class NetConnectNotAllowedError < StandardError; end;
# documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html]
# for more information on creating custom response objects.
#
# +options+ may also be an +Array+ containing a list of the above-described +Hash+.
# In this case, FakeWeb will rotate through each provided response, you may optionally
# provide:
# +options+ may also be an +Array+ containing a list of the above-described
# +Hash+. In this case, FakeWeb will rotate through each provided response,
# you may optionally provide:
#
# <tt>:times</tt>::
# The number of times this response will be used. Decremented by one each time it's called.
Expand All @@ -106,48 +105,59 @@ 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)")
when 3
Registry.instance.register_uri(*args)
when 2
print_missing_http_method_deprecation_warning(*args)
Registry.instance.register_uri(:any, *args)
else
raise ArgumentError.new("wrong number of arguments (#{args.length} for 3)")
end

Registry.instance.register_uri(method, uri, options)
end

# call-seq:
# FakeWeb.response_for(method, uri)
# FakeWeb.response_for(uri)
#
# Returns the faked Net::HTTPResponse object associated with +uri+.
# Returns the faked Net::HTTPResponse object associated with +method+ and +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.first
else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)")
when 2
Registry.instance.response_for(*args, &block)
when 1
print_missing_http_method_deprecation_warning(*args)
Registry.instance.response_for(:any, *args, &block)
else
raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)")
end

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

# call-seq:
# FakeWeb.registered_uri?(method, uri)
# FakeWeb.registered_uri?(uri)
#
# Returns true if +uri+ is registered with FakeWeb. You can optionally
# specify +method+ to limit the search to a certain HTTP method (or use
# <tt>:any</tt> to explicitly check against any method).
# Returns true if a +method+ request for +uri+ is registered with FakeWeb.
# Specify a method of <tt>:any</tt> to check for against all HTTP methods.
def self.registered_uri?(*args)
method = :any
case args.length
when 2 then method, uri = args
when 1 then uri = args.first
else raise ArgumentError.new("wrong number of arguments (#{args.length} for method = :any, uri)")
when 2
Registry.instance.registered_uri?(*args)
when 1
print_missing_http_method_deprecation_warning(*args)
Registry.instance.registered_uri?(:any, *args)
else
raise ArgumentError.new("wrong number of arguments (#{args.length} for 2)")
end

Registry.instance.registered_uri?(method, uri)
end

end
private

def self.print_missing_http_method_deprecation_warning(*args)
method = caller.first.match(/`(.*?)'/)[1]
new_args = args.map { |a| a.inspect }.unshift(":any")
new_args.last.gsub!(/^\{|\}$/, "").gsub!("=>", " => ") if args.last.is_a?(Hash)
$stderr.puts
$stderr.puts "Deprecation warning: FakeWeb requires an HTTP method argument (or use :any). Try this:"
$stderr.puts " FakeWeb.#{method}(#{new_args.join(', ')})"
$stderr.puts "Called at #{caller[1]}"
end
end
18 changes: 9 additions & 9 deletions test/test_fake_authentication.rb
Expand Up @@ -2,24 +2,24 @@

class TestFakeAuthentication < Test::Unit::TestCase
def setup
FakeWeb.register_uri('http://user:pass@mock/auth.txt', :string => 'authorized')
FakeWeb.register_uri('http://user2:pass@mock/auth.txt', :string => 'wrong user')
FakeWeb.register_uri('http://mock/auth.txt', :string => 'unauthorized')
FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :string => 'authorized')
FakeWeb.register_uri(:get, 'http://user2:pass@mock/auth.txt', :string => 'wrong user')
FakeWeb.register_uri(:get, 'http://mock/auth.txt', :string => 'unauthorized')
end

def test_register_uri_with_authentication
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
assert FakeWeb.registered_uri?('http://user:pass@mock/test_example.txt')
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
assert FakeWeb.registered_uri?(:get, 'http://user:pass@mock/test_example.txt')
end

def test_register_uri_with_authentication_doesnt_trigger_without
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
assert !FakeWeb.registered_uri?('http://mock/test_example.txt')
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
assert !FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
end

def test_register_uri_with_authentication_doesnt_trigger_with_incorrect_credentials
FakeWeb.register_uri('http://user:pass@mock/test_example.txt', :string => "example")
assert !FakeWeb.registered_uri?('http://user:wrong@mock/test_example.txt')
FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
assert !FakeWeb.registered_uri?(:get, 'http://user:wrong@mock/test_example.txt')
end

def test_unauthenticated_request
Expand Down

0 comments on commit 0142d47

Please sign in to comment.