Permalink
Browse files

Deprecate FakeWeb.register_uri's :string and :file options in favor o…

…f a unified :body option, which takes a filename or string to be used as the body of the response
  • Loading branch information...
1 parent 7507d49 commit 92075e0390974a3b42d67314c678ae6d984dade6 @chrisk committed Jun 14, 2009
View
@@ -1,5 +1,10 @@
fakeweb (development)
+* DEPRECATION: Calling FakeWeb.register_uri with a :string or :file option is
+ now deprecated. Both options have been replaced with a unified :body option,
+ since they supply the response body (as opposed to :response, which supplies
+ the full response including headers) [Chris Kampmeier]
+
* add support for specifying HTTP headers as options to FakeWeb.register_uri
when using the :string or :file response types, since those methods only
specify a response body [David Michael, Chris Kampmeier]
View
@@ -34,7 +34,7 @@ Start by requiring FakeWeb:
=== Registering basic string responses
- FakeWeb.register_uri(:get, "http://example.com/test1", :string => "Hello World!")
+ FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")
Net::HTTP.get(URI.parse("http://example.com/test1"))
=> "Hello World!"
@@ -52,7 +52,7 @@ Start by requiring FakeWeb:
=== Adding a custom status to the response
- FakeWeb.register_uri(:get, "http://example.com/", :string => "Nothing to be found 'round here",
+ FakeWeb.register_uri(:get, "http://example.com/", :body => "Nothing to be found 'round here",
:status => ["404", "Not Found"])
Net::HTTP.start("example.com") do |req|
@@ -64,7 +64,7 @@ Start by requiring FakeWeb:
=== Responding to any HTTP method
- FakeWeb.register_uri(:any, "http://example.com", :string => "response for any HTTP method")
+ FakeWeb.register_uri(:any, "http://example.com", :body => "response for any HTTP method")
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
@@ -81,8 +81,8 @@ a response more than once before rotating, by specifying a <tt>:times</tt>
option for that response.)
FakeWeb.register_uri(:delete, "http://example.com/posts/1",
- [{:string => "Post 1 deleted.", :status => ["200", "OK"]},
- {:string => "Post not found", :status => ["404", "Not Found"]}])
+ [{:body => "Post 1 deleted.", :status => ["200", "OK"]},
+ {:body => "Post not found", :status => ["404", "Not Found"]}])
Net::HTTP.start("example.com") do |req|
req.delete("/posts/1").body # => "Post 1 deleted"
@@ -95,8 +95,8 @@ option for that response.)
You can stub requests that use basic authentication with +userinfo+ strings in
the URIs:
- 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")
+ FakeWeb.register_uri(:get, "http://example.com/secret", :body => "Unauthorized", :status => ["401", "Unauthorized"])
+ FakeWeb.register_uri(:get, "http://user:pass@example.com/secret", :body => "Authorized")
Net::HTTP.start("example.com") do |http|
req = Net::HTTP::Get.new("/secret")
@@ -133,12 +133,11 @@ without a corresponding test change.
=== Specifying HTTP response headers
-When you register a response using the <tt>:string</tt> or <tt>:file</tt>
-options, you're only setting the body of the response. If you want to add
-headers to these responses, simply add the header as an option to
-+register_uri+:
+When you register a response using the <tt>:body</tt> option, you're only
+setting the body of the response. If you want to add headers to these responses,
+simply add the header as an option to +register_uri+:
- FakeWeb.register_uri(:get, "http://example.com/hello.txt", :string => "Hello", :content_type => "text/plain")
+ FakeWeb.register_uri(:get, "http://example.com/hello.txt", :body => "Hello", :content_type => "text/plain")
This sets the "Content-Type" header in the response.
View
@@ -52,18 +52,17 @@ class NetConnectNotAllowedError < StandardError; end;
# 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:
+ # +Hash+ or an +Array+ of +Hashes+ (see below) that must contain one of these
+ # two keys:
#
- # <tt>:string</tt>::
- # Takes a +String+ argument that is returned as the body of the response.
- # FakeWeb.register_uri(:get, 'http://example.com/', :string => "Hello World!")
- # <tt>:file</tt>::
- # Takes a valid filesystem path to a file that is slurped and returned as
- # the body of the response.
- # FakeWeb.register_uri(:post, 'http://example.com/', :file => "/tmp/my_response_body.txt")
+ # <tt>:body</tt>::
+ # A string which is used as the body of the response. If the string refers
+ # to a valid filesystem path, the contents of that file will be read and used
+ # as the body of the response instead. (This used to be two options,
+ # <tt>:string</tt> and <tt>:file</tt>, respectively. These are now deprecated.)
# <tt>:response</tt>::
- # Either an <tt>Net::HTTPResponse</tt>, an +IO+ or a +String+.
+ # Either an <tt>Net::HTTPResponse</tt>, an +IO+, or a +String+ which is used
+ # as the full response for the request.
#
# The easier way by far is to pass the <tt>:response</tt> option to
# +register_uri+ as a +String+ or an (open for reads) +IO+ object which
@@ -104,11 +103,10 @@ class NetConnectNotAllowedError < StandardError; end;
# specified URL is requested. Any +Exception+ class is valid. Example:
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
#
- # If you're using the <tt>:string</tt> or <tt>:file</tt> response type, you can
- # pass additional options to specify the HTTP headers to be used in the response.
- # Example:
+ # If you're using the <tt>:body</tt> response type, you can pass additional
+ # options to specify the HTTP headers to be used in the response. Example:
#
- # FakeWeb.register_uri(:get, "http://example.com/index.txt", :string => "Hello", :content_type => "text/plain")
+ # FakeWeb.register_uri(:get, "http://example.com/index.txt", :body => "Hello", :content_type => "text/plain")
def self.register_uri(*args)
case args.length
when 3
@@ -2,13 +2,18 @@ module FakeWeb
class Responder #:nodoc:
attr_accessor :method, :uri, :options, :times
- KNOWN_OPTIONS = [:exception, :file, :response, :status, :string].freeze
+ KNOWN_OPTIONS = [:body, :exception, :response, :status].freeze
def initialize(method, uri, options, times)
self.method = method
self.uri = uri
self.options = options
self.times = times ? times : 1
+
+ if options.has_key?(:file) || options.has_key?(:string)
+ print_file_string_options_deprecation_warning
+ options[:body] = options.delete(:file) || options.delete(:string)
+ end
end
def response(&block)
@@ -17,7 +22,7 @@ def response(&block)
else
code, msg = meta_information
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
- response.instance_variable_set(:@body, content)
+ response.instance_variable_set(:@body, body)
headers_extracted_from_options.each { |name, value| response[name] = value }
end
@@ -39,21 +44,14 @@ def headers_extracted_from_options
}
end
- def content
- [ :file, :string ].each do |map_option|
- next unless options.has_key?(map_option)
- return self.send("#{map_option}_response", options[map_option])
- end
-
- return ''
- end
+ def body
+ return '' unless options.has_key?(:body)
- def file_response(path)
- IO.read(path)
- end
-
- def string_response(string)
- string
+ if !options[:body].include?("\0") && File.exists?(options[:body]) && !File.directory?(options[:body])
+ File.read(options[:body])
+ else
+ options[:body]
+ end
end
def baked_response
@@ -103,5 +101,13 @@ def meta_information
options.has_key?(:status) ? options[:status] : [200, 'OK']
end
+ def print_file_string_options_deprecation_warning
+ which = options.has_key?(:file) ? :file : :string
+ $stderr.puts
+ $stderr.puts "Deprecation warning: FakeWeb's :#{which} option has been renamed to :body."
+ $stderr.puts "Just replace :#{which} with :body in your FakeWeb.register_uri calls."
+ $stderr.puts "Called at #{caller[6]}"
+ end
+
end
end
@@ -2,23 +2,23 @@
class TestFakeAuthentication < Test::Unit::TestCase
def setup
- 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')
+ FakeWeb.register_uri(:get, 'http://user:pass@mock/auth.txt', :body => 'authorized')
+ FakeWeb.register_uri(:get, 'http://user2:pass@mock/auth.txt', :body => 'wrong user')
+ FakeWeb.register_uri(:get, 'http://mock/auth.txt', :body => 'unauthorized')
end
def test_register_uri_with_authentication
- FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
+ FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "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(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
+ FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "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(:get, 'http://user:pass@mock/test_example.txt', :string => "example")
+ FakeWeb.register_uri(:get, 'http://user:pass@mock/test_example.txt', :body => "example")
assert !FakeWeb.registered_uri?(:get, 'http://user:wrong@mock/test_example.txt')
end
@@ -43,7 +43,7 @@ def test_incorrectly_authenticated_request
end
def test_basic_auth_support_is_transparent_to_oauth
- FakeWeb.register_uri(:get, "http://sp.example.com/protected", :string => "secret")
+ FakeWeb.register_uri(:get, "http://sp.example.com/protected", :body => "secret")
# from http://oauth.net/core/1.0/#auth_header
auth_header = <<-HEADER
Oops, something went wrong.

0 comments on commit 92075e0

Please sign in to comment.