Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into regexes

  • Loading branch information...
commit 83ffe8c76a0dd3191047b1025e7e047e858b28e5 2 parents bfbcaa0 + 0142d47
@chrisk authored
View
7 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
View
7 README.rdoc
@@ -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
@@ -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")
View
80 lib/fake_web.rb
@@ -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.
@@ -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.
@@ -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
View
18 test/test_fake_authentication.rb
@@ -2,9 +2,9 @@
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_and_pattern
@@ -13,18 +13,18 @@ def test_register_uri_with_authentication_and_pattern
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
View
146 test/test_fake_web.rb
@@ -8,8 +8,8 @@ def setup
end
def test_register_uri
- FakeWeb.register_uri('http://mock/test_example.txt', :string => "example")
- assert FakeWeb.registered_uri?('http://mock/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :string => "example")
+ assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')
end
def test_register_uri_with_wrong_number_of_arguments
@@ -41,48 +41,48 @@ def test_response_for_with_wrong_number_of_arguments
def test_register_uri_without_domain_name
assert_raises URI::InvalidURIError do
- FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')
end
end
def test_register_uri_with_port_and_check_with_port
- FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com:3000/')
+ FakeWeb.register_uri(:get, 'http://example.com:3000/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/')
end
def test_register_uri_with_port_and_check_without_port
- FakeWeb.register_uri('http://example.com:3000/', :string => 'foo')
- assert !FakeWeb.registered_uri?('http://example.com/')
+ FakeWeb.register_uri(:get, 'http://example.com:3000/', :string => 'foo')
+ assert !FakeWeb.registered_uri?(:get, 'http://example.com/')
end
def test_register_uri_with_default_port_for_http_and_check_without_port
- FakeWeb.register_uri('http://example.com:80/', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com/')
+ FakeWeb.register_uri(:get, 'http://example.com:80/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/')
end
def test_register_uri_with_default_port_for_https_and_check_without_port
- FakeWeb.register_uri('https://example.com:443/', :string => 'foo')
- assert FakeWeb.registered_uri?('https://example.com/')
+ FakeWeb.register_uri(:get, 'https://example.com:443/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'https://example.com/')
end
def test_register_uri_with_no_port_for_http_and_check_with_default_port
- FakeWeb.register_uri('http://example.com/', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com:80/')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com:80/')
end
def test_register_uri_with_no_port_for_https_and_check_with_default_port
- FakeWeb.register_uri('https://example.com/', :string => 'foo')
- assert FakeWeb.registered_uri?('https://example.com:443/')
+ FakeWeb.register_uri(:get, 'https://example.com/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'https://example.com:443/')
end
def test_register_uri_with_no_port_for_https_and_check_with_443_on_http
- FakeWeb.register_uri('https://example.com/', :string => 'foo')
- assert !FakeWeb.registered_uri?('http://example.com:443/')
+ FakeWeb.register_uri(:get, 'https://example.com/', :string => 'foo')
+ assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/')
end
def test_register_uri_with_no_port_for_http_and_check_with_80_on_https
- FakeWeb.register_uri('http://example.com/', :string => 'foo')
- assert !FakeWeb.registered_uri?('https://example.com:80/')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'foo')
+ assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/')
end
def test_register_uri_for_any_method_explicitly
@@ -92,7 +92,9 @@ def test_register_uri_for_any_method_explicitly
assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")
assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")
assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")
- assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
+ capture_stderr do # silence deprecation warning
+ assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")
+ end
end
def test_register_uri_for_get_method_only
@@ -102,12 +104,14 @@ def test_register_uri_for_get_method_only
assert !FakeWeb.registered_uri?(:put, "http://example.com/users")
assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")
assert !FakeWeb.registered_uri?(:any, "http://example.com/users")
- assert !FakeWeb.registered_uri?("http://example.com/users")
+ capture_stderr do # silence deprecation warning
+ assert !FakeWeb.registered_uri?("http://example.com/users")
+ end
end
def test_response_for_with_registered_uri
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
- assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
end
def test_response_for_with_matching_registered_uri
@@ -136,7 +140,7 @@ def test_response_for_with_any_method_explicitly
end
def test_content_for_registered_uri_with_port_and_request_with_port
- FakeWeb.register_uri('http://example.com:3000/', :string => 'test example content')
+ FakeWeb.register_uri(:get, 'http://example.com:3000/', :string => 'test example content')
Net::HTTP.start('example.com', 3000) do |http|
response = http.get('/')
assert_equal 'test example content', response.body
@@ -144,7 +148,7 @@ def test_content_for_registered_uri_with_port_and_request_with_port
end
def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port
- FakeWeb.register_uri('http://example.com:80/', :string => 'test example content')
+ FakeWeb.register_uri(:get, 'http://example.com:80/', :string => 'test example content')
Net::HTTP.start('example.com') do |http|
response = http.get('/')
assert_equal 'test example content', response.body
@@ -152,7 +156,7 @@ def test_content_for_registered_uri_with_default_port_for_http_and_request_witho
end
def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port
- FakeWeb.register_uri('http://example.com/', :string => 'test example content')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'test example content')
Net::HTTP.start('example.com', 80) do |http|
response = http.get('/')
assert_equal 'test example content', response.body
@@ -160,7 +164,7 @@ def test_content_for_registered_uri_with_no_port_for_http_and_request_with_defau
end
def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port
- FakeWeb.register_uri('https://example.com:443/', :string => 'test example content')
+ FakeWeb.register_uri(:get, 'https://example.com:443/', :string => 'test example content')
http = Net::HTTP.new('example.com', 443)
http.use_ssl = true
response = http.get('/')
@@ -168,7 +172,7 @@ def test_content_for_registered_uri_with_default_port_for_https_and_request_with
end
def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port
- FakeWeb.register_uri('https://example.com/', :string => 'test example content')
+ FakeWeb.register_uri(:get, 'https://example.com/', :string => 'test example content')
http = Net::HTTP.new('example.com', 443)
http.use_ssl = true
response = http.get('/')
@@ -176,8 +180,8 @@ def test_content_for_registered_uri_with_no_port_for_https_and_request_with_defa
end
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port
- FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
- FakeWeb.register_uri('http://example.com/', :string => 'port 80')
+ FakeWeb.register_uri(:get, 'http://example.com:3000/', :string => 'port 3000')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'port 80')
Net::HTTP.start('example.com') do |http|
response = http.get('/')
assert_equal 'port 80', response.body
@@ -185,8 +189,8 @@ def test_content_for_registered_uris_with_ports_on_same_domain_and_request_witho
end
def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port
- FakeWeb.register_uri('http://example.com:3000/', :string => 'port 3000')
- FakeWeb.register_uri('http://example.com/', :string => 'port 80')
+ FakeWeb.register_uri(:get, 'http://example.com:3000/', :string => 'port 3000')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'port 80')
Net::HTTP.start('example.com', 3000) do |http|
response = http.get('/')
assert_equal 'port 3000', response.body
@@ -217,7 +221,10 @@ def test_content_for_registered_uri_with_any_method_explicitly
def test_content_for_registered_uri_with_any_method_implicitly
FakeWeb.allow_net_connect = false
- FakeWeb.register_uri("http://example.com/", :string => "test example content")
+ capture_stderr do # silence deprecation warning
+ FakeWeb.register_uri("http://example.com/", :string => "test example content")
+ end
+
Net::HTTP.start('example.com') do |http|
assert_equal 'test example content', http.get('/').body
assert_equal 'test example content', http.post('/', nil).body
@@ -227,7 +234,7 @@ def test_content_for_registered_uri_with_any_method_implicitly
end
def test_mock_request_with_block
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
Net::HTTP.start('mock') do |http|
response = http.get('/test_example.txt')
assert_equal 'test example content', response.body
@@ -235,7 +242,7 @@ def test_mock_request_with_block
end
def test_mock_request_with_undocumented_full_uri_argument_style
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
Net::HTTP.start('mock') do |query|
response = query.get('http://mock/test_example.txt')
assert_equal 'test example content', response.body
@@ -243,7 +250,7 @@ def test_mock_request_with_undocumented_full_uri_argument_style
end
def test_mock_request_with_undocumented_full_uri_argument_style_and_query
- FakeWeb.register_uri('http://mock/test_example.txt?a=b', :string => 'test query content')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :string => 'test query content')
Net::HTTP.start('mock') do |query|
response = query.get('http://mock/test_example.txt?a=b')
assert_equal 'test query content', response.body
@@ -251,7 +258,7 @@ def test_mock_request_with_undocumented_full_uri_argument_style_and_query
end
def test_mock_post
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
response = nil
Net::HTTP.start('mock') do |query|
response = query.post('/test_example.txt', '')
@@ -261,7 +268,7 @@ def test_mock_post
def test_mock_post_with_string_as_registered_uri
response = nil
- FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
+ FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :string => 'foo')
Net::HTTP.start('mock') do |query|
response = query.post('/test_string.txt', '')
end
@@ -270,7 +277,7 @@ def test_mock_post_with_string_as_registered_uri
def test_mock_get_with_request_as_registered_uri
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
- FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
+ FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
response = nil
Net::HTTP.start('mock') do |query|
response = query.get('/test_response')
@@ -280,7 +287,7 @@ def test_mock_get_with_request_as_registered_uri
end
def test_mock_get_with_request_from_file_as_registered_uri
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.get('/')
@@ -290,7 +297,7 @@ def test_mock_get_with_request_from_file_as_registered_uri
end
def test_mock_post_with_request_from_file_as_registered_uri
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
+ FakeWeb.register_uri(:post, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.post('/', '')
@@ -300,8 +307,8 @@ def test_mock_post_with_request_from_file_as_registered_uri
end
def test_proxy_request
- FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
- FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
+ FakeWeb.register_uri(:get, 'http://www.example.com/', :string => "hello world")
+ FakeWeb.register_uri(:get, 'http://your.proxy.host/', :string => "lala")
proxy_addr = 'your.proxy.host'
proxy_port = 8080
@@ -312,7 +319,7 @@ def test_proxy_request
end
def test_https_request
- FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
+ FakeWeb.register_uri(:get, 'https://www.example.com/', :string => "Hello World")
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
response = http.get('/')
@@ -320,7 +327,7 @@ def test_https_request
end
def test_register_unimplemented_response
- FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
+ FakeWeb.register_uri(:get, 'http://mock/unimplemented', :response => 1)
assert_raises StandardError do
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
end
@@ -362,7 +369,7 @@ def test_real_https_request
def test_real_request_on_same_domain_as_mock
setup_expectations_for_real_apple_hot_news_request
- FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
+ FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :string => 'foo')
resp = nil
Net::HTTP.start('images.apple.com') do |query|
@@ -373,7 +380,7 @@ def test_real_request_on_same_domain_as_mock
end
def test_mock_request_on_real_domain
- FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
+ FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :string => 'foo')
resp = nil
Net::HTTP.start('images.apple.com') do |query|
resp = query.get('/test_string.txt')
@@ -382,7 +389,7 @@ def test_mock_request_on_real_domain
end
def test_mock_post_that_raises_exception
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
+ FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => StandardError)
assert_raises(StandardError) do
Net::HTTP.start('mock') do |query|
query.post('/raising_exception.txt', 'some data')
@@ -391,7 +398,7 @@ def test_mock_post_that_raises_exception
end
def test_mock_post_that_raises_an_http_error
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
+ FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)
assert_raises(Net::HTTPError) do
Net::HTTP.start('mock') do |query|
query.post('/raising_exception.txt', '')
@@ -407,7 +414,7 @@ def test_raising_an_exception_that_requires_an_argument_to_instantiate
end
def test_mock_instance_syntax
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
response = nil
uri = URI.parse('http://mock/test_example.txt')
http = Net::HTTP.new(uri.host, uri.port)
@@ -422,7 +429,7 @@ def test_mock_via_nil_proxy
response = nil
proxy_address = nil
proxy_port = nil
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
uri = URI.parse('http://mock/test_example.txt')
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
uri.host, (uri.port or 80))
@@ -434,7 +441,7 @@ def test_mock_via_nil_proxy
end
def test_response_type
- FakeWeb.register_uri('http://mock/test_example.txt', :string => "test")
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :string => "test")
Net::HTTP.start('mock') do |http|
response = http.get('/test_example.txt')
assert_kind_of(Net::HTTPSuccess, response)
@@ -442,7 +449,7 @@ def test_response_type
end
def test_mock_request_that_raises_an_http_error_with_a_specific_status
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
exception = assert_raises(Net::HTTPError) do
Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
end
@@ -451,7 +458,7 @@ def test_mock_request_that_raises_an_http_error_with_a_specific_status
end
def test_mock_rotate_responses
- FakeWeb.register_uri('http://mock/multiple_test_example.txt',
+ FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',
[ {:file => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},
{:string => "thrice", :times => 3},
{:string => "ever_more"} ])
@@ -463,7 +470,7 @@ def test_mock_rotate_responses
end
def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.get('/')
@@ -473,7 +480,7 @@ def test_mock_request_using_response_with_transfer_encoding_header_has_valid_tra
end
def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.get('/')
@@ -482,7 +489,7 @@ def test_mock_request_using_response_without_transfer_encoding_header_does_not_h
end
def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header
- FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.get('/')
@@ -492,7 +499,7 @@ def test_mock_request_using_response_from_curl_has_original_transfer_encoding_he
end
def test_txt_file_should_have_three_lines
- FakeWeb.register_uri('http://www.google.com/', :file => File.dirname(__FILE__) + '/fixtures/test_txt_file')
+ FakeWeb.register_uri(:get, 'http://www.google.com/', :file => File.dirname(__FILE__) + '/fixtures/test_txt_file')
response = nil
Net::HTTP.start('www.google.com') do |query|
response = query.get('/')
@@ -509,7 +516,7 @@ def test_registering_using_response_with_string_containing_null_byte
# contains null byte") since :response first tries to find by filename.
# The string should be treated as a response body, instead, and an
# EOFError is raised when the byte is encountered.
- FakeWeb.register_uri("http://example.com", :response => "test\0test")
+ FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")
assert_raise EOFError do
Net::HTTP.get(URI.parse("http://example.com"))
end
@@ -526,4 +533,25 @@ def test_http_version_from_file_response
response = Net::HTTP.start("example.com") { |http| http.get("/") }
assert_equal "1.0", response.http_version
end
+
+ def test_register_uri_without_method_argument_prints_deprecation_warning
+ warning = capture_stderr do
+ FakeWeb.register_uri("http://example.com", :string => "test")
+ end
+ assert_match /deprecation warning: fakeweb/i, warning
+ end
+
+ def test_registered_uri_without_method_argument_prints_deprecation_warning
+ warning = capture_stderr do
+ FakeWeb.registered_uri?("http://example.com")
+ end
+ assert_match /deprecation warning: fakeweb/i, warning
+ end
+
+ def test_response_for_without_method_argument_prints_deprecation_warning
+ warning = capture_stderr do
+ FakeWeb.response_for("http://example.com")
+ end
+ assert_match /deprecation warning: fakeweb/i, warning
+ end
end
View
16 test/test_fake_web_open_uri.rb
@@ -7,17 +7,17 @@ def setup
end
def test_content_for_registered_uri
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
- assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
end
def test_mock_open
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
assert_equal 'test example content', open('http://mock/test_example.txt').read
end
def test_mock_open_with_string_as_registered_uri
- FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
+ FakeWeb.register_uri(:get, 'http://mock/test_string.txt', :string => 'foo')
assert_equal 'foo', open('http://mock/test_string.txt').string
end
@@ -31,21 +31,21 @@ def test_real_open
end
def test_mock_open_that_raises_exception
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => StandardError)
assert_raises(StandardError) do
open('http://mock/raising_exception.txt')
end
end
def test_mock_open_that_raises_an_http_error
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
assert_raises(OpenURI::HTTPError) do
open('http://mock/raising_exception.txt')
end
end
def test_mock_open_that_raises_an_http_error_with_a_specific_status
- FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
+ FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
exception = assert_raises(OpenURI::HTTPError) do
open('http://mock/raising_exception.txt')
end
@@ -54,7 +54,7 @@ def test_mock_open_that_raises_an_http_error_with_a_specific_status
end
def test_mock_open_with_block
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
+ FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
open('http://mock/test_example.txt') do |f|
assert 'test example content', f.readlines
end
View
8 test/test_helper.rb
@@ -8,6 +8,14 @@
module FakeWebTestHelper
+ def capture_stderr
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind && $stderr.read
+ ensure
+ $stderr = STDERR
+ end
+
# Sets several expectations (using Mocha) that a real HTTP request makes it
# past FakeWeb to the socket layer. You can use this when you need to check
# that a request isn't handled by FakeWeb.
View
32 test/test_query_string.rb
@@ -7,35 +7,35 @@ def setup
end
def test_register_uri_string_with_query_params
- FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
+ FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?a=1&b=1')
- FakeWeb.register_uri(URI.parse("http://example.org/?a=1&b=1"), :string => "foo")
- assert FakeWeb.registered_uri?("http://example.org/?a=1&b=1")
+ FakeWeb.register_uri(:post, URI.parse("http://example.org/?a=1&b=1"), :string => "foo")
+ assert FakeWeb.registered_uri?(:post, "http://example.org/?a=1&b=1")
end
def test_register_uri_with_query_params_and_check_in_different_order
- FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
+ FakeWeb.register_uri(:get, 'http://example.com/?a=1&b=1', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?b=1&a=1')
- FakeWeb.register_uri(URI.parse('http://example.org/?a=1&b=1'), :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.org/?b=1&a=1')
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/?a=1&b=1'), :string => 'foo')
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/?b=1&a=1')
end
def test_registered_uri_gets_recognized_with_empty_query_params
- FakeWeb.register_uri('http://example.com/', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com/?')
+ FakeWeb.register_uri(:get, 'http://example.com/', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/?')
- FakeWeb.register_uri(URI.parse('http://example.org/'), :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.org/?')
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/'), :string => 'foo')
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/?')
end
def test_register_uri_with_empty_query_params_and_check_with_none
- FakeWeb.register_uri('http://example.com/?', :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.com/')
+ FakeWeb.register_uri(:get, 'http://example.com/?', :string => 'foo')
+ assert FakeWeb.registered_uri?(:get, 'http://example.com/')
- FakeWeb.register_uri(URI.parse('http://example.org/?'), :string => 'foo')
- assert FakeWeb.registered_uri?('http://example.org/')
+ FakeWeb.register_uri(:post, URI.parse('http://example.org/?'), :string => 'foo')
+ assert FakeWeb.registered_uri?(:post, 'http://example.org/')
end
def test_registry_sort_query_params
Please sign in to comment.
Something went wrong with that request. Please try again.